2018-09-29漁船數(shù)據(jù)的信號(hào)傳輸成功率報(bào)表(全部統(tǒng)計(jì))

-- coding: utf-8 --

import os
import datetime
import pymysql
import matplotlib
import matplotlib.pyplot as plt
from pylab import mpl
import time
import xlwt

1、設(shè)置可視化繪圖參數(shù)

def parse_datetime(s):
try:
if s.find(' ') != -1:
return datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
else:
return datetime.datetime.strptime(s, "%Y-%m-%d")
except:
print ("string '%s' is not a valid date or datetime")
def datetime_toString(dt):
return dt.strftime("%Y-%m-%d %H:%M:%S")

def datetime_toTimestamp(dt):
return time.mktime(dt.timetuple())

def timestamp_toString(sp):
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sp))

def execute_sql(sql_cmd):
db = pymysql.connect()
cursor = db.cursor()
cursor.execute(sql_cmd)
return cursor.fetchall()

tmp_path = 'D:/tmp'

if not os.path.exists(tmp_path):

os.mkdir(tmp_path)

if dest_path is None:

dest_path = os.path.join(path, '%s_%s' % (from_date.date(), to_date.date()))

通過(guò)日期區(qū)間和漁船設(shè)備號(hào)查找數(shù)據(jù)

