-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateReport.py
174 lines (145 loc) · 6.59 KB
/
generateReport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import xlsxwriter
import requests
import json
import sys
import getopt
import pandas as pd
import os
import yaml
import logging
from datetime import datetime
from datetime import timedelta
usageData = {}
clusterList = []
clusterTopicSummary = []
countryDomainTopicSummary = []
billingData = {}
clusterBill = {}
reportConf = {}
promURL = ""
promUser = ""
promPassword = ""
def parseResult(targetMonth):
report = ReportWriter(targetMonth, billingData)
report.printClusterTopicSummary(clusterTopicSummary)
report.printCountryDomainTopicSummary(countryDomainTopicSummary, len(clusterTopicSummary))
report.close()
def collectData(dateStart, dateEnd):
global clusterTopicSummary
global countryDomainTopicSummary
# fetch cluster topics summary
temp = fetchFromPrometheus("sum by (kafka_id) (confluent_kafka_topic_partitions_count)", dateStart, dateEnd)
for metric in temp["result"]:
clusterTopicSummary.append({'kafka_id': metric["metric"]["kafka_id"], 'value': metric["value"][1]})
# fetch topic info from country domain
temp = fetchFromPrometheus("sum by (country,kafka_id,businessDomain) (confluent_kafka_topic_partitions_count)", dateStart, dateEnd)
for metric in temp["result"]:
countryDomainTopicSummary.append({'country': metric["metric"]["country"], 'businessDomain': metric["metric"]["businessDomain"], 'kafka_id': metric["metric"]["kafka_id"], 'value': metric["value"][1]})
def fetchFromPrometheus(query, dateStart, dateEnd):
promQueryParams = {"query": query,
"start": dateStart.strftime("%Y-%m-%d"),
"end": dateEnd.strftime("%Y-%m-%d")}
res = requests.get(promURL, params=promQueryParams, auth=(promUser, promPassword))
usageData = res.json()
if usageData["status"] == "success":
return usageData["data"]
else:
print("Failed to fetch from Prometheus: {}".format(json.dumps(data[1], indent=4)))
def findEndofMonth(currentDate):
targetYear = currentDate.year
targetMonth = currentDate.month+1
if targetMonth == 13:
targetYear=targetYear+1
targetMonth = 1
return datetime.strptime("{}-{}".format(targetYear, targetMonth), "%Y-%m") - timedelta(days=1)
def parseBillingData(billingCSV):
global billingData
global clusterBill
billingData = pd.read_csv(billingCSV)
clusterBill = billingData.groupby(['LogicalClusterID'])['Total'].sum()
def main():
targetMonth = ''
dateStart = datetime.now()
dateEnd = datetime.now()
global promURL
global promUser
global promPassword
billingCSV = ''
global reportConf
reportConf = os.environ.get('CC_REPORT_CONFIG_PATH', './config/report.yml')
with open(reportConf, 'r') as file:
reportConf = yaml.safe_load(file)
promURL = reportConf['config']['prometheusURL']
promUser = reportConf['config']['username']
promPassword = reportConf['config']['password']
# Take the input from argument if supplied (Automation mode)
# Ask for user input is no argument is provided (User mode)
try:
opts, args = getopt.getopt(sys.argv[1:], "hr:b:",["reportingMonth=","billingCSV="])
except getopt.GetoptError:
print("generateReport.py --reportingMonth(-r)=<reportingMonth> --billingCSV(-b)=<billingCSV>")
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print("generateReport.py --reportingMonth(-r)=<reportingMonth> --billingCSV(-b)=<billingCSV>")
sys.exit()
elif opt in ("-r", "--reportingMonth"):
targetMonth = arg
elif opt in ("-b", "--billingCSV"):
billingCSV = arg
try:
dateStart = datetime.strptime(targetMonth, "%Y-%m")
dateEnd = findEndofMonth(dateStart)
except ValueError:
print("Incorrect data format, should be YYYY-MM")
parseBillingData(billingCSV)
collectData(dateStart, dateEnd)
parseResult(targetMonth)
class ReportWriter:
def __init__(self, targetMonth, billingData):
self.writer = pd.ExcelWriter('UsageReport-{}.xlsx'.format(targetMonth), engine='xlsxwriter')
self.workbook = self.writer.book
self.summaryWorksheet = self.writer.book.add_worksheet("Summary")
clusterBill.to_excel(self.writer, sheet_name='ClusterBilling')
self.detailWorksheet = self.writer.book.add_worksheet("Detail")
self.targetMonth = targetMonth
def printClusterTopicSummary(self, clusterTopicSummary):
self.summaryWorksheet.set_column('A:A', 20)
self.summaryWorksheet.write('A1', 'Report Period')
self.summaryWorksheet.write('B1', self.targetMonth)
row=5
self.summaryWorksheet.write('A4', 'Cluster ID')
self.summaryWorksheet.write('B4', 'Topic Count')
self.summaryWorksheet.write('C4', 'Total($)')
for metric in clusterTopicSummary:
self.summaryWorksheet.write('A'+str(row), metric["kafka_id"])
self.summaryWorksheet.write('B'+str(row), metric["value"])
self.summaryWorksheet.write_formula('C'+str(row), '=VLOOKUP(A'+str(row)+',ClusterBilling!A:B,2)')
row=row+1
def printCountryDomainTopicSummary(self, countryDomainTopicSummary, totalCluster):
self.detailWorksheet.write('A1', 'Country')
self.detailWorksheet.write('B1', 'Business Domain')
self.detailWorksheet.write('C1', 'Cluster ID')
self.detailWorksheet.write('D1', 'Topic Count')
self.detailWorksheet.write('E1', 'Usage%')
self.detailWorksheet.write('F1', 'Price')
self.detailWorksheet.set_column('A:A', 10)
self.detailWorksheet.set_column('B:B', 20)
self.detailWorksheet.set_column('C:C', 20)
self.detailWorksheet.set_column('D:D', 20)
self.detailWorksheet.set_column('E:E', 20)
self.detailWorksheet.set_column('F:F', 20)
# self.detailWorksheet.add_table('A2:D'+str(len(countryDomainTopicSummary)), {'data': countryDomainTopicSummary})
row = 2
for metric in countryDomainTopicSummary:
self.detailWorksheet.write('A'+str(row), metric["country"])
self.detailWorksheet.write('B'+str(row), metric["businessDomain"])
self.detailWorksheet.write('C'+str(row), metric["kafka_id"])
self.detailWorksheet.write('D'+str(row), metric["value"])
self.detailWorksheet.write_formula('E'+str(row), '=D'+str(row)+'/VLOOKUP(C'+str(row)+',Summary!A4:B'+str(4+totalCluster)+',2,false)')
self.detailWorksheet.write_formula('F'+str(row), '=E'+str(row)+'*VLOOKUP(C'+str(row)+',ClusterBilling!A:B,2,false)')
row=row+1
def close(self):
self.writer.save()
# self.workbook.close()
main()