visio.chapril.org-tools/statistiques/getStats_ng.py

72 lines
1.8 KiB
Python
Raw Normal View History

2022-12-19 13:14:22 +01:00
#! /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()))
for (k,v) in response.json().items():
db.dbInsert(timestamp,k,v)
#db.dbQuery()
# Principal
if __name__ == '__main__':
runMain()
sys.exit(0)