browser: normalise all usage percentages
[sheet.git] / browser.plp
index 831962492a300d14ea3982ac67e31028d0bb42ca..19011ab21d91ae8b601244de11f0f2ce58a32ed4 100644 (file)
@@ -123,7 +123,13 @@ $canihas ||= {
                }
        } @browsers
 }; # fallback hash based on release semantics
-my $scorediv = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
+my $usagemax = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
+
+my $usagepct = 1;  # score multiplier for 0..100 result
+# normalise usage percentage to only include shown browsers
+$usagepct = 100 / featurescore({  # yes for every possible version
+       map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
+});
 
 print '<table class="mapped">';
 print '<col span="3">';  # should match first thead row
@@ -141,7 +147,7 @@ my $header = join('',
                                $_, @{ $caniuse->{agents}->{$_} }{'prefix', 'type'},
                        ),
                        join(' ',
-                               sprintf('%.1f%%', sum(values %{ $canihas->{$_} })),
+                               sprintf('%.1f%%', sum(values %{ $canihas->{$_} }) * $usagepct),
                                $name,
                        ),
                        do {
@@ -159,7 +165,7 @@ print '<td>' x 3;
 for my $browser (@browsers) {
        printf('<td title="%s"%s>%s',
                join(' ',
-                       sprintf('%.1f%%', $canihas->{$browser}->{$_}),
+                       sprintf('%.1f%%', $canihas->{$browser}->{$_} * $usagepct),
                        "version $_",
                ),
                (map {
@@ -174,7 +180,7 @@ say '<tfoot>', $header, '</tfoot>';
 
 sub featurescore {
        # relative amount of support for given feature
-       state $statspts = { y=>10, 'y x'=>10, a=>5, 'a x'=>5, j=>2, 'p j'=>2, 'p p'=>2, p=>1 };
+       state $statspts = { y=>1, 'y x'=>1, a=>.5, 'a x'=>.5, j=>.2, 'p j'=>.2, 'p p'=>.2, p=>.1 };
        my $rank = 0;
        if (my $row = shift) {
                if ($canihas) {
@@ -258,13 +264,13 @@ sub saybrowsercols {
                                join(' ',
                                        X => $CSTATS{ ref $data eq 'HASH' && $data->{$prev} || 'u' },
                                        !$usage ? ('p0') : ('p',
-                                               sprintf('p%01d', $usage / 10),
-                                               sprintf('p%02d', $usage),
+                                               sprintf('p%01d', $usage * ($usagepct - .0001) / 10),
+                                               sprintf('p%02d', $usage * ($usagepct - .0001)),
                                        ),
-                                       sprintf('pp%02d', $usage / $scorediv),
+                                       sprintf('pp%02d', $usage / $usagemax),
                                ),
                                scalar @span,
-                               sprintf('%.1f%% %s', $usage, join(', ',
+                               sprintf('%.1f%% %s', $usage * $usagepct, join(', ',
                                        map { ref $_ eq 'CODE' ? $_->($browser) : $_ }
                                        map { $DSTATS{$_} // () }
                                        map { split / /, $_ }
@@ -281,10 +287,7 @@ sub saybrowsercols {
 
 sub sayusagecol {
        my ($id) = @_;
-       state $maxscore = featurescore({  # yes for every possible version
-               map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
-       });
-       print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
+       print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) * $usagepct;
 }
 
 say '<tbody>';