Skip to content

Commit df1024a

Browse files
committed
fix: make sampleBufferBytes generic to handle Buffer<Record>
dailyTempRecords and weeklyTempRecords are Buffer<Record>, not Buffer<DownloadsSnapshot>. Adding a serialize parameter makes the helper work for both element types. Made-with: Cursor
1 parent cbe8bc3 commit df1024a

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

backend/main/DownloadLog.mo

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ module {
467467
sum * total / sampled;
468468
};
469469

470-
func sampleBufferBytes(buf : Buffer.Buffer<DownloadsSnapshot>) : Nat {
470+
func sampleBufferBytes<T>(buf : Buffer.Buffer<T>, serialize : T -> Blob) : Nat {
471471
let total = buf.size();
472472
if (total == 0) return 0;
473473
let stride = if (total <= SAMPLE_SIZE) 1 else total / SAMPLE_SIZE;
@@ -476,7 +476,7 @@ module {
476476
var sampled = 0;
477477
for (v in buf.vals()) {
478478
if (i % stride == 0) {
479-
sum += (to_candid (v) : Blob).size();
479+
sum += serialize(v).size();
480480
sampled += 1;
481481
};
482482
i += 1;
@@ -495,11 +495,11 @@ module {
495495
};
496496
dailySnapshots = {
497497
count = dailySnapshots.size();
498-
bytes = sampleBufferBytes(dailySnapshots);
498+
bytes = sampleBufferBytes<DownloadsSnapshot>(dailySnapshots, func(v) = to_candid (v));
499499
};
500500
weeklySnapshots = {
501501
count = weeklySnapshots.size();
502-
bytes = sampleBufferBytes(weeklySnapshots);
502+
bytes = sampleBufferBytes<DownloadsSnapshot>(weeklySnapshots, func(v) = to_candid (v));
503503
};
504504
dailySnapshotsByPackageName = {
505505
count = dailySnapshotsByPackageName.size();
@@ -519,11 +519,11 @@ module {
519519
};
520520
dailyTempRecords = {
521521
count = dailyTempRecords.size();
522-
bytes = sampleBufferBytes(dailyTempRecords);
522+
bytes = sampleBufferBytes<Record>(dailyTempRecords, func(v) = to_candid (v));
523523
};
524524
weeklyTempRecords = {
525525
count = weeklyTempRecords.size();
526-
bytes = sampleBufferBytes(weeklyTempRecords);
526+
bytes = sampleBufferBytes<Record>(weeklyTempRecords, func(v) = to_candid (v));
527527
};
528528
};
529529
};

0 commit comments

Comments
 (0)