-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
241 lines (202 loc) · 7.88 KB
/
Copy pathmain.py
File metadata and controls
241 lines (202 loc) · 7.88 KB
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
"""
Anomaly Detection CLI
Usage:
main.py (-h | --help)
main.py [--action=<action>] [--start=<start>] [--end=<end>] [--metric=<metric>]
[--granularity=<granularity>] [--window=<window>] [--sigma=<sigma>]
[--slice=<slice>] [--file=<file>] [--run_type=<run_type>] [--user=<user>]
Options:
-h --help Show this screen
--action=<action> Action to perform (search, report)
--start=<start> Start date for the anomaly search
--end=<end> End date for the anomaly search
--metric=<metric> Metric name from metrics_hierarchy.yml
--granularity=<granularity> Granularity name (hour, day, week, month)
--window=<window> Window size – days ago from a specific date
--sigma=<sigma> Sigma value – number of std errors to trigger an alert
--slice=<slice> Slice value – slice values from metrics_hierarchy.yml
--file=<file> File to read reports from
--run_type=<run_type> Type of run ("logs" if you want just the lates metrics values)
--user=<user> User
Examples:
python main.py --start=2024-01-01 --end=2024-04-01 `: check all metrics for the date range`
python main.py --start=2024-01-01 --end=2024-04-01 --metric=new_registered_users
python main.py --metric=new_registered_users --granularity=hour --window=168 --sigma=3 --slice=web
python main.py --metric=new_registered_users --granularity=hour --window=168 --sigma=3
python main.py --metric=new_registered_users --granularity=hour
python main.py --metric=new_registered_users `: check all metric granularities`
python main.py --slice=Android `: filter all metrics from the configuration that have the slice Android`
python main.py `: run the anomaly search for all metrics from the configuration`
"""
import logging
from docopt import docopt
from datetime import datetime
from os import makedirs
from metric_watch.constants import FILTERS, MUTAGENS
from metric_watch.create_charts import run as create_charts
from metric_watch.helpers import search_anomalies, metrics, name_to_metric
from metric_watch.generate_queries import run_generation
from metric_watch.metrics_config import Metric, Granularity, Slice, DateRange
from metric_watch.reports import main as provide_reports
from metric_watch.views import create as create_views
from shutil import rmtree
logging.basicConfig(level=logging.INFO)
def log_arguments(args: dict) -> None:
"""Log parsed arguments."""
logging.info("\nParsed arguments:")
for key, value in args.items():
logging.info(f" {key}: {value}")
logging.info("")
def parse_arguments() -> dict:
"""Parse command-line arguments."""
def validate_date_range(start_date: str, end_date: str):
"""Validate the date range."""
try:
start = datetime.strptime(start_date, "%Y-%m-%d")
end = datetime.strptime(end_date, "%Y-%m-%d")
if start >= end:
raise ValueError("Start date must be before end date")
except ValueError as e:
raise ValueError(f"Invalid date range: {e}")
arguments = docopt(__doc__)
if arguments.get("--start") and arguments.get("--window"):
raise ValueError("Cannot use both 'start' and 'window' arguments")
if arguments.get("--start") and arguments.get("--end"):
validate_date_range(arguments["--start"], arguments["--end"])
result = {k.replace("--", ""): v for k, v in arguments.items()}
result["is_regular"] = (
False
if set(FILTERS + MUTAGENS).intersection(
{k for k, v in result.items() if v is not None}
)
else True
)
if result.get("run_type") == "logs":
result["is_regular"] = True
log_arguments(result)
return result
def get_metric_by_name(metric_name: str) -> Metric:
"""Retrieve a metric by its name from the configuration."""
metric = name_to_metric.get(metric_name)
if not metric:
raise ValueError(f"Metric {metric_name} not found in the configuration")
return metric
def filter_config(**kwargs) -> None:
"""Filter the configuration based on the arguments."""
global metrics
if "metric" in kwargs:
metrics = [get_metric_by_name(kwargs["metric"])]
return # Skip other filters if a metric is found
if "slice" in kwargs:
metrics = [
metric
for metric in metrics
if any(
kwargs.get("slice") in slice.values
for granularity in metric.granularities
for slice in granularity.slices
)
]
def create_granularities(
for_metric: Metric,
start: str,
end: str,
granularity: str,
window: int,
sigma: int,
slice: str,
**kwargs,
) -> list:
"""Create a list of granularities for a metric."""
if not granularity:
granularity = "day"
if not sigma:
sigma = 3
slices = (
[Slice(name=slice, values=[slice])]
if slice
else [g.slices for g in for_metric.granularities if g.name == granularity][0]
)
if start is not None and end is not None:
try:
delta = datetime.strptime(end, "%Y-%m-%d") - datetime.strptime(
start, "%Y-%m-%d"
)
except ValueError as e:
raise ValueError(f"Invalid date range: {e}")
else:
if granularity == "hour":
window = delta.days * 24
elif granularity == "day":
window = delta.days
elif granularity == "week":
window = delta.days // 7
elif granularity == "month":
window = delta.days // 30
return [
Granularity(
name=granularity,
window_size=window,
sigma_n=sigma,
slices=slices,
)
]
def mutate_config(**kwargs) -> None:
"""Mutate the configuration based on the arguments."""
global metrics
mutated_metrics = []
for metric in metrics:
granularities = (
create_granularities(for_metric=metric, **kwargs)
if any([kwargs.get("granularity"), kwargs.get("start")])
else None
)
date_range = DateRange(kwargs["start"], kwargs["end"])
mutated_metrics.append(
Metric(
name=metric.name,
description=metric.description,
formula=metric.formula,
depends_on=metric.depends_on,
granularities=granularities if granularities else metric.granularities,
date_range=(
date_range
if any([kwargs["start"], kwargs["end"]])
else metric.date_range
),
)
)
metrics = mutated_metrics
def prepare_config(**kwargs) -> None:
"""Prepare tool run configurations."""
if any(kwargs.get(m) for m in FILTERS):
filter_config(**kwargs)
if any(kwargs.get(m) for m in MUTAGENS):
mutate_config(**kwargs)
def clean_artefacts() -> None:
"""Remove previous run artefacts."""
rmtree("artefacts", ignore_errors=True)
makedirs("artefacts")
def create_entities() -> None:
"""Create database and visualization entities."""
global metrics
clean_artefacts()
run_generation(metrics)
create_views()
create_charts()
def main() -> None:
"""Run the tool based on the command-line arguments."""
kwargs = parse_arguments()
prepare_config(**kwargs)
if "action" in kwargs:
if kwargs["action"] == "search":
search_anomalies(metrics, **kwargs)
return
if kwargs["action"] == "report":
provide_reports(**kwargs)
return
create_entities()
search_anomalies(metrics, **kwargs)
provide_reports(**kwargs)
if __name__ == "__main__":
main()