-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleuth-export.py
110 lines (89 loc) · 2.94 KB
/
sleuth-export.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
import argparse
import csv
import os
import sys
from datetime import datetime, timedelta, date
from enum import Enum
from typing import Tuple, List
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
parser = argparse.ArgumentParser(
description="Extract Accelerate metrics from Sleuth by project"
)
parser.add_argument(
"file", default="report.csv", nargs="?", help="the csv file to create"
)
parser.add_argument(
"--api-key", dest="api_key", required=True, help="Your Sleuth API key"
)
parser.add_argument(
"--org-slug", dest="org_slug", required=True, help="Your Sleuth organization slug"
)
args = parser.parse_args()
class Metric(Enum):
LEAD_TIME = "MetricLeadTimeChartType"
FREQUENCY = "MetricFrequencyChartType"
FAILURE_RATE = "MetricFailureRateChartType"
MTTR = "MetricMTTRChartType"
def main():
client = get_client()
with open(args.file, "w") as f:
writer = csv.writer(f)
writer.writerow(["Project", "Metric", "Period", "Value"])
for project_slug, project_name in find_project_slugs(client):
print(f"Processing project {project_name}")
for metric in Metric:
print(f"... metric {metric.name}")
data: List[Tuple[str, float]] = get_metric_data(
client, project_slug, metric
)
for label, val in data:
writer.writerow([project_name, metric.name, label, val])
print(f"Wrote {args.file}")
def get_client():
transport = RequestsHTTPTransport(
url="https://app.sleuth.io/graphql",
headers=dict(authorization=f"apikey {args.api_key}"),
)
return Client(transport=transport)
def find_project_slugs(client) -> List[Tuple[str, str]]:
query = gql(
f"""
query {{
projects(orgSlug:"{args.org_slug}") {{
slug
name
}}
}}
"""
)
# Execute the query on the transport
result = client.execute(query)
return [(p["slug"], p["name"]) for p in result["projects"]]
def get_metric_data(client, project_slug, chart_type: Metric):
now = date.today()
past = now - timedelta(days=14)
query = gql(
f"""
query {{
metric(orgSlug:"{args.org_slug}",
projectSlug:"{project_slug}",
environmentSlug:"production",
metricType:{chart_type.name},
deploymentSlugs:[],
startDate:"{past.isoformat()}",
endDate:"{now.isoformat()}"
) {{
... on {chart_type.value} {{
labels
datapoints
}}
}}
}}
"""
)
# Execute the query on the transport
result = client.execute(query)
return zip(result["metric"]["labels"], result["metric"]["datapoints"])
if __name__ == "__main__":
main()