def get_signal_info_by_device_no(from_date, to_date,device_no):
print ("Partition ship data by device_no")
process_day = from_date
rs =[]
signal_change_points= []
data_time_temp1 = 0
data_time_temp2 = 0
signal_change_point_temp1 = 0
signal_change_point_temp2 = 0
pre_temp_dts =[]
temp_dts =[]
GPRS_TIME = 0
LBS_TIME = 0
GPRS_loss_num = 0
GPRS_loss_point = []
GPRS_fact_num = 0
LBS_fact_num = 0
LBS_loss_num =0
LBS_loss_point = []
flag = 0 #用于日期遞增
flag1 = 0 #判斷信號(hào)頻率轉(zhuǎn)換的標(biāo)志位
flag2 = 1 #判斷GPRS轉(zhuǎn)為北斗信號(hào)后的頻率為2分鐘一次還是5分鐘一次的標(biāo)志位
flag3 = 1 #判斷第12標(biāo)志位為1時(shí)出現(xiàn)10分鐘一次的GPRS信號(hào)
while process_day <= to_date:
table_name = process_day.strftime("t_acq_data_%Y%m%d")
get_ship_data_by_date_cmd = ("select ALARM_STATUS,ACQ_TIME,DATA_TYPE from %s where DEVICE_NO = %s"
% (table_name,device_no))
pre_temp_dts = execute_sql(get_ship_data_by_date_cmd)
if pre_temp_dts == ():
print('--------------device_no為'+device_no+'在'+str(process_day.date())+'這天沒(méi)有數(shù)據(jù)')
else:
for pre_temp_dt in pre_temp_dts:
temp_dts.append({'ALARM_STATUS': str(pre_temp_dt[0]), 'ACQ_TIME': pre_temp_dt[1], 'DATA_TYPE': pre_temp_dt[2]})
flag +=1
process_day += datetime.timedelta(1)
if (temp_dts) == []:
print('--------------device_no為'+device_no+'在這幾天都沒(méi)有數(shù)據(jù)')
else:
if (int(temp_dts[0].get('ALARM_STATUS'),2) & int(8192)):
data_type_begin = 2
flag1 = 4
else:
data_type_begin = 1
data_time_temp1 = datetime_toTimestamp(temp_dts[0].get('ACQ_TIME'))
signal_change_point_temp1 = data_time_temp1
signal_change_points.append(timestamp_toString(data_time_temp1))
for temp_dt in temp_dts:
data_time_temp2 = datetime_toTimestamp(temp_dt.get('ACQ_TIME'))#下一個(gè)點(diǎn)的時(shí)間以秒數(shù)計(jì)算
if (2 if int(temp_dt.get('ALARM_STATUS'),2) & int(8192) else 1) == 1:#判斷是否GPRS信號(hào)
GPRS_fact_num += 1
if flag1 == 4:
flag2 = 1
signal_change_points.append(timestamp_toString(data_time_temp2))
signal_change_point_temp2 = data_time_temp2
LBS_TIME += signal_change_point_temp2 - signal_change_point_temp1
signal_change_point_temp1 = signal_change_point_temp2
if (int(temp_dt.get('ALARM_STATUS'),2) & int(32)):#5分鐘一次
flag1 =1
elif (int(temp_dt.get('ALARM_STATUS'),2) & int(2048)):#10分鐘一次
flag1 =2
else:#2分鐘一次
flag1 =3
elif (int(temp_dt.get('ALARM_STATUS'),2) & int(32)):#5分鐘一次
if (flag1 == 1) and (data_time_temp2-data_time_temp1) > (560+60):
GPRS_loss_num += (round((data_time_temp2-data_time_temp1)/60/5)-1)
GPRS_loss_point.append(datetime_toString(temp_dt.get('ACQ_TIME')))
flag1 =1
elif (int(temp_dt.get('ALARM_STATUS'),2) & int(2048)):#10分鐘一次
if (flag1 == 2) and (flag3 == 1) and (data_time_temp2-data_time_temp1) < (10
60+60) and (data_time_temp2-data_time_temp1) > (1060-60):
print('--------------------警告:設(shè)備號(hào)為'+device_no+'第12標(biāo)志位為1時(shí)出現(xiàn)10分鐘一次的GPRS信號(hào)----------------------------')
flag3 = 2
if (flag1 == 2) and (data_time_temp2-data_time_temp1) > (10
60+120):
GPRS_loss_num += (round((data_time_temp2-data_time_temp1)/60/10)-1)
GPRS_loss_point.append(datetime_toString(temp_dt.get('ACQ_TIME')))
flag1 =2
else:#2分鐘一次
if (flag1 == 3) and (data_time_temp2-data_time_temp1) > (260+40):
GPRS_loss_num += (round((data_time_temp2-data_time_temp1)/60/2)-1)
GPRS_loss_point.append(datetime_toString(temp_dt.get('ACQ_TIME')))
flag1 =3
elif (2 if int(temp_dt.get('ALARM_STATUS'),2) & int(8192) else 1) == 2:#判斷是否北斗信號(hào)
LBS_fact_num += 1
if not (flag1 == 4):
signal_change_points.append(timestamp_toString(data_time_temp2))
signal_change_point_temp2 = data_time_temp2
GPRS_TIME += (signal_change_point_temp2 - signal_change_point_temp1)
signal_change_point_temp1 = signal_change_point_temp2
else:
if flag2 == 1 and (data_time_temp2-data_time_temp1) > (2
60+40):
LBS_loss_num += (round((data_time_temp2-data_time_temp1)/60/2)-1)
LBS_loss_point.append(datetime_toString(temp_dt.get('ACQ_TIME')))
elif flag2 == 1 and (data_time_temp2-data_time_temp1) > (560-40) and (data_time_temp2-data_time_temp1) < (560+40):
flag2 = 2
elif flag2 == 2 and (data_time_temp2-data_time_temp1) > (5*60+60):
LBS_loss_num += (round((data_time_temp2-data_time_temp1)/60/5)-1)
LBS_loss_point.append(datetime_toString(temp_dt.get('ACQ_TIME')))
flag1 = 4
elif int(temp_dt.get('ALARM_STATUS'),2) & int(49152):
print('-------------------存在報(bào)警信息-------------------------------')
data_time_temp1=data_time_temp2
signal_change_point_temp2 = datetime_toTimestamp(temp_dts[-1].get('ACQ_TIME'))
signal_change_points.append(timestamp_toString(signal_change_point_temp2))
if not flag1 == 4:
GPRS_TIME += signal_change_point_temp2-signal_change_point_temp1
else:
LBS_TIME += signal_change_point_temp2-signal_change_point_temp1

print('GPRS_fact_num:'+ str(GPRS_fact_num))
print('GPRS_loss_num:'+ str(GPRS_loss_num))
print('LBS_fact_num:'+ str(LBS_fact_num))
print('LBS_loss_num:'+ str(LBS_loss_num))
print('GPRS_TIME:'+ str(GPRS_TIME))
print('LBS_TIME:'+ str(LBS_TIME))
print('signal_change_points:'+ str(signal_change_points))
print('GPRS_loss_point:'+ str(GPRS_loss_point))
print('LBS_loss_point:'+ str(LBS_loss_point))
if GPRS_fact_num == 0:
    gprs_success = None
