| 1 | package Test::CPAN::Health::Report; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use autodie qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | use Carp qw(croak carp); | ||
| 8 | use List::Util qw(sum0); | ||
| 9 | use Readonly; | ||
| 10 | use Params::Validate::Strict qw(validate_strict); | ||
| 11 | use Scalar::Util qw(blessed); | ||
| 12 | |||
| 13 | our $VERSION = '0.1.0'; | ||
| 14 | |||
| 15 | # Hard ceilings: a critical check failure caps the overall score to prevent a | ||
| 16 | # dist with known CVEs or widespread test failures from scoring 90+. | ||
| 17 | Readonly::Hash my %HARD_CAPS => ( | ||
| 18 | security_advisories => 60, | ||
| 19 | cpan_testers => 75, | ||
| 20 | ); | ||
| 21 | |||
| 22 | Readonly::Scalar my $MAX_SCORE => 100; | ||
| 23 | Readonly::Scalar my $MIN_SCORE => 0; | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | Test::CPAN::Health::Report - Aggregated results and overall score for a distribution health run | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use Test::CPAN::Health::Report; | ||
| 32 | |||
| 33 | my $report = Test::CPAN::Health::Report->new(checks => \@check_objects); | ||
| 34 | |||
| 35 | $report->add_result($result); | ||
| 36 | |||
| 37 | printf "Score: %d/100\n", $report->overall_score; | ||
| 38 | |||
| 39 | for my $result (@{ $report->results }) { | ||
| 40 | printf " %s: %s\n", $result->check_id, $result->status; | ||
| 41 | } | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | Holds all L<Test::CPAN::Health::Result> objects produced by a run and | ||
| 46 | computes the weighted overall score. | ||
| 47 | |||
| 48 | The scoring formula is a weighted mean of per-check scores: | ||
| 49 | |||
| 50 | score = sum(result.score * check.weight) | ||
| 51 | --------------------------------- | ||
| 52 | sum(check.weight) | ||
| 53 | |||
| 54 | for all results where score is defined and status is not 'skip' | ||
| 55 | |||
| 56 | Hard caps are applied after the weighted mean: if the C<SecurityAdvisories> | ||
| 57 | check fails, the overall score is capped at 60; if C<CPANTesters> fails, | ||
| 58 | it is capped at 75. This prevents a distribution with known CVEs or a | ||
| 59 | poor CI pass rate from achieving a misleadingly high headline score. | ||
| 60 | |||
| 61 | =head1 LIMITATIONS | ||
| 62 | |||
| 63 | =over 4 | ||
| 64 | |||
| 65 | =item * Checks added after C<overall_score> has been called are included on | ||
| 66 | the next call (the score is computed lazily and cached until invalidated by | ||
| 67 | C<add_result>). | ||
| 68 | |||
| 69 | =back | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub new { | ||
| 74 | my ($class, %args) = @_; | ||
| 75 | |||
| 76 | %args = %{ validate_strict( | ||
| 77 | schema => { | ||
| 78 | checks => { type => 'arrayref', optional => 1, default => [] }, | ||
| 79 | }, | ||
| 80 | input => \%args, | ||
| 81 | ) }; | ||
| 82 | |||
| 83 | # Build a weight map keyed by check id for O(1) lookup during scoring. | ||
| 84 | my %weight_for; | ||
| 85 | for my $check (@{$args{checks}}) { | ||
| 86 | $weight_for{ $check->id } = $check->weight; | ||
| 87 | } | ||
| 88 | |||
| 89 | my $self = bless { | ||
| 90 | _results => [], | ||
| 91 | _weight_for => \%weight_for, | ||
| 92 | _cached_score => undef, | ||
| 93 | _score_dirty => 1, | ||
| 94 | }, $class; | ||
| 95 | |||
| 96 | return $self; | ||
| 97 | } | ||
| 98 | |||
| 99 | =head2 add_result | ||
| 100 | |||
| 101 | =head3 PURPOSE | ||
| 102 | |||
| 103 | Append a single Result to the report and invalidate the cached score. | ||
| 104 | |||
| 105 | =head3 API SPECIFICATION | ||
| 106 | |||
| 107 | =head4 INPUT | ||
| 108 | |||
| 109 | result Test::CPAN::Health::Result required | ||
| 110 | |||
| 111 | =head4 OUTPUT | ||
| 112 | |||
| 113 | Returns C<$self> to allow chaining. | ||
| 114 | |||
| 115 | =head3 MESSAGES | ||
| 116 | |||
| 117 | Code | Severity | Message | Resolution | ||
| 118 | ------+----------+------------------------------------+--------------------- | ||
| 119 | RPT01 | FATAL | result must be a Result object | Pass a Result instance | ||
| 120 | |||
| 121 | =head3 FORMAL SPECIFICATION | ||
| 122 | |||
| 123 | -- Z schema (placeholder) -- | ||
| 124 | AddResultOp | ||
| 125 | Report | ||
| 126 | Report' | ||
| 127 | result? : Result | ||
| 128 | ------------------------------------------------------- | ||
| 129 | Report'.results = Report.results ++ [result?] | ||
| 130 | Report'.score_dirty = true | ||
| 131 | |||
| 132 | =head3 SIDE EFFECTS | ||
| 133 | |||
| 134 | Invalidates the cached overall score. | ||
| 135 | |||
| 136 | =head3 USAGE EXAMPLE | ||
| 137 | |||
| 138 | $report->add_result($result)->add_result($other_result); | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub add_result { | ||
| 143 | my ($self, $result) = @_; | ||
| 144 | |||
| 145 | croak 'result must be a Test::CPAN::Health::Result' | ||
| 146 | unless blessed($result) && $result->isa('Test::CPAN::Health::Result'); | ||
| 147 | |||
| 148 | push @{$self->{_results}}, $result; | ||
| 149 | $self->{_score_dirty} = 1; | ||
| 150 | $self->{_cached_score} = undef; | ||
| 151 | |||
| 152 | return $self; | ||
| 153 | } | ||
| 154 | |||
| 155 | =head2 overall_score | ||
| 156 | |||
| 157 | =head3 PURPOSE | ||
| 158 | |||
| 159 | Compute and return the weighted overall health score in the range 0..100. | ||
| 160 | The result is cached until C<add_result> invalidates it. | ||
| 161 | |||
| 162 | Hard caps are applied last: a failing SecurityAdvisories caps the score at | ||
| 163 | 60; a failing CPANTesters caps it at 75. Both caps may apply simultaneously | ||
| 164 | (the lower cap wins). | ||
| 165 | |||
| 166 | =head3 API SPECIFICATION | ||
| 167 | |||
| 168 | =head4 INPUT | ||
| 169 | |||
| 170 | None. | ||
| 171 | |||
| 172 | =head4 OUTPUT | ||
| 173 | |||
| 174 | Integer in the range 0..100. | ||
| 175 | |||
| 176 | =head3 MESSAGES | ||
| 177 | |||
| 178 | Code | Severity | Message | Resolution | ||
| 179 | ------+----------+------------------------------------+--------------------- | ||
| 180 | | | | | ||
| 181 | |||
| 182 | =head3 FORMAL SPECIFICATION | ||
| 183 | |||
| 184 | -- Z schema (placeholder) -- | ||
| 185 | OverallScoreOp | ||
| 186 | results : seq Result | ||
| 187 | weights : check_id --> N1 | ||
| 188 | score : 0..100 | ||
| 189 | ------------------------------------------------------- | ||
| 190 | let scorable == {r : results | r.score /= undefined /\ r.status /= skip} | ||
| 191 | score = floor(sum{r : scorable @ r.score * weights(r.check_id)} | ||
| 192 | / sum{r : scorable @ weights(r.check_id)}) | ||
| 193 | security_advisories_fail => score <= 60 | ||
| 194 | cpan_testers_fail => score <= 75 | ||
| 195 | |||
| 196 | =head3 SIDE EFFECTS | ||
| 197 | |||
| 198 | None. | ||
| 199 | |||
| 200 | =head3 USAGE EXAMPLE | ||
| 201 | |||
| 202 | printf "%d/100\n", $report->overall_score; | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | sub overall_score { | ||
| 207 | my ($self) = @_; | ||
| 208 | |||
| 209 | return $self->{_cached_score} unless $self->{_score_dirty}; | ||
| 210 | |||
| 211 | my ($weighted_sum, $total_weight) = (0, 0); | ||
| 212 | my $cap = $MAX_SCORE; | ||
| 213 | |||
| 214 | for my $result (@{$self->{_results}}) { | ||
| 215 | next if $result->is_skip; | ||
| 216 | |||
| 217 | # Apply hard cap rules before folding scores in. | ||
| 218 | if (exists $HARD_CAPS{ $result->check_id } && $result->is_fail) { | ||
| 219 | my $ceiling = $HARD_CAPS{ $result->check_id }; | ||
| 220 | $cap = $ceiling if $ceiling < $cap; | ||
| 221 | } | ||
| 222 | |||
| 223 | next unless defined $result->score; | ||
| 224 | |||
| 225 | my $weight = $self->{_weight_for}{ $result->check_id } // 1; | ||
| 226 | $weighted_sum += $result->score * $weight; | ||
| 227 | $total_weight += $weight; | ||
| 228 | } | ||
| 229 | |||
| 230 | my $raw = $total_weight > 0 | ||
| 231 | ? int($weighted_sum / $total_weight + 0.5) | ||
| 232 | : 0; | ||
| 233 | |||
| 234 | my $score = $raw < $cap ? $raw : $cap; | ||
| 235 | $score = $MIN_SCORE if $score < $MIN_SCORE; | ||
| 236 | |||
| 237 | $self->{_cached_score} = $score; | ||
| 238 | $self->{_score_dirty} = 0; | ||
| 239 | |||
| 240 | return $score; | ||
| 241 | } | ||
| 242 | |||
| 243 | =head2 results | ||
| 244 | |||
| 245 | =head3 PURPOSE | ||
| 246 | |||
| 247 | Returns all Result objects in insertion order. | ||
| 248 | |||
| 249 | =head3 API SPECIFICATION | ||
| 250 | |||
| 251 | =head4 INPUT | ||
| 252 | |||
| 253 | None. | ||
| 254 | |||
| 255 | =head4 OUTPUT | ||
| 256 | |||
| 257 | Arrayref of L<Test::CPAN::Health::Result> objects. | ||
| 258 | |||
| 259 | =head3 MESSAGES | ||
| 260 | |||
| 261 | Code | Severity | Message | Resolution | ||
| 262 | ------+----------+------------------------------------+--------------------- | ||
| 263 | | | | | ||
| 264 | |||
| 265 | =head3 FORMAL SPECIFICATION | ||
| 266 | |||
| 267 | -- Z schema (placeholder) -- | ||
| 268 | results : seq Result | ||
| 269 | |||
| 270 | =head3 SIDE EFFECTS | ||
| 271 | |||
| 272 | None. | ||
| 273 | |||
| 274 | =head3 USAGE EXAMPLE | ||
| 275 | |||
| 276 | for my $r (@{ $report->results }) { ... } | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub results { my ($self) = @_; return $self->{_results} } | ||
| 281 | |||
| 282 | =head2 by_status | ||
| 283 | |||
| 284 | =head3 PURPOSE | ||
| 285 | |||
| 286 | Group results by their status string. | ||
| 287 | |||
| 288 | =head3 API SPECIFICATION | ||
| 289 | |||
| 290 | =head4 INPUT | ||
| 291 | |||
| 292 | None. | ||
| 293 | |||
| 294 | =head4 OUTPUT | ||
| 295 | |||
| 296 | Hashref mapping status strings to arrayrefs of Result objects. | ||
| 297 | |||
| 298 | =head3 MESSAGES | ||
| 299 | |||
| 300 | Code | Severity | Message | Resolution | ||
| 301 | ------+----------+------------------------------------+--------------------- | ||
| 302 | | | | | ||
| 303 | |||
| 304 | =head3 FORMAL SPECIFICATION | ||
| 305 | |||
| 306 | -- Z schema (placeholder) -- | ||
| 307 | by_status : status --> seq Result | ||
| 308 | ------------------------------------------------------- | ||
| 309 | forall s : dom(by_status) @ forall r : by_status(s) @ r.status = s | ||
| 310 | |||
| 311 | =head3 SIDE EFFECTS | ||
| 312 | |||
| 313 | None. | ||
| 314 | |||
| 315 | =head3 USAGE EXAMPLE | ||
| 316 | |||
| 317 | my $failures = $report->by_status->{fail}; | ||
| 318 | |||
| 319 | =cut | ||
| 320 | |||
| 321 | sub by_status { | ||
| 322 | my ($self) = @_; | ||
| 323 | |||
| 324 | my %grouped; | ||
| 325 | for my $result (@{$self->{_results}}) { | ||
| 326 | push @{ $grouped{ $result->status } }, $result; | ||
| 327 | } | ||
| 328 | |||
| 329 | return \%grouped; | ||
| 330 | } | ||
| 331 | |||
| 332 | =head2 by_category | ||
| 333 | |||
| 334 | =head3 PURPOSE | ||
| 335 | |||
| 336 | Group results by the category of the originating check. Requires that | ||
| 337 | each Result's C<data> hashref carries a C<category> key (populated by | ||
| 338 | the Runner when it adds results). | ||
| 339 | |||
| 340 | =head3 API SPECIFICATION | ||
| 341 | |||
| 342 | =head4 INPUT | ||
| 343 | |||
| 344 | None. | ||
| 345 | |||
| 346 | =head4 OUTPUT | ||
| 347 | |||
| 348 | Hashref mapping category strings to arrayrefs of Result objects. | ||
| 349 | |||
| 350 | =head3 MESSAGES | ||
| 351 | |||
| 352 | Code | Severity | Message | Resolution | ||
| 353 | ------+----------+------------------------------------+--------------------- | ||
| 354 | | | | | ||
| 355 | |||
| 356 | =head3 FORMAL SPECIFICATION | ||
| 357 | |||
| 358 | -- Z schema (placeholder) -- | ||
| 359 | by_category : category --> seq Result | ||
| 360 | |||
| 361 | =head3 SIDE EFFECTS | ||
| 362 | |||
| 363 | None. | ||
| 364 | |||
| 365 | =head3 USAGE EXAMPLE | ||
| 366 | |||
| 367 | my $security_results = $report->by_category->{security}; | ||
| 368 | |||
| 369 | =cut | ||
| 370 | |||
| 371 | sub by_category { | ||
| 372 | my ($self) = @_; | ||
| 373 | |||
| 374 | my %grouped; | ||
| 375 | for my $result (@{$self->{_results}}) { | ||
| 376 | my $cat = $result->data->{category} // 'unknown'; | ||
| 377 | push @{ $grouped{$cat} }, $result; | ||
| 378 | } | ||
| 379 | |||
| 380 | return \%grouped; | ||
| 381 | } | ||
| 382 | |||
| 383 | # Convenience counts used by reporters and the CLI exit-code logic. | ||
| 384 | |||
| 385 | sub pass_count { my ($self) = @_; return scalar grep { $_->is_pass } @{$self->{_results}} } | ||
| 386 | sub warn_count { my ($self) = @_; return scalar grep { $_->is_warn } @{$self->{_results}} } | ||
| 387 | sub fail_count { my ($self) = @_; return scalar grep { $_->is_fail } @{$self->{_results}} } | ||
| 388 | sub skip_count { my ($self) = @_; return scalar grep { $_->is_skip } @{$self->{_results}} } | ||
| 389 | sub error_count { my ($self) = @_; return scalar grep { $_->is_error } @{$self->{_results}} } | ||
| 390 | |||
| 391 | =head2 as_hash | ||
| 392 | |||
| 393 | =head3 PURPOSE | ||
| 394 | |||
| 395 | Serialise the entire report to a plain hashref (for JSON reporter). | ||
| 396 | |||
| 397 | =head3 API SPECIFICATION | ||
| 398 | |||
| 399 | =head4 INPUT | ||
| 400 | |||
| 401 | None. | ||
| 402 | |||
| 403 | =head4 OUTPUT | ||
| 404 | |||
| 405 | Hashref with keys: overall_score, pass, warn, fail, skip, error, results. | ||
| 406 | |||
| 407 | =head3 MESSAGES | ||
| 408 | |||
| 409 | Code | Severity | Message | Resolution | ||
| 410 | ------+----------+------------------------------------+--------------------- | ||
| 411 | | | | | ||
| 412 | |||
| 413 | =head3 FORMAL SPECIFICATION | ||
| 414 | |||
| 415 | -- Z schema (placeholder) -- | ||
| 416 | AsHashOp | ||
| 417 | report : Report | ||
| 418 | output : Hashref | ||
| 419 | |||
| 420 | =head3 SIDE EFFECTS | ||
| 421 | |||
| 422 | None. | ||
| 423 | |||
| 424 | =head3 USAGE EXAMPLE | ||
| 425 | |||
| 426 | my $href = $report->as_hash; | ||
| 427 | |||
| 428 | =cut | ||
| 429 | |||
| 430 | sub as_hash { | ||
| 431 | my ($self) = @_; | ||
| 432 | |||
| 433 | return { | ||
| 434 | overall_score => $self->overall_score, | ||
| 435 | pass => $self->pass_count, | ||
| 436 | warn => $self->warn_count, | ||
| 437 | fail => $self->fail_count, | ||
| 438 | skip => $self->skip_count, | ||
| 439 | error => $self->error_count, | ||
| 440 | results => [ map { $_->as_hash } @{$self->{_results}} ], | ||
| 441 | }; | ||
| 442 | } | ||
| 443 | |||
| 444 | =head1 AUTHOR | ||
| 445 | |||
| 446 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 447 | |||
| 448 | =head1 LICENSE AND COPYRIGHT | ||
| 449 | |||
| 450 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 451 | |||
| 452 | This program is free software; you can redistribute it and/or modify it | ||
| 453 | under the terms of the GNU General Public License as published by the Free | ||
| 454 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 455 | any later version. | ||
| 456 | |||
| 457 | =cut | ||
| 458 | |||
| 459 | 1; | ||