def get_all_users(): today = datetime.date.today().strftime("%Y%m%d") vpmn = get_vpmn_user() if get_vpmn_user() else 0 pyq = get_pyq_user() if get_pyq_user() else 0 hjh = get_hjh_user() if get_hjh_user() else 0 crbt = get_crbt_user() if get_crbt_user() else 0 vrbt = get_vrbt_user() if get_vrbt_user() else 0 ctx_only = get_ctxonly_user() if get_ctxonly_user() else 0 ctx_group = get_ctx_group() if get_ctxonly_user() else 0 # 转换格式,方便直接入库,key与数据库中表字段一致 users_db = { 'date': today, 'vpmn_volte': vpmn, 'crbt_volte': crbt, 'hjh_volte': hjh, 'pyq_volte': pyq, 'ctx_group': ctx_group, 'ctx_user': ctx_only, 'vrbt': vrbt } try: db.insert('users',**users_db) except Exception as e: logging.error("用户数入库错误" + str(e)) return users_db
def create(i): isint(i.userid) isvarchar(i.name,1,50) return db.insert('groups', userid=i.userid,name=i.name,createdate=datetime.now(), _test=is_debug)
def register(i): isvarchar(i.email,1,50) isvarchar(i.password,1,100) return db.insert('users', id=i.id,email=i.email,password=i.password,registerdate=datetime.now(),status=1, _test=is_debug)
def create(uid,gid,content): isbigint(uid) isbigint(gid) istext(gid) summary = content[:150] return db.insert('notes', groupid=gid,userid=uid,notes=content,summary= summary,createdate=datetime.now(),status=1, _test=is_debug)
def get_v_users(filename): today = datetime.date.today().strftime("%Y%m%d") all_users = get_data(filename) user_db = {} user_db['date'] = today for x in all_users: try: x.remove('') except ValueError: pass user_type = x[0].strip(' ') if user_type in config.users_tilte.keys(): user_name = config.users_tilte.get(user_type) user_value = int(x[1].strip(' ')) user_db[user_name] = user_value crbt_temp = get_crbt_user() crbt = crbt_temp if crbt_temp else 0 vrbt_temp = get_vrbt_user() vrbt = vrbt_temp if vrbt_temp else 0 ctx_only_temp = get_ctxonly_user() ctx_only = ctx_only_temp if ctx_only_temp else 0 ctx_group_temp = get_ctx_group() ctx_group = ctx_group_temp if ctx_group_temp else 0 crbt23g = get_crbt23g_user() newcy, ccp = get_cy_ccp() scim = get_scim_user() user_db.update({ 'crbt_volte': crbt, 'vrbt': vrbt, 'ctx_user': ctx_only, 'ctx_group': ctx_group, 'crbt_23g': crbt23g, 'newcy': newcy, 'ccp': ccp, 'scim': scim }) try: db.insert('users',**user_db) except Exception as e: logging.error("用户数入库错误" + str(e)) return user_db
def get_vsstj(filename): vss_db = {} yesterday = (datetime.date.today() - datetime.timedelta(days=1)) vss_db['date'] = yesterday.strftime("%Y%m%d") with open(filename) as file: # 提取一周活性用户数 hx_user_temp = blocks(file, keywords="hx_user") hx_user = float(hx_user_temp[-1].split(" ")[0]) # 提取短号短信总下发量 vss_data = blocks(file, keywords="---VSS") vss_total = sum([float(x.split(" ")[-1]) for x in vss_data[1:]]) # 提取短号彩信总下发量 mms_data = blocks(file, keywords="---MMS") mms_total = sum([float(x.split(" ")[-1]) for x in mms_data[1:]]) # 数据入库 vss_db['hx_user'] = hx_user vss_db['vss_total'] = vss_total vss_db['mms_total'] = mms_total try: db.insert(table="vss", **vss_db) except Exception as e: logging.error("短号短信数据入库失败: " + str(e))
def get_caps(filename): yesterday = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d") caps_data = [] caps_db = {} record_data = get_data(filename) for value in record_data: caps_name = config.caps_tilte.get(value[0]) try: caps_value = float(value[1]) except ValueError: # 跳过数据为空 caps_value = "" caps_data.append( [caps_name, caps_name.split('_')[0].upper(), caps_value]) caps_db[caps_name] = caps_value caps_db['date'] = yesterday try: db.insert("caps", **caps_db) except Exception as e: logging.error("CAPS数据入库错误" + str(e)) return caps_data
def create(uid, gid, content): isbigint(uid) isbigint(gid) istext(gid) summary = content[:150] return db.insert('notes', groupid=gid, userid=uid, notes=content, summary=summary, createdate=datetime.now(), status=1, _test=is_debug)
def upload(): file = request.files['file'] if file and allowed_file(file.filename): filename = randFileName(file.filename) fullPath = os.path.join(savePath(), filename) file.save(fullPath) key = '' while True: key = encode(int("{}{}".format(curNum(), ranNum()))) if not db.select(key): break db.insert({ 'id': key, 'fileName': file.filename, 'realPath': fullPath, 'mime': file.content_type, 'fileSize': os.stat(fullPath).st_size }) return key
'''TEST DB''' dados = { "airline": "azul", "airport_origin": "CWB", "airport_destination": "VCP", "date_departure": "2014-01-01", "date_return": "2014-01-10", "weekday": "Monday", "min": 100, "avg": 150, "max": 200 } db.insert("RangeSearch", dados) db.selectAll("RangeSearch", ["*"]) test_cases = { (2013, 12, 12): calendar.date_split("2013-12-12"), (2013, 12, 12): calendar.date_split("2013/12/12", separator="/"), (0000, 00, 00): calendar.date_split("0000-00-00"), (0000, 0, 120): calendar.date_split("0000#0#120", separator="#"), 1: calendar.datecmp("2013-10-30", "2012-10-10"), -1: calendar.datecmp("2011-10-10", "2012-10-10"), 0: calendar.datecmp("2013-10-10", "2013-10-10"), "2013-11-30": calendar.date_to_str(datetime.datetime(2013, 11, 30)), "2013-11-10": calendar.date_to_str(datetime.datetime(2013, 11, 10)), "Saturday": calendar.date_to_weekday("2013-11-30"), "Sunday": calendar.date_to_weekday("2013-12-01"),
def cpu_analyse(cpu_file): title = ['网元', '主机', 'CPU', '内存', 'IO'] data = TxtParse(cpu_file, sep='|', titles=title) df = data.get_df() # 增加对应的网元类型 df["网元类型"] = list(map(lambda x: get_cluster_type(x), df["网元"])) # 数据分割,当天,前一天,前一周 num = df.index[df['网元'].isin(["全网昨天CPU&内存数据获取"]) == True].tolist()[0] num1 = df.index[df['网元'].isin(["全网前天CPU&内存数据"]) == True].tolist()[0] num2 = df.index[df['网元'].isin(["全网前一周CPU&内存数据"]) == True].tolist()[0] # 昨天AS主机性能数据 df_td = df.loc[:num - 1] # 全网昨天性能数据 df_ytd = df.loc[num + 1:num1 - 1] # 全网前天性能数据 df_b4ytd = df.loc[num1 + 1:num2 - 1] # 全网前一周性能数据 df_wk = df.loc[num2 + 1:] # 将数据进行分组处理,并转成list,方便生成HTML # list_td_grp, list_td_grp_html = get_max_cpu_host(df_td, col="网元") list_td_grp_html = get_max_cpu_host(df_td, col="网元") # 数据入库 yesterday = datetime.date.today() - datetime.timedelta(days=1) yesterday = yesterday.strftime("%Y%m%d") for values in list_td_grp_html[1:]: # 数值使用float转换,否则插入数据库Mysql会报错 db.insert("as_pfmc", date=yesterday, cluste=values[0], max_cpu=float(values[1]), max_cpu_host=values[2], max_mem=float(values[3]), max_mem_host=values[4], max_io=float(values[5]), max_io_host=values[6]) list_ytd_grp = get_max_cpu(df_ytd, col="网元类型") list_b4ytd_grp = get_max_cpu(df_b4ytd, col="网元类型") list_wk_grp = get_max_cpu(df_wk, col="网元类型") # 转为DataFrame df_ytd_grp = pd.DataFrame(list_ytd_grp[1:], columns=["网元类型", "CPU", "MEM", "IO"]) df_b4ytd_grp = pd.DataFrame(list_b4ytd_grp[1:], columns=["网元类型", "CPU", "MEM", "IO"]) df_wk_grp = pd.DataFrame(list_wk_grp[1:], columns=["网元类型", "CPU", "MEM", "IO"]) # 昨天、前天、上周数据合并为一张表 df_temp = pd.merge(df_b4ytd_grp, df_wk_grp, how="left", on="网元类型") df_merge = pd.merge(df_ytd_grp, df_temp, how="left", on="网元类型") # 计算同比增长(前一周比较) df_merge["t_cpu_growth"] = (df_merge["CPU"] - df_merge["CPU_y"]) / df_merge["CPU_y"] df_merge["t_mem_growth"] = (df_merge["MEM"] - df_merge["MEM_y"]) / df_merge["MEM_y"] # 计算环比增长(前一天比较) df_merge["h_cpu_growth"] = (df_merge["CPU"] - df_merge["CPU_x"]) / df_merge["CPU_x"] df_merge["h_mem_growth"] = (df_merge["MEM"] - df_merge["MEM_x"]) / df_merge["MEM_x"] # 将小数转换成百分数 for col in ["t_cpu_growth", "t_mem_growth", "h_cpu_growth", "h_mem_growth"]: df_merge[col] = df_merge[col].apply(lambda x: format(x, ".2%")) # 将CPU MEM统一保留2位小数 for col in ["CPU", "MEM"]: df_merge[col] = df_merge[col].apply(lambda x: format(x, ".2f")) # 输出表格 output_data = [["网元类型", "性能指标分析"]] for i in range(len(df_merge.index)): str = "CPU占用: 最高 {}% ,同比(前一周同一天的数据对比){} ,环比(前一天数据对比){}, 内存占用: 最高 {}% ,同比{} ,环比\ {}".format(df_merge.ix[i]["CPU"],check(df_merge.ix[i]["t_cpu_growth"]),check(df_merge.ix[i]["h_cpu_growth"]),df_merge.ix[i]["MEM"],check(df_merge.ix[i]["t_mem_growth"]),check(df_merge.ix[i]["h_mem_growth"])) output_data.append([config.cluters_map.get(df_merge.ix[i]["网元类型"]), str]) t_start = '<table class="tableizer-table" cellspacing=0 width="60%";>\n' t_end = '</table>\n' zyjh_html = parseHtml(output_data, "作业计划指标") cpu_today_html = parseHtml(list_td_grp_html, "AS主机昨日性能指标") cpu_yes_html = parseHtml(list_ytd_grp, "全网主机昨日性能指标") cpu_html = cpu_today_html + t_end + "<br></br>" + t_start + cpu_yes_html return zyjh_html, cpu_html