DataStatistics.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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,factory_info):
  13. if '骑享' in factory_info:
  14. df_fltinfo=df_fltinfo[~df_fltinfo['product_id'].str.contains('TJMCL')]
  15. start_time=start_time+str(' 00:00:00')
  16. start_time=datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')
  17. start_time=start_time+datetime.timedelta(days=1)
  18. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  19. SftyPlt_Data_Total=len(df_fltinfo)#总报警数
  20. CS_Warning_Total_Finish=df_fltinfo[df_fltinfo['batpos']==1]
  21. CS_Warning_Total_Finish_Count=len(CS_Warning_Total_Finish) #平台报警总运维数
  22. SftyPlt_Data_day=len(df_fltinfo[df_fltinfo['start_time']>=start_time])#日、周总报警数
  23. CS_Warning_day_Finish=CS_Warning_Total_Finish[CS_Warning_Total_Finish['start_time']>=start_time]#日/周运维数
  24. CS_Warning_day_Finish_Count=len(CS_Warning_day_Finish)
  25. SftyPlt_EmgcyData_day=df_fltinfo[df_fltinfo['start_time']>=start_time] #紧急报警数
  26. SftyPlt_EmgcyData_day=SftyPlt_EmgcyData_day[SftyPlt_EmgcyData_day['level']>3]
  27. SftyPlt_EmgcyData_day_Count=len(SftyPlt_EmgcyData_day)
  28. SftyPlt_EmgcyData_day_Finish=SftyPlt_EmgcyData_day[SftyPlt_EmgcyData_day['batpos']==1]
  29. SftyPlt_EmgcyData_day_Finish_Count=len(SftyPlt_EmgcyData_day_Finish)
  30. if int(SftyPlt_Data_day)>0:
  31. OprationManageRate=round(float(CS_Warning_day_Finish_Count/SftyPlt_Data_day)*100,2)
  32. else:
  33. OprationManageRate=100
  34. if int(SftyPlt_EmgcyData_day_Count)>0:
  35. OprationManageEmgcyRate=round(float(SftyPlt_EmgcyData_day_Finish_Count/SftyPlt_EmgcyData_day_Count)*100,2)
  36. else:
  37. OprationManageEmgcyRate=100
  38. PK504FltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('PK504')])
  39. PK502FltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('PK502')])
  40. PK500FltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('PK500')])
  41. MGMCLFltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('MGMCL')])
  42. MGMLXFltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('MGMLX')])
  43. # print('总报警数=',SftyPlt_Data_Total)
  44. # print('总完成运维数=',CS_Warning_Total_Finish_Count)
  45. # print('周报警数=',SftyPlt_Data_day)
  46. # print('周完成运维数=',CS_Warning_day_Finish_Count)
  47. # print('周紧急报警数=',SftyPlt_EmgcyData_day)
  48. # print('周紧急完成运维数=',SftyPlt_EmgcyData_day_Finish_Count)
  49. # print('周运维率=',OprationManageRate)
  50. # print('周紧急运维率=',OprationManageEmgcyRate)
  51. # print('PK504总报警=',PK504FltData_Count)
  52. # print('PK502总报警=',PK502FltData_Count)
  53. # print('PK500总报警=',PK500FltData_Count)
  54. # print('格林美总报警=',MGMCLFltData_Count)
  55. # print('自研总报警=',MGMLXFltData_Count)
  56. 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'])
  57. Celltype=DataFrame(columns=['PK504','PK502','PK500','MGMCL','MGMLX'])
  58. 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]
  59. Celltype.loc[0]=[PK504FltData_Count,PK502FltData_Count,PK500FltData_Count,MGMCLFltData_Count,MGMLXFltData_Count]
  60. elif '金茂换电' in factory_info:
  61. df_fltinfo=df_fltinfo[df_fltinfo['product_id'].str.contains('TJMCL')]
  62. start_time=start_time+str(' 00:00:00')
  63. start_time=datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')
  64. start_time=start_time+datetime.timedelta(days=1)
  65. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  66. SftyPlt_Data_Total=len(df_fltinfo)#总报警数
  67. CS_Warning_Total_Finish=df_fltinfo[df_fltinfo['batpos']==1]
  68. CS_Warning_Total_Finish_Count=len(CS_Warning_Total_Finish) #平台报警总运维数
  69. SftyPlt_Data_day=len(df_fltinfo[df_fltinfo['start_time']>=start_time])#日、周总报警数
  70. CS_Warning_day_Finish=CS_Warning_Total_Finish[CS_Warning_Total_Finish['start_time']>=start_time]#日/周运维数
  71. CS_Warning_day_Finish_Count=len(CS_Warning_day_Finish)
  72. SftyPlt_EmgcyData_day=df_fltinfo[df_fltinfo['start_time']>=start_time] #紧急报警数
  73. SftyPlt_EmgcyData_day=SftyPlt_EmgcyData_day[SftyPlt_EmgcyData_day['level']>3]
  74. SftyPlt_EmgcyData_day_Count=len(SftyPlt_EmgcyData_day)
  75. SftyPlt_EmgcyData_day_Finish=SftyPlt_EmgcyData_day[SftyPlt_EmgcyData_day['batpos']==1]
  76. SftyPlt_EmgcyData_day_Finish_Count=len(SftyPlt_EmgcyData_day_Finish)
  77. if int(SftyPlt_Data_day)>0:
  78. OprationManageRate=round(float(CS_Warning_day_Finish_Count/SftyPlt_Data_day)*100,2)
  79. else:
  80. OprationManageRate=100
  81. if int(SftyPlt_EmgcyData_day_Count)>0:
  82. OprationManageEmgcyRate=round(float(SftyPlt_EmgcyData_day_Finish_Count/SftyPlt_EmgcyData_day_Count)*100,2)
  83. else:
  84. OprationManageEmgcyRate=100
  85. TJMCLFltData_Count=len(df_fltinfo[df_fltinfo['product_id'].str.contains('TJMCL')])
  86. # print('总报警数=',SftyPlt_Data_Total)
  87. # print('总完成运维数=',CS_Warning_Total_Finish_Count)
  88. # print('周报警数=',SftyPlt_Data_day)
  89. # print('周完成运维数=',CS_Warning_day_Finish_Count)
  90. # print('周紧急报警数=',SftyPlt_EmgcyData_day)
  91. # print('周紧急完成运维数=',SftyPlt_EmgcyData_day_Finish_Count)
  92. # print('周运维率=',OprationManageRate)
  93. # print('周紧急运维率=',OprationManageEmgcyRate)
  94. # print('PK504总报警=',PK504FltData_Count)
  95. # print('PK502总报警=',PK502FltData_Count)
  96. # print('PK500总报警=',PK500FltData_Count)
  97. # print('格林美总报警=',MGMCLFltData_Count)
  98. # print('自研总报警=',MGMLXFltData_Count)
  99. 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'])
  100. Celltype=DataFrame(columns=['TJMCL'])
  101. 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]
  102. Celltype.loc[0]=[TJMCLFltData_Count]
  103. else:
  104. 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'])
  105. Celltype=DataFrame(columns=['None'])
  106. return FltAlarmInfo,Celltype
  107. def WeekInfoSta(df_fltinfo,start_time,end_time):
  108. df_fltinfo=df_fltinfo[df_fltinfo['start_time']>=start_time]
  109. FaultLvlCount=DataFrame(columns=['level','count'])
  110. FaultLvlCount=df_fltinfo.groupby('level').count().T.head(1).T
  111. FaultLvlCount=FaultLvlCount.reset_index(drop=False)
  112. return FaultLvlCount
  113. def SftyWrngClsfy(df_fltinfo):
  114. DsnSaftyCode=[]
  115. DataSaftyCode=[]
  116. SamplingSaftyCode=[2,10,50,53,54,55,56]
  117. HvSaftyCode=[]
  118. SysSaftyCode=[]
  119. 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]
  120. CtrlSatyCode=[4,12,18,20,21,22,57]
  121. StateSaftyCode=[27,28,52]
  122. DsnSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(DsnSaftyCode)])
  123. DataSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(DataSaftyCode)])
  124. HvSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(HvSaftyCode)])
  125. SysSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(SysSaftyCode)])
  126. SamplingSatyCount=len(df_fltinfo[df_fltinfo['code'].isin(SamplingSaftyCode)])
  127. CellSaftyCount=len(df_fltinfo[df_fltinfo['code'].isin(CellSaftyCode)])
  128. CtrlSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(CtrlSatyCode)])
  129. StateSaftyCodeCount=len(df_fltinfo[df_fltinfo['code'].isin(StateSaftyCode)])
  130. SatftyCount=DataFrame(columns=['DsnSaftyCodeCount','DataSaftyCodeCount','HvSaftyCodeCount','SysSaftyCodeCount','SamplingSatyCount','CellSaftyCount','CtrlSaftyCodeCount','StateSaftyCodeCount'])
  131. SatftyCount.loc[0]=[DsnSaftyCodeCount,DataSaftyCodeCount,HvSaftyCodeCount,SysSaftyCodeCount,SamplingSatyCount,CellSaftyCount,CtrlSaftyCodeCount,StateSaftyCodeCount]
  132. # print('设计安全分数为',DsnSaftyCodeCount)
  133. # print('数据安全分数为',DataSaftyCodeCount)
  134. # print('高压安全分数为',HvSaftyCodeCount)
  135. # print('系统安全分数为',SysSaftyCodeCount)
  136. # print('采样安全分数为',SamplingSatyCount)
  137. # print('电芯安全分数为',CellSaftyCount)
  138. # print('控制安全分数为',CtrlSaftyCodeCount)
  139. # print('状态安全分数为',StateSaftyCodeCount)
  140. # print('---------------------------------------------------------------------------')
  141. return SatftyCount
  142. def AccumInfo(df_last_accum,df_FirstDataTime,end_time,factory_info):
  143. if '骑享' in factory_info:
  144. df_last_accum=df_last_accum[~df_last_accum['devcode'].str.contains('TJMCL')]
  145. MaxAccumAh=df_last_accum['dsg_ahaccum'].max()
  146. TotalAccumAh=df_last_accum['dsg_ahaccum'].sum()
  147. PK504_Ah=df_last_accum[df_last_accum['devcode'].str.contains('PK504')]
  148. PK504_Ah_Max=(PK504_Ah['dsg_ahaccum'].max())/60
  149. PK502_Ah=df_last_accum[df_last_accum['devcode'].str.contains('PK502')]
  150. PK502_Ah_Max=(PK502_Ah['dsg_ahaccum'].max())/40
  151. PK500_Ah=df_last_accum[df_last_accum['devcode'].str.contains('PK500')]
  152. PK500_Ah_Max=(PK500_Ah['dsg_ahaccum'].max())/40
  153. MGMCL_Ah=df_last_accum[df_last_accum['devcode'].str.contains('MGMCL')]
  154. MGMCL_Ah_Max=(MGMCL_Ah['dsg_ahaccum'].max())/50
  155. MGMLX_Ah=df_last_accum[df_last_accum['devcode'].str.contains('MGMLX')]
  156. MGMLX_Ah_Max=(MGMLX_Ah['dsg_ahaccum'].max())/50
  157. MaxCycle=round(max(PK504_Ah_Max,PK502_Ah_Max,PK500_Ah_Max,MGMCL_Ah_Max,MGMLX_Ah_Max),0)
  158. deltatimeMax=0
  159. deltatime=DataFrame(columns=['runningdate'])
  160. df_FirstDataTime=df_FirstDataTime[~df_FirstDataTime['sn'].str.contains('TJMCL')]
  161. df_FirstDataTime=df_FirstDataTime.dropna(how='any')
  162. df_FirstDataTime=df_FirstDataTime.reset_index(drop=False)
  163. for i in range(0,len(df_FirstDataTime)):
  164. End_time=datetime.datetime.now()
  165. df_FirstDataTime.loc[i,'first_data_time']=df_FirstDataTime.loc[i,'first_data_time'].strftime('%Y-%m-%d')
  166. df_FirstDataTime.loc[i,'first_data_time']=datetime.datetime.strptime(df_FirstDataTime.loc[i,'first_data_time'],'%Y-%m-%d')
  167. deltatime.loc[i,'runningdate']=(End_time-df_FirstDataTime.loc[i,'first_data_time'])
  168. deltatime.loc[i,'runningdate']= deltatime.loc[i,'runningdate'].total_seconds()/3600
  169. MaxRunningHour=int(deltatime['runningdate'].max())
  170. TotalRunHour=int(deltatime['runningdate'].sum())
  171. elif '金茂换电' in factory_info:
  172. df_last_accum=df_last_accum[df_last_accum['devcode'].str.contains('TJMCL')]
  173. df_last_accum=df_last_accum[df_last_accum['dsg_ahaccum']<429496729]
  174. MaxAccumAh=df_last_accum['dsg_ahaccum'].max()
  175. TotalAccumAh=df_last_accum['dsg_ahaccum'].sum()
  176. TJMCL_Ah=df_last_accum[df_last_accum['devcode'].str.contains('TJMCL')]
  177. TJMCL_Ah_Max=(TJMCL_Ah['dsg_ahaccum'].max())/(228*2)
  178. MaxCycle=round(TJMCL_Ah_Max,0)
  179. deltatimeMax=0
  180. deltatime=DataFrame(columns=['runningdate'])
  181. df_FirstDataTime=df_FirstDataTime[df_FirstDataTime['sn'].str.contains('TJMCL')]
  182. df_FirstDataTime=df_FirstDataTime.dropna(how='any')
  183. df_FirstDataTime=df_FirstDataTime.reset_index(drop=False)
  184. for i in range(0,len(df_FirstDataTime)):
  185. End_time=datetime.datetime.now()
  186. df_FirstDataTime.loc[i,'first_data_time']=df_FirstDataTime.loc[i,'first_data_time'].strftime('%Y-%m-%d')
  187. df_FirstDataTime.loc[i,'first_data_time']=datetime.datetime.strptime(df_FirstDataTime.loc[i,'first_data_time'],'%Y-%m-%d')
  188. deltatime.loc[i,'runningdate']=(End_time-df_FirstDataTime.loc[i,'first_data_time'])
  189. deltatime.loc[i,'runningdate']= deltatime.loc[i,'runningdate'].total_seconds()/3600
  190. MaxRunningHour=int(deltatime['runningdate'].max())
  191. TotalRunHour=int(deltatime['runningdate'].sum())
  192. return MaxAccumAh,TotalAccumAh,MaxCycle,MaxRunningHour,TotalRunHour
  193. def FltBatPosition(df_last_pos,df_fltinfo,factory_info):
  194. df_Diag_Ram=df_fltinfo[df_fltinfo['batpos']==0].drop_duplicates(subset=['product_id'],keep=False)
  195. all_location_info=DataFrame(columns=['factory','product_id','lat','lon'])
  196. df_last_pos.rename(columns={'devcode':'product_id'},inplace=True)
  197. df_Diag_Ram_Pos_a=pd.concat([df_Diag_Ram,df_last_pos]).drop_duplicates(subset=['product_id'],keep='first')
  198. df_Diag_Ram_Pos_b=pd.concat([df_Diag_Ram,df_last_pos]).drop_duplicates(subset=['product_id'],keep=False)
  199. df_Diag_Ram_Pos=df_Diag_Ram_Pos_a.append(df_Diag_Ram_Pos_b).drop_duplicates(subset=['product_id'],keep=False)
  200. df_Diag_Ram_Pos=df_Diag_Ram_Pos.reset_index(drop=True)
  201. for i in range(0,len(df_Diag_Ram_Pos)):
  202. df_last_pos_list=df_last_pos['product_id'].values.tolist()
  203. if str(df_Diag_Ram_Pos.loc[i,'product_id']) in df_last_pos_list:
  204. pos_la_temp=df_last_pos[df_last_pos['product_id']==(df_Diag_Ram_Pos.loc[i,'product_id'])].reset_index(drop=True)
  205. all_location_info.loc[i,'lat']=pos_la_temp.loc[0,'latitude']
  206. all_location_info.loc[i,'lon']=pos_la_temp.loc[0,'longitude']
  207. all_location_info.loc[i,'factory']=factory_info
  208. all_location_info.loc[i,'product_id']=df_Diag_Ram_Pos.loc[i,'product_id']
  209. return all_location_info