Browse Source

增加统计数据文档

Eric412V 3 years ago
parent
commit
7f5c49e14b
2 changed files with 260 additions and 0 deletions
  1. 120 0
      LIB/MIDDLE/SaftyCenter/DataSta/DataStatistics.py
  2. 140 0
      LIB/MIDDLE/SaftyCenter/DataSta/main.py

+ 120 - 0
LIB/MIDDLE/SaftyCenter/DataSta/DataStatistics.py

@@ -0,0 +1,120 @@
+import numpy as np
+import pandas as pd
+from User.eric.Common import FeiShuData
+from User.eric.Common import DBDownload
+import time, datetime
+from datetime import timedelta
+from pandas.core.frame import DataFrame
+from apscheduler.schedulers.blocking import BlockingScheduler
+
+class DataSta():
+    def __init__(self) -> None:
+        pass
+    def SaftyWarningSta(CS_Data,df_fltinfo,start_time,end_time):
+        SftyPlt_Data_Total=len(df_fltinfo)#总报警数
+        CS_Warning_Total_Finish=CS_Data[CS_Data['状态']=='已完结']
+        CS_Warning_Total_Finish=CS_Warning_Total_Finish[CS_Warning_Total_Finish['业务分类A']=='安全平台报警']
+        CS_Warning_Total_Finish_Count=len(CS_Warning_Total_Finish) #平台报警总运维数
+        SftyPlt_Data_day=len(df_fltinfo[df_fltinfo['start_time']>=start_time])#周总报警数
+        CS_Warning_day_Finish=CS_Warning_Total_Finish[CS_Warning_Total_Finish['发生时间']>=start_time]#周运维数
+        CS_Warning_day_Finish_Count=len(CS_Warning_day_Finish)
+        SftyPlt_EmgcyData_day=df_fltinfo[df_fltinfo['start_time']>=start_time] #周紧急报警数
+        SftyPlt_EmgcyData_day=len(SftyPlt_EmgcyData_day[SftyPlt_EmgcyData_day['level']>3])
+        SftyPlt_EmgcyData_day_Finish=CS_Warning_day_Finish[CS_Warning_day_Finish['运维紧急程度']=='紧急']
+        SftyPlt_EmgcyData_day_Finish_Count=len(SftyPlt_EmgcyData_day_Finish)
+        if SftyPlt_Data_Total>0:
+            OprationManageRate=round(float(CS_Warning_day_Finish_Count/SftyPlt_Data_day)*100,2)
+        else:
+            OprationManageRate=100
+        if SftyPlt_EmgcyData_day>0:
+            OprationManageEmgcyRate=round(float(SftyPlt_EmgcyData_day_Finish_Count/SftyPlt_EmgcyData_day)*100,2)
+        else:
+            OprationManageEmgcyRate=100
+        PK504FltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('PK504')])
+        PK502FltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('PK502')])
+        PK500FltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('PK500')])
+        MGMCLFltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('MGMCL')])
+        MGMLXFltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('MGMLX')])
+        # print('总报警数=',SftyPlt_Data_Total)
+        # print('总完成运维数=',CS_Warning_Total_Finish_Count)
+        # print('周报警数=',SftyPlt_Data_day)
+        # print('周完成运维数=',CS_Warning_day_Finish_Count)
+        # print('周紧急报警数=',SftyPlt_EmgcyData_day)
+        # print('周紧急完成运维数=',SftyPlt_EmgcyData_day_Finish_Count)
+        # print('周运维率=',OprationManageRate)
+        # print('周紧急运维率=',OprationManageEmgcyRate)
+        # print('PK504总报警=',PK504FltData_Count)
+        # print('PK502总报警=',PK502FltData_Count)
+        # print('PK500总报警=',PK500FltData_Count)
+        # print('格林美总报警=',MGMCLFltData_Count)
+        # print('自研总报警=',MGMLXFltData_Count)
+        FltAlarmInfo=DataFrame(columns=['SftyPlt_Data_Total','CS_Warning_Total_Finish_Count','SftyPlt_Data_day','CS_Warning_day_Finish_Count','SftyPlt_EmgcyData_day','SftyPlt_EmgcyData_day_Finish_Count','OprationManageRate','OprationManageEmgcyRate'])
+        Celltype=DataFrame(columns=['PK504','PK502','PK500','MGMCL','MGMLX'])
+        FltAlarmInfo.loc[0]=[SftyPlt_Data_Total,CS_Warning_Total_Finish_Count,SftyPlt_Data_day,CS_Warning_day_Finish_Count,SftyPlt_EmgcyData_day,SftyPlt_EmgcyData_day_Finish_Count,OprationManageRate,OprationManageEmgcyRate]
+        Celltype.loc[0]=[PK504FltData_Count,PK502FltData_Count,PK500FltData_Count,MGMCLFltData_Count,MGMLXFltData_Count]
+        return FltAlarmInfo,Celltype
+    def WeekInfoSta(df_fltinfo,start_time,end_time):
+        df_fltinfo=df_fltinfo[df_fltinfo['start_time']>=start_time]
+        FaultLvlCount=DataFrame(columns=['level','count'])
+        FaultLvlCount=df_fltinfo.groupby('level').count().T.head(1).T
+        FaultLvlCount=FaultLvlCount.reset_index(drop=False)
+        return FaultLvlCount
+    def SftyWrngClsfy(df_fltinfo):
+        DsnSaftyCode=[]
+        DataSaftyCode=[]
+        SamplingSaftyCode=[2,10,50,53,54,55,56]
+        HvSaftyCode=[]
+        SysSaftyCode=[]
+        CellSaftyCode=[3,5,6,7,8,9,10,11,13,14,15,16,17,19,20,21,15,16,17,19,23,24,25,26,29,30,31,51,119]
+        CtrlSatyCode=[4,12,18,20,21,22]
+        StateSaftyCode=[27,28,52]
+        
+        DsnSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(DsnSaftyCode)])
+        DataSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(DataSaftyCode)])
+        HvSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(HvSaftyCode)])
+        SysSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(SysSaftyCode)])
+        SamplingSatyCount=len(df_fltinfo[df_fltinfo['code'].isin(SamplingSaftyCode)])
+        CellSaftyCount=len(df_fltinfo[df_fltinfo['code'].isin(CellSaftyCode)])
+        CtrlSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(CtrlSatyCode)])
+        StateSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(StateSaftyCode)])
+        SatftyCount=DataFrame(columns=['DsnSaftyCodeCount','DataSaftyCodeCount','HvSaftyCodeCount','SysSaftyCodeCount','SamplingSatyCount','CellSaftyCount','CtrlSaftyCodeCount','StateSaftyCodeCount'])
+        SatftyCount.loc[0]=[DsnSaftyCodeCount,DataSaftyCodeCount,HvSaftyCodeCount,SysSaftyCodeCount,SamplingSatyCount,CellSaftyCount,CtrlSaftyCodeCount,StateSaftyCodeCount]
+        # print('设计安全分数为',DsnSaftyCodeCount)
+        # print('数据安全分数为',DataSaftyCodeCount)
+        # print('高压安全分数为',HvSaftyCodeCount)
+        # print('系统安全分数为',SysSaftyCodeCount)
+        # print('采样安全分数为',SamplingSatyCount)
+        # print('电芯安全分数为',CellSaftyCount)
+        # print('控制安全分数为',CtrlSaftyCodeCount)
+        # print('状态安全分数为',StateSaftyCodeCount)
+        # print('---------------------------------------------------------------------------')
+        return SatftyCount
+    def AccumInfo(df_last_accum,df_FirstDataTime,end_time):
+        df_last_accum=df_last_accum[~df_last_accum['devcode'].str.contains('TJMCL')]
+        MaxAccumAh=df_last_accum['dsg_ahaccum'].max()
+        TotalAccumAh=df_last_accum['dsg_ahaccum'].sum()
+        PK504_Ah=df_last_accum[df_last_accum['devcode'].str.contains('PK504')]
+        PK504_Ah_Max=(PK504_Ah['dsg_ahaccum'].max())/60
+        PK502_Ah=df_last_accum[df_last_accum['devcode'].str.contains('PK502')]
+        PK502_Ah_Max=(PK502_Ah['dsg_ahaccum'].max())/40
+        PK500_Ah=df_last_accum[df_last_accum['devcode'].str.contains('PK500')]
+        PK500_Ah_Max=(PK500_Ah['dsg_ahaccum'].max())/40
+        MGMCL_Ah=df_last_accum[df_last_accum['devcode'].str.contains('MGMCL')]
+        MGMCL_Ah_Max=(MGMCL_Ah['dsg_ahaccum'].max())/50
+        MGMLX_Ah=df_last_accum[df_last_accum['devcode'].str.contains('MGMLX')]
+        MGMLX_Ah_Max=(MGMLX_Ah['dsg_ahaccum'].max())/50
+        MaxCycle=round(max(PK504_Ah_Max,PK502_Ah_Max,PK500_Ah_Max,MGMCL_Ah_Max,MGMLX_Ah_Max),0)
+        deltatimeMax=0
+        deltatime=DataFrame(columns=['runningdate'])
+        df_FirstDataTime=df_FirstDataTime.dropna(how='any')
+        df_FirstDataTime=df_FirstDataTime.reset_index(drop=False)
+        for i in range(0,len(df_FirstDataTime)):
+            End_time=datetime.datetime.now()
+            df_FirstDataTime.loc[i,'first_data_time']=df_FirstDataTime.loc[i,'first_data_time'].strftime('%Y-%m-%d')
+            df_FirstDataTime.loc[i,'first_data_time']=datetime.datetime.strptime(df_FirstDataTime.loc[i,'first_data_time'],'%Y-%m-%d')
+            deltatime.loc[i,'runningdate']=(End_time-df_FirstDataTime.loc[i,'first_data_time'])
+            deltatime.loc[i,'runningdate']= deltatime.loc[i,'runningdate'].total_seconds()/60
+        MaxRunningHour=int(deltatime['runningdate'].max())
+        TotalRunHour=int(deltatime['runningdate'].sum())
+        return MaxAccumAh,TotalAccumAh,MaxCycle,MaxRunningHour,TotalRunHour
+

