I have class:
export class DayValue {
date: string;
status: string;
}
and statusToPointsMap:
public static map: Map<string, number> = new Map([
['COMPLETED', 100],
['FAILED', -100],
['SKIPPED', 0],
]);
I want to create function which calculates avg result per given period of time, ie:
[
[
"2020-05-02",
"COMPLETED"
],
[
"2020-05-01",
"FAILED"
],
[
"2020-04-30",
"FAILED"
],
[
"2020-04-29",
"SKIPPED"
],
[
"2020-03-13",
"SKIPPED"
]
]
to (groupped by month)
[ ["05-2020",0],
[ "04-2020",-40],
["03-2020",0]
]
or(groupped by year):
["2020",-16]
I created a function which works as I wanted, but I'm wonder if there is any way to to it smarter?
private calculateAvgPerPeriod(values: DayValue[], period: string): any[] {
const resultArray = [];
from(values).pipe(
groupBy(dayValue => this.datePipe.transform(dayValue.date, period)),
mergeMap(group => group.pipe(toArray())),
map(array => [
this.datePipe.transform(array[0].date, period),
array
.map(day => this.statusToPointsMap.get(day.status))
.reduce((prev, curr) => prev + curr, 0) / array.length
]
))
.subscribe(entry => {
resultArray.push(entry);
});
return resultArray;
}