mirror of
https://github.com/pezkuwichain/pezkuwi-telemetry.git
synced 2026-05-06 05:28:02 +00:00
Restructure the js app (#243)
* prettier * linter * add prettier, and format the code * remove common, merge it with frontend * refactor the app * better lint and code fix * travis for the frontend app * travis build script Signed-off-by: Daniel Maricic <daniel@woss.io> * lint and build * update the README.md Signed-off-by: Daniel Maricic <daniel@woss.io> * change the commands to reflect refactor Signed-off-by: Daniel Maricic <daniel@woss.io> * prettier and tslint are friends Signed-off-by: Daniel Maricic <daniel@woss.io> * code that wasn't linted properly before Signed-off-by: Daniel Maricic <daniel@woss.io> * prettier rc got deleted * workgin on making the travis pass Signed-off-by: Daniel Maricic <daniel@woss.io> * travis build please? Signed-off-by: Daniel Maricic <daniel@woss.io> * update readme.md Signed-off-by: Daniel Maricic <daniel@woss.io> * dockerfile deleted from fronted - out of scope Signed-off-by: Daniel Maricic <daniel@woss.io> * remove Signed-off-by: Daniel Maricic <daniel@woss.io> * tsconfig Signed-off-by: Daniel Maricic <daniel@woss.io> * found the reason why EOL wasn't happening Signed-off-by: Daniel Maricic <daniel@woss.io> * type for the event in the ConnectionInput as suggested * strictnullCheck to true * noImplicitAny * noUnusedParams * AfgHandling * update * fix Location.tsx * Few minor fixes * remove connection input and revert to original * esnext fixes the imports for icons and non default `* as ` * update to the tsconfig.test.json don't use commonjs please * fixed wrong comment for TIMEOUT_BASE * return totem.svg and type decraration of maybe Signed-off-by: Daniel Maricic <daniel@woss.io> Co-authored-by: Will <w.kopp@kigroup.de>
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { Milliseconds, Timestamp } from './types';
|
||||
|
||||
/**
|
||||
* PhantomData akin to Rust, because sometimes you need to be smarter than
|
||||
* the compiler.
|
||||
*/
|
||||
export abstract class PhantomData<P> {
|
||||
public __PHANTOM__: P;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opaque type, similar to `opaque type` in Flow, or new types in Rust/C.
|
||||
* These should be produced only by manually casting `t as Opaque<T, P>`.
|
||||
*
|
||||
* `P` can be anything as it's never actually used. Using strings is okay:
|
||||
*
|
||||
* ```
|
||||
* type MyType = Opaque<number, 'MyType'>;
|
||||
* ```
|
||||
*/
|
||||
export type Opaque<T, P> = T & PhantomData<P>;
|
||||
|
||||
/**
|
||||
* Just a readable shorthand for null-ish-able types, akin to `T?` in Flow.
|
||||
*/
|
||||
export type Maybe<T> = T | null | undefined;
|
||||
|
||||
/**
|
||||
* Asynchronous sleep
|
||||
*/
|
||||
export function sleep(time: Milliseconds): Promise<void> {
|
||||
return new Promise<void>((resolve, _reject) => {
|
||||
setTimeout(() => resolve(), time);
|
||||
});
|
||||
}
|
||||
|
||||
export const timestamp = Date.now as () => Timestamp;
|
||||
|
||||
export function noop() {}
|
||||
|
||||
/**
|
||||
* Keep track of last N numbers pushed onto internal stack.
|
||||
* Provides means to get an average of said numbers.
|
||||
*/
|
||||
export class NumStats<T extends number> {
|
||||
private readonly stack: Array<T>;
|
||||
private readonly history: number;
|
||||
private index = 0;
|
||||
|
||||
constructor(history: number) {
|
||||
if (history < 1) {
|
||||
throw new Error('Must track at least one number');
|
||||
}
|
||||
|
||||
this.history = history;
|
||||
this.stack = new Array(history);
|
||||
}
|
||||
|
||||
public push(val: T) {
|
||||
this.stack[this.index++ % this.history] = val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get average value of all values on the stack.
|
||||
*
|
||||
* @return {T} average value
|
||||
*/
|
||||
public average(): T {
|
||||
if (this.index === 0) {
|
||||
return 0 as T;
|
||||
}
|
||||
|
||||
const list = this.nonEmpty();
|
||||
let sum = 0;
|
||||
|
||||
for (const n of list as Array<number>) {
|
||||
sum += n;
|
||||
}
|
||||
|
||||
return (sum / list.length) as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get average value of all values of the stack after filtering
|
||||
* out a number of highest and lowest values
|
||||
*
|
||||
* @param {number} extremes number of high/low values to ignore
|
||||
* @return {T} average value
|
||||
*/
|
||||
public averageWithoutExtremes(extremes: number): T {
|
||||
if (this.index === 0) {
|
||||
return 0 as T;
|
||||
}
|
||||
|
||||
const list = this.nonEmpty();
|
||||
const count = list.length - extremes * 2;
|
||||
|
||||
if (count < 1) {
|
||||
// Not enough entries to remove desired number of extremes,
|
||||
// fall back to regular average
|
||||
return this.average();
|
||||
}
|
||||
|
||||
let sum = 0;
|
||||
|
||||
for (const n of list.sort((a, b) => a - b).slice(extremes, -extremes)) {
|
||||
sum += n;
|
||||
}
|
||||
|
||||
return (sum / count) as T;
|
||||
}
|
||||
|
||||
private nonEmpty(): Readonly<Array<number>> {
|
||||
return this.index < this.history
|
||||
? this.stack.slice(0, this.index)
|
||||
: this.stack;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user