+ 140 - 0
LIB/MIDDLE/SaftyCenter/DataSta/main.py

@@ -0,0 +1,140 @@
+import numpy as np
+import pandas as pd
+from User.eric.Common import FeiShuData
+from User.eric.Common import DBDownload
+import time, datetime
+from pandas.core.frame import DataFrame
+from apscheduler.schedulers.blocking import BlockingScheduler
+from DataStatistics import DataSta
+
+#............................主程序................................... 
+   
+
+
+def Week_Task():
+    all_period_fault_info=DataFrame(columns=['factory','week','level1_count','level2_count','level3_count','level4_count','level5_count','solve_rate'])
+
+    #............................获取数据................................
+    toweek='Week'+time.strftime('%W')
+    CS_Data=FeiShuData.getFeiShuDATA()
+    host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
+    port=3306
+    db='safety_platform'
+    user='qx_read'
+    password='Qx@123456'
+    mode=2
+    tablename1='all_fault_info'
+    DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
+    with DBRead as DBRead:
+        df_fltinfo=DBRead.getdata('product_id','level','code','start_time',tablename=tablename1,factory='骑享',sn='',timename='',st='',sp='')#dbdownload经过了改编
+    #............................获取时间................................      
+    end_time=datetime.datetime.now()
+    # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
+    start_time=end_time-datetime.timedelta(days=7)
+    start_time=start_time.strftime('%Y-%m-%d')
+    end_time=end_time.strftime('%Y-%m-%d')
+    FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(CS_Data,df_fltinfo,start_time,end_time)
+    FaultLvlCount=DataSta.WeekInfoSta(df_fltinfo,start_time,end_time)
+    lvl1=FaultLvlCount[FaultLvlCount['level']==1]['product_id'].values
+    lvl2=FaultLvlCount[FaultLvlCount['level']==2]['product_id'].values
+    lvl3=FaultLvlCount[FaultLvlCount['level']==3]['product_id'].values
+    lvl4=FaultLvlCount[FaultLvlCount['level']==4]['product_id'].values
+    lvl5=FaultLvlCount[FaultLvlCount['level']==5]['product_id'].values
+    all_period_fault_info.loc[0,'factory']='骑享'
+    all_period_fault_info.loc[0,'week']=toweek
+    all_period_fault_info.loc[0,'level1_count']=lvl1
+    all_period_fault_info.loc[0,'level2_count']=lvl2
+    all_period_fault_info.loc[0,'level3_count']=lvl3
+    all_period_fault_info.loc[0,'level4_count']=lvl4
+    all_period_fault_info.loc[0,'level5_count']=lvl5
+    all_period_fault_info.loc[0,'solve_rate']=FltAlarmInfo.loc[0,'OprationManageRate']
+    
+def Minutes_Task():
+    
+    #............................获取数据................................
+    host='172.16.121.236'
+    port=3306
+    db='fastfun'
+    user='readonly'
+    password='Fast1234'
+    mode=3
+    tablename1='ff_battery_accum'
+    DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
+    with DBRead as DBRead:
+        df_last_accum=DBRead.getdata('devcode','dsg_phaccum','dsg_ahaccum',tablename=tablename1,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
+
+    host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
+    port=3306
+    db='qx_cas'
+    user='qx_read'
+    password='Qx@123456'
+    mode=3
+    tablename2='bat_first_data_time'
+    DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
+    with DBRead as DBRead:
+        df_FirstDataTime=DBRead.getdata('sn','first_data_time',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
+    CS_Data=FeiShuData.getFeiShuDATA()
+    host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
+    port=3306
+    db='safety_platform'
+    user='qx_read'
+    password='Qx@123456'
+    mode=2
+    tablename1='all_fault_info'
+    DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
+    with DBRead as DBRead:
+        df_fltinfo=DBRead.getdata('product_id','level','code','start_time',tablename=tablename1,factory='骑享',sn='',timename='',st='',sp='')#dbdownload经过了改编
+    #............................获取时间................................
+    end_time=datetime.datetime.now()
+    # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
+    start_time=end_time-datetime.timedelta(days=1)
+    start_time=start_time.strftime('%Y-%m-%d')
+    end_time=end_time.strftime('%Y-%m-%d')
+    #............................执行程序................................
+    FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(CS_Data,df_fltinfo,start_time,end_time)
+    SatftyCount=DataSta.SftyWrngClsfy(df_fltinfo)
+    MaxAccumAh,TotalAccumAh,MaxCycle,MaxRunningHour,TotalRunHour=DataSta.AccumInfo(df_last_accum,df_FirstDataTime,end_time)
+
+    all_statistic_info=DataFrame(columns=['factory','total_alarm','alarm_total_today','alarm_not_close_today','alarm_close_today','alarm_uregent_total_today','alarm_uregent_close_today','alarm_uregent_not_close_today','alarm_close_total','run_time_total','dischrg_total','odo_total','max_dischrg_one','max_runtime_one','max_cycle_one','max_odo_one','alarm_close_total','alarm_total','cell_type','cell_type_count','cell_safety_risk_count','data_safety_risk_count','status_safety_risk_count','hv_safety_risk_count','system_safety_risk_count','sample_safety_risk_count','controller_safety_risk_count','design_safety_risk_count'])
+    all_statistic_info.loc[0,'factory']='骑享'
+    all_statistic_info.loc[0,'total_alarm']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total']
+    all_statistic_info.loc[0,'alarm_total_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']
+    all_statistic_info.loc[0,'alarm_close_today']=FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count']
+    all_statistic_info.loc[0,'alarm_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']-FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count']
+    all_statistic_info.loc[0,'alarm_uregent_total_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']
+    all_statistic_info.loc[0,'alarm_uregent_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count']
+    all_statistic_info.loc[0,'alarm_uregent_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']-FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count']
+    all_statistic_info.loc[0,'run_time_total']=TotalRunHour
+    all_statistic_info.loc[0,'dischrg_total']=TotalAccumAh
+    all_statistic_info.loc[0,'odo_total']=0
+    all_statistic_info.loc[0,'max_dischrg_one']=MaxAccumAh
+    all_statistic_info.loc[0,'max_runtime_one']=MaxRunningHour
+    all_statistic_info.loc[0,'max_cycle_one']=MaxCycle
+    all_statistic_info.loc[0,'max_odo_one']=0
+    all_statistic_info.loc[0,'alarm_close_total']=FltAlarmInfo.loc[0,'CS_Warning_Total_Finish_Count']
+    all_statistic_info.loc[0,'alarm_total']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total']
+    CellType=Celltype.columns.tolist()
+    CellType=','.join(CellType) 
+    all_statistic_info.loc[0,'cell_type']=str(CellType)
+    CellTypeCount=str(Celltype.loc[0].values)
+    CellTypeCount=''.join(CellTypeCount) 
+    all_statistic_info.loc[0,'cell_type_count']=str(CellTypeCount)
+    all_statistic_info.loc[0,'cell_safety_risk_count']=SatftyCount.loc[0,'CellSaftyCount']
+    all_statistic_info.loc[0,'data_safety_risk_count']=SatftyCount.loc[0,'DataSaftyCodeCount']
+    all_statistic_info.loc[0,'status_safety_risk_count']=SatftyCount.loc[0,'StateSaftyCodeCount']
+    all_statistic_info.loc[0,'hv_safety_risk_count']=SatftyCount.loc[0,'HvSaftyCodeCount']
+    all_statistic_info.loc[0,'system_safety_risk_count']=SatftyCount.loc[0,'SysSaftyCodeCount']
+    all_statistic_info.loc[0,'sample_safety_risk_count']=SatftyCount.loc[0,'SamplingSatyCount']
+    all_statistic_info.loc[0,'controller_safety_risk_count']=SatftyCount.loc[0,'CtrlSaftyCodeCount']
+    all_statistic_info.loc[0,'design_safety_risk_count']=SatftyCount.loc[0,'DsnSaftyCodeCount']
+#定时任务....................................................................................................................................................................... 
+#Week_Task()
+Minutes_Task()
+scheduler = BlockingScheduler()
+scheduler.add_job(Week_Task, 'interval', days=7, id='Week_Task')
+scheduler.add_job(Minutes_Task, 'interval', seconds=300, id='Hour_Task')
+try:  
+    scheduler.start()
+except Exception as e:
+    scheduler.shutdown()
+    print(repr(e))