Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(entities): add rolling average behavior
Closes #285
- Loading branch information
Showing
6 changed files
with
237 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { Entity } from './entity.dto'; | ||
import _ from 'lodash'; | ||
import { RollingAverageOptions } from './entities.config'; | ||
import Timeout = NodeJS.Timeout; | ||
|
||
export class RollingAverageProxyHandler implements ProxyHandler<Entity> { | ||
private values: Array<TimedValue<any>> = []; | ||
private updateInterval: Timeout; | ||
|
||
constructor(private readonly config: RollingAverageOptions) {} | ||
|
||
set(target: Entity, p: PropertyKey, value: any, receiver: any): boolean { | ||
if (p == 'state') { | ||
this.values.push(new TimedValue<any>(value)); | ||
this.updateTargetState(target); | ||
|
||
if (this.updateInterval === undefined) { | ||
setInterval(() => this.updateTargetState(target), 1000); | ||
} | ||
} else { | ||
target[p] = value; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private updateTargetState(target: Entity) { | ||
this.values = this.values.filter((value, index, array) => { | ||
return ( | ||
value.msAgo <= this.config.window * 1000 || | ||
array[index + 1]?.msAgo <= this.config.window * 1000 | ||
); | ||
}); | ||
|
||
if (this.values.length > 0) { | ||
const weights = this.values.reduce<Map<any, number>>( | ||
(map, value, index, array) => { | ||
const weight = | ||
_.clamp(value.msAgo, this.config.window * 1000) - | ||
(array[index + 1]?.msAgo || 0); | ||
if (map.has(value.value)) { | ||
map.set(value.value, map.get(value.value) + weight); | ||
} else { | ||
map.set(value.value, weight); | ||
} | ||
return map; | ||
}, | ||
new Map() | ||
); | ||
|
||
if (Array.from(weights.keys()).every((x) => typeof x === 'number')) { | ||
// actual weighted average | ||
const [weightedValueSum, weightSum] = Array.from( | ||
weights.entries() | ||
).reduce<[number, number]>( | ||
(previous, value) => { | ||
return [previous[0] + value[0] * value[1], previous[1] + value[1]]; | ||
}, | ||
[0, 0] | ||
); | ||
target.state = weightedValueSum / weightSum; | ||
} else { | ||
// state with max weight wins | ||
const winner = Array.from(weights.entries()).reduce<[any, number]>( | ||
(previous, value) => { | ||
return previous[0] == undefined || value[1] > previous[1] | ||
? value | ||
: previous; | ||
}, | ||
[undefined, 0] | ||
); | ||
target.state = winner[0]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
class TimedValue<T> { | ||
value: T; | ||
readonly createdAt: Date = new Date(); | ||
|
||
constructor(value: T) { | ||
this.value = value; | ||
} | ||
|
||
get msAgo(): number { | ||
return Date.now() - this.createdAt.getTime(); | ||
} | ||
} |