-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_testing.py
64 lines (48 loc) · 1.63 KB
/
log_testing.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
"""Exploration of aws logs for tg bot / api usage. Potentially useful for future stats features"""
import boto3
from datetime import datetime
import json
from collections import Counter
from dotenv import load_dotenv
import os
load_dotenv()
LOG_GROUP = os.environ.get("LOG_GROUP")
client = boto3.client("logs")
response = client.describe_log_streams(
logGroupName=LOG_GROUP,
orderBy="LastEventTime",
descending=True,
)
for stream in response["logStreams"]:
print(stream["logStreamName"], stream["lastEventTimestamp"])
t = datetime(2023, 11, 28, 0, 0)
# find streams with relevant events
streams = list(
filter(
lambda x: x["lastEventTimestamp"] >= int(t.timestamp() * 1000),
response["logStreams"],
)
)
events = client.filter_log_events(
logGroupName=LOG_GROUP,
logStreamNames=[stream["logStreamName"] for stream in streams],
startTime=int(t.timestamp() * 1000),
filterPattern='{$.type = "telegram.callback"}',
)
def handle_callback_log(log):
data = json.loads(log["message"])
message = json.loads(data["message"])
result = {
"timestamp": data["timestamp"],
"user": message["callback_query"]["from"]["username"],
"item": message["callback_query"]["data"],
}
return result
data = list(map(handle_callback_log, events["events"]))
song_counter = Counter(d["item"] for d in data if d["item"].startswith("song:"))
composer_counter = Counter(d["item"] for d in data if d["item"].startswith("composer:"))
collection_counter = Counter(
d["item"] for d in data if d["item"].startswith("collection:")
)
for song in song_counter.most_common(10):
print(song)