-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache_stats.py
executable file
·191 lines (154 loc) · 4.87 KB
/
cache_stats.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
#!/usr/bin/env python3
"""Cache stats tool.
2024/Jun/11 @ Zdenek Styblik <[email protected]>
"""
import argparse
import logging
import sys
import traceback
from dataclasses import dataclass
import rss2irc
from lib import CachedData
BUCKET_COUNT = 10
@dataclass
class Bucket:
"""Class represents a time range bucket."""
ts_min: int
ts_max: int
count: int
def calc_distribution(
logger: logging.Logger, cache: CachedData, buckets
) -> int:
"""Calculate item distribution inside cache."""
keys = list(buckets.keys())
error_cnt = 0
for item in cache.items.values():
try:
timestamp = get_timestamp(item)
except (KeyError, TypeError, ValueError):
error_cnt += 1
logger.debug("%s", traceback.format_exc())
continue
accounted = False
for key in keys:
if (
timestamp >= buckets[key].ts_min
and timestamp <= buckets[key].ts_max
):
buckets[key].count += 1
accounted = True
if not accounted:
logger.debug("Unaccounted key: %s", timestamp)
error_cnt += 1
return error_cnt
def get_timestamp(data) -> int:
"""Convert input data to int.
:raises: KeyError, TypeError, ValueError
"""
if isinstance(data, (int, float)):
return int(data)
elif isinstance(data, dict):
if "expiration" not in data:
raise KeyError("dict has no key 'expiration'")
return int(data["expiration"])
raise TypeError("unsupported type '{}'".format(type(data)))
def get_timestamp_minmax(
logger: logging.Logger, cache: CachedData
) -> (int, int, int):
"""Return timestamp min, max and no. of errors."""
ts_min = 99999999999
ts_max = 0
error_cnt = 0
for item in cache.items.values():
try:
timestamp = get_timestamp(item)
except (KeyError, TypeError, ValueError):
error_cnt += 1
logger.debug("%s", traceback.format_exc())
continue
if timestamp < ts_min:
ts_min = timestamp
if timestamp > ts_max:
ts_max = timestamp
return ts_min, ts_max, error_cnt
def generate_buckets(
logger: logging.Logger, ts_min: int, ts_max: int, step: int
):
"""Generate time range buckets."""
buckets = {}
for i in range(0, BUCKET_COUNT):
lower = ts_min + i * step
if i != 0:
# compensate for overlaps
lower += 1
higher = ts_min + (i + 1) * step
if i + 1 == BUCKET_COUNT:
# compensate for remainder
higher = ts_max
key = (lower, higher)
buckets[key] = Bucket(ts_min=lower, ts_max=higher, count=0)
logger.debug("%i:%s:%s", i, key, higher - lower)
return buckets
def main():
"""Read cache file and print-out stats."""
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
logger = logging.getLogger("cache_stats")
args = parse_args()
if args.verbosity:
logger.setLevel(logging.DEBUG)
cache = rss2irc.read_cache(logger, args.cache)
logger.info(
"Number of items in cache '%s' is %d.", args.cache, len(cache.items)
)
if not cache.items:
logger.info("Nothing to do.")
sys.exit(0)
ts_min, ts_max, error_cnt = get_timestamp_minmax(logger, cache)
ts_diff = ts_max - ts_min
logger.info("Min timestamp %i", ts_min)
logger.info("Max timestamp %i", ts_max)
logger.info("Diff timestamp %i", ts_diff)
logger.info("Error count: %i", error_cnt)
if ts_diff == 0:
logger.info("%s:%s%%", (ts_min, ts_max), 100)
sys.exit(0)
if ts_diff < 0:
raise ValueError("Timestamp diff cannot be less than 0")
step = ts_diff // BUCKET_COUNT
remainder = ts_diff % BUCKET_COUNT
logger.info("Step: %s", step)
logger.info("Remainder: %s", remainder)
buckets = generate_buckets(logger, ts_min, ts_max, step)
error_cnt = calc_distribution(logger, cache, buckets)
logger.info("Error count: %i", error_cnt)
logger.info("Cache distribution:")
logger.info("---")
item_cnt = len(cache.items)
for key, value in buckets.items():
pct = round(value.count / item_cnt * 100, 1)
logger.info("%s:%s%%", key, pct)
logger.info("---")
logger.info("All done.")
sys.exit(0)
def parse_args() -> argparse.Namespace:
"""Return parsed CLI args."""
parser = argparse.ArgumentParser()
parser.add_argument(
"-v",
"--verbose",
dest="verbosity",
action="store_true",
default=False,
help="Increase logging verbosity.",
)
parser.add_argument(
"--cache",
dest="cache",
type=str,
default=None,
required=True,
help="File which contains cache.",
)
return parser.parse_args()
if __name__ == "__main__":
main()