一段程序代码的修改,将原来按分钟活动的方式改为秒级滑动,可以提供数据分析的精度

  1. #!/usr/bin/env python
  2. #-*- coding: cp936 -*-
  3. #===============================================================================
  4. #开发需求:与数据分离工具共同使用,该程序主要用来分析单位时间的单表入库平均值
  5. #1.设定一个分析时间片,单位为分钟,AnalyseTimeArea
  6. #2.可设定需要分析数据库表所在字段,AnalyseField
  7. #===============================================================================
  8. import threading,os,time
  9. class ReadConfig():
  10.     def Base_Config(self):
  11.         #配置话单路径
  12.         TelPath = os.getcwd()+"//Analyse"
  13.         #print TelPath
  14.         return TelPath
  15. class GetPath(ReadConfig):
  16.     def Get_Path(self):
  17.         #获取路径下分析文件,建立字典
  18.         dict = {}
  19.         dir_temp = []
  20.         file_temp = []
  21.         basepath = self.Base_Config()
  22.         #print basepath
  23.         for root, dirs, files in os.walk(self.Base_Config()):
  24.             #获取文件夹节点,获取文件夹对应文件
  25.             for dir in dirs:
  26.                 dir_temp.append(dir)
  27.         dir_temp.sort()
  28.         for i in dir_temp:
  29.             #print self.Base_Config()+"//"+str(i)
  30.             for root, dirs, files in os.walk(self.Base_Config()+"//"+str(i)):
  31.                 #获取对应文件夹下的数据
  32.                 file_temp.append(files)
  33.         for i in range(len(dir_temp)):
  34.             #建立数据字典
  35.             dict[dir_temp[i]] = file_temp[i]
  36.         return basepath,dict
  37. class ThreadMain(threading.Thread,GetPath):
  38.     def __init__(self):
  39.         #设定一个分析时间片,单位为秒级滑动,如小于1分钟(30秒:0.5分钟)
  40.         #self.AnalyseTimeArea=1.0,后续分析使用float型
  41.         self.AnalyseTimeArea = 30
  42.         #需要分析数据库表所在字段
  43.         self.AnalyseField = 9
  44.         basepath,dict = self.Get_Path()
  45.         #print basepath,dict
  46.         self.dict = dict
  47.         self.basepath = basepath
  48.     def Thread_Contorl(self):
  49.         #读取字典的索引,索引为文件夹节点self.dict.keys()[i]
  50.         self.Add_Tel_Path_temp2 = []
  51.         #print self.dict.keys()
  52.         for i in range(len(self.dict.keys())):
  53.             self.Add_Tel_Path_temp1 = []
  54.             #print self.dict.keys()[i],self.dict[self.dict.keys()[i]]
  55.             for y in range(len(self.dict[self.dict.keys()[i]])):
  56.                 #print self.dict.keys()[i],self.dict[self.dict.keys()[i]]
  57.                 #print len(self.dict[self.dict.keys()[i]])
  58.                self.Add_Tel_Path =  self.basepath+"//"+self.dict.keys()[i]+"//"+self.dict[self.dict.keys()[i]][y]
  59.                self.Add_Tel_Path_temp1.append(self.Add_Tel_Path)
  60.             #print Add_Tel_Path_temp1
  61.             self.Add_Tel_Path_temp2.append(self.Add_Tel_Path_temp1)
  62.         #Add_Tel_Path_temp2得到已文件夹节点分类数组,格式如下[[2008-3-8],[2008-3-10],[2008-3-9]]
  63.         return self.Add_Tel_Path_temp2
  64.     #================================================================================
  65.     # 已文件夹为单位进行时间分割计算每日的话单总流量
  66.     # Method Name:AnalyseMethod_Countdayflow
  67.     # Author:xinkai Add:2008-3-10
  68.     #================================================================================
  69.     def AnalyseMethod_Countdayflow(self):
  70.         self.CountFlow = 0
  71.         #由线程类获得分析数组
  72.         self.AnalyseList = self.Thread_Contorl()
  73.         #print AnalyseList
  74.         for i in range(len(self.AnalyseList)):
  75.             #print self.dict.keys()[i],AnalyseList[i]
  76.             #===============================================================================
  77.             #            此处需要加判断日期以及有效话单
  78.             #===============================================================================
  79.             for y in range(len(self.AnalyseList[i])):
  80.                 #print AnalyseList[i][y]
  81.                 self.AnalyseData = open(self.AnalyseList[i][y],'r')
  82.                 #self.CountFlow += len(self.AnalyseData.readlines())
  83.                 print "执行过程[ %s ]:第%s文件大小为%s文件" % (self.dict.keys()[i],y,len(self.AnalyseData.readlines()))
  84.                 self.CountFlow += len(self.AnalyseData.readlines())
  85.         self.AnalyseData.close()
  86.                 #print self.dict.keys()[i]+str(len(AnalyseData.readlines()))+"/n"
  87.             #print "[",self.dict.keys()[i]+" ]当日数据总流量为:"+str(self.CountFlow)
  88.     #================================================================================
  89.     # 已文件夹为单位进行时间分割按照划定的时间片对每日话单分时流量进行统计
  90.     # Method Name:AnalyseMethod_CountTimeFlow
  91.     # Author:xinkai Add:2008-3-10
  92.     #================================================================================
  93.     def AnalyseMethod_CountTimeFlow(self):
  94.         #Analyse_CountTimeFlow = open('Analyse_CountTimeFlow.log','w')
  95.         #由线程类获得分析数组
  96.         self.AnalyseList = self.Thread_Contorl()
  97.         for i in range(len(self.AnalyseList)):
  98.             temp = []
  99.             self.AnalyseStack = []
  100.             self.AnalyseStackSend = []
  101.             for y in range(len(self.AnalyseList[i])):
  102.                 #已时间文件夹为分割点,分类读取话单进行分析
  103.                 #用来计算每个文件中每个话单记录的时间堆栈
  104.                 self.AnalyseData = open(self.AnalyseList[i][y],'r')
  105.                 self.AnalyseStackSend.append(self.AnalyseData.readlines())
  106.                 #用来计算基础时间轴的堆栈AnalyseStack
  107.                 self.AnalyseData = open(self.AnalyseList[i][y],'r')
  108.                 self.AnalyseStack.append(self.AnalyseData.readlines()[0])
  109.             #已文件夹时间点分割,获取基础时间轴
  110.             self.BaseTimeCenter = self.AnalyseStack[0].split(',')[self.AnalyseField]
  111.             #转化话单发送时间为整数
  112.             count = 0
  113.             self.ChangeBaseTime = self.ChageTime(self.BaseTimeCenter)
  114.             for h in range(len(self.AnalyseList[i])):
  115.                 for z in self.AnalyseStackSend[h]:
  116.                     self.TelTime = self.ChageTime(z.split(',')[self.AnalyseField])
  117.                     #print str(self.TelTime)+"////"+str(self.ChangeBaseTime)
  118.                     import math
  119.                     #获得已基础时间为轴的时间差值
  120.                     #print int(math.fabs(self.TelTime - self.ChangeBaseTime))/60
  121.                     #print "TelTime",self.TelTime
  122.                     print "ChangeBaseTime",self.ChangeBaseTime
  123. #                    if (self.TelTime - self.ChangeBaseTime) == 0:
  124. #                        count += 1
  125. #                        print "count",count
  126.                     temp.append((int(math.fabs(self.TelTime - self.ChangeBaseTime))) / self.AnalyseTimeArea)
  127.             #Analyse_CountTimeFlow.writelines("话单时间:%s/n初始时间片    时间片    分时流量/n" % (self.dict.keys()[i]))
  128.             #Analyse_CountTimeFlow.close()
  129.             print self.BaseTimeCenter[1:11]
  130.             for j in range(86400/self.AnalyseTimeArea):
  131.                 if temp.count(j) <> 0:
  132.                     #Analyse_CountTimeFlow = open('Analyse_CountTimeFlow.log','a')
  133.                     #print j%self.AnalyseTimeArea
  134.                     print "分析日期,%s,分析时间片,%s,秒钟,第,%s,时间片,分时流量为,%s,条," % (self.BaseTimeCenter[1:11],self.AnalyseTimeArea,j,temp.count(j))
  135.                     #Analyse_CountTimeFlow.writelines("%s    %s    %s/n" % (self.AnalyseTimeArea,j,temp.count(j)))
  136.                     #print "总计分析时间片=",self.AnalyseTimeArea*(j+1)
  137.         time.sleep(1000)
  138.     def ChageTime(self,datatime):
  139.         #初始化变量
  140.         self.YearMD = ""
  141.         self.TimeCS = ""
  142.         #引用时间参数
  143.         self.datatime = datatime
  144.         self.SplitDataTime2 = self.datatime[1:11].split('-')
  145.         self.SplitDataTime3 = self.datatime[12:20].split('.')
  146.         #分割结果['2008', '03', '10'] ['11', '18', '18']
  147.         #组合分割数据转换时间为整数
  148.         for i in self.SplitDataTime2:
  149.             self.YearMD += i
  150.         for h in self.SplitDataTime3:
  151.             self.TimeCS += h
  152.         self.newTime = self.YearMD + self.TimeCS
  153.         return int(self.newTime)
  154.     def __del__(self):
  155.         pass
  156. class ControlThread(ThreadMain):
  157.     def __init__(self):
  158.         basepath,dict = self.Get_Path()
  159.         self.dict = dict
  160.         self.basepath = basepath
  161.     def Control_Thread(self):
  162.         #控制使用分析线程
  163.         print "-----------------------------------------"
  164.         ObjectThread = ThreadMain()
  165.         print "开始计算每日流量:"
  166.         ObjectThread.AnalyseMethod_Countdayflow()
  167.         print "计算结束"
  168.         print "-----------------------------------------"
  169.         import time
  170.         time.sleep(0.01)
  171.         print "开始计算分时流量:"
  172.         ObjectThread.AnalyseMethod_CountTimeFlow()
  173.         print "计算结束"
  174.         print "-----------------------------------------"
  175. if __name__ == "__main__":
  176.     ObjectControl = ControlThread()
  177.     ObjectControl.Control_Thread()
  178.     #path = ReadConfig(path)

你可能感兴趣的:(一段程序代码的修改,将原来按分钟活动的方式改为秒级滑动,可以提供数据分析的精度)