Skip to content

Commit d1595c6

Browse files
committed
Start of metrics payload management
1 parent 975a4d8 commit d1595c6

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

megalinter/reporters/ApiReporter.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class ApiReporter(Reporter):
2323
payload: dict = {"linters": []}
2424
linter_payloads: list[dict] = []
2525
payloadFormatted: dict = {}
26+
api_metrics_url: str | None = None
27+
metrics_payload: str = ""
2628

2729
def __init__(self, params=None):
2830
# Deactivate Api reporter by default
@@ -49,6 +51,17 @@ def produce_report(self):
4951
self.format_payload()
5052
# Call API
5153
self.send_to_api()
54+
# Handle Metrics API if requested
55+
if (
56+
config.exists(self.master.request_id, "API_REPORTER_METRICS_URL") or
57+
config.exists(self.master.request_id, "NOTIF_API_METRICS_URL")
58+
):
59+
self.api_metrics_url = config.get_first_var_set(self.master.request_id, [
60+
"API_REPORTER_METRICS_URL","NOTIF_API_METRICS_URL"
61+
])
62+
self.build_metrics_payload()
63+
self.send_to_metrics_api()
64+
5265

5366
def build_payload(self):
5467
# Git info
@@ -77,8 +90,12 @@ def build_payload(self):
7790
"linterKey": linter.name,
7891
"data": {},
7992
}
93+
linter_payload_data = {
94+
"linterDocUrl": linter_doc_url,
95+
"jobUrl": repo_info["job_url"]
96+
}
8097
# Status
81-
linter_payload["severity"] = (
98+
linter_payload_data["severity"] = (
8299
"success"
83100
if linter.status == "success" and linter.return_code == 0
84101
else (
@@ -87,10 +104,6 @@ def build_payload(self):
87104
else "error"
88105
)
89106
)
90-
linter_payload_data = {
91-
"linterDocUrl": linter_doc_url,
92-
"jobUrl": repo_info["job_url"]
93-
}
94107
linter_payload_data["severityIcon"] = (
95108
"✅"
96109
if linter.status == "success" and linter.return_code == 0
@@ -205,3 +218,39 @@ def send_to_api(self):
205218
f"[Api Reporter] Error posting data to {self.api_url}:"
206219
f"Connection error {str(e)}"
207220
)
221+
222+
# Build something like MetricName,source=sfdx-hardis,orgIdentifier=hardis-group metric=12.7,min=0,max=70,percent=0.63
223+
def build_metrics_payload(self):
224+
metric_base_tags= (
225+
f"source={self.payload.source}," +
226+
f"orgIdentifier={self.payload.orgIdentifier},"+
227+
f"gitIdentifier={self.payload.gitIdentifier},"
228+
)
229+
all_metrics_lines = []
230+
for linter in self.master.linters:
231+
if linter.is_active is True:
232+
metric_id = linter.linter_name
233+
metric_line = (
234+
metric_id +","+
235+
metric_base_tags +
236+
f"descriptor={linter.descriptor_id},"+
237+
f"linter={linter.linter_name},"+
238+
f"linterKey={linter.name}"+
239+
" "
240+
)
241+
metric_line += f"metric={linter.total_number_errors}"
242+
# Number of files & errors
243+
if linter.cli_lint_mode != "project":
244+
metric_line += f",numberFilesFound={len(linter.files)}"
245+
# Fixed files
246+
if linter.try_fix is True:
247+
metric_line += f",numberErrorsFixed={len(linter.number_fixed)}"
248+
# Elapsed time
249+
if self.master.show_elapsed_time is True:
250+
metric_line += f",elapsedTime={round(linter.elapsed_time_s, 2)}"
251+
all_metrics_lines += [metric_line]
252+
self.metrics_payload = ",".join(all_metrics_lines)
253+
254+
255+
def send_to_metrics_api(self):
256+
pass

0 commit comments

Comments
 (0)