def get_mon_data(dateFrom, dateTo, tenant_id): data = [] domain_data = [] domain_id_list = [ i.domain_id for i in Domain.objects.filter(tenant_id=tenant_id) if i.domain_id is not None ] report_class = ReportManage() ReportForm = reportApi.ReportForm(dateFrom, dateTo, reportType='fiveminutes') d = {} IO = {} for i in domain_id_list: ret = report_class.getFlowReport(ReportForm, i) flowPoints = ret.getFlowPoints() if flowPoints is not None: for j in flowPoints: domain_data.append([ j.point.strftime("%Y-%m-%d"), float('%0.2f' % (float(j.flow))) ]) for k, v in domain_data: d[k] = d.setdefault(k, 0.0) + float(v) IO[k] = IO.setdefault(k, []) + [v] for k in sorted(d): io = max(IO[k]) * 8 / (5 * 60) data.append(MonData(date=k, top_io=io, total_flow=d[k])) return data
def get_hit_report(dateFrom, dateTo): '''请求数统计算法''' Hit = 0 domain_hit = 0 Kwag2 = {} report = ReportManage() ReportForm = reportApi.ReportForm(dateFrom=dateFrom, dateTo=dateTo, reportType='daily') tenant_list = [i.tenant_id for i in Domain.objects.all()] tenant_set = set(tenant_list) for i in tenant_set: domain_id_list = [ j.domain_id for j in Domain.objects.filter(tenant_id=i) if j.domain_id is not None ] for p in domain_id_list: ret = report.getHitReport(ReportForm, p) hit_list = ret.getHitPoints() if hit_list is not None: for k in hit_list: domain_hit += int(k.hit) Hit += domain_hit Kwag2[i] = Hit return Kwag2
def get_tenant_flow(dateFrom, dateTo, tenant_id=None): FLOW_DATA = 0.0 Kwag1 = {} report = ReportManage() ReportForm = reportApi.ReportForm(dateFrom=dateFrom, dateTo=dateTo, reportType='daily') domain_id_list = [ j.domain_id for j in Domain.objects.filter(tenant_id=tenant_id) if j.domain_id is not None ] for i in domain_id_list: ret = report.getFlowReport(ReportForm, i) flowPoints = ret.flowSummary if flowPoints is not None: FLOW_DATA += float(flowPoints) Kwag1[i] = FLOW_DATA return Kwag1
def get_onedomain_lastday_cdnflows(domainId): api = reportApi.ReportApi("syscloudcdn", "491fbc7ac81e48544660") TempDate = datetime.date.today() - datetime.timedelta(days=1) LastDate = TempDate.strftime('%Y-%m-%d') reportForm = reportApi.ReportForm() reportForm.dateFrom = "%s 00:00:00" % LastDate reportForm.dateTo = "%s 23:59:59" % LastDate reportForm.reportType = reportApi.REPORT_TYPE_DAILY result = api.getFlowReport(reportForm, domainId) flows = result.getFlowPoints() amount = 0 if flows is None: return 0 for each in flows: #print each.point,each.flow,each.getFlow(); tmp = string.atof(each.flow.encode()) amount += tmp return int(amount / 1024)
def get_tenant_hit(dateFrom, dateTo, tenant_id=None): Hit = 0 domain_hit = 0 Kwag2 = {} report = ReportManage() ReportForm = reportApi.ReportForm(dateFrom=dateFrom, dateTo=dateTo, reportType='daily') domain_id_list = [ j.domain_id for j in Domain.objects.filter(tenant_id=tenant_id) if j.domain_id is not None ] for i in domain_id_list: ret = report.getHitReport(ReportForm, i) hit_list = ret.getHitPoints() if hit_list is not None: for k in hit_list: domain_hit += int(k.hit) Hit += domain_hit Kwag2[i] = Hit return Kwag2
def get_onedomain_lastday_bandwidth(domainId): api = reportApi.ReportApi("syscloudcdn", "491fbc7ac81e48544660") TempDate = datetime.date.today() - datetime.timedelta(days=1) LastDate = TempDate.strftime('%Y-%m-%d') reportForm = reportApi.ReportForm() reportForm.dateFrom = "%s 00:00:00" % LastDate reportForm.dateTo = "%s 23:59:59" % LastDate reportForm.reportType = reportApi.REPORT_TYPE_5_MINUTES result = api.getFlowReport(reportForm, domainId) flows = result.getFlowPoints() amount = 0 bandwidth = [] if flows is None: return 0 for each in flows: #print each.point,each.flow;#each.getFlow(); tmp = string.atof(each.flow.encode()) bandwidth.append(tmp) return max(bandwidth)
def get_flow_report(dateFrom, dateTo): '''流量统计算法''' Flow = 0 Kwag1 = {} report = ReportManage() ReportForm = reportApi.ReportForm(dateFrom=dateFrom, dateTo=dateTo, reportType='daily') tenant_list = [i.tenant_id for i in Domain.objects.all()] tenant_set = set(tenant_list) for i in tenant_set: domain_id_list = [ j.domain_id for j in Domain.objects.filter(tenant_id=i) if j.domain_id is not None ] for p in domain_id_list: ret = report.getFlowReport(ReportForm, p) flowPoints = ret.flowSummary if flowPoints is not None: Flow += float(flowPoints) Kwag1[i] = Flow return Kwag1
def get_onedomain_lastmonth_cdnflows(domainId): api = reportApi.ReportApi("syscloudcdn", "491fbc7ac81e48544660") Lastmonth_first = str( datetime.date(datetime.date.today().year, datetime.date.today().month - 1, 1)) Lastmonth_last = str( datetime.date(datetime.date.today().year, datetime.date.today().month, 1) - datetime.timedelta(1)) reportForm = reportApi.ReportForm() reportForm.dateFrom = "%s 00:00:00" % Lastmonth_first reportForm.dateTo = "%s 23:59:59" % Lastmonth_last reportForm.reportType = reportApi.REPORT_TYPE_DAILY result = api.getFlowReport(reportForm, domainId) flows = result.getFlowPoints() amount = 0 if flows is None: return 0 for each in flows: tmp = string.atof(each.flow.encode()) amount += tmp return amount
import sys reload(sys) if __name__ == '__main__': pass import logging import api.reportApi as reportApi logging.basicConfig(level = logging.DEBUG) api = reportApi.ReportApi("syscloudcdn", "491fbc7ac81e48544660") #cname=hjdbh1802goaxl.wscloudcdn.com, domainId=1228328, domainNam=downloads.phoneunet.com domainId = "1228328" reportForm = reportApi.ReportForm() reportForm.dateFrom = "2015-09-01 01:00:00" reportForm.dateTo = "2015-09-06 12:00:00" #reportForm.dateFrom = "2013-10-01 01:00:00" #reportForm.dateTo = "2013-10-18 12:00:00" reportForm.reportType = reportApi.REPORT_TYPE_DAILY #logging.debug("获取全部域名的流量报表") #print ("获取全部域名的流量报表") #result = api.getFlowReport(reportForm) #print 'result:', result.getRet(), result.getMsg(), result.getXCncRequestId() #print 'flowPoints:', result.getFlowPoints() #exit(0);