def cmd_update_stat(): if get_update_running_state_done(): # flash('后台任务未结束,请稍后再试') pass else: # type of fcode default is str, not int # fcode = request.args.get('fcode', type=int) or request.form.get('fcode', type=int) # fcode = request.form.get('fcode', type=int) fcode = request.args.get('fcode', type=int) update_sqlite_stat(fcode) # flash('数据已开始后台更新,请稍后刷新查看') # record oplog userid = current_user.id fcode = None opcode = 4 opcount = None opmsg = 'update statistic data' datetime_obj = get_datetime_now_obj() kwargs_oplog = { 'userid': userid, 'fcode': fcode, 'opcode': opcode, 'opcount': opcount, 'opmsg': opmsg, 'timestamp': datetime_obj, } insert_operation_log(**kwargs_oplog) return redirect(url_for('blue_rasp.vf_stat'))
def post(self): try: # method style 1 # data = json.loads(request.get_data(as_text=True)) # method style 2 data = request.get_json() fcode = data.get('fcode') # opcode = data.get('opcode') opcode = 2 opcount = data.get('opcount') opmsg = data.get('opmsg') # timestamp = data.get('timestamp') timestamp = get_datetime_now_obj() kwargs_oplog = { 'userid': None, 'fcode': fcode, 'opcode': opcode, 'opcount': opcount, 'opmsg': opmsg, 'timestamp': timestamp, } # print('==kwargs_oplog==', kwargs_oplog) insert_operation_log(**kwargs_oplog) resp_obj = { 'errno': 0, 'msg': 'rasp upgrade notice success', } except Exception as e: logger.error('rasp upgrade notice error') logger.error(e) resp_obj = { 'errno': 1, 'msg': 'rasp upgrade notice fail', } return resp_obj
def logout(): # record oplog userid = current_user.id fcode = None opcode = 6 opcount = None opmsg = '' datetime_obj = get_datetime_now_obj() kwargs_oplog = { 'userid': userid, 'fcode': fcode, 'opcode': opcode, 'opcount': opcount, 'opmsg': opmsg, 'timestamp': datetime_obj, } insert_operation_log(**kwargs_oplog) logout_user() return redirect(url_for('blue_auth.login'))
def cmd_restart(): # record oplog userid = current_user.id fcode = None opcode = 102 opcount = None opmsg = 'restart gecloud service' datetime_obj = get_datetime_now_obj() kwargs_oplog = { 'userid': userid, 'fcode': fcode, 'opcode': opcode, 'opcount': opcount, 'opmsg': opmsg, 'timestamp': datetime_obj, } insert_operation_log(**kwargs_oplog) cmd = 'systemctl restart gecloud.service' ret = my_check_retcode(cmd) return redirect(url_for('blue_account.admin', info=ret))
def cmd_reset_runningstates(): reset_update_running_state_done() info = 'r_update_sqlite_stat_running: {}'.format( get_update_running_state_done()) # record oplog userid = current_user.id fcode = None opcode = 101 opcount = None opmsg = 'reset running parameters' datetime_obj = get_datetime_now_obj() kwargs_oplog = { 'userid': userid, 'fcode': fcode, 'opcode': opcode, 'opcount': opcount, 'opmsg': opmsg, 'timestamp': datetime_obj, } insert_operation_log(**kwargs_oplog) return redirect(url_for('blue_account.admin', info=info))
def login(): if request.method == 'GET': next_page = request.args.get('next') return render_template('auth_login.html', next_page=next_page) username = request.form.get('username') password = request.form.get('password') next_page = request.form.get('next') # user = User.query.filter_by(username=username, password=password).first() user = User.query.filter_by(username=username).first() if not user or not user.verify_password(password): logger.warn('[login] {} login failed'.format(username)) return render_template('auth_login.html', warning="login failed!") login_user(user) logger.info('[login] {} login success'.format(username)) # record oplog userid = current_user.id fcode = None opcode = 5 opcount = None opmsg = '' datetime_obj = get_datetime_now_obj() kwargs_oplog = { 'userid': userid, 'fcode': fcode, 'opcode': opcode, 'opcount': opcount, 'opmsg': opmsg, 'timestamp': datetime_obj, } insert_operation_log(**kwargs_oplog) if next_page: return redirect(next_page) else: return redirect(url_for('blue_main.vf_index'))
def put(self): # get current datetime cur_datetime = get_datetime_now_obj() try: # data = json.loads(request.get_data()) # data = json.loads(request.get_data().decode('utf-8')) data = json.loads(request.get_data(as_text=True)) except Exception as e: print(e) response_msg = {'errno': 5, 'msg': 'load request data error'} logger.error('response_msg: {}'.format(response_msg)) # return resp return response_msg try: fcode = data.get('fcode') except Exception: response_msg = {'errno': 1, 'msg': 'get fcode error type1'} logger.error('response_msg: {}'.format(response_msg)) # return resp return response_msg else: if fcode is None: try: fcode = data.get('testdatas')[0].get('factorycode') except IndexError as e: response_msg = {'errno': 2, 'msg': 'get fcode error type2'} logger.error('response_msg: {}'.format(response_msg)) # return resp return response_msg pin = data.get('pin') testdatas = data.get('testdatas') num_recv_1 = data.get('count') num_recv_2 = len(testdatas) if not num_recv_1 == num_recv_2: response_msg = { 'errno': 3, 'fcode': fcode, 'msg': 'count number mismatch' } logger.error('response_msg: {}'.format(response_msg)) #record oplog kwargs_oplog = { 'userid': None, 'fcode': fcode, 'opcode': 1, 'opcount': None, 'opmsg': 'error: count number mismatch', 'timestamp': cur_datetime, } insert_operation_log(**kwargs_oplog) # return resp return response_msg num_recv = num_recv_1 try: save_to_database(testdatas, num_recv) # 1. exception except Exception as e: response_msg = {'errno': 4, 'fcode': fcode, 'msg': str(e)} logger.error('response_msg: {}'.format(response_msg)) #record oplog kwargs_oplog = { 'userid': None, 'fcode': fcode, 'opcode': 1, 'opcount': num_recv, 'opmsg': 'error: save to database failed', 'timestamp': cur_datetime, } insert_operation_log(**kwargs_oplog) # return resp return response_msg # 2. success else: # 2.1 update upload time if fcode != None and num_recv > 0: update_sqlite_lastuploadtime(fcode, cur_datetime) # 2.2 record oplog database # opcode = 1 # opcount = num_recv # opmsg = 'upload success' # timestamp = cur_datetime # insert_operation_log(fcode, opcode, opcount, opmsg, timestamp) kwargs_oplog = { 'userid': None, 'fcode': fcode, 'opcode': 1, 'opcount': num_recv, 'opmsg': 'upload success', 'timestamp': cur_datetime, } insert_operation_log(**kwargs_oplog) # 2.3 record log file response_msg = { 'errno': 0, 'fcode': fcode, 'msg': 'upload success', 'pin': pin, 'count': num_recv } logger.info('response_msg: {}'.format(response_msg)) return response_msg
def cmd_download_testdata(): # 1. determine csv or excel download_type = request.form.get('download_type', type=str) if download_type not in ['csv', 'xls']: abort() # 2. fetch fcode from request.form and session try: fcode = fetch_fcode() except KeyError as e: # logger.error(e) logger.error('KeyError session["fcode"]') return redirect(url_for('blue_rasp.vf_stat')) except FcodeNotSupportError as e: logger.warn(str(e)) return redirect(url_for('blue_rasp.vf_stat')) # 3. get testdatscloud basic query myquery_mysql_testdatascloud = forge_myquery_mysql_testdatascloud_by_fcode( g.myquery_mysql_testdatascloud, fcode) # 4. search handling code # for download, fetch_clearsearchsession will always return False # clearsearchsession = fetch_clearsearchsession() clearsearchsession = None search_kwargs_page, search_kwargs_db = fetch_search_kwargs_testdatas( clearsearchsession) search_args_db = list(search_kwargs_db.values()) # if any of search params is not None, filter further # if any(search_args): if len(list(filter(lambda x: x is not None, search_args_db))) > 0: myquery_mysql_testdatascloud = forge_myquery_mysql_testdatascloud_by_search( myquery_mysql_testdatascloud, **search_kwargs_db) total_count = myquery_mysql_testdatascloud.count() if total_count >= 65535: return redirect(url_for('blue_error.vf_downloadoverflow')) # 5. gen csv/excel file and return # timestamp = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S') datetime_obj = get_datetime_now_obj() timestamp = datetime_obj.strftime('%Y_%m_%d_%H_%M_%S') shortname = 'TestdataCloud-' + timestamp + '.' + download_type genfolder = os.path.join(topdir, 'pub', download_type) filename = os.path.join(genfolder, shortname) empty_folder_filesonly(genfolder) if download_type == 'csv': gen_csv_by_query(myquery_mysql_testdatascloud, filename) # todo elif download_type == 'xls': pass else: pass # 普通下载 # return send_from_directory(genfolder, excelname, as_attachment=True) # 流式读取 response = Response(send_file(filename), content_type='application/octet-stream') response.headers[ "Content-disposition"] = 'attachment; filename=%s' % shortname # 6. record oplog # timestamp = get_datetime_now_obj() userid = current_user.id fcode = None opcode = 3 opcount = total_count opmsg = 'download csv' kwargs_oplog = { 'userid': userid, 'fcode': fcode, 'opcode': opcode, 'opcount': opcount, 'opmsg': opmsg, 'timestamp': datetime_obj, } insert_operation_log(**kwargs_oplog) # 7. return return response