|
1 | 1 | #!/usr/bin/env python3 |
2 | | -from typing import Literal, Optional |
| 2 | +from typing import Final, Literal, Optional |
3 | 3 |
|
4 | 4 | import fire |
5 | 5 | import pandas as pd |
6 | 6 | import matplotlib.pyplot as plt |
7 | 7 |
|
8 | | -from src.query import QueryConfig, fetch_votes, Boto3Config |
9 | | -from src.transform import unique_over_time, count_votes, time_series_total_count |
| 8 | +from gql import gql, Client |
| 9 | +from gql.transport.aiohttp import AIOHTTPTransport |
| 10 | + |
| 11 | + |
| 12 | +DKW_ENDPOINT: Final[dict[str, str]] = { |
| 13 | + "stg": "https://dkw.dev.cloudnativedays.jp/query", |
| 14 | + "prd": "https://dkw.cloudnativedays.jp/query", |
| 15 | +} |
10 | 16 |
|
11 | 17 |
|
12 | 18 | class Command: |
13 | 19 | """CFP Vote Counter CLI""" |
14 | 20 |
|
15 | | - def generate_csv(self, event: str, env: Literal["stg", "prd"] = "prd", span: int = 3600, |
16 | | - region: Optional[str] = None): |
| 21 | + def generate_csv(self, event: str, env: Literal["stg", "prd"] = "prd"): |
17 | 22 | """ |
18 | 23 | Generate transformed CFP vote csv |
19 | 24 |
|
20 | 25 | :param event: Event Abbreviation (e.g. cndt2022) |
21 | 26 | :param env: Environment (stg|prd) |
22 | | - :param span: Seconds of span where multiple votes from the same GIP would be considered as the same one |
23 | | - :param region: Region of Dynamo vote table |
24 | 27 | """ |
25 | | - conf = QueryConfig(event, env) |
26 | | - boto3_conf = Boto3Config(region) |
27 | | - res = fetch_votes(conf, boto3_conf) |
28 | | - if res["Count"] == 0: |
29 | | - print("No votes found.") |
30 | | - return |
31 | 28 |
|
32 | | - df = pd.DataFrame(res["Items"]) |
33 | | - df = unique_over_time(df, span) |
34 | | - sr = count_votes(df) |
35 | | - print(sr.to_csv()) |
36 | | - |
37 | | - def time_series(self, event: str, env: Literal["stg", "prd"] = "prd", span: int = 3600, |
38 | | - region: Optional[str] = None, file: str = None): |
39 | | - """ |
40 | | - Generate transformed CFP vote csv |
| 29 | + transport = AIOHTTPTransport(url=DKW_ENDPOINT[env]) |
| 30 | + client = Client(transport=transport, fetch_schema_from_transport=True) |
41 | 31 |
|
42 | | - :param event: Event Abbreviation (e.g. cndt2022) |
43 | | - :param env: Environment (stg|prd) |
44 | | - :param span: Seconds of span where multiple votes from the same GIP would be considered as the same one |
45 | | - :param region: Region of Dynamo vote table |
46 | | - :param file: File name of time-series graph figure |
| 32 | + query = gql( |
47 | 33 | """ |
48 | | - conf = QueryConfig(event, env) |
49 | | - boto3_conf = Boto3Config(region) |
50 | | - res = fetch_votes(conf, boto3_conf) |
51 | | - if res["Count"] == 0: |
52 | | - print("No votes found.") |
53 | | - return |
54 | | - |
55 | | - df = pd.DataFrame(res["Items"]) |
56 | | - df = unique_over_time(df, span) |
57 | | - df = time_series_total_count(df, span) |
58 | | - print(df) |
59 | | - df.plot() |
60 | | - if file: |
61 | | - plt.savefig(file) |
62 | | - else: |
63 | | - plt.show() |
| 34 | + query getVoteCounts($confName: ConfName!){ |
| 35 | + voteCounts(confName: $confName) { |
| 36 | + talkId |
| 37 | + count |
| 38 | + } |
| 39 | + } |
| 40 | + """) |
| 41 | + |
| 42 | + data = client.execute(query, {"confName": event}) |
| 43 | + df = ( |
| 44 | + pd.DataFrame(data["voteCounts"]) |
| 45 | + .sort_values(["count", "talkId"], ascending=[False, True]) |
| 46 | + .reset_index(drop=True) |
| 47 | + ) |
| 48 | + print(df.to_csv()) |
64 | 49 |
|
65 | 50 |
|
66 | 51 | if __name__ == "__main__": |
|
0 commit comments