def change_tuning_baseline(name, val): """change baseline info""" session = tables.get_session() if session is None: return try: tuning_table = TuningTable() tuning_table.update_baseline(name, val, session) session.commit() except SQLAlchemyError as err: LOGGER.error('Change tuning baseline value failed: %s', err) session.close()
def tuning_exist(name): """check if tuning exist""" exist = False session = tables.get_session() if session is None: return exist try: tuning_table = TuningTable() exist = tuning_table.check_exist_by_name(TuningTable, name, session) except SQLAlchemyError as err: LOGGER.error('Check if tuning exist failed: %s', err) session.close() return exist
def get_tuning_status(name): """get tuning status""" session = tables.get_session() if session is None: return 'break' status = 'break' try: tuning_table = TuningTable() status = tuning_table.get_field_by_name(TuningTable.tuning_status, name, session) except SQLAlchemyError as err: LOGGER.error('Get tuning status failed: %s', err) session.close() return status
def change_tuning_status(table_name, name): """change tuning_table status""" session = tables.get_session() if session is None: return None try: tuning_table = TuningTable() total_round = tuning_table.get_field_by_name(TuningTable.total_round, name, session) data_round = session.execute('select max(' + table_name + '._round) from ' + table_name).scalar() if total_round is not None and total_round == data_round: tuning_table.update_status(name, 'finished', session) session.commit() except SQLAlchemyError as err: LOGGER.error('Change tuning_table status failed: %s', err) session.close()
def user_ip_list(uid): """get all ip as list for given user""" session = tables.get_session() res = [] if session is None: return res try: ip_table = IpAddrs() res.extend(ip_table.get_ips_by_uid(uid, session)) except SQLAlchemyError as err: LOGGER.error('Get user ip list failed: %s', err) return [] finally: session.close() return res
def add_new_tuning(name, engine, rounds, tip): """add new tuning to tuning_table""" session = tables.get_session() if session is None: return try: ip_table = IpAddrs() if not ip_table.find_ip(tip, session): ip_table.insert_ip_by_user(tip, 0, session) tuning_table = TuningTable() tid = tuning_table.get_max_tid(session) + 1 tuning_table.insert_new_tuning(tid, name, engine, rounds, tip, session) session.commit() except SQLAlchemyError as err: LOGGER.error('Add new tuning to tuning_table failed: %s', err) session.close()
def create_admin(pwd): """create admin account""" session = tables.get_session() if session is None: return {'success': False, 'reason': 'failed'} try: user_account = UserAccount() user_account.insert_new_user(0, '*****@*****.**', 'Admin', pwd, session, 'admin') session.commit() except SQLAlchemyError as err: LOGGER.error('Create admin account failed: %s', err) return {'success': False, 'reason': 'failed'} finally: session.close() return {'success': True}
def add_ip(uid, ip_addrs): """add ip to table""" session = tables.get_session() if session is None: return False res = False try: ip_table = IpAddrs() if not ip_table.check_exist_by_ip(ip_addrs, uid, session): res = ip_table.insert_ip_by_user(ip_addrs, uid, session) session.commit() except SQLAlchemyError as err: LOGGER.error('Insert new ip failed: %s', err) return res finally: session.close() return res
def count_user(): """count the number of user in user_account table""" session = tables.get_session() if session is None: return 0 count = 0 try: user_account = UserAccount() uid = user_account.get_max_uid(session) if uid is None: return 0 return uid + 1 except SQLAlchemyError as err: LOGGER.error('Count user number failed: %s', err) return count finally: session.close() return count
def rename_tuning(name, new_name): """rename tuning""" session = tables.get_session() if session is None: return False, 'connect' try: tuning_table = TuningTable() if not tuning_table.check_exist_by_name(TuningTable, name, session): return False, 'tuning not exist' if tuning_table.check_exist_by_name(TuningTable, new_name, session): return False, 'duplicate' tuning_table.update_tuning_name(name, new_name, session) session.commit() except SQLAlchemyError as err: LOGGER.error('Rename tuning failed: %s', err) return False, 'error' finally: session.close() return True, ''
def ip_info_list(ip_addr): """get analysis and tuning list with ip_addr""" session = tables.get_session() if session is None: return {'getData': False} response = {} try: response['getData'] = True tuning_table = TuningTable() response['tuning'] = tuning_table.get_all_tunings_by_ip( ip_addr, session) collection_table = CollectionTable() response['analysis'] = collection_table.get_all_collection_by_ip( ip_addr, session) except SQLAlchemyError as err: LOGGER.error('Get analysis and tuning list failed: %s', err) return response finally: session.close() return response
def create_user(email, pwd, name): """create new user""" session = tables.get_session() if session is None: return False, False try: user_account = UserAccount() password = user_account.get_field_by_key(UserAccount.password, UserAccount.email, email, session) if password is not None: return False, True uid = user_account.get_max_uid(session) + 1 user_account.insert_new_user(uid, email, name, pwd, session) session.commit() except SQLAlchemyError as err: LOGGER.error('Create new user failed: %s', err) return False, False finally: session.close() return True, False
def add_tuning_data(name, iters, line): """add new round to tuning_data table""" session = tables.get_session() if session is None: return None try: tuning_table = TuningTable() table_name = str(tuning_table.get_field_by_name(TuningTable.tuning_id, name, session)) table_name = 'tuning_' + table_name keys, vals, pairs = table_tuning_data.execute_tuning_data(table_name, iters, line, session) if keys is None: LOGGER.info('Data in tuning_data does not match what desired') return None sql = 'insert into ' + table_name + ' ' + keys + ' values ' + vals session.execute(text(sql), pairs) session.commit() except SQLAlchemyError as err: LOGGER.error('Add new round to tuning_data table failed: %s', err) return None finally: session.close() return table_name
def get_tuning_list(uid, status): """get all tuning with status 'status' as a list""" session = tables.get_session() if session is None: return None try: ip_table = IpAddrs() tuning_table = TuningTable() ips = ip_table.get_ips_by_uid(uid, session) res = [] for tip in ips: if status == 'all': res.extend(tuning_table.get_all_tunings_by_ip(tip, session)) else: res.extend(tuning_table.get_status_tuning_by_ip(status, tip, session)) if len(res) > 0: res = sorted(res, key=(lambda x:x['date']), reverse=True) except SQLAlchemyError as err: LOGGER.error('Get tuning list failed: %s', err) return None finally: session.close() return res
def create_tuning_data_table(line): """create new tuning_data table""" session = tables.get_session() if session is None: return try: tuning_table = TuningTable() tid = tuning_table.get_max_tid(session) table_name = 'tuning_' + str(tid) metadata = MetaData() table, init_key, init_val, pairs = table_tuning_data.initial_table(table_name, metadata, line) if table is None: LOGGER.info('Data in tuning_data does not match what desired') return engine = create_engine(tables.get_db_url()) metadata.create_all(engine) sql = 'insert into ' + table_name + ' ' + init_key + ' values ' + init_val session.execute(text(sql), pairs) session.commit() except SQLAlchemyError as err: LOGGER.error('Create new tuning data table failed: %s', err) finally: session.close()
def change_user_pwd(uid, pwd, new_pwd): """check if user password match pwd""" session = tables.get_session() if session is None: return {'oldMatch': False} response = {} try: user_account = UserAccount() password = user_account.get_field_by_key(UserAccount.password, UserAccount.user_id, uid, session) if password is None or password != pwd: return {'oldMatch': False} response['oldMatch'] = True response['newMatch'] = user_account.update_password( uid, new_pwd, session) session.commit() except SQLAlchemyError as err: LOGGER.error('Check user password failed: %s', err) return response finally: session.close() return response
def get_tuning_info(status, name): """get info about tuning""" session = tables.get_session() if session is None: return {'isExist': False} response = {} try: tuning_table = TuningTable() response['status'] = status response['file_name'] = name response['engine'] = tuning_table.get_field_by_name(TuningTable.tuning_engine, name, session) response['round'] = tuning_table.get_field_by_name(TuningTable.total_round, name, session) response['base'] = tuning_table.get_field_by_name(TuningTable.baseline, name, session) tid = tuning_table.get_field_by_name(TuningTable.tuning_id, name, session) response['parameter'] = table_tuning_data.get_param_by_table_name('tuning_' + str(tid), session) response['line'] = 0 except SQLAlchemyError as err: LOGGER.error('Get tuning info failed: %s', err) return {'isExist': False} finally: session.close() response['isExist'] = True return response
def user_exist(email, pwd): """check if user exist""" session = tables.get_session() uid = -1 account_name = '' if session is None: return uid, account_name try: user_account = UserAccount() password = user_account.get_field_by_key(UserAccount.password, UserAccount.email, email, session) if password is not None and password == pwd: uid = user_account.get_field_by_key(UserAccount.user_id, UserAccount.email, email, session) account_name = user_account.get_field_by_key( UserAccount.account_name, UserAccount.email, email, session) except SQLAlchemyError as err: LOGGER.error('User login failed: %s', err) return -1, account_name finally: session.close() return uid, account_name
def get_tuning_data(status, name, line): """get each round data""" session = tables.get_session() if session is None: return {'isExist': False} response = {} try: tuning_table = TuningTable() response['status'] = status response['file_name'] = name response['initial_line'] = int(line) tid = tuning_table.get_field_by_name(TuningTable.tuning_id, name, session) total_round = tuning_table.get_field_by_name(TuningTable.total_round, name, session) table_name = 'tuning_' + str(tid) response['parameter'] = table_tuning_data.get_param_by_table_name(table_name, session) response['line'], response['cost'], response['data'] = \ table_tuning_data.get_tuning_data(total_round, table_name, line, session) except SQLAlchemyError as err: LOGGER.error('Get tuning data failed: %s', err) return {'isExist': False} finally: session.close() response['isExist'] = True return response