def index(): dbHelper = DbAzureBlob() if not os.path.isfile(DATABASE_NAME): print("reseting") dbHelper.setupDatabase() conn = sqlite3.connect(DATABASE_NAME) row = conn.execute( "SELECT * FROM jobs WHERE processed = 0 ORDER BY RANDOM() LIMIT 1" ).fetchone() if (row == None): return json.dumps({ 'filename': "NULL", 'processed': 1, }) id = row[0] filename = row[1] conn.close() return Response(json.dumps({ 'id': id, 'filename': filename, 'processed': 0 }), status=200, mimetype='application/json')
def getProgress(): if not os.path.isfile(DATABASE_NAME): DbAzureBlob().setupDatabase() current_time = datetime.now() conn = sqlite3.connect(DATABASE_NAME) cursor = conn.execute("SELECT * FROM jobs WHERE detected IS NOT NULL;") time_data = conn.execute("SELECT * FROM time WHERE id = 1;").fetchone() start_time = datetime.strptime(time_data[1], '%Y-%m-%d %H:%M:%S') if (time_data[3] == 1): current_time = datetime.strptime(time_data[2], '%Y-%m-%d %H:%M:%S') total_time = (current_time - start_time).total_seconds() if time_data[4] == 0: total_time = 0 pictures = [] for row in cursor: obj = {"filename": row[1], "detected": row[3]} pictures.append(obj) data = { "success": True, "pictures": pictures, "total_time": int(total_time) } json_data = json.dumps(data) return json_data
def getFile(): if not os.path.isfile(DATABASE_NAME): DbAzureBlob().setupDatabase() conn = sqlite3.connect(DATABASE_NAME) user = request.args.get('user') row = conn.execute( "SELECT * FROM jobs WHERE processed = 0 ORDER BY RANDOM() LIMIT 1" ).fetchone() if (row == None): return json.dumps({ 'filename': "NULL", 'processed': 1, }) id = row[0] filename = row[1] conn.execute("UPDATE jobs set processed = 1 where id = " + str(id) + ";") conn.commit() conn.close() return Response(json.dumps({ 'id': id, 'filename': filename, 'processed': 0 }), status=200, mimetype='application/json')
def index(): dbHelper = DbAzureBlob() if not os.path.isfile(DATABASE_NAME): print("reseting") dbHelper.setupDatabase() conn = sqlite3.connect(DATABASE_NAME) row = conn.execute( "SELECT * FROM jobs WHERE processed = 0 ORDER BY RANDOM() LIMIT 1" ).fetchone() if (row == None): current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') conn.execute("UPDATE time set finished = 1, finish_time = \"" + current_time + "\" where id = 1;") conn.commit() return json.dumps({ 'filename': "NULL", 'processed': 1, }) cursor = conn.execute("SELECT * FROM time WHERE id = 1;") if (cursor.fetchone()[4] == 0): current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') conn.execute("UPDATE time set started = 1, start_time = \"" + current_time + "\" where id = 1;") conn.commit() id = row[0] filename = row[1] conn.execute("UPDATE jobs set processed = 1 where id = " + str(id) + ";") conn.commit() conn.close() return Response(json.dumps({ 'filename': filename, 'processed': 0 }), status=200, mimetype='application/json')
def getProgress(): if not os.path.isfile(DATABASE_NAME): DbAzureBlob().setupDatabase() req_start_time = datetime.utcnow() TIME_SPAN = 15 processed_count = 0 total_time = 0 current_time = datetime.utcnow() conn = sqlite3.connect(DATABASE_NAME) total_rows = conn.execute("SELECT COUNT(*) FROM jobs").fetchone() rows = conn.execute( "SELECT * FROM jobs WHERE detected IS NOT NULL;").fetchall() if len(rows) > 0: time_range = conn.execute( "SELECT MIN(start_time), MAX(end_time) from jobs WHERE detected IS NOT NULL;" ).fetchone() start_time = datetime.strptime(time_range[0], '%Y-%m-%d %H:%M:%S.%f') interval_start_time = current_time - timedelta(seconds=TIME_SPAN) processed_count = conn.execute( "SELECT COUNT(*) FROM jobs WHERE detected IS NOT NULL AND end_time > ? ;", (str(interval_start_time), )).fetchone()[0] conn.close() speed = processed_count / TIME_SPAN if (len(rows) == 0): total_time = 0 elif (len(rows) != total_rows): total_time = (current_time - start_time).total_seconds() else: total_time = ( datetime.strptime(time_range[1], '%Y-%m-%d %H:%M:%S.%f') - start_time).total_seconds() pictures = [] for row in rows: obj = {"filename": row[1], "detected": row[3]} pictures.append(obj) data = { "success": True, "pictures": pictures, "speed": speed, "total_time": int(total_time) } json_data = json.dumps(data) req_end_time = datetime.utcnow() print("request spend: " + str((req_start_time - req_end_time).total_seconds())) return json_data
def processed(): if not os.path.isfile(DATABASE_NAME): DbAzureBlob().setupDatabase() conn = sqlite3.connect(DATABASE_NAME) id = request.args.get('id') filename = urllib.parse.unquote(request.args.get('filename')) detected = request.args.get('detected') start_time = request.args.get('start_time') end_time = request.args.get('end_time') worker_id = request.args.get('worker_id') if(filename == None or detected == None): return json.dumps({"success":True,"status_code":200}) conn.execute("UPDATE jobs set detected = ? , start_time = ? , end_time = ? , worker_id = ? where filename = ?", (str2bool(detected), start_time, end_time, worker_id, filename ) ) conn.commit() conn.close() return json.dumps({"success":True,"status_code":200})
def processed(): if not os.path.isfile(DATABASE_NAME): DbAzureBlob().setupDatabase() conn = sqlite3.connect(DATABASE_NAME) filename = request.args.get('filename') detected = request.args.get('detected') if (filename == None or detected == None): return json.dumps({"success": True, "status_code": 200}) if (detected == "true"): conn.execute("UPDATE jobs set detected = 1 where filename = \"" + filename + "\";") else: conn.execute("UPDATE jobs set detected = 0 where filename = \"" + filename + "\";") conn.commit() conn.close() return json.dumps({"success": True, "status_code": 200})
def getFile(): if not os.path.isfile(DATABASE_NAME): DbAzureBlob().setupDatabase() conn = sqlite3.connect(DATABASE_NAME) timeout_time = datetime.utcnow() - timedelta(seconds=45) row = conn.execute("SELECT * FROM jobs WHERE processed = 0 or (processed = 1 and detected is NULL and processed_time < ?) ORDER BY RANDOM() LIMIT 1 ;", (timeout_time,)).fetchone() if(row == None ): return json.dumps({ 'filename':"NULL", 'processed':1, }) id = row[0] filename = row[1] conn.execute("UPDATE jobs set processed = 1, processed_time = ? where id = ? ;", (datetime.utcnow(), id)) conn.commit() conn.close() return Response(json.dumps({'id': id, 'filename': filename, 'processed': 0}), status=200, mimetype='application/json')
"detected":detected, "filename":urllib.parse.quote(filename), "start_time": start_time, "end_time": end_time, "worker_id": socket.gethostname() }) except: print("Failed to send response") #make a request jobserver_url = "http://" + os.getenv('IP_JOB_SERVER', "localhost") print("JOB SERVER URL: ", jobserver_url) counter = 0 dbHelper = DbAzureBlob() while True: response = getFilename(jobserver_url) if(response == False): print("Failed to get response from jobserver") time.sleep(1) continue if(response['processed'] == 1): print("response is processed") time.sleep(1) filename = response['filename'] realFilename = filename
#grab the filename request def sendRes(url, filename, detected): r = requests.get(url + "/processed", params={ "detected": detected, "filename": filename }) #make a request jobserver_url = "http://" + os.getenv('IP_JOB_SERVER', "localhost") print("JOB SERVER URL: ", jobserver_url) counter = 0 dbHelper = DbAzureBlob() while True: response = getFilename(jobserver_url) if (response == False): print("Failed to get response from jobserver") time.sleep(1) continue if (response['processed'] == 1): time.sleep(1) filename = response['filename'] realFilename = filename