tfse-admin-api-v0.2/Board/board_impl.py

146 lines
5.2 KiB
Python
Raw Normal View History

2022-03-11 06:11:06 +08:00
import os
2022-03-28 15:00:12 +08:00
import re
2022-02-17 16:58:39 +08:00
import time
2022-03-28 15:00:12 +08:00
import json
2022-02-17 16:58:39 +08:00
import pandas as pd
2022-02-18 14:23:50 +08:00
from Board.board_db import FIND_DATA_COUNT, FIND_DATA, UPSERT_DATA, FIND_DATA_FILTER, FIND_DATA_FILTER_SORT_LIMIT, \
REMOVE_COLLECTION, INSERT_DATA_MANY, INSERT_DATA
2022-03-25 17:08:30 +08:00
from DBHelper.MongoHelper import MongoHelper
2022-02-17 16:58:39 +08:00
2022-02-16 17:03:12 +08:00
2022-03-25 17:08:30 +08:00
def get_monitor_data_impl():
db = MongoHelper("tfse_v0.21")
monitor_data = dict()
2022-02-17 16:58:39 +08:00
2022-03-25 17:08:30 +08:00
def log_data_impl():
monitor_data['异常日志'] = 0
2022-02-17 16:58:39 +08:00
2022-03-25 17:08:30 +08:00
def feedback_data_impl():
monitor_data['留言反馈'] = 0
2022-02-18 14:23:50 +08:00
2022-03-25 17:08:30 +08:00
def services_data_impl():
monitor_data['服务次数'] = db.find_all_data_with_count(
"企业数据",
"评价记录",
{"进行状态": "完成"}
)
2022-02-18 14:23:50 +08:00
2022-03-25 17:08:30 +08:00
def verified_company_impl():
monitor_data['认证企业'] = db.find_all_data_with_count(
"应用端",
"企业用户",
{"已认证": ""}
)
log_data_impl()
feedback_data_impl()
services_data_impl()
verified_company_impl()
return monitor_data
def get_rating_static_impl():
db = MongoHelper("tfse_v0.21")
2022-03-28 15:00:12 +08:00
# 日期正则表达式 20xx-xx-xx格式
# data_regex = "((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$"
# 日期匹配结果
# match_result = re.match(data_regex, start)
# 若没有匹配到需求格式日期则默认查询起始日期是30天以前
# if not match_result:
# start = time.strftime("%Y-%m-%d", time.localtime(time.time() - 60*60*24*30))
# 默认查询起始日期是30天以前
start = time.strftime("%Y-%m-%d", time.localtime(time.time() - 60*60*24*30))
2022-03-25 17:08:30 +08:00
records = db.find_data_with_aggregate(
"企业数据",
"评价记录",
[
2022-03-28 15:00:12 +08:00
{"$match": {"进行状态": "完成", "评价完成日期": {"$gte": start}}},
2022-03-25 17:08:30 +08:00
{"$group": {"_id": "$评价完成日期", "count": {"$sum": 1}}},
{"$sort": {"_id": 1}}
]
)
2022-03-28 15:00:12 +08:00
# 聚类统计用的_id字段更名为date
2022-03-25 17:08:30 +08:00
for data in records:
data['date'] = data.pop('_id')
return records
2022-02-18 14:23:50 +08:00
def static_dashboard_data_impl():
2022-02-17 16:58:39 +08:00
def monitor_data():
"""
监测数据
"""
data = dict()
data['认证企业'] = FIND_DATA_COUNT("TFSE", "用户", "用户信息", {"已认证": ""})
data['服务次数'] = FIND_DATA_COUNT("TFSE", "评价", "评价记录", {"进行状态": "完成"})
data['留言反馈'] = 0
data['异常日志'] = 0
2022-02-18 14:23:50 +08:00
REMOVE_COLLECTION('ADMIN', '看板', '监测数据')
INSERT_DATA("ADMIN", "看板", "监测数据", data)
2022-02-17 16:58:39 +08:00
def services_static():
"""
服务次数
delta_seconds 统计服务次数的时间范围
start_date_time_stamp 统计起始时间的时间戳
start_date 统计起始日期
"""
delta_seconds = 60*60*24*365
start_date_time_stamp = time.time() - delta_seconds
start_date = time.strftime("%Y-%m-%d", time.localtime(start_date_time_stamp))
records = FIND_DATA("TFSE", "评价", "评价记录", {"进行状态": "完成", "评价时间": {"$gt": start_date}})
df_rating_times = pd.DataFrame(pd.DataFrame(records)[['评价时间']].value_counts()).sort_index()
times_statics_by_date = dict(zip([tuple_date[0] for tuple_date in df_rating_times.index.values.tolist()], df_rating_times[0].tolist()))
for item in times_statics_by_date.items():
data = dict()
data['日期'] = item[0]
data['服务次数'] = item[1]
UPSERT_DATA("ADMIN", "看板", "服务统计", {"日期": item[0]}, data)
def industry_distribute():
"""
行业分布
"""
2022-03-11 06:11:06 +08:00
with open(os.path.abspath(os.path.dirname(__file__)+'/static/tyc_industry_II_to_I.json'), encoding='utf8') as f:
industry_refers = json.load(f)
2022-02-17 16:58:39 +08:00
records = FIND_DATA_FILTER("TFSE", "企业", "公司基本信息", {}, {"工商信息.行业": 1})
df_industry = pd.DataFrame([record['工商信息'] for record in records])
2022-03-11 06:11:06 +08:00
df_industry['行业'] = df_industry['行业'].apply(lambda x: industry_refers[x])
2022-02-17 16:58:39 +08:00
df_industry_count = df_industry['行业'].value_counts()
dict_industry = dict(zip(df_industry_count.index.tolist(), df_industry_count.values.tolist()))
for item in dict_industry.items():
data = dict()
data['行业名称'] = item[0]
data['公司数量'] = item[1]
UPSERT_DATA("ADMIN", "看板", "行业分布", {"行业名称": item[0]}, data)
def new_company_info():
"""
新增企业信息
"""
2022-02-18 14:23:50 +08:00
records = FIND_DATA_FILTER_SORT_LIMIT("TFSE", "用户", "用户信息", {"已认证": ""}, {"企业ID": 1, "企业名称": 1, "注册时间": 1}, [("注册时间", -1)], 10)
REMOVE_COLLECTION('ADMIN', '看板', '新增企业信息')
INSERT_DATA_MANY('ADMIN', '看板', '新增企业信息', records)
2022-02-17 16:58:39 +08:00
def main_process():
monitor_data()
services_static()
industry_distribute()
2022-02-18 14:23:50 +08:00
new_company_info()
2022-02-17 16:58:39 +08:00
return 0
2022-02-18 14:23:50 +08:00
return main_process()