def is_table_empty(table): db = get_db() cur = db.cursor() cur.execute('SELECT COUNT(*) FROM {}'.format(table)) if len(cur.fetchall()) > 0: return False return True
def print_table(table): print('\n --- REGISTERED THIN CLIENTS ARE: ---\n') if is_table_empty(client_table) is not True: db = get_db() cur = db.cursor() cur.execute("SELECT * FROM thinClients") for row in cur.fetchall(): client = [] for val in row: client.append(str(val)) print(client) else: print('Empty Table: ' + client_table) print('\n --- END OF TABLE ---\n')
def list_packages(): if request.method == 'GET': db = get_db() cur = db.cursor() error = None res_path = os.getcwd() + '/thinClient_server/resources' packages = [f for f in os.listdir(res_path) if f.endswith('.zip')] if error is None: return jsonify(packages) # Bad Request return flask.make_response(flask.Response(error), 400)
def list_clients(): if request.method == 'GET': db = get_db() cur = db.cursor() error = None clients = [] if is_table_empty is not True: cur.execute('SELECT id FROM thinClients') for client in cur.fetchall(): clients.append(str(client[0])) error = 'OK' else: error = 'No known Clients' return jsonify(clients) # Bad Request return flask.make_response(flask.Response(error), 400)
def show_client(): if request.method == 'POST': client_id = str(request.form['client_id']) db = get_db() cur = db.cursor() error = None client_info = [] if is_table_empty is not True: # check if client is alive cur.execute( 'SELECT id, latest_heartbeat FROM thinClients WHERE id=?', (client_id, )) for row in cur.fetchall(): dbtime = datetime.strptime(row[1], '%Y-%m-%d %H:%M:%S') timestamp = '{0:%Y-%m-%d %H:%M:%S}'.format(datetime.now()) curtime = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') #print(str(dtime)) if (dbtime + timedelta(seconds=30) < datetime.now()): db.execute('UPDATE thinClients SET alive=? WHERE id=?', (0, client_id)) db.commit() else: db.execute('UPDATE thinClients SET alive=? WHERE id=?', (1, client_id)) db.commit() # select information cur.execute('SELECT * FROM thinClients WHERE id=?', (client_id, )) for client in cur.fetchall(): for val in client: client_info.append(val) return jsonify(client_info) # Bad Request return flask.make_response(flask.Response(error), 400)
def latest_heartbeat(): if request.method == 'POST': thinClient_id = request.form['id'] thinClient_cpu = request.form['cpu'] thinClient_ram = request.form['ram'] thinClient_gpu = request.form['gpu'] #thinClient_id = request.form.get('id') db = get_db() cur = db.cursor() error = None if not thinClient_id: error = 'thinClient_id is required.' # If client exists update timestamp elif db.execute( 'SELECT latest_heartbeat FROM thinClients WHERE id = ?', (thinClient_id, )).fetchone() is not None: timestamp = '{0:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()) db.execute('UPDATE thinClients SET latest_heartbeat=? WHERE id=?', (timestamp, thinClient_id)) db.commit() error = 'ThinClient {} is already registered.'.format( thinClient_id) # if new client register in db if error is None: timestamp = '{0:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()) db.execute( 'INSERT INTO thinClients (id, latest_heartbeat, cpu, ram_in_gb, gpu) VALUES (?,?,?,?,?)', (thinClient_id, timestamp, thinClient_cpu, thinClient_ram, thinClient_gpu)) # print('Registered Client with id: '+thinClient_id) db.commit() # OK #print_table(client_table) return flask.make_response(flask.Response('OK'), 200) flash(error) #print_table(client_table) return flask.make_response(flask.Response('OK'), 200) # Bad Request return flask.make_response(flask.Response(error), 400)
def table_size(table): db = get_db() cur = db.cursor() cur.execute('SELECT COUNT(*) FROM {}'.format(table)) print('table size: ' + str(len(cur.fetchall())))