def tmp(): av = [] final_start = datetime.datetime(2012, 5, 1, 0, 0, 0) start = final_start end = datetime.datetime.now() one_day = datetime.timedelta(days=1) while start < end: # 本月(start)的起止时间点 start_time, end_time = get_start_end_for_month(start) alarm = SysAlarm.objects.filter(start_time__gte=start_time, start_time__lte=end_time) # 本月总时间 all_min = timediff(start_time, end_time, 'minute') # 初始: 本月可用时间=本月总时间 av_time = all_min # 初始: 本月可用时间(因自身原因导致故障)=本月总时间 av_self_time = all_min for a in alarm: if a.type in c.self_alarm_type: av_self_time = av_time - timediff(a.start_time, a.end_time, 'minute') av_time = av_time - timediff(a.start_time, a.end_time, 'minute') # 全部原因导致的系统可用率 av_percent = round(((av_time + 0.0000000000001) / all_min), 6) # 自身原因导致的系统可用率 av_self_percent = round(((av_self_time + 0.0000000000001) / all_min), 6) av_color = c.green if av_percent > 0.9999 else c.red av_self_color = c.green if av_self_percent > 0.9999 else c.red av_percent = to_percent(av_percent) av_self_percent = to_percent(av_self_percent) av.append({'month':start.strftime("%Y-%m-%d"), 'time':av_time, 'self_time':av_self_time, 'percent':av_percent, 'self_percent':av_self_percent, 'color':av_color, 'self_color':av_self_color}) start = end_time + one_day return av
def get_week_report_add_way_and_platform(start_time, end_time): '''为[周报_收藏方式&&平台]获取数据''' try: conn = MySQLdb.connect(**c.db_self_config) cursor = conn.cursor() having_fix, and_fix = _get_fix(start_time, end_time) and_fix = and_fix.replace("gmt_create", "o.gmt_create") #remove_guide = " url not regexp '^http://kan.sohu.com/help/guide-' " # 去掉测试用户的id tmp = ' and user_id !=' tmp += ' and user_id !='.join(map(lambda x:str(x), c.test_id)) tmp = tmp.replace("user_id", "o.user_id") # 收藏总数 sql_total = "select count(oo.object_key) from stats_oper o left join stats_operobject oo on oo.oper_id = o.id\ where 1=1 %s %s and o.oper_type_id in (1,35)" % (and_fix, tmp) cursor.execute(sql_total) result = cursor.fetchone() total = int(result[0]) print total # 段落收藏 sql_partial_total = "select count(oo.object_key) from stats_oper o left join stats_operobject oo on oo.oper_id = o.id\ where 1=1 %s %s and o.oper_type_id in (1,35) \ and object_key regexp '.*\"content\":.*\"content_source\".*partial.*' " % (and_fix, tmp) cursor.execute(sql_partial_total) result = cursor.fetchone() partial = int(result[0]) print partial # 所有通过链接收藏的文章, 这一项包括chrome链接收藏+手机链接收藏 sql_url_total = "select count(oo.object_key) from stats_oper o left join stats_operobject oo on oo.oper_id = o.id\ where 1=1 %s %s and o.oper_type_id in (1,35) and object_key not regexp '.*\"content\".*'" % (and_fix, tmp) # print sql_url_total cursor.execute(sql_url_total) result = cursor.fetchone() url_total = int(result[0]) # print url_total # 收藏平台 platform_total = 0; platforms = {'Android':0, 'Darwin':0, 'Linux':0, 'Macintosh':0, 'unknown':0, 'Windows':0} for p in platforms.keys(): sql = "select count(*) from stats_oper o left join stats_ua u on o.ua_id = u.id \ where 1=1 %s and oper_type_id in (1,35) and platform = '%s' %s" % (tmp, p, and_fix) cursor.execute(sql) result = cursor.fetchone() platforms[p] = int(result[0]) platform_total += platforms[p] # print p # chrome通过链接收藏 phone_url = platforms['Android'] + platforms['Darwin'] chrome_url = url_total - phone_url # page整页收藏 page = total - url_total - partial way = {} way_page = round((page + 0.001) / total, 6) way_chrome_url = round((chrome_url + 0.001) / total, 6) way_phone_url = round((phone_url + 0.001) / total, 6) way_partial = round((partial + 0.001) / total, 6) way['total'] = [total, 1, "收藏方式总数", to_percent(1) ] way['page'] = [page, way_page, "chrome+bookmarklet整页收藏", to_percent(way_page)] way['chrome_url'] = [chrome_url, way_chrome_url, "chrome链接收藏", to_percent(way_chrome_url)] way['phone_url'] = [phone_url, way_phone_url , "手机收藏", to_percent(way_phone_url)] way['partial'] = [partial, way_partial , "chrome段落收藏", to_percent(way_partial)] platform = {} platform_windows = round((platforms['Windows'] + 0.001) / platform_total, 6) platform_macintosh = round((platforms['Macintosh'] + 0.001) / platform_total, 6) platform_linux = round((platforms['Linux'] + 0.001) / platform_total, 6) platform_unknown = round((platforms['unknown'] + 0.001) / platform_total, 6) platform_android = round((platforms['Android'] + 0.001) / platform_total, 6) platform_darwin = round((platforms['Darwin'] + 0.001) / platform_total, 6) platform['total'] = [platform_total, 1, "收藏平台总数"] platform['Windows'] = [platforms['Windows'], platform_windows, "Windows", to_percent(platform_windows)] platform['Macintosh'] = [platforms['Macintosh'], platform_macintosh, "Macintosh", to_percent(platform_macintosh)] platform['Linux'] = [platforms['Linux'], platform_linux, "Linux", to_percent(platform_linux)] platform['unknown'] = [platforms['unknown'], platform_unknown, "unknown", to_percent(platform_unknown)] platform['Android'] = [platforms['Android'], platform_android, "Android", to_percent(platform_android)] platform['Darwin'] = [platforms['Darwin'], platform_darwin, "Darwin", to_percent(platform_darwin)] ret = {'way':way, 'platform':platform} return ret except Exception, e: c.logger.error(e) return str(e)