Ajout gestion DB sqlite

This commit is contained in:
David 2022-12-18 23:07:58 +01:00
parent 637a0c9519
commit 2e60a3fa6c

View File

@ -11,16 +11,53 @@ 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