else:
    gprs_success =  GPRS_fact_num/(GPRS_fact_num + GPRS_loss_num)
if LBS_fact_num == 0:
    lbs_success = None
else:
    lbs_success =  LBS_fact_num/(LBS_fact_num + LBS_loss_num)
if GPRS_TIME + LBS_TIME == 0:
    gprs_time_percent = None
    lbs_time_percent = None
else:
    gprs_time_percent = GPRS_TIME/(GPRS_TIME + LBS_TIME)
    lbs_time_percent = LBS_TIME/(GPRS_TIME + LBS_TIME)

return   gprs_success,lbs_success,gprs_time_percent,lbs_time_percent  

def get_device_nos():
get_device_nos_by_date_cmd = ("select distinct(DEVICE_NO) from t_device where DEVICE_TYPE = 2")
temp_device_nos = execute_sql(get_device_nos_by_date_cmd)
device_nos=[]
for temp_device_no in temp_device_nos:
device_nos.append(temp_device_no[0])
return device_nos

def data_toExcel():
workbook = xlwt.Workbook(encoding='utf-8')
booksheet = workbook.add_sheet('Sheet 1', cell_overwrite_ok=True)
c = ['設(shè)備NO', '統(tǒng)計(jì)數(shù)據(jù)時(shí)間區(qū)間','GPRS成功率', 'GPRS占比', '北斗成功率','北斗占比','設(shè)備代數(shù)']
#用于初步處理后的數(shù)據(jù)存儲(chǔ)
data_toexcel = []
for i in range(7):
booksheet.write(0, i, c[i])
device_nos = get_device_nos()
flag = 1
for device_no in device_nos:
print('---------------------------------------------------------------------------------------------------------')
print('ship which is now handled is device_no:'+ str(device_no))
gprs_success,lbs_success,gprs_time_percent,lbs_time_percent = get_signal_info_by_device_no(from_date, to_date,device_no)

    data_toexcel.append({"DEVICE_NO": device_no, "COUNT_TIME_INTERVAL": '2018-08-26~2018-08-28', "GPRS_SUCCESS": gprs_success, "GPRS_TIME_PERCENT": gprs_time_percent,\
              "LBS_SUCCESS": lbs_success,"LBS_TIME_PERCENT": lbs_time_percent, "DEVICE_TYPE": '二代'})
    booksheet.write(flag, 0, data_toexcel[flag-1].get('DEVICE_NO'))
    booksheet.write(flag, 1, data_toexcel[flag-1].get('COUNT_TIME_INTERVAL'))
    booksheet.write(flag, 2, data_toexcel[flag-1].get('GPRS_SUCCESS'))
    booksheet.write(flag, 3, data_toexcel[flag-1].get('GPRS_TIME_PERCENT'))
    booksheet.write(flag, 4, data_toexcel[flag-1].get('LBS_SUCCESS'))
    booksheet.write(flag, 5, data_toexcel[flag-1].get('LBS_TIME_PERCENT'))
    booksheet.write(flag, 6, data_toexcel[flag-1].get('DEVICE_TYPE'))
    flag += 1
dataname = r'D:/設(shè)備數(shù)據(jù)報(bào)表.xls'
workbook.save(dataname)

count_100=0
count_95=0
count_90=0
count_85=0
count_80=0
count_75=0
count_70=0
count_65=0
count_60=0
count_55=0
count_50=0
count_45=0
count_40=0
count_35=0
count_30=0
count_25=0
count_20=0
count_15=0
device_nos = get_device_nos()
gprs_success,lbs_success,gprs_time_percent,lbs_time_percent = get_signal_info_by_device_no(from_date, to_date,device_no)

