Commit 07446ae5 by 陈森彬

测试

parent 34c195bb
Showing with 41 additions and 30 deletions
......@@ -6,8 +6,10 @@ import pymongo
from utils.glo import Glo
from urllib.parse import quote_plus
from utils.mysql import MySqlOperator
glo =Glo()
mysql_db = MySqlOperator('dashboard')
glo = Glo()
MG_HOST_SET = {
'test': '192.168.1.237',
......@@ -30,6 +32,9 @@ def get_mongo_conf():
return conf
filter_pn = ("cdi", "richardsonrfpd", "company", "wpg",)
class MongoOperator:
def __init__(self):
......@@ -41,19 +46,19 @@ class MongoOperator:
# element14 chip1stop ti
self.colls = ('alliedelec', 'arrow', 'avnet', 'buerklin', 'digikey', 'master', 'rs', 'rochester',
'verical', "powell", 'microchip', 'tme', 'heilind', 'maxim', 'aipco', 'company', 'rutronik',
'mouser', 'corestaff', 'wpg', 'szlc', 'element14', 'chip1stop', 'future',"nep","cdi",
"nac","richardsonrfpd","pui")
'mouser', 'corestaff', 'wpg', 'szlc', 'element14', 'chip1stop', 'future', "nep", "cdi",
"nac", "richardsonrfpd", "pui")
@staticmethod
def get_unexpired_time(plat):
if plat == 'powell':
ts = int(time.time()) - 3600 * 24 * 7 # 5d
elif plat in ('digikey',):
elif plat in ('digikey', 'avnet',):
ts = int(time.time()) - 3600 * 72 # 72h
elif plat in ('microchip', 'heilind', 'element14', 'chip1stop', 'future'):
elif plat in ('microchip', 'heilind', 'element14', 'chip1stop', 'future',):
ts = int(time.time()) - 3600 * 24 # 24h
elif plat in ("richardsonrfpd",):
ts = int(time.time()) - 3600 * 96 # 96h
else:
ts = int(time.time()) - 3600 * 48 # 48
return ts
......@@ -70,15 +75,16 @@ class MongoOperator:
@staticmethod
def get_mongo_query():
stock_query ={"stock.1":{"$lte":0}} #获取库存为零
tiered_query ={"tiered": []} #获取价格阶梯为空
stock_query = {"stock.1": {"$lte": 0}} # 获取库存为零
tiered_query = {"tiered": []} # 获取价格阶梯为空
total_query = {}
return stock_query,tiered_query,total_query
return stock_query, tiered_query, total_query
def get_platform_data_expired_rate(self):
msg = '数据过期率监控预警 过去3小时内:\n'
demo = '平台:{} 数据过期率为{:.2f}%'
msgs = []
expired_dict = {}
for plat in self.colls:
coll = self.db[plat]
ts = self.get_unexpired_time(plat)
......@@ -99,9 +105,13 @@ class MongoOperator:
msg_line = demo.format(plat, rate)
msgs.append(msg_line)
msg += msg_line + '\n'
expired_dict[plat] = "{:.2f}".format(rate)
else:
print(plat, expired, total)
if msgs:
ts = int(time.time())
for k, v in expired_dict.items():
mysql_db.insert_expired_data(ts, k, v)
return msg
else:
return
......@@ -109,41 +119,43 @@ class MongoOperator:
def get_platform_none_data_rate(self):
msg = '空数据预警 过去3小时内:\n'
demo1 = '平台:{} 第一次运行,库存空数据率为{:.2f}%,价格空数据率为{:.2f}%'
demo2 = '平台:{} 相比上一次监控库存空数据率变化为{:.2f}%,价格空数据率变化为{:.2f}%'
demo2 = '平台:{} 相比上一次监控库存空数据率变化为{:.2f}%,价格空数据率变化为{:.2f}%,当前空库存率为{:.2f}%,当前空价格率为{:.2f}%'
msgs = []
for plat in self.colls:
coll = self.db[plat]
snq, tnq,tq = self.get_mongo_query()
snq, tnq, tq = self.get_mongo_query()
stock_num = coll.count(snq)
tiered_num = coll.count(tnq)
total = coll.count(tq)
if total > 0 :
if total > 0:
stock_rate = stock_num / total * 100
tiered_rate = tiered_num / total * 100
old_stock_rate = glo.get_value(plat)["stock"] if glo.get_value(plat)else 0
old_tiered_rate = glo.get_value(plat)["tiered"] if glo.get_value(plat) else 0
old_stock_rate = glo.get_value(plat)["stock"] if glo.get_value(plat) else None
old_tiered_rate = glo.get_value(plat)["tiered"] if glo.get_value(plat) else None
if stock_rate >= 10 or tiered_rate >= 10:
msg_line =""
if old_stock_rate ==0 :
msg_line = ""
if old_stock_rate is None or old_tiered_rate is None:
print(plat, "空库存数量", stock_num, "空价格数量", stock_num, "总量", total
, "空库存率", stock_rate, "空价格率", tiered_rate, )
msg_line = demo1.format(plat, stock_rate,tiered_rate)
msg_line = demo1.format(plat, stock_rate, tiered_rate)
else:
stock__ontrast_ratio = stock_rate - old_stock_rate
tiered__ontrast_ratio = tiered_rate - old_tiered_rate
if not -10 < stock__ontrast_ratio < 10 or not -10 < tiered__ontrast_ratio < 10 :
msg_line = demo2.format(plat, stock__ontrast_ratio, tiered__ontrast_ratio)
print(plat, "空库存数量", stock_num, "空价格数量", stock_num, "总量", total , "空库存率",
stock_rate, "空价格率", tiered_rate, "空库存变化率", stock__ontrast_ratio,
"空价格变化率", tiered__ontrast_ratio,)
glo.set_value(plat, {"stock":stock_rate,"tiered":tiered_rate})
stock_ontrast_ratio = stock_rate - old_stock_rate
tiered_ontrast_ratio = tiered_rate - old_tiered_rate
if not -3 < stock_ontrast_ratio < 3 or not -3 < tiered_ontrast_ratio < 3 or stock_rate > 90 or tiered_rate > 90:
if plat not in filter_pn:
msg_line = demo2.format(plat, stock_ontrast_ratio, tiered_ontrast_ratio, stock_rate,
tiered_rate)
print(plat, "空库存数量", stock_num, "空价格数量", stock_num, "总量", total, "空库存率",
stock_rate, "空价格率", tiered_rate, "空库存变化率", stock_ontrast_ratio,
"空价格变化率", tiered_ontrast_ratio, )
glo.set_value(plat, {"stock": stock_rate, "tiered": tiered_rate})
if msg_line:
msgs.append(msg_line)
msg += msg_line + '\n'
else:
print(plat,"空库存数量",stock_num,"空价格数量",stock_num,"总量",total)
print(plat, "空库存数量", stock_num, "空价格数量", stock_num, "总量", total)
if msgs:
return msg
else:
return
\ No newline at end of file
return
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment