visio.chapril.org-tools/statistiques/getStats.py
2022-12-18 23:07:58 +01:00

67 lines
1.7 KiB
Python
Executable File

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Imports
import os
import sys
import requests
import time
import datetime
# Constantes
api_url = 'http://localhost:8080/colibri/stats'
statsPath = '/srv/visio.chapril.org/statistiques'
dbPath = '/srv/visio.chapril.org/stats_sqlite.db'
# Classes
class SQLite:
def __init__(self):
if not os.path.isfile(dbPath):
self.__initDb()
def __initDb(self):
self.__openDb()
cursor.execute(''' create table jististats(
id integer primary key autoincrement,
timestamp text,
key_field text,
value_field text
)
''')
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 jististats""")
self.__closeDb()
def dbInsert(self,ts,k,v):
self.__openDb()
self.cursor.execute(f"""INSERT INTO jististats (timestamp,key_field,value_field) VALUES ('{ts}','{k}','{v}')""")
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")
# Principal
if __name__ == '__main__':
runMain()
sys.exit(0)