# Influxdb configuration

This section will display a brief description of how influxdb was configured in terms of database (Buckets) and task creation. Furthermore a section of useful command line interface.

## Buckets

The main idea is to have two types of Buckets (i.e. data containers), one that records all temporal data in real time and one that records historical data

* **Temporal Bucket**: In this project was created a temporal bucket denominated **TAU_1YEAR_TEST**; this bucket records all the real time data with an accuracy of 3 seconds. The bucket is considered temporal because it has a retention policy of one year, which means that delete data over a period of more than one year.

* **Historical Bucket**: In order not to lose the data, influxdb offers the possibility to periodically send data from the time Bucket to the historical one, in this case without retention policy and with an accuracy of 1 hour.

### Bucket configuration

1. **Create Bucket**: Data/Buckets/Create Bucket

![Create Bucket](../img/influx_1.png)

2. **TAU_1YEAR_TEST**: Define name / delete data older than 1 year / create

![TAU_1YEAR](../img/influx_2.png)

3. **TAU_HIST_TEST**: Define name / delete data never / create

![TAU_HIST](../img/influx_3.png)

### Data reception from NodeRed

The communication between NodeRed and Influxdb is achieved through the interaction of the VM server with the raspberry pi.

**On the influxdb side** it is necessary to know:

1. **Organization**: organization/ about/ copy organization ID

![org](../img/influx_6.png)

2. **Bucket name**: Data/ Bucket / copy buckets name

![B_name](../img/influx_7.png)

3. **API token**: In this case is the admin token that has read/write authorization over buckets 

Data/ API Tokens / select dt_admin's token

![token](../img/influx_8.png)

**On the nodered side:**

In the nodered flows it is necessary to set up the influxdb blocks assigning organization, token, bucket, measure name and influxdb server.

1. Set organization, Bucket and measure name

![node_red1](../img/nodered_1.png)

2. Set server and token

![node_red2](../img/nodered_2.png)


## Tasks

An InfluxDB task is a scheduled Flux script that takes a stream of input data, modifies or analyzes it in some way, then writes the modified data back to InfluxDB.

In this case tasks are used to do the downsampling method and transfer data from real time bucket into historical one.

The flux codes are into:

* [Flux tasks](/Influxdb/Tasks/).

This codes have the functionality to send every hour the mean, max and min data of every power measure of M1 and M2 and save them into **TAU_HIST_TEST**

### Create Task on GUI

1. Task / Create task

![node_red2](../img/influx_4.png)

2. Define Task name - scheduled time - flux code

![node_red2](../img/influx_5.png)


## Useful command line interface (CLI)

To use command line it is necessary to enter into docker console of influxdb in the virtual machine.

1. **Delete data** : Is not possible to delete data over the GUI, so to do this with command line 

* Deleting all data specific bucket 

```bash
influx delete --host https://digitaltwin.polito.it:8087 -t dt_admin_token -o organization_name -b bucket_name --start '2022-01-01T00:00:00Z' -stop '2022-03-01T00:00:00Z'
```

* Deleting data specific bucket according to measure

```bash
influx delete --host https://digitaltwin.polito.it:8087 -t dt_admin_token -o organization_name -b bucket_name --start '2022-01-01T00:00:00Z' -stop '2022-03-01T00:00:00Z' --predicate '_measurement="example-measurement"'
```

2. **Create user**: 

```bash
influx user create --host https://digitaltwin.polito.it:8087 -t dt_admin_token -n user_name -p user_password -o bucket_name
```

For more information about influxdb CLI check:

* [Influxdb CLI](https://docs.influxdata.com/influxdb/v2.0/get-started/).
 
