Skip to content

Commit 90ca554

Browse files
committed
Add the possibility to get only top-level metrics in the web api
1 parent bf13438 commit 90ca554

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/web/metrics.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct WebMetricsPayload {
1010
pub id: String,
1111
pub file_name: String,
1212
pub code: String,
13+
pub unit: bool,
1314
}
1415

1516
#[derive(Debug, Serialize)]
@@ -21,13 +22,15 @@ pub struct WebMetricsResponse<'a> {
2122
#[derive(Debug, Deserialize)]
2223
pub struct WebMetricsInfo {
2324
pub file_name: String,
25+
pub unit: Option<String>,
2426
}
2527

2628
pub struct WebMetricsCallback {}
2729

2830
pub struct WebMetricsCfg {
2931
pub id: String,
3032
pub path: PathBuf,
33+
pub unit: bool,
3134
}
3235

3336
impl Callback for WebMetricsCallback {
@@ -36,6 +39,17 @@ impl Callback for WebMetricsCallback {
3639

3740
fn call<T: TSParserTrait>(cfg: Self::Cfg, parser: &T) -> Self::Res {
3841
let spaces = metrics(parser, &cfg.path);
42+
let spaces = if cfg.unit {
43+
if let Some(mut spaces) = spaces {
44+
spaces.spaces.clear();
45+
Some(spaces)
46+
} else {
47+
None
48+
}
49+
} else {
50+
spaces
51+
};
52+
3953
serde_json::to_value(WebMetricsResponse { id: cfg.id, spaces }).unwrap()
4054
}
4155
}

src/web/server.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ fn metrics_json(item: web::Json<WebMetricsPayload>, _req: HttpRequest) -> HttpRe
9393
let cfg = WebMetricsCfg {
9494
id: payload.id,
9595
path,
96+
unit: payload.unit,
9697
};
9798
HttpResponse::Ok().json(action::<WebMetricsCallback>(
9899
&language,
@@ -116,6 +117,10 @@ fn metrics_plain(code: Bytes, info: Query<WebMetricsInfo>) -> HttpResponse {
116117
let cfg = WebMetricsCfg {
117118
id: "".to_string(),
118119
path,
120+
unit: info
121+
.unit
122+
.as_ref()
123+
.map_or(false, |s| s == "1" || s == "true"),
119124
};
120125
HttpResponse::Ok().json(action::<WebMetricsCallback>(
121126
&language,
@@ -519,10 +524,11 @@ mod tests {
519524
);
520525
let req = test::TestRequest::post()
521526
.uri("/metrics")
522-
.set_json(&WebCommentPayload {
527+
.set_json(&WebMetricsPayload {
523528
id: "1234".to_string(),
524529
file_name: "test.py".to_string(),
525530
code: "def foo():\n pass\n".to_string(),
531+
unit: false,
526532
})
527533
.to_request();
528534

@@ -569,6 +575,47 @@ mod tests {
569575
assert_eq!(res, expected);
570576
}
571577

578+
#[test]
579+
fn test_web_metrics_json_unit() {
580+
let mut app = test::init_service(
581+
App::new().service(web::resource("/metrics").route(web::post().to(metrics_json))),
582+
);
583+
let req = test::TestRequest::post()
584+
.uri("/metrics")
585+
.set_json(&WebMetricsPayload {
586+
id: "1234".to_string(),
587+
file_name: "test.py".to_string(),
588+
code: "def foo():\n pass\n".to_string(),
589+
unit: true,
590+
})
591+
.to_request();
592+
593+
let res: Value = test::read_response_json(&mut app, req);
594+
let expected = json!({
595+
"id": "1234",
596+
"spaces": {"kind": "unit",
597+
"line": 1,
598+
"metrics": {"cyclomatic": 1.0,
599+
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
600+
"difficulty": 1.0,
601+
"effort": 4.754_887_502_163_468,
602+
"length": 3.0,
603+
"level": 1.0,
604+
"operands": 1.0,
605+
"operators": 2.0,
606+
"size": 3.0,
607+
"time": 0.264_160_416_786_859_36,
608+
"unique_operands": 1.0,
609+
"unique_operators": 2.0,
610+
"volume": 4.754_887_502_163_468},
611+
"loc": {"lloc": 2.0, "sloc": 3.0}},
612+
"name": "test.py",
613+
"spaces": []}
614+
});
615+
616+
assert_eq!(res, expected);
617+
}
618+
572619
#[test]
573620
fn test_web_metrics_plain() {
574621
let mut app = test::init_service(

0 commit comments

Comments
 (0)