一段流量分析工具代码

阅读更多
# !/usr/bin/envpython
#
-*-coding:cp936-*-
import threading,os,time

class ReadConfig():

def Base_Config(self):

# 配置话单路径
TelPath = " d:\bak "
# printTelPath
return TelPath


class GetPath(ReadConfig):

def Get_Path(self):

# 获取路径下分析文件,建立字典
dict = {}
dir_temp
= []
file_temp
= []
basepath
= self.Base_Config()
# printbasepath
for root,dirs,files in os.walk(self.Base_Config()):
# 获取文件夹节点,获取文件夹对应文件
for dir in dirs:
dir_temp.append(dir)
dir_temp.sort()
for i in dir_temp:
# printself.Base_Config()+"\"+str(i)
for root,dirs,files in os.walk(self.Base_Config() + " \ " + str(i)):
# 获取对应文件夹下的数据
file_temp.append(files)
for i in range(len(dir_temp)):
# 建立数据字典
dict[dir_temp[i]] = file_temp[i]
return basepath,dict

class ThreadMain(threading.Thread,GetPath):

def __init__ (self):

# 设定一个分析时间片,单位为分钟
self.AnalyseTimeArea = 5
basepath,dict
= self.Get_Path()
# printbasepath,dict
self.dict = dict
self.basepath
= basepath

def Thread_Contorl(self):

# 读取字典的索引,索引为文件夹节点self.dict.keys()[i]
self.Add_Tel_Path_temp2 = []
# printself.dict.keys()
for i in range(len(self.dict.keys())):
self.Add_Tel_Path_temp1
= []
# printself.dict.keys()[i],self.dict[self.dict.keys()[i]]
for y in range(len(self.dict[self.dict.keys()[i]])):
# printself.dict.keys()[i],self.dict[self.dict.keys()[i]]
# printlen(self.dict[self.dict.keys()[i]])
self.Add_Tel_Path = self.basepath + " \ " + self.dict.keys()[i] + " \ " + self.dict[self.dict.keys()[i]][y]
self.Add_Tel_Path_temp1.append(self.Add_Tel_Path)
# printAdd_Tel_Path_temp1
self.Add_Tel_Path_temp2.append(self.Add_Tel_Path_temp1)
# Add_Tel_Path_temp2得到已文件夹节点分类数组,格式如下[[2008-3-8],[2008-3-10],[2008-3-9]]
return self.Add_Tel_Path_temp2

# ================================================================================
# 已文件夹为单位进行时间分割计算每日的话单总流量
# MethodName:AnalyseMethod_Countdayflow
# Author:xinkaiAdd:2008-3-10
# ================================================================================
def AnalyseMethod_Countdayflow(self):

self.CountFlow
= 0
# 由线程类获得分析数组
self.AnalyseList = self.Thread_Contorl()
# printAnalyseList
for i in range(len(self.AnalyseList)):
self.CountFlow
= 0
# printself.dict.keys()[i],AnalyseList[i]
# ===============================================================================
# 此处需要加判断日期以及有效话单
# ===============================================================================
for y in range(len(self.AnalyseList[i])):
# printAnalyseList[i][y]
self.AnalyseData = open(self.AnalyseList[i][y], ' r ' )
self.CountFlow
+= len(self.AnalyseData.readlines())
# printself.dict.keys()[i]+str(len(AnalyseData.readlines()))+" "
print self.dict.keys()[i] + " 当日数据总流量为: " + str(self.CountFlow)

# ================================================================================
# 已文件夹为单位进行时间分割按照划定的时间片对每日话单分时流量进行统计
# MethodName:AnalyseMethod_CountTimeFlow
# Author:xinkaiAdd:2008-3-10
# ================================================================================
def AnalyseMethod_CountTimeFlow(self):

Analyse_CountTimeFlow
= open( ' Analyse_CountTimeFlow.log ' , ' w ' )
# 由线程类获得分析数组
self.AnalyseList = self.Thread_Contorl()
for i in range(len(self.AnalyseList)):
temp
= []
self.AnalyseStack
= []
self.AnalyseStackSend
= []
for y in range(len(self.AnalyseList[i])):
# 已时间文件夹为分割点,分类读取话单进行分析
# 用来计算每个文件中每个话单记录的时间堆栈
self.AnalyseData = open(self.AnalyseList[i][y], ' r ' )
self.AnalyseStackSend.append(self.AnalyseData.readlines())
# 用来计算基础时间轴的堆栈AnalyseStack
self.AnalyseData = open(self.AnalyseList[i][y], ' r ' )
self.AnalyseStack.append(self.AnalyseData.readlines()[0])
# 已文件夹时间点分割,获取基础时间轴
self.BaseTimeCenter = self.AnalyseStack[0].split( ' , ' )[ 21 ]
# 转化话单发送时间为整数
self.ChangeBaseTime = self.ChageTime(self.BaseTimeCenter)
for h in range(len(self.AnalyseList[i])):
for z in self.AnalyseStackSend[h]:
self.TelTime
= self.ChageTime(z.split( ' , ' )[ 21 ])
# printstr(self.TelTime)+"//\"+str(self.ChangeBaseTime)
import math
# 获得已基础时间为轴的时间差值
# printint(math.fabs(self.TelTime-self.ChangeBaseTime))/60
temp.append((int(math.fabs(self.TelTime - self.ChangeBaseTime)) / 60 ) / self.AnalyseTimeArea)
print self.dict.keys()[i]
Analyse_CountTimeFlow.writelines(
" 话单时间:%s 初始时间片时间片分时流量 " % (self.dict.keys()[i]))
Analyse_CountTimeFlow.close()
for j in range( 1440 / int(self.AnalyseTimeArea)):
if temp.count(j) <> 0:
Analyse_CountTimeFlow
= open( ' Analyse_CountTimeFlow.log ' , ' a ' )
print " 分析时间片:%s分钟;第%s时间片;分时流量为%s条; " % (self.AnalyseTimeArea,j,temp.count(j))
Analyse_CountTimeFlow.writelines(
" %s%s%s " % (self.AnalyseTimeArea,j,temp.count(j)))

def ChageTime(self,datatime):

# 初始化变量
self.YearMD = ""
self.TimeCS
= ""

# 引用时间参数
self.datatime = datatime
self.SplitDataTime1
= self.datatime.split( ' ' )
self.SplitDataTime2
= self.SplitDataTime1[0].split( ' / ' )
self.SplitDataTime3
= self.SplitDataTime1[ 1 ].split( ' : ' )
# 分割结果['2008','03','10']['11','18','18']

# 组合分割数据转换时间为整数
for i in self.SplitDataTime2:
self.YearMD
+= i
for h in self.SplitDataTime3:
self.TimeCS
+= h
self.newTime
= self.YearMD + self.TimeCS
return int(self.newTime)

def __del__ (self):
pass

class ControlThread(ThreadMain):

def __init__ (self):

basepath,dict
= self.Get_Path()
self.dict
= dict
self.basepath
= basepath

def Control_Thread(self):

# 控制使用分析线程
print " ----------------------------------------- "
ObjectThread
= ThreadMain()
print " 开始计算每日流量: "
ObjectThread.AnalyseMethod_Countdayflow()
print " 计算结束 "
print " ----------------------------------------- "
import time
time.sleep(
0.01 )
print " 开始计算分时流量: "
ObjectThread.AnalyseMethod_CountTimeFlow()
print " 计算结束 "
print " ----------------------------------------- "

if __name__ == " __main__ " :

ObjectControl
= ControlThread()
ObjectControl.Control_Thread()

你可能感兴趣的:(OS,thread,J#)