@@ -23,6 +23,8 @@ class ApiReporter(Reporter):
23
23
payload : dict = {"linters" : []}
24
24
linter_payloads : list [dict ] = []
25
25
payloadFormatted : dict = {}
26
+ api_metrics_url : str | None = None
27
+ metrics_payload : str = ""
26
28
27
29
def __init__ (self , params = None ):
28
30
# Deactivate Api reporter by default
@@ -49,6 +51,17 @@ def produce_report(self):
49
51
self .format_payload ()
50
52
# Call API
51
53
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
+
52
65
53
66
def build_payload (self ):
54
67
# Git info
@@ -77,8 +90,12 @@ def build_payload(self):
77
90
"linterKey" : linter .name ,
78
91
"data" : {},
79
92
}
93
+ linter_payload_data = {
94
+ "linterDocUrl" : linter_doc_url ,
95
+ "jobUrl" : repo_info ["job_url" ]
96
+ }
80
97
# Status
81
- linter_payload ["severity" ] = (
98
+ linter_payload_data ["severity" ] = (
82
99
"success"
83
100
if linter .status == "success" and linter .return_code == 0
84
101
else (
@@ -87,10 +104,6 @@ def build_payload(self):
87
104
else "error"
88
105
)
89
106
)
90
- linter_payload_data = {
91
- "linterDocUrl" : linter_doc_url ,
92
- "jobUrl" : repo_info ["job_url" ]
93
- }
94
107
linter_payload_data ["severityIcon" ] = (
95
108
"✅"
96
109
if linter .status == "success" and linter .return_code == 0
@@ -205,3 +218,39 @@ def send_to_api(self):
205
218
f"[Api Reporter] Error posting data to { self .api_url } :"
206
219
f"Connection error { str (e )} "
207
220
)
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