1
Fork 0

class aangemaakt met basic sql functies, moet nog uitgebreid worden

This commit is contained in:
Ventilaar 2021-10-27 13:33:07 +02:00
parent 64ccdfe52b
commit a3b34d69e3
1 changed files with 64 additions and 27 deletions

View File

@ -12,32 +12,74 @@ db_location = root.find('sqlite_location').text
# initialiseer flask
app = Flask(__name__)
# initialiseer sqlite
conn = sql.connect(db_location)
# class om SQL transacties mee uit te voeren
class Mydb:
def __init__(self):
"""
start de connectie en voer direct een check uit of de hosts tabel bestaat.
Zoniet dan returnt de functie een exception met een print. De database is waarschijnlijk dan niet goed opgezet.
"""
self.conn = sql.connect(db_location)
self.cur = self.conn.cursor()
def test_db():
cur.execute(''' SELECT name FROM sqlite_master WHERE type='table' AND name='hosts' ''')
if cur.fetchone()[0] == 0: # als tabel niet bestaat
print('tabel hosts bestaat niet, database wordt opnieuw aangemaakt')
self.cur.execute(''' SELECT name FROM sqlite_master WHERE type='table' AND name='hosts' ''')
if self.cur.fetchone() is None: # als tabel niet bestaat
print("Database bevat geen tabel 'hosts', is de database wel goed opgezet?")
raise Exception
def set_addhost(self, hostname, os_release, os_type, total_ram):
"""
Voegt een host toe aan de hosts tabel met waardes die hoogstwaarschijnlijk statisch zijn.
:param hostname: str
:param os_release: str
:param os_type: str
:param total_ram: str
:return: None
"""
self.cur.execute(''' INSERT INTO hosts (hostname, os_release, os_type, total_ram) VALUES \
('%s', '%s', '%s', '%s')''' % (hostname, os_release, os_type, total_ram))
self.conn.commit()
def get_hostdbid(self, hostname):
"""
Returnt een int met de PK van de opgegeven host uit de hosts tabel
:param hostname: str var of hostname to query
:return: int of the database PK for specific hostname
"""
self.cur.execute(''' SELECT entryID FROM hosts WHERE hostname=('%s') ''' % hostname)
return self.cur.fetchone()[0]
def set_hoststats(self, hostname, cpu_usage, memory_used, troughput_up, troughput_down):
"""
Voegt dynamische data entry toe in de stats tabel met de opgegeven variabelen. Vraagt trouwens ook de host PK op
voordat de query wordt uitgevoerd.
:param hostname: str
:param cpu_usage: str
:param memory_used: str
:param troughput_up: str
:param troughput_down: str
:return: None
"""
self.cur.execute(''' INSERT INTO stats (host, cpu_usage, memory_used, troughput_up, troughput_down) VALUES \
('%s', '%s', '%s', '%s', '%s')''' %
(self.get_hostdbid(hostname), cpu_usage, memory_used, troughput_up, troughput_down))
self.conn.commit()
def get_latesthostdata(self, hostname):
pass
def __del__(self):
self.conn.commit()
self.conn.close()
def create_db_struct():
cur.execute(''' CREATE TABLE cijfers (cijfer INTEGER) ''') # maak tabel aan met 1 kolom
con.commit() # commit alle wijzigingen
def insert_data(data):
"""
simpele functie om de gekregen dict te inserten in de sqlite database
"""
#dit is de document root
# dit is de document root
@app.route('/')
def index():
return root
return 'root'
# POST DATA
@app.route('/api/v1/host/post', methods=['POST'])
@ -46,10 +88,5 @@ def host_post():
return '{"status": 200}' # return een json dict met waarde status 200
if __name__ == '__main__':
app.run(debug=flask_debug) # start flask
app.run(debug=flask_debug) # start flask