Skip to content

Commit

Permalink
Fix group counting logic and update display #336
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl authored and brendanheywood committed Apr 22, 2024
1 parent 2e4dc11 commit 6735abf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ public static function full_request(\stdClass $profile): string {
* Row is of the form: 2^(k-1) - 2^k : 2^(v-1).
*
* @param int $durationexponent The exponent (k) of the high end of the duration range.
* @param int $count The fuzzy count (v), or zero if no value.
* @param int|null $count The fuzzy count (v), or null if no value.
* @return array
*/
private static function make_histogram_record(int $durationexponent, int $count): array {
private static function make_histogram_record(int $durationexponent, ?int $count = null): array {
$high = pow(2, $durationexponent);
$low = ($high === 1) ? 0 : pow(2, $durationexponent - 1);
$val = $count ? pow(2, $count - 1) : 0;
$val = isset($count) ? pow(2, $count) : 0;
return [
'low' => $low,
'high' => $high,
Expand All @@ -219,7 +219,7 @@ public static function make_histogram(\stdClass $record): array {
foreach ($counts as $storeddurationexponent => $fuzzycount) {
// Fill in lines that do not have stored values.
while ($durationexponent < $storeddurationexponent) {
$histogramrecords[] = self::make_histogram_record($durationexponent, 0);
$histogramrecords[] = self::make_histogram_record($durationexponent);
++$durationexponent;
}
$histogramrecords[] = self::make_histogram_record($storeddurationexponent, $fuzzycount);
Expand Down
9 changes: 7 additions & 2 deletions classes/page_group.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ public static function record_fuzzy_counts(profile $profile, ?int $month = null)

$existing = $pagegroup->to_record();

// Check if the page group existed before this call, can be determined by fuzzydurationcounts.
$fuzzydurationcounts = $pagegroup->get('fuzzydurationcounts');
$pagegroupexisted = !empty($fuzzydurationcounts);

// Fuzzy increment the count.
$fuzzycount = manager::approximate_increment($pagegroup->get('fuzzycount'));
$fuzzycount = $pagegroupexisted ? manager::approximate_increment($pagegroup->get('fuzzycount')) : 0;
$pagegroup->set('fuzzycount', $fuzzycount);

// Fuzzy increment count for the duration slice.
Expand All @@ -168,7 +172,8 @@ public static function record_fuzzy_counts(profile $profile, ?int $month = null)
if ($exp < 0) {
$exp = 0;
}
$fuzzydurationcounts[$exp] = manager::approximate_increment($fuzzydurationcounts[$exp] ?? 0);
$fuzzydurationexists = $pagegroupexisted && isset($fuzzydurationcounts[$exp]);
$fuzzydurationcounts[$exp] = $fuzzydurationexists ? manager::approximate_increment($fuzzydurationcounts[$exp]) : 0;
$pagegroup->set('fuzzydurationcounts', $fuzzydurationcounts);

// Add the duration to the fuzzy sum, treating each second as an event for counting.
Expand Down
2 changes: 1 addition & 1 deletion classes/page_group_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function col_month(\stdClass $record): string {
* @return string
*/
public function col_fuzzycount(\stdClass $record): string {
return pow(2, $record->fuzzycount - 1) . ' - ' . pow(2, $record->fuzzycount);
return pow(2, $record->fuzzycount);
}

/**
Expand Down

0 comments on commit 6735abf

Please sign in to comment.