#! /usr/bin/env python3 # -*- coding: utf-8 -*- # Imports import os import sys import requests import time import datetime import sqlite3 # Constantes api_url = 'http://localhost:8080/colibri/stats' statsPath = '/srv/visio.chapril.org/statistiques' dbPath = '/srv/visio.chapril.org/statistiques/stats_sqlite.db' dbName = 'jitsi_stats' # Classes class SQLite: def __init__(self): if not os.path.isfile(dbPath): self.__initDb() def __initDb(self): self.__openDb() self.cursor.execute(f''' create table {dbName}( id integer primary key autoincrement, timestamp text, key_field text, value_field text ) ''') self.conn.commit() self.__closeDb() def __openDb(self): self.conn = sqlite3.connect(dbPath) self.cursor = self.conn.cursor() def __closeDb(self): self.cursor.close() self.conn.close() def dbQuery(self,query='SELECT'): self.__openDb() self.cursor.execute(f"""SELECT * FROM {dbName}""") rows = self.cursor.fetchall() self.__closeDb() return rows def dbInsert(self,ts,k,v): self.__openDb() self.cursor.execute(f"""INSERT INTO {dbName} (timestamp,key_field,value_field) VALUES ('{ts}','{k}','{v}')""") self.conn.commit() self.__closeDb() # Fonctions def runMain(): db = SQLite() response = requests.get(api_url,timeout=1) element = datetime.datetime.strptime(response.json()['current_timestamp'],'%Y-%m-%d %H:%M:%S.%f') timestamp = int(time.mktime(element.timetuple())) with open(f'/{statsPath}/jisti_meet_stats_{timestamp}.csv','w') as fh: for (k,v) in response.json().items(): db.dbInsert(timestamp,k,v) fh.write(f"{k};{v};{timestamp}\n") db.dbQuery() # Principal if __name__ == '__main__': runMain() sys.exit(0)