File: influxdb_import.py

package info (click to toggle)
biomaj3 3.1.24-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 784 kB
  • sloc: python: 4,495; sh: 359; makefile: 154
file content (90 lines) | stat: -rw-r--r-- 2,752 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''
Import biomaj banks statistics in Influxdb if never done before.....
'''
from influxdb import InfluxDBClient
from biomaj.bank import Bank
from biomaj_core.config import BiomajConfig
import sys

if len(sys.argv) != 2:
    print('Usage: influxdb_import.py path_to_global.properties')
    sys.exit(1)

BiomajConfig.load_config(config_file=sys.argv[1])

influxdb = None
try:
    host = BiomajConfig.global_config.get('GENERAL', 'influxdb.host')
    user = BiomajConfig.global_config.get('GENERAL', 'influxdb.user')
    password = BiomajConfig.global_config.get('GENERAL', 'influxdb.password')
    port = BiomajConfig.global_config.get('GENERAL', 'influxdb.port')
    database = BiomajConfig.global_config.get('GENERAL', 'influxdb.db')
    influxdb = InfluxDBClient(host=host, database=database, port=port, username=user, password=password)
except Exception as e:
    print('Failed to connect to influxdb, check configuration in global.properties: ' + str(e))
    sys.exit(1)

res = influxdb.query('select last("value") from "biomaj.banks.quantity"')
if res:
    print('Found data in influxdb, update info....')

banks = Bank.list()
nb_banks = 0
metrics = []
for bank in banks:
    productions = bank['production']
    total_size = 0
    latest_size = 0
    if not productions:
        continue
    nb_banks += 1
    latest_size = productions[len(productions) - 1]['size']
    if not latest_size:
        latest_size = 0
    for production in productions:
        if 'size' in production and production['size']:
            total_size += production['size']

    influx_metric = {
            "measurement": 'biomaj.production.size.total',
            "fields": {
                "value": float(total_size)
            },
            "tags": {
                "bank": bank['name']
            },
            "time": int(production['session'])
    }
    metrics.append(influx_metric)
    influx_metric = {
            "measurement": 'biomaj.production.size.latest',
            "fields": {
                "value": float(latest_size)
            },
            "tags": {
                "bank": bank['name']
            },
            "time": int(production['session'])
    }
    metrics.append(influx_metric)
    influx_metric = {
                "measurement": 'biomaj.bank.update.new',
                "fields": {
                    "value": 1
                },
                "tags": {
                    "bank": bank['name']
                },
            "time": int(production['session'])
    }
    metrics.append(influx_metric)

influx_metric = {
     "measurement": 'biomaj.banks.quantity',
         "fields": {
             "value": nb_banks
             }
}
metrics.append(influx_metric)

influxdb.write_points(metrics, time_precision="s")