修復了出現無效數據行會出現錯誤的BUG
修復了最小值統計方法的錯誤
===================下面開始咯log.py========
# -*- coding: cp936 -*-
#上壹句不可以刪!表示中文路徑是GBK編碼
import?datetime
#處理時間的模塊
def?sparse(target='log.txt') :
tgfile = file(target,"r") event={} #event是壹個字典,key是事件的編號,value是數據(可以利用嵌套來擴展數據) linelog = "Not Empty" while?linelog:linelog = tgfile.readline()
data = linelog.split('?')
#按空格將壹行數據分為列表
# print?data #testing
if?len(data) > 4 : #有效的數據行
time1 = data[2][1:] + '?' + data[3][:-1] #將時間處理為(字符串):年-月-日 小時:分鐘:秒 time2 = datetime.datetime.strptime(time1,'%Y-%m-%d %H:%M:%S') #將時間識別為datetime類 if data[5] == "begin:" and data[6][:2] == "OK" :#我不知道有沒有 request?begin: fail 這個東西,沒有就把後半刪掉吧!
if?not?event.has_key(data[0]) :
#第壹次發生某id的事件時初始化數據 event[data[0]]=[[1,time2,0]] #我設置的value是壹個列表,每個元素是壹次記錄,包括[是否沒結束,開始時間,結束時間]。else :
event[data[0]].append([1,time2,0]) #已經有過記錄了就在記錄後加壹條新記錄 if?data[5] == "end:"?and?data[6][:2] == "OK" :#我想應該沒有不出現begin就直接end的事件吧……
event[data[0]][-1][0]=0 #最後壹條記錄中寫入:事件已經結束
event[data[0]][-1][2]=time2 #最後壹條記錄寫入:記錄結束時間
#如果還要處理其他的什麽情形在這裏添加if的判斷 tgfile.close() return?eventdef?analysis(target='log.txt') :
event = sparse(target) #調用上面定於的sparse方法。其實簡單的處理用不著這麽做的……單純為了擴展性 static = {} #用於統計結果的字典(其key和event中的key相同) for?oneevent?in?event :#每個事件的記錄
static[oneevent]=[0,0,0,0,-1]
#初始化每個事件的統計:[成功發生次數,總發生次數,總發生時間,最大發生時間,最小發生時間]
for?onerecord?in?event[oneevent] :
#每個事件的壹次記錄 static[oneevent][0] += 1 #總發生次數加壹 if onerecord[0] == 0 : #成功事件static[oneevent][1] += 1
time_delta = onerecord[2] - onerecord[1]
#計算結果是壹個timedelta類型
inttimedelta = time_delta.days *24*60*60 + time_delta.seconds
#將時間差轉化為以秒計算的整數
if inttimedelta > static[oneevent][3] :
static[oneevent][3] = inttimedelta #統計最大值if inttimedelta < static[oneevent][4] or static[oneevent][4] < 0 :
static[oneevent][4] = inttimedelta #統計最小值static[oneevent][2] += inttimedelta
return static===================下面是log.txt===========
#1?0.0.0.0?[2007-06-12?23:27:08]?request?begin:?OK?
#3?0.0.0.0?[2007-06-12?23:28:08]?request?begin:?fail
#1?0.0.0.0?[2007-06-12?23:37:08]?request?begin:?OK?
#1?0.0.0.0?[2007-06-12?23:37:18]?request?for?a?data:?OK
#1?0.0.0.0?[2007-06-12?23:37:19]?received?some?data:?OK
#1?0.0.0.0?[2007-06-13?00:27:08]?request?end:?OK?
#2?0.0.0.0?[2007-06-13?00:37:08]?request?begin:?OK
#2?0.0.0.0?[2007-06-13?00:47:08]?request?end:?OK?
system?ERROR?:reboot
Another Invalid Line
#1?0.0.0.0?[2007-06-13?23:28:18]?request?begin:?OK?
#7?0.0.0.0?[2007-06-13?23:29:08]?request?begin:?OK?
#7?0.0.0.0?[2007-06-13?23:30:18]?request?end:?OK?
#4?0.0.0.0?[2007-06-13?23:33:08]?request?begin:?OK?
#4?0.0.0.0?[2007-06-13?23:35:23]?request?end:?OK?
#4?0.0.0.0?[2007-06-13?23:37:08]?request?begin:?OK?
#4?0.0.0.0?[2007-06-13?23:43:38]?request?end:?OK?
#5?0.0.0.0?[2007-06-13?23:47:08]?request?begin:?OK?
#1?0.0.0.0?[2007-06-13?23:57:48]?request?begin:?OK?
#5?0.0.0.0?[2007-06-13?23:59:08]?request?end:?OK?
===================下面是使用和輸出========
import?log
output = log.analysis()
#或者直接log.analysis()
=============輸出============
{'#2': [1, 1, 600, 600, 600], '#1': [4, 1, 3000, 3000, 3000], '#7': [1, 1, 70, 70, 70], '#5': [1, 1, 720, 720, 720], '#4': [2, 2, 525, 390, 135]}
比如#1事件,總次數output['#1'][0]==4次
成功次output['#1'][1]==1次
失敗次output['#1'][0]-output['#1'][1]==3次
總時間output['#1'][2]==3000秒
平均時間output['#1'][2]/output['#1'][1]==3000/1==3000秒
最大時間output['#1'][3]==3000秒
最小時間output['#1'][4]==3000秒
***有len(output)==5種ID事件