def vk_stats(): db = DBVk() data = db.get_stats() res = '<h3>Stats:</h3>' for key in data: res += '- {}: {}<br>\n'.format(key, data[key]) res += '<br>\n<br>\n<h3>MUST_WORK: {}</h3>'.format(MUST_WORK) return res
def compares_face(): res = {} done = False user_id = request.form['user_id'] face, comment = get_request_face() if face is not None: db = DBVk() res['found'] = db.select_compare(face, user_id) done = True res['done'] = done res['comment'] = comment return json.dumps(res)
def get_task(): if not MUST_WORK: return '{"sleep":1}' worker = request.values['name'] ip = get_ip() db = DBVk() res = db.select_task_debt(worker) if res is not None: expire = db.update_task(res['id'], worker, ip, res['down'], res['up'], days=EXPIRE_DAYS) res['expire'] = expire res['comment'] = 'Your debt' else: res = db.select_task_free() if res is not None: expire = db.update_task(res['id'], worker, ip, res['down'], res['up'], days=EXPIRE_DAYS) res['expire'] = expire res['comment'] = 'Someone`s debt' else: ind, down = db.select_task_new() up = down - 1 + TASK_SIZE expire = db.insert_task(ind, worker, ip, down, up, days=EXPIRE_DAYS) res = { 'id': ind, 'worker': worker, 'ip': ip, 'down': down, 'up': up, 'expire': expire, } res['comment'] = 'New task' log_inf('GetTask: {}@{} got task ID {} as {}'.format( worker, ip, res['id'], res['comment'])) db.commit_db() return json.dumps(res)
def find_person(): # TODO: remove WHERE from this method res = {} done = False bits_threshold = int(request.form['bits_threshold'] ) if 'bits_threshold' in request.form else None where = request.form['where'] if 'where' in request.form else '' face, comment = get_request_face() if face is not None: db = DBVk() res['found'] = db.select_matches(face, bits_threshold=bits_threshold, where=where) done = True res['done'] = done res['comment'] = comment return json.dumps(res)
def find_threshold(): res = {} done = False comment = '' limit_first = int(request.form['limit_first']) limit_second = int(request.form['limit_second']) where = request.form['where'] if 'where' in request.form else '' face, comment = get_request_face() if face is not None: db = DBVk() res['found'] = db.select_threshold(values=face, limit_first=limit_first, limit_second=limit_second, where=where) done = True res['done'] = done res['comment'] = comment return json.dumps(res, default=str)
def vk_tasks(): db = DBVk() data = db.select_tasks_opened() res = '<h3>Opened tasks:</h3>' for row in data: res += str(row) + '<br>\n' res += '<br>\n<h3>Last seen:</h3>' now = int(time.time()) for worker, cur in sorted(activities_.items(), key=lambda x: x[0]): dif = now - cur if dif < 60 * 4: res += '- {}: {}'.format(worker, dif) elif dif < 60 * 60 * 24: res += '- {}: long time ago'.format(worker) else: res += '- {}: very long time ago'.format(worker) res += '<br>\n' times, notes = time_report() res += '<br>\n<h3>Part time:</h3>' for worker, cur in sorted(times.items(), key=lambda x: x[0]): if worker[0] == '_': res += '{}<br><br>\n'.format(cur) else: res += '{}: {}<br>\n'.format(worker, cur) res += '<br>\n<h3>Events:</h3>' for key, cnt in sorted(notes.items(), key=lambda x: x[0]): res += '{}: {}<br>\n'.format(key, cnt) res += '<br>\n<h3>MUST_WORK: {}</h3>'.format(MUST_WORK) res += '<br>\n<a href="/?work=1" target="_blank">Enable</a> ' res += '/ <a href="/?work=0" target="_blank">Disable</a>' return res
def f_kwargs_db(kwargs): kwargs['db'] = DBVk()
def test_load(): db = DBVk() res = person2db(184639, 7255377, 35462, db=db, vka=VkAccess(VK_TOKEN)) print(res) person2db(people, db=db) files2db(db=db, path='xxxxx')
def submit_task(): res = {} comment = '' if not MUST_WORK: code = 0 else: code = 2 # code = 3 if len(request.files) == 0: comment = 'file not found' elif len(request.files) > 1: comment = 'too many files' else: file = None for key in request.files: file = request.files[key] break data = None try: data = load_pickle(file) except: pass try: if data is not None and isinstance( data, dict) and 'vk_task_id' in data: ip = get_ip() worker = request.form['name'] ind = data['vk_task_id'] down = int(data['vk_from']) up = int(data['vk_to']) label = 'Task {} from {} to {}'.format(ind, down, up) label += ' Rows: {}'.format(len(data['data'])) res['file'] = label db = DBVk() can_load = False meta = db.select_task_id(ind) if meta is None: comment = 'task was not found' else: if meta['up'] == meta['down']: comment = 'task is already completed' code = 3 elif meta['worker'] != worker: comment = 'different name' log_er( 'SubmitTask: {}@{} tries to submit task of {}, ID is {}' .format(worker, ip, meta['worker'], ind)) elif down - meta['down'] < 3: can_load = True else: comment = 'need user_id since {}, not {}'.format( meta['down'], down) if can_load: for row in data['data']: # print('\n- Row:') # print(row) db.insert_features(row[0], row[1], row[2]) new_down = min(up, meta['up']) db.update_task(ind, worker, ip, new_down, meta['up'], days=EXPIRE_DAYS) db.commit_db() code = 1 comment = 'very well ' + str(len(data['data'])) log_inf( 'SubmitTask: {}@{} loaded task ID {} from {} to {}' .format(worker, ip, ind, down, up)) now = int(time.time()) if worker in activities_: time_log(worker, now - activities_[worker], up - down, data.get('notes')) activities_[worker] = now else: log_inf( 'SubmitTask: {}@{} cannot load task ID {} from {} to {} because "{}"' .format(worker, ip, ind, down, up, comment)) except Exception as ex: log_ex(ex, label, show=1) comment = 'very bad file' res['code'] = code res['descr'] = SUBMIT_CODES.get(code, '?') res['comment'] = comment # print(res) return json.dumps(res)