|
1 | 1 | import logging |
2 | 2 | from datetime import datetime, timedelta, timezone |
3 | 3 |
|
4 | | -from flask import Flask, request, url_for, render_template_string |
| 4 | +from flask import Flask, render_template_string, request, url_for |
5 | 5 |
|
6 | 6 | from .query_cost_aws import ( |
7 | 7 | query_hub_names, |
8 | 8 | query_total_costs, |
9 | 9 | query_total_costs_per_component, |
10 | 10 | query_total_costs_per_hub, |
11 | 11 | ) |
| 12 | +from .query_usage import query_usage |
12 | 13 |
|
13 | 14 | app = Flask(__name__) |
14 | 15 | logging.basicConfig(level=logging.INFO) |
15 | 16 |
|
16 | 17 |
|
17 | | -def _parse_from_to_in_query_params(): |
| 18 | +def _parse_from_to_in_query_params(api_provider: str = "prometheus" or "aws"): |
18 | 19 | """ |
19 | 20 | Parse "from" and "to" query parameters, expected to be passed as YYYY-MM-DD |
20 | | - formatted strings or including time as well. |
| 21 | + api_providerted strings or including time as well. |
| 22 | +
|
| 23 | + Args: |
| 24 | + api_provider (str): The api_provider, such as "prometheus" or "aws". Formats dates accordingly. |
21 | 25 |
|
22 | 26 | - "to" defaults to current date (UTC) |
23 | | - - "from" defaults to 30 days before what to is set to |
| 27 | + - "from" defaults to 30 days prior to the "to" date |
| 28 | +
|
| 29 | + Returns: |
| 30 | + from_date and to_date as datetime.date objects, or their string api_providers, according to the `api_provider` argument. |
24 | 31 |
|
25 | 32 | Note that Python 3.11 is required to parse a datetime like |
26 | 33 | 2024-07-27T15:50:18.231Z with a Z in the end, and that Grafana's |
27 | 34 | `${__from:date}` variable is UTC based, but as soon as its adjusted with a |
28 | | - custom format, it no longer is UTC based. Due to that, we need to be able to |
| 35 | + custom api_provider, it no longer is UTC based. Due to that, we need to be able to |
29 | 36 | parse the full datetime string. |
30 | 37 | """ |
31 | | - now_date = datetime.now(timezone.utc).date() |
| 38 | + now_date = datetime.now(timezone.utc).replace( |
| 39 | + hour=0, minute=0, second=0, microsecond=0 |
| 40 | + ) |
32 | 41 | if request.args.get("to"): |
33 | | - to_date = datetime.fromisoformat(request.args["to"]).date() |
| 42 | + to_date = datetime.fromisoformat(request.args["to"]) |
34 | 43 | else: |
35 | 44 | to_date = now_date |
36 | 45 | if request.args.get("from"): |
37 | | - from_date = datetime.fromisoformat(request.args["from"]).date() |
| 46 | + from_date = datetime.fromisoformat(request.args["from"]) |
38 | 47 | else: |
39 | 48 | from_date = to_date - timedelta(days=30) |
40 | 49 |
|
41 | | - # the to_date isn't included when declaring start/end dates against the AWS |
42 | | - # CE API, so we try to add one day to it to make it inclusive |
43 | | - to_date = to_date + timedelta(days=1) |
44 | 50 | # prevent "end date past the beginning of next month" errors |
45 | 51 | if to_date > now_date: |
46 | 52 | to_date = now_date |
47 | 53 | # prevent "Start date (and hour) should be before end date (and hour)" |
48 | 54 | if from_date >= now_date: |
49 | 55 | from_date = to_date - timedelta(days=1) |
50 | 56 |
|
51 | | - # format back to YYYY-MM-DD strings |
52 | | - from_date = from_date.strftime("%Y-%m-%d") |
53 | | - to_date = to_date.strftime("%Y-%m-%d") |
| 57 | + if api_provider == "prometheus": |
| 58 | + return from_date.isoformat(), to_date.isoformat() |
| 59 | + else: |
| 60 | + # "aws" to_dates are exclusive, so we add one day to include it |
| 61 | + to_date = to_date + timedelta(days=1) |
| 62 | + from_date = from_date.strftime("%Y-%m-%d") |
| 63 | + to_date = to_date.strftime("%Y-%m-%d") |
| 64 | + return from_date, to_date |
54 | 65 |
|
55 | | - return from_date, to_date |
56 | 66 |
|
57 | 67 | @app.route("/") |
58 | 68 | def index(): |
| 69 | + """ |
| 70 | + Index page that lists all available endpoints in the application. |
| 71 | + """ |
59 | 72 | links = [] |
60 | 73 | for rule in app.url_map.iter_rules(): |
61 | 74 | # Skip static routes and those requiring parameters |
62 | | - if rule.endpoint != 'static' and len(rule.arguments) == 0: |
| 75 | + if rule.endpoint != "static" and len(rule.arguments) == 0: |
63 | 76 | url = url_for(rule.endpoint) |
64 | 77 | links.append((rule.endpoint, url)) |
65 | | - |
| 78 | + |
66 | 79 | # Render links using a simple HTML template |
67 | | - return render_template_string(''' |
| 80 | + return render_template_string( |
| 81 | + """ |
68 | 82 | <h1>Available Endpoints</h1> |
69 | 83 | <ul> |
70 | 84 | {% for endpoint, url in links %} |
71 | 85 | <li><a href="{{ url }}">{{ endpoint }}</a></li> |
72 | 86 | {% endfor %} |
73 | 87 | </ul> |
74 | | - ''', links=links) |
| 88 | + """, |
| 89 | + links=links, |
| 90 | + ) |
75 | 91 |
|
76 | 92 |
|
77 | 93 | @app.route("/health/ready") |
78 | 94 | def ready(): |
| 95 | + """ |
| 96 | + Readiness probe endpoint. |
| 97 | + """ |
79 | 98 | return ("200: OK", 200) |
80 | 99 |
|
81 | 100 |
|
82 | 101 | @app.route("/hub-names") |
83 | 102 | def hub_names(): |
84 | | - from_date, to_date = _parse_from_to_in_query_params() |
| 103 | + """ |
| 104 | + Endpoint to query hub names. |
| 105 | + """ |
| 106 | + from_date, to_date = _parse_from_to_in_query_params(api_provider="aws") |
85 | 107 |
|
86 | 108 | return query_hub_names(from_date, to_date) |
87 | 109 |
|
88 | 110 |
|
89 | 111 | @app.route("/total-costs") |
90 | 112 | def total_costs(): |
91 | | - from_date, to_date = _parse_from_to_in_query_params() |
| 113 | + """ |
| 114 | + Endpoint to query total costs. |
| 115 | + """ |
| 116 | + from_date, to_date = _parse_from_to_in_query_params(api_provider="aws") |
92 | 117 |
|
93 | 118 | return query_total_costs(from_date, to_date) |
94 | 119 |
|
95 | 120 |
|
96 | 121 | @app.route("/total-costs-per-hub") |
97 | 122 | def total_costs_per_hub(): |
98 | | - from_date, to_date = _parse_from_to_in_query_params() |
| 123 | + """ |
| 124 | + Endpoint to query total costs per hub. |
| 125 | + """ |
| 126 | + from_date, to_date = _parse_from_to_in_query_params(api_provider="aws") |
99 | 127 |
|
100 | 128 | return query_total_costs_per_hub(from_date, to_date) |
101 | 129 |
|
102 | 130 |
|
103 | 131 | @app.route("/total-costs-per-component") |
104 | 132 | def total_costs_per_component(): |
105 | | - from_date, to_date = _parse_from_to_in_query_params() |
| 133 | + """ |
| 134 | + Endpoint to query total costs per component. |
| 135 | + """ |
| 136 | + from_date, to_date = _parse_from_to_in_query_params(api_provider="aws") |
| 137 | + hub_name = request.args.get("hub") |
| 138 | + component = request.args.get("component") |
| 139 | + |
| 140 | + return query_total_costs_per_component(from_date, to_date, hub_name, component) |
| 141 | + |
| 142 | + |
| 143 | +@app.route("/total-usage") |
| 144 | +def total_usage(): |
| 145 | + """ |
| 146 | + Endpoint to query total usage. |
| 147 | + Expects 'from' and 'to' query parameters in the api_provider YYYY-MM-DD. |
| 148 | + Optionally accepts 'hub', 'component' and 'user', query parameters. |
| 149 | + """ |
| 150 | + from_date, to_date = _parse_from_to_in_query_params(api_provider="prometheus") |
106 | 151 | hub_name = request.args.get("hub") |
| 152 | + component_name = request.args.get("component") |
| 153 | + user_name = request.args.get("user") |
107 | 154 |
|
108 | | - return query_total_costs_per_component(from_date, to_date, hub_name) |
| 155 | + return query_usage(from_date, to_date, hub_name, component_name, user_name) |
0 commit comments