Chinaunix首页 | 论坛 | 博客
  • 博客访问: 656960
  • 博文数量: 149
  • 博客积分: 3901
  • 博客等级: 中校
  • 技术积分: 1558
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-16 14:33
文章分类

全部博文(149)

文章存档

2014年(2)

2013年(10)

2012年(32)

2011年(21)

2010年(84)

分类: 云计算

2011-11-24 19:24:12











  1. #数据抽取
  1. sql = """
  2. select
  3.  DATE_FORMAT(time,'%Y-%m-%d') as day,
  4.  DATE_FORMAT(time,'%H:%i') as time ,
  5.  pv
  6. from
  7.  minute_stat
  8. """

  9. sql += """
  10. where
  11.  time >= '%s' and
  12.  time <='%s' and
  13.  type=%s and id=%s
  14. """ % (start_time,end_time,ctype,cid)

  15. mycursor.execute(sql)
  16. datas={}
  17. for day,time,pv in mycursor.fetchall() :
  18.     if not datas.has_key(day) : datas[day]={}
  19.     if not datas[day].has_key(time) : datas[day][time]=''
  20.     datas[day][time]=pv

  21. input=open('/home/skynet/public/coding/R/3d_pv/data.txt','w')
  22. times = utils.getMin5s('2011-01-01 00:00','2011-01-01 23:59','%H:%M')

  23. #print>>input,"\t"+"\t".join(times)
  24. for day in utils.getdiffDays(start_time,end_time):
  25.     #row = [day]
  26.     row = []
  27.     for te in times :
  28.         if datas.has_key(day) and datas[day].has_key(te):
  29.             row.append( datas[day][te] )
  30.         else:
  31.             row.append(0)
  32.     print len(row)
  33.     print>>input,"\t".join([ str(c) for c in row ] )
  34. input.close()


#获取两个时间段的所有时间,返回list 
def getdiffDays(beginDate,endDate,format='%Y-%m-%d'): 
    tformat="%Y-%m-%d"; 
    bd=str2date(beginDate,tformat) 
    ed=str2date(endDate,tformat) 
    oneday=datetime.timedelta(days=1)  
    num=daydiff(beginDate,endDate)+1  
    li=[] 
    for i in range(0,num):  
        li.append( date2str(ed,format) )  
        ed=ed-oneday 
    return li[::-1]



#两个日期相隔多少天,例:2008-10-03和2008-10-01是相隔两天 
def daydiff(beginDate,endDate):
    format="%Y-%m-%d";
    bd=str2date(beginDate,format)
    ed=str2date(endDate,format)
    oneday=datetime.timedelta(days=1)
    count=0
    while bd!=ed:
        ed=ed - oneday
        count+=1
    return count




  1. png(filename = "Rplotd.png",
  2.     width = 480, height = 480, units = "px", pointsize = 12,
  3.      bg = "white")
  4. z<-matrix(scan('data.txt',0),ncol=288,byrow=TRUE)
  5. persp(x = seq(0, 1, length.out = nrow(z)),
  6.       y = seq(0, 1, length.out = ncol(z)), col = "lightblue",
  7.       xlab = "day", ylab = "min-5", zlab = "pv",
  8.       z,theta = 45,phi = 45)
  9. dev.off()




阅读(2021) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~