import pandas as pd
from datetime import timedelta
con = 'postgresql://postgres:[email protected]/rpt_repair'
sql = 'select a.id, a.procedure,a.content, a.failure_begin, a.repair_start, a.repair_end, a.debug_end,a.workers,b.name from simple_enroll_simplerollinrecord as a left join simple_enroll_failuretype as b on a.failure_type_id=b.id'
sql += ' where failure_begin >= \'2020-07-01\' and failure_begin < \'2020-07-31\''
def prepare(sql, con):
q = pd.read_sql_query(sql, con)
q['停机'] = q['debug_end'] - q['failure_begin']
q['停机'] = q['停机'].apply(lambda x: int(x.seconds/60))
q['维修'] = q['repair_end'] - q['repair_start']
q['维修'] = q['维修'].apply(lambda x: int(x.seconds/60))
q['等待'] = q['repair_start'] - q['failure_begin']
q['等待'] = q['等待'].apply(lambda x: int(x.seconds/60))
q['次数'] = 1
q['failure_begin']=q['failure_begin']+timedelta(hours=8)
q['班'] = ''
return q
def ban(x):
ban = ''
day_range= [8,9,10,11,12,13,14,15,16,17,18,19]
night_range = [0,1,2,3,4,5,6,7,20,21,22,23]
dt = x.loc['failure_begin']
dt2 = x.loc['debug_end']
if dt.timetuple().tm_hour in day_range and dt2.timetuple().tm_hour in day_range:
ban = '白'
if dt.timetuple().tm_hour in night_range and dt2.timetuple().tm_hour in night_range:
ban = '夜'
return ban
q = prepare(sql, con)
q['班'] = q.apply(ban,axis=1)
q['日'] = q['failure_begin'].apply(lambda x: x.days_in_month)
def ranking(q):
from collections import Counter
itr=q.iterrows()
workers = []
w = ''
for index, row in q.iterrows():
t = row.loc['workers'].split(',')
workers += t
result = Counter(workers)
ss = sorted(result.items(),key=lambda p: p[1], reverse=True)
for s in ss:
print(s[0],'\t',s[1])
return workers
s1=q.groupby('班').agg({'停机':['sum'], '等待':['sum'],'维修':['sum'], '次数':['sum']})
s1.columns = s1.columns.droplevel(1)
w=ranking(q)
张家晟 73
程宝令 56
王远涛 56
王恒茂 55
王健 54
赵志勇 51
栾昊 49
张洪剑 48
赵志鹏 43
田广磊 38
林杰 36
葛德言 32
王君 30
朱之钊 22
林刚 18
姜福杰 16
曹树鑫 5
s1
|
停机 |
等待 |
维修 |
次数 |
班 |
|
|
|
|
|
19797 |
4890 |
14790 |
217 |
夜 |
9358 |
1178 |
7475 |
68 |
白 |
9882 |
1851 |
7411 |
68 |
q_jg=q.query('procedure=="加工"')
w=ranking(q_jg)
张家晟 32
程宝令 29
赵志勇 28
栾昊 20
王远涛 20
赵志鹏 18
张洪剑 18
王君 17
王恒茂 16
王健 16
葛德言 14
林刚 13
田广磊 11
林杰 10
姜福杰 9
朱之钊 8
曹树鑫 4
q_zz=q.query('procedure=="铸造"')
w=ranking(q_zz)
王恒茂 23
张家晟 22
王健 22
张洪剑 18
王远涛 17
赵志鹏 16
程宝令 16
田广磊 16
栾昊 15
赵志勇 13
林杰 10
王君 7
葛德言 7
姜福杰 3
朱之钊 2
林刚 1
w2=list(set(w))
w2
['王健',
'葛德言',
'赵志勇',
'王君',
'林刚',
'栾昊',
'王远涛',
'田广磊',
'林杰',
'程宝令',
'姜福杰',
'赵志鹏',
'张家晟',
'朱之钊',
'张洪剑',
'王恒茂']
def 技能(procedure,workers, sql):
result = []
for w in workers:
sql_per = sql + ' and workers like \'%%' + w + '%%\''
q_zhangjias = prepare(sql_per, con)
q_zhangjias_jg = q_zhangjias.query('procedure=="%s"' % procedure)
t = q_zhangjias_jg.agg({'次数':['sum'], '维修':['sum','mean']})
result.append({'姓名':w, '次数':'%d' % t['次数']['sum'], '平均维修': '%.1f' % t['维修']['mean']})
print('姓名','\t','次数','\t','平均维修时间(分)')
for r in result:
print(r['姓名'],'\t',r['次数'],'\t',r['平均维修'])
技能('铸造', w2, sql)
姓名 次数 平均维修时间(分)
王健 22 101.8
葛德言 7 145.0
赵志勇 13 83.1
王君 7 39.3
林刚 1 55.0
栾昊 15 110.7
王远涛 17 116.8
田广磊 16 99.8
林杰 10 120.0
程宝令 16 84.3
姜福杰 3 89.3
赵志鹏 16 73.0
张家晟 22 73.9
朱之钊 2 132.5
张洪剑 18 137.9
王恒茂 23 66.7
技能('加工', w2, sql)
姓名 次数 平均维修时间(分)
王健 16 162.0
葛德言 14 82.1
赵志勇 28 90.9
王君 17 171.5
林刚 13 146.9
栾昊 20 153.8
王远涛 20 88.0
田广磊 11 62.3
林杰 10 105.2
程宝令 29 91.2
姜福杰 9 32.8
赵志鹏 18 108.6
张家晟 32 98.3
朱之钊 8 235.6
张洪剑 18 155.6
王恒茂 16 103.1