if not gprs_success ==None:
    if(gprs_success<=1.0 and gprs_success>0.95):
        count_100+=1
    if(gprs_success<=0.95 and gprs_success>0.9):
        count_95+=1
    if(gprs_success<=0.9 and gprs_success>0.85):
        count_90+=1
    if(gprs_success<=0.85 and gprs_success>0.80):
        count_85+=1
    if(gprs_success<=0.8 and gprs_success>0.75):
        count_80+=1
    if(gprs_success<=0.75 and gprs_success>0.70):
        count_75+=1
    if(gprs_success<=0.7 and gprs_success>0.65):
        count_70+=1
    if(gprs_success<=0.65 and gprs_success>0.60):
        count_65+=1
    if(gprs_success<=0.6 and gprs_success>0.55):
        count_60+=1
    if(gprs_success<=0.55 and gprs_success>0.50):
        count_55+=1
    if(gprs_success<=0.50 and gprs_success>0.45):
        count_50+=1
    if(gprs_success<=0.45 and gprs_success>0.40):
        count_45+=1
    if(gprs_success<=0.40 and gprs_success>0.35):
        count_40+=1
    if(gprs_success<=0.35 and gprs_success>0.30):
        count_35+=1
    if(gprs_success<=0.30 and gprs_success>0.25):
        count_30+=1
    if(gprs_success<=0.25 and gprs_success>0.20):
        count_25+=1
    if(gprs_success<=0.20 and gprs_success>0.15):
        count_20+=1
    if(gprs_success<=0.15):
        count_15+=1

name_list = ['95%-100%','90%-95%','85%-90%','80%-85%','75%-80%','70%-75%','65%-70%','60%-65%','55%-60%','50%-55%','45%-50%','40%-45%','35%-40%','30%-35%','25%-30%','20%-25%','15%-20%','0%-15%']
num_list = [count_100,count_95,count_90,count_85,count_80,count_75,count_70,count_65,count_60,count_55,count_50,count_45,count_40,count_35,count_30,count_25,count_20,count_15]
num_list1 = [0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2]
x =list(range(len(num_list)))
total_width, n = 0.8, 2
width = total_width / n
myfont = matplotlib.font_manager.FontProperties(fname=r'C:/Windows/Fonts/simsun.ttc',size=14) # 這一行
mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默認(rèn)字體
plt.title('GPRS統(tǒng)計(jì)', fontproperties=myfont)
rect1 = plt.bar(x, num_list, width=width,label='成功率',fc = 'lightskyblue')
plt.legend((rect1,),("123",))
for i in range(len(x)):
x[i] = x[i] + width
plt.bar(x, num_list1, width=width, label=u'占比',tick_label = name_list,fc = 'yellowgreen')
plt.legend()
plt.show()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蠢笋,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌趣惠,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件眉撵,死亡現(xiàn)場(chǎng)離奇詭異空繁,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)圾叼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)蛤克,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人夷蚊,你說(shuō)我怎么就攤上這事构挤。” “怎么了惕鼓?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,116評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵筋现,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我箱歧,道長(zhǎng)矾飞,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,371評(píng)論 1 279
  • 正文 為了忘掉前任呀邢,我火速辦了婚禮洒沦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘岗钩。我一直安慰自己钦椭,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布来氧。 她就那樣靜靜地躺著括尸,像睡著了一般仲智。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上姻氨,一...
    開(kāi)封第一講書(shū)人閱讀 49,111評(píng)論 1 285
  • 那天钓辆,我揣著相機(jī)與錄音,去河邊找鬼肴焊。 笑死前联,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的娶眷。 我是一名探鬼主播似嗤,決...
    沈念sama閱讀 38,416評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼届宠!你這毒婦竟也來(lái)了烁落?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,053評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤豌注,失蹤者是張志新(化名)和其女友劉穎伤塌,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體轧铁,經(jīng)...
    沈念sama閱讀 43,558評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡每聪,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了齿风。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片药薯。...
    茶點(diǎn)故事閱讀 38,117評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖救斑,靈堂內(nèi)的尸體忽然破棺而出童本,到底是詐尸還是另有隱情,我是刑警寧澤脸候,帶...
    沈念sama閱讀 33,756評(píng)論 4 324
  • 正文 年R本政府宣布穷娱,位于F島的核電站,受9級(jí)特大地震影響纪他,放射性物質(zhì)發(fā)生泄漏鄙煤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評(píng)論 3 307
  • 文/蒙蒙 一茶袒、第九天 我趴在偏房一處隱蔽的房頂上張望梯刚。 院中可真熱鬧,春花似錦薪寓、人聲如沸亡资。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,315評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)锥腻。三九已至嗦董,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瘦黑,已是汗流浹背京革。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,539評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留幸斥,地道東北人匹摇。 一個(gè)月前我還...
    沈念sama閱讀 45,578評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像甲葬,于是被迫代替她去往敵國(guó)和親廊勃。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容