DataStatistics.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import numpy as np
  2. import pandas as pd
  3. from LIB.MIDDLE.SaftyCenter.Common import FeiShuData
  4. from LIB.MIDDLE.SaftyCenter.Common import DBDownload
  5. import time, datetime
  6. from datetime import timedelta
  7. from pandas.core.frame import DataFrame
  8. from apscheduler.schedulers.blocking import BlockingScheduler
  9. class DataSta():
  10. def __init__(self) -> None:
  11. pass
  12. def SaftyWarningSta(df_fltinfo,start_time,end_time):
  13. start_time=datetime.datetime.strptime(start_time, '%Y-%m-%d')
  14. start_time=start_time+datetime.timedelta(days=1)
  15. start_time=start_time.strftime('%Y-%m-%d')
  16. SftyPlt_Data_Total=len(df_fltinfo)#总报警数
  17. CS_Warning_Total_Finish=df_fltinfo[df_fltinfo['batpos']==1]
  18. CS_Warning_Total_Finish_Count=len(CS_Warning_Total_Finish) #平台报警总运维数
  19. SftyPlt_Data_day=len(df_fltinfo[df_fltinfo['start_time']>=start_time])#日、周总报警数
  20. CS_Warning_day_Finish=CS_Warning_Total_Finish[CS_Warning_Total_Finish['start_time']>=start_time]#日/周运维数
  21. CS_Warning_day_Finish_Count=len(CS_Warning_day_Finish)
  22. SftyPlt_EmgcyData_day=df_fltinfo[df_fltinfo['start_time']>=start_time] #紧急报警数
  23. SftyPlt_EmgcyData_day=SftyPlt_EmgcyData_day[SftyPlt_EmgcyData_day['level']>3]
  24. SftyPlt_EmgcyData_day_Count=len(SftyPlt_EmgcyData_day[SftyPlt_EmgcyData_day['level']>3])
  25. SftyPlt_EmgcyData_day_Finish=SftyPlt_EmgcyData_day[SftyPlt_EmgcyData_day['batpos']==1]
  26. SftyPlt_EmgcyData_day_Finish_Count=len(SftyPlt_EmgcyData_day_Finish)
  27. if int(SftyPlt_Data_Total)>0:
  28. OprationManageRate=round(float(CS_Warning_day_Finish_Count/SftyPlt_Data_day)*100,2)
  29. else:
  30. OprationManageRate=100
  31. if int(SftyPlt_EmgcyData_day_Count)>0:
  32. OprationManageEmgcyRate=round(float(SftyPlt_EmgcyData_day_Finish_Count/SftyPlt_EmgcyData_day_Count)*100,2)
  33. else:
  34. OprationManageEmgcyRate=100
  35. PK504FltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('PK504')])
  36. PK502FltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('PK502')])
  37. PK500FltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('PK500')])
  38. MGMCLFltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('MGMCL')])
  39. MGMLXFltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('MGMLX')])
  40. # print('总报警数=',SftyPlt_Data_Total)
  41. # print('总完成运维数=',CS_Warning_Total_Finish_Count)
  42. # print('周报警数=',SftyPlt_Data_day)
  43. # print('周完成运维数=',CS_Warning_day_Finish_Count)
  44. # print('周紧急报警数=',SftyPlt_EmgcyData_day)
  45. # print('周紧急完成运维数=',SftyPlt_EmgcyData_day_Finish_Count)
  46. # print('周运维率=',OprationManageRate)
  47. # print('周紧急运维率=',OprationManageEmgcyRate)
  48. # print('PK504总报警=',PK504FltData_Count)
  49. # print('PK502总报警=',PK502FltData_Count)
  50. # print('PK500总报警=',PK500FltData_Count)
  51. # print('格林美总报警=',MGMCLFltData_Count)
  52. # print('自研总报警=',MGMLXFltData_Count)
  53. 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'])
  54. Celltype=DataFrame(columns=['PK504','PK502','PK500','MGMCL','MGMLX'])
  55. FltAlarmInfo.loc[0]=[SftyPlt_Data_Total,CS_Warning_Total_Finish_Count,SftyPlt_Data_day,CS_Warning_day_Finish_Count,SftyPlt_EmgcyData_day_Count,SftyPlt_EmgcyData_day_Finish_Count,OprationManageRate,OprationManageEmgcyRate]
  56. Celltype.loc[0]=[PK504FltData_Count,PK502FltData_Count,PK500FltData_Count,MGMCLFltData_Count,MGMLXFltData_Count]
  57. return FltAlarmInfo,Celltype
  58. def WeekInfoSta(df_fltinfo,start_time,end_time):
  59. df_fltinfo=df_fltinfo[df_fltinfo['start_time']>=start_time]
  60. FaultLvlCount=DataFrame(columns=['level','count'])
  61. FaultLvlCount=df_fltinfo.groupby('level').count().T.head(1).T
  62. FaultLvlCount=FaultLvlCount.reset_index(drop=False)
  63. return FaultLvlCount
  64. def SftyWrngClsfy(df_fltinfo):
  65. DsnSaftyCode=[]
  66. DataSaftyCode=[]
  67. SamplingSaftyCode=[2,10,50,53,54,55,56]
  68. HvSaftyCode=[]
  69. SysSaftyCode=[]
  70. 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]
  71. CtrlSatyCode=[4,12,18,20,21,22,57]
  72. StateSaftyCode=[27,28,52]
  73. DsnSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(DsnSaftyCode)])
  74. DataSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(DataSaftyCode)])
  75. HvSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(HvSaftyCode)])
  76. SysSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(SysSaftyCode)])
  77. SamplingSatyCount=len(df_fltinfo[df_fltinfo['code'].isin(SamplingSaftyCode)])
  78. CellSaftyCount=len(df_fltinfo[df_fltinfo['code'].isin(CellSaftyCode)])
  79. CtrlSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(CtrlSatyCode)])
  80. StateSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(StateSaftyCode)])
  81. SatftyCount=DataFrame(columns=['DsnSaftyCodeCount','DataSaftyCodeCount','HvSaftyCodeCount','SysSaftyCodeCount','SamplingSatyCount','CellSaftyCount','CtrlSaftyCodeCount','StateSaftyCodeCount'])
  82. SatftyCount.loc[0]=[DsnSaftyCodeCount,DataSaftyCodeCount,HvSaftyCodeCount,SysSaftyCodeCount,SamplingSatyCount,CellSaftyCount,CtrlSaftyCodeCount,StateSaftyCodeCount]
  83. # print('设计安全分数为',DsnSaftyCodeCount)
  84. # print('数据安全分数为',DataSaftyCodeCount)
  85. # print('高压安全分数为',HvSaftyCodeCount)
  86. # print('系统安全分数为',SysSaftyCodeCount)
  87. # print('采样安全分数为',SamplingSatyCount)
  88. # print('电芯安全分数为',CellSaftyCount)
  89. # print('控制安全分数为',CtrlSaftyCodeCount)
  90. # print('状态安全分数为',StateSaftyCodeCount)
  91. # print('---------------------------------------------------------------------------')
  92. return SatftyCount
  93. def AccumInfo(df_last_accum,df_FirstDataTime,end_time):
  94. df_last_accum=df_last_accum[~df_last_accum['devcode'].str.contains('TJMCL')]
  95. MaxAccumAh=df_last_accum['dsg_ahaccum'].max()
  96. TotalAccumAh=df_last_accum['dsg_ahaccum'].sum()
  97. PK504_Ah=df_last_accum[df_last_accum['devcode'].str.contains('PK504')]
  98. PK504_Ah_Max=(PK504_Ah['dsg_ahaccum'].max())/60
  99. PK502_Ah=df_last_accum[df_last_accum['devcode'].str.contains('PK502')]
  100. PK502_Ah_Max=(PK502_Ah['dsg_ahaccum'].max())/40
  101. PK500_Ah=df_last_accum[df_last_accum['devcode'].str.contains('PK500')]
  102. PK500_Ah_Max=(PK500_Ah['dsg_ahaccum'].max())/40
  103. MGMCL_Ah=df_last_accum[df_last_accum['devcode'].str.contains('MGMCL')]
  104. MGMCL_Ah_Max=(MGMCL_Ah['dsg_ahaccum'].max())/50
  105. MGMLX_Ah=df_last_accum[df_last_accum['devcode'].str.contains('MGMLX')]
  106. MGMLX_Ah_Max=(MGMLX_Ah['dsg_ahaccum'].max())/50
  107. MaxCycle=round(max(PK504_Ah_Max,PK502_Ah_Max,PK500_Ah_Max,MGMCL_Ah_Max,MGMLX_Ah_Max),0)
  108. deltatimeMax=0
  109. deltatime=DataFrame(columns=['runningdate'])
  110. df_FirstDataTime=df_FirstDataTime.dropna(how='any')
  111. df_FirstDataTime=df_FirstDataTime.reset_index(drop=False)
  112. for i in range(0,len(df_FirstDataTime)):
  113. End_time=datetime.datetime.now()
  114. df_FirstDataTime.loc[i,'first_data_time']=df_FirstDataTime.loc[i,'first_data_time'].strftime('%Y-%m-%d')
  115. df_FirstDataTime.loc[i,'first_data_time']=datetime.datetime.strptime(df_FirstDataTime.loc[i,'first_data_time'],'%Y-%m-%d')
  116. deltatime.loc[i,'runningdate']=(End_time-df_FirstDataTime.loc[i,'first_data_time'])
  117. deltatime.loc[i,'runningdate']= deltatime.loc[i,'runningdate'].total_seconds()/3600
  118. MaxRunningHour=int(deltatime['runningdate'].max())
  119. TotalRunHour=int(deltatime['runningdate'].sum())
  120. return MaxAccumAh,TotalAccumAh,MaxCycle,MaxRunningHour,TotalRunHour
  121. def FltBatPosition(df_last_pos,df_fltinfo):
  122. df_Diag_Ram=df_fltinfo[df_fltinfo['batpos']==0].drop_duplicates(subset=['product_id'],keep=False)
  123. all_location_info=DataFrame(columns=['factory','product_id','lat','lon'])
  124. df_last_pos.rename(columns={'devcode':'product_id'},inplace=True)
  125. df_Diag_Ram_Pos_a=pd.concat([df_Diag_Ram,df_last_pos]).drop_duplicates(subset=['product_id'],keep='first')
  126. df_Diag_Ram_Pos_b=pd.concat([df_Diag_Ram,df_last_pos]).drop_duplicates(subset=['product_id'],keep=False)
  127. df_Diag_Ram_Pos=df_Diag_Ram_Pos_a.append(df_Diag_Ram_Pos_b).drop_duplicates(subset=['product_id'],keep=False)
  128. df_Diag_Ram_Pos=df_Diag_Ram_Pos.reset_index(drop=True)
  129. for i in range(0,len(df_Diag_Ram_Pos)):
  130. df_last_pos_list=df_last_pos['product_id'].values.tolist()
  131. if str(df_Diag_Ram_Pos.loc[i,'product_id']) in df_last_pos_list:
  132. pos_la_temp=df_last_pos[df_last_pos['product_id']==(df_Diag_Ram_Pos.loc[i,'product_id'])].reset_index(drop=True)
  133. all_location_info.loc[i,'lat']=pos_la_temp.loc[0,'latitude']
  134. all_location_info.loc[i,'lon']=pos_la_temp.loc[0,'longitude']
  135. all_location_info.loc[i,'factory']='骑享'
  136. all_location_info.loc[i,'product_id']=df_Diag_Ram_Pos.loc[i,'product_id']
  137. return all_location_info