-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsimony.py
executable file
·328 lines (284 loc) · 9.41 KB
/
parsimony.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python3
import os
import boto3
import configparser
from datetime import date, timedelta
from fastapi import FastAPI, Request
import logging
from botocore.exceptions import BotoCoreError
from slack_bolt import App # type: ignore
from slack_bolt.adapter.fastapi import SlackRequestHandler
from quickchart import QuickChart, QuickChartFunction
account_count = 1
accounts = []
def get_days():
today = date.today()
start = today - timedelta(days=8)
end = today - timedelta(days=1)
start = str(start)
end = str(end)
return start, end
def get_cost(aws_client: boto3.client, start: str, end: str):
response = aws_client.get_cost_and_usage(
TimePeriod={"Start": start, "End": end},
Granularity="DAILY",
# Filter={
# 'Dimensions': {
# 'Key': 'LINKED_ACCOUNT',
# 'Values': accounts,
# 'MatchOptions': ['EQUALS']
# }
# },
Metrics=[
"AmortizedCost",
],
)
return response
def get_chart(cost_response: dict):
qc = QuickChart()
qc.width = 500
qc.height = 300
qc.background_color = "transparent"
labels = []
data = []
results = cost_response["ResultsByTime"]
for result in results:
labels.append(result["TimePeriod"]["Start"])
formatted_amount = round(
float(
result["Total"]["AmortizedCost"]["Amount"]), 2)
data.append(formatted_amount)
qc.config = {
"type": "line",
"data": {
"labels": labels,
"datasets": [{
"label": "Cost in $",
"data": data,
"backgroundColor": QuickChartFunction(
"getGradientFillHelper('vertical', \
['rgba(63, 100, 249, 0.2)', \
'rgba(255, 255, 255, 0.2)'])"
),
}]
},
"options": {
"plugins": {
"tickFormat": {
"style": "currency",
"currency": "USD"
},
"datalabels": {
"anchor": "end",
"align": "top",
"color": "#fff",
"backgroundColor": "rgba(34, 139, 34, 0.6)",
"borderColor": "rgba(34, 139, 34, 1.0)",
"borderWidth": 1,
"borderRadius": 5,
"formatter": QuickChartFunction(
'''(value) => { return '$' + value; }'''
),
}
}
},
}
return qc.get_url()
def generate_config(config_file: str):
parsed_config = configparser.ConfigParser()
parsed_config.read(config_file)
if not parsed_config.has_section("AWS"):
logging.warning(
"AWS section not defined in config.ini. \
Assuming AWS keys are provided by ENV VAR"
)
return parsed_config
config = generate_config("config.ini")
url = config["quickchart"]["url"]
# I hate how this boto checking is implemented...
# but I don't know how to do this better.
try:
client = boto3.client(
"ce",
aws_access_key_id=config["AWS"]["access_key"],
aws_secret_access_key=config["AWS"]["aws_secret_key"],
)
except KeyError:
try:
client = boto3.client("ce")
sts = boto3.client("sts")
sts.get_caller_identity()
except BotoCoreError as e:
logging.warning(
"no AWS credentials found, assuming credentials will be provided later \
or your forgot to provide via ENV VAR or config.ini \n \
actual BotoCore error: " + format(str(e))
)
try:
slack_token = config["slack"]["slack_bot_token"]
slack_signing_secret = config["slack"]["slack_signing_secret"]
except KeyError:
if os.environ.get("SLACK_BOT_TOKEN") and os.environ.get("SLACK_SIGNING_SECRET"):
slack_token = os.environ.get("SLACK_BOT_TOKEN")
slack_signing_secret = os.environ.get("SLACK_SIGNING_SECRET")
else:
logging.critical("no slack app credentials found")
os._exit(2)
app = App(token=slack_token, signing_secret=slack_signing_secret)
app_handler = SlackRequestHandler(app)
def account_modal_view(number_of_accounts: int):
view = {
"title": {
"type": "plain_text",
"text": "Configure Parsimony"
},
"submit": {
"type": "plain_text",
"text": "Submit"
},
"blocks": [
{
"type": "actions",
"elements": [
{
"type": "button",
"action_id": "add_account",
"text": {
"type": "plain_text",
"text": "Add another account "
}
}
]
}
],
"type": "modal",
"callback_id": "config_view"
}
for i in range(number_of_accounts):
account_input = {
"type": "input",
"block_id": f"account_{i}",
"element": {
"type": "plain_text_input",
"action_id": f"account_input_{i}",
"placeholder": {
"type": "plain_text",
"text": "Account Number"
}
},
"label": {
"type": "plain_text",
"text": "AWS Account"
}
}
# noinspection PyTypeChecker
view["blocks"].append(account_input)
return view
api = FastAPI()
@api.post("/slack/events")
async def endpoint(req: Request):
return await app_handler.handle(req)
@api.get("/healthcheck")
async def health_check():
return {"healthy": "true"}
@app.event("app_home_opened") # type: ignore
def update_home_tab(client, event, logger):
try:
# views.publish is the method that your
# app uses to push a view to the Home tab
client.views_publish(
# the user that opened your app's app home
user_id=event["user"],
# the view object that appears in the app home
view={
"type": "home",
"callback_id": "home_view",
# body of the view
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Thanks for using Parsimony* :tada:",
},
},
{"type": "divider"},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Available Commands Are: \n `/parsimony` \
- run parsimony with defaults",
},
},
],
},
)
except Exception as e:
logger.error(f"Error publishing home tab: {e}")
@app.command("/parsimony") # type: ignore
def slash_parsimony(ack, respond, logger):
ack()
try:
start, end = get_days()
cost = get_cost(client, start, end)
chart_url = get_chart(cost)
ack()
respond(
{
"response_type": "in_channel",
"blocks": [
{
"type": "image",
"title": {"type": "plain_text",
"text": "Latest data"},
"block_id": "quickchart-image",
"image_url": chart_url,
"alt_text": "Chart showing latest data",
}
],
}
)
except Exception as e:
logger.error(f"Error publishing slash parsimony: {e}")
@app.command("/parsimony-config") # type: ignore
def open_modal(ack, body, client):
# Acknowledge the command request
ack()
# Call views_open with the built-in client
client.views_open(
# Pass a valid trigger_id within 3 seconds of receiving it
# if you are seeing invalid trigger id, token might be wrong
trigger_id=body["trigger_id"],
# View payload
view=account_modal_view(account_count)
)
@app.action("add_account") # type: ignore
def update_modal(ack, body, client):
global account_count
account_count += 1
# Acknowledge the button request
ack()
# Call views_update with the built-in client
client.views_update(
# Pass the view_id
view_id=body["view"]["id"],
# String that represents view state
# to protect against race conditions
hash=body["view"]["hash"],
# View payload with updated blocks
view=account_modal_view(account_count)
)
@app.view("config_view") # type: ignore
def handle_submission(ack, body, client, view, logger):
ack()
global accounts
for account_input in view["state"]["values"]:
for account in view["state"]["values"][f"{account_input}"]:
accounts.append(str((view["state"]
["values"]
[f"{account_input}"]
[f"{account}"]
["value"])))
if __name__ == "__main__":
app.start(port=int(os.environ.get("PORT", 3000)))