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

74 lines
1.9 KiB
Python
Raw Normal View History

2022-12-10 09:19:21 +01:00
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Imports
import os
import sys
import requests
import time
import datetime
2022-12-18 23:09:41 +01:00
import sqlite3
2022-12-10 09:19:21 +01:00
# Constantes
api_url = 'http://localhost:8080/colibri/stats'
2022-12-17 11:20:59 +01:00
statsPath = '/srv/visio.chapril.org/statistiques'
2022-12-18 23:13:14 +01:00
dbPath = '/srv/visio.chapril.org/statistiques/stats_sqlite.db'
2022-12-18 23:48:52 +01:00
dbName = 'jitsi_stats'
2022-12-10 09:19:21 +01:00
# Classes
2022-12-18 23:07:58 +01:00
class SQLite:
def __init__(self):
if not os.path.isfile(dbPath):
self.__initDb()
2022-12-10 09:19:21 +01:00
2022-12-18 23:07:58 +01:00
def __initDb(self):
self.__openDb()
2022-12-18 23:48:52 +01:00
self.cursor.execute(f''' create table {dbName}(
2022-12-18 23:07:58 +01:00
id integer primary key autoincrement,
timestamp text,
key_field text,
value_field text
)
''')
2022-12-18 23:16:39 +01:00
self.conn.commit()
2022-12-18 23:07:58 +01:00
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()
2022-12-18 23:48:52 +01:00
self.cursor.execute(f"""SELECT * FROM {dbName}""")
2022-12-18 23:16:39 +01:00
rows = self.cursor.fetchall()
2022-12-18 23:07:58 +01:00
self.__closeDb()
2022-12-18 23:16:39 +01:00
return rows
2022-12-18 23:07:58 +01:00
def dbInsert(self,ts,k,v):
self.__openDb()
2022-12-19 10:01:25 +01:00
self.cursor.execute(f"""INSERT INTO {dbName} (timestamp,key_field,value_field) VALUES ('{ts}','{k}','{v}')""")
2022-12-18 23:16:39 +01:00
self.conn.commit()
2022-12-18 23:07:58 +01:00
self.__closeDb()
2022-12-10 09:19:21 +01:00
# Fonctions
def runMain():
2022-12-18 23:07:58 +01:00
db = SQLite()
2022-12-10 09:19:21 +01:00
response = requests.get(api_url,timeout=1)
element = datetime.datetime.strptime(response.json()['current_timestamp'],'%Y-%m-%d %H:%M:%S.%f')
2022-12-18 22:51:41 +01:00
timestamp = int(time.mktime(element.timetuple()))
2022-12-17 11:20:59 +01:00
with open(f'/{statsPath}/jisti_meet_stats_{timestamp}.csv','w') as fh:
2022-12-10 09:19:21 +01:00
for (k,v) in response.json().items():
2022-12-18 23:07:58 +01:00
db.dbInsert(timestamp,k,v)
2022-12-10 09:19:21 +01:00
fh.write(f"{k};{v};{timestamp}\n")
2022-12-18 23:16:39 +01:00
db.dbQuery()
2022-12-10 09:19:21 +01:00
# Principal
if __name__ == '__main__':
runMain()
sys.exit(0)