in templates/aws-cloudfront-monitoring/source/lambda.d/metric_collector_download_speed_cdn/metric_collector_download_speed_cdn.py [0:0]
def gen_detailed_by_interval(metric, start_time, end_time, domain):
interval_list = []
start_datetime = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
end_datetime = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
temp_datetime = start_datetime
detailed_data = []
while True:
log.info("[gen_detailed_by_interval] Setup interval list")
interval_item = {}
interval_item['start'] = temp_datetime.strftime("%Y-%m-%d %H:%M:%S")
temp_datetime += timedelta(minutes=5)
if not temp_datetime < end_datetime:
interval_item['end'] = end_datetime.strftime("%Y-%m-%d %H:%M:%S")
athena_qs = assemble_speed(metric, interval_item['start'],
interval_item['end'], domain)
athena_query_result = schedule_athena_query(athena_qs)
interval_item['QueryId'] = athena_query_result['QueryExecutionId']
interval_list.append(interval_item)
break
interval_item['end'] = temp_datetime.strftime("%Y-%m-%d %H:%M:%S")
athena_qs_5m = assemble_speed(metric, interval_item['start'],
interval_item['end'], domain)
athena_query_result_5m = schedule_athena_query(athena_qs_5m)
interval_item['QueryId'] = athena_query_result_5m['QueryExecutionId']
interval_list.append(interval_item)
for item in interval_list:
log.info("[gen_detailed_by_interval] Start to get query result")
speed_item = {}
speed_item["domain"] = domain
geo_info = {}
query_result = get_athena_query_result(athena_client, item['QueryId'])
log.info(json.dumps(query_result))
speed_item["timestamp"] = str(int(format_date_time(item['end'])))
for row in query_result['ResultSet']['Rows']:
if row['Data'][0]['VarCharValue'] != "speed":
row_speed = row['Data'][0]['VarCharValue']
row_country = row['Data'][1]['VarCharValue']
row_isp = row['Data'][2]['VarCharValue']
if row_country not in speed_item:
isp_json = {
"250K": 0,
"500K": 0,
"750K": 0,
"1M": 0,
"2M": 0,
"3M": 0,
"4M": 0,
"Other": 0
}
country_json = {}
isp_json = count_by_speed(row_speed, isp_json)
country_json[row_isp] = isp_json
speed_item[row_country] = country_json
else:
if row_isp not in speed_item[row_country]:
isp_json = {
"250K": 0,
"500K": 0,
"750K": 0,
"1M": 0,
"2M": 0,
"3M": 0,
"4M": 0,
"Other": 0
}
isp_json = count_by_speed(row_speed, isp_json)
speed_item[row_country][row_isp] = isp_json
else:
speed_item[row_country][row_isp] = count_by_speed(
row_speed, speed_item[row_country][row_isp])
for value in speed_item:
if value != "domain" and value != "timestamp":
for isp_item in speed_item[value]:
temp = speed_item[value][isp_item]
total = temp["250K"] + temp["500K"] + temp["750K"] + temp[
"1M"] + temp["2M"] + temp["3M"] + temp["4M"] + temp[
"Other"]
speed_item[value][isp_item]["250K"] = Decimal(
temp["250K"] / total).quantize(Decimal('0.00'))
speed_item[value][isp_item]["500K"] = Decimal(
temp["500K"] / total).quantize(Decimal('0.00'))
speed_item[value][isp_item]["750K"] = Decimal(
temp["750K"] / total).quantize(Decimal('0.00'))
speed_item[value][isp_item]["1M"] = Decimal(
temp["1M"] / total).quantize(Decimal('0.00'))
speed_item[value][isp_item]["2M"] = Decimal(
temp["2M"] / total).quantize(Decimal('0.00'))
speed_item[value][isp_item]["3M"] = Decimal(
temp["3M"] / total).quantize(Decimal('0.00'))
speed_item[value][isp_item]["4M"] = Decimal(
temp["4M"] / total).quantize(Decimal('0.00'))
speed_item[value][isp_item]["Other"] = Decimal(
temp["Other"] / total).quantize(Decimal('0.00'))
detailed_data.append(speed_item)
log.info("[gen_detailed_by_interval] Generated data: ")
log.info(str(detailed_data))
return detailed_data