Skip to content

Commit

Permalink
Try an exponential decay top 10 table for a smoother top N experience.
Browse files Browse the repository at this point in the history
  • Loading branch information
thebracket committed Jan 31, 2025
1 parent 07fdc48 commit 6af32ae
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export class Top10Downloaders extends BaseDashlet {
this.timeCache.tick();

let items = this.timeCache.get();
items.sort((a, b) => {
return b.bits_per_second.down - a.bits_per_second.down;
});
// Limit to 10 entries
items = items.slice(0, 10);
let t = TopNTableFromMsgData(items);

// Display it
Expand Down Expand Up @@ -107,4 +102,4 @@ export class Top10Downloaders extends BaseDashlet {
target.appendChild(table);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export class Worst10Downloaders extends BaseDashlet {
this.timeCache.tick();

let items = this.timeCache.get();
items.sort((a, b) => {
return a.bits_per_second.down - b.bits_per_second.down;
});
// Limit to 10 entries
items = items.slice(0, 10);
let t = TopNTableFromMsgData(items);

// Display it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export class Worst10Retransmits extends BaseDashlet {
this.timeCache.tick();

let items = this.timeCache.get();
items.sort((a, b) => {
return a.tcp_retransmits.down - b.tcp_retransmits.down;
});
// Limit to 10 entries
items = items.slice(0, 10);
let t = TopNTableFromMsgData(items);

// Display it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
// Keeps items for up to 10 seconds. Items are tagged by a key, which will
// be used to avoid duplicates.
const alpha = 0.3;

export class TimedCache {
constructor(maxAge = 10) {
this.cache = new Map();
this.maxAge = maxAge;
constructor(maxAgeSeconds) {
this.entries = new Map();
this.maxAge = maxAgeSeconds;
}

addOrUpdate(key, value) {
this.cache.set(key, { value: value, age: 0 });
addOrUpdate(key, value, score) {
if (!this.entries.has(key)) {
// New entry
this.entries.set(key, { value: value, score: score, lastSeen: Date.now() });
} else {
// Update existing entry
let entry = this.entries.get(key);
entry.value = value;
entry.score = alpha * score + (1 - alpha) * entry.score;
entry.lastSeen = Date.now();
}
}

tick() {
let toRemove = [];
this.cache.forEach((val, key) => {
val.age += 1;
if (val.age > this.maxAge) {
toRemove.push(key);
// Remove older than maxAge seconds
let now = Date.now();
this.entries.forEach((v, k) => {
if (now - v.lastSeen > this.maxAge * 1000) {
this.entries.delete(k);
}
});
toRemove.forEach((key) => {
this.cache.delete(key)
});
}

get() {
let result = [];
this.cache.forEach((val) => {
result.push(val.value);
})
return result;
// Sort by score, descending
let entries = Array.from(this.entries.values());
entries.sort((a, b) => {
return b.score - a.score;
});

// Map to only have the value
entries = entries.map((v) => {
return v.value;
});

// Return the top 10
return entries.slice(0, 10);
}
}
}

0 comments on commit 6af32ae

Please sign in to comment.