Deploy script (#52)

* No-downtime deploy script
* Easier to use helper for average numbers
This commit is contained in:
Maciej Hirsz
2018-09-25 11:53:57 +02:00
committed by GitHub
parent e0012099c0
commit df56e33bf6
9 changed files with 91 additions and 29 deletions
-19
View File
@@ -1,19 +0,0 @@
import { Milliseconds } from './types';
export function blockAverage(blockTimes: Array<number>): Milliseconds {
let count = 0;
let sum = 0;
for (const time of blockTimes) {
if (time) {
count += 1;
sum += time;
}
}
if (count === 0) {
return 0 as Milliseconds;
}
return (sum / count) as Milliseconds;
}
+77
View File
@@ -35,3 +35,80 @@ export function sleep(time: Milliseconds): Promise<void> {
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;
}
}
+1 -2
View File
@@ -1,6 +1,5 @@
export * from './helpers';
export * from './id';
export * from './block';
export * from './stringify';
import * as Types from './types';
@@ -9,4 +8,4 @@ import * as FeedMessage from './feed';
export { Types, FeedMessage };
// Increment this if breaking changes were made to types in `feed.ts`
export const VERSION: Types.FeedVersion = 13 as Types.FeedVersion;
export const VERSION: Types.FeedVersion = 14 as Types.FeedVersion;