| 1 |
''' |
| 2 |
Import biomaj banks statistics in Influxdb if never done before..... |
| 3 |
''' |
| 4 |
from influxdb import InfluxDBClient |
| 5 |
from biomaj.bank import Bank |
| 6 |
from biomaj_core.config import BiomajConfig |
| 7 |
import sys |
| 8 |
|
| 9 |
if len(sys.argv) != 2: |
| 10 |
print('Usage: influxdb_import.py path_to_global.properties') |
| 11 |
sys.exit(1) |
| 12 |
|
| 13 |
BiomajConfig.load_config(config_file=sys.argv[1]) |
| 14 |
|
| 15 |
influxdb = None |
| 16 |
try: |
| 17 |
host = BiomajConfig.global_config.get('GENERAL', 'influxdb.host') |
| 18 |
user = BiomajConfig.global_config.get('GENERAL', 'influxdb.user') |
| 19 |
password = BiomajConfig.global_config.get('GENERAL', 'influxdb.password') |
| 20 |
port = BiomajConfig.global_config.get('GENERAL', 'influxdb.port') |
| 21 |
database = BiomajConfig.global_config.get('GENERAL', 'influxdb.db') |
| 22 |
influxdb = InfluxDBClient(host=host, database=database, port=port, username=user, password=password) |
| 23 |
except Exception as e: |
| 24 |
print('Failed to connect to influxdb, check configuration in global.properties: ' + str(e)) |
| 25 |
sys.exit(1) |
| 26 |
|
| 27 |
res = influxdb.query('select last("value") from "biomaj.banks.quantity"') |
| 28 |
if res: |
| 29 |
print('Found data in influxdb, update info....') |
| 30 |
|
| 31 |
banks = Bank.list() |
| 32 |
nb_banks = 0 |
| 33 |
metrics = [] |
| 34 |
for bank in banks: |
| 35 |
productions = bank['production'] |
| 36 |
total_size = 0 |
| 37 |
latest_size = 0 |
| 38 |
if not productions: |
| 39 |
continue |
| 40 |
nb_banks += 1 |
| 41 |
latest_size = productions[len(productions) - 1]['size'] |
| 42 |
if not latest_size: |
| 43 |
latest_size = 0 |
| 44 |
for production in productions: |
| 45 |
if 'size' in production and production['size']: |
| 46 |
total_size += production['size'] |
| 47 |
|
| 48 |
influx_metric = { |
| 49 |
"measurement": 'biomaj.production.size.total', |
| 50 |
"fields": { |
| 51 |
"value": float(total_size) |
| 52 |
}, |
| 53 |
"tags": { |
| 54 |
"bank": bank['name'] |
| 55 |
}, |
| 56 |
"time": int(production['session']) |
| 57 |
} |
| 58 |
metrics.append(influx_metric) |
| 59 |
influx_metric = { |
| 60 |
"measurement": 'biomaj.production.size.latest', |
| 61 |
"fields": { |
| 62 |
"value": float(latest_size) |
| 63 |
}, |
| 64 |
"tags": { |
| 65 |
"bank": bank['name'] |
| 66 |
}, |
| 67 |
"time": int(production['session']) |
| 68 |
} |
| 69 |
metrics.append(influx_metric) |
| 70 |
influx_metric = { |
| 71 |
"measurement": 'biomaj.bank.update.new', |
| 72 |
"fields": { |
| 73 |
"value": 1 |
| 74 |
}, |
| 75 |
"tags": { |
| 76 |
"bank": bank['name'] |
| 77 |
}, |
| 78 |
"time": int(production['session']) |
| 79 |
} |
| 80 |
metrics.append(influx_metric) |
| 81 |
|
| 82 |
influx_metric = { |
| 83 |
"measurement": 'biomaj.banks.quantity', |
| 84 |
"fields": { |
| 85 |
"value": nb_banks |
| 86 |
} |
| 87 |
} |
| 88 |
metrics.append(influx_metric) |
| 89 |
|
| 90 |
influxdb.write_points(metrics, time_precision="s") |