Reduce network traffic of API response

This commit is contained in:
syuilo 2018-10-21 10:05:15 +09:00
parent 969b6dbcad
commit fed04ef5ae
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
1 changed files with 49 additions and 6 deletions

View File

@ -1,3 +1,4 @@
const nestedProperty = require('nested-property');
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import { INote } from '../models/note'; import { INote } from '../models/note';
@ -11,8 +12,13 @@ type Partial<T> = {
[P in keyof T]?: Partial<T[P]>; [P in keyof T]?: Partial<T[P]>;
}; };
type ArrayValue<T> = {
[P in keyof T]: T[P] extends number ? Array<T[P]> : ArrayValue<T[P]>;
};
type Span = 'day' | 'hour'; type Span = 'day' | 'hour';
//#region Chart Core
type ChartDocument<T extends Obj> = { type ChartDocument<T extends Obj> = {
_id: mongo.ObjectID; _id: mongo.ObjectID;
@ -119,17 +125,18 @@ abstract class Chart<T> {
protected inc(inc: Partial<T>, group?: Obj): void { protected inc(inc: Partial<T>, group?: Obj): void {
const query: Obj = {}; const query: Obj = {};
const dive = (path: string, x: Obj) => { const dive = (x: Obj, path?: string) => {
Object.entries(x).forEach(([k, v]) => { Object.entries(x).forEach(([k, v]) => {
const p = path ? `${path}.${k}` : k;
if (typeof v === 'number') { if (typeof v === 'number') {
query[path == null ? `data.${k}` : `data.${path}.${k}`] = v; query[`data.${p}`] = v;
} else { } else {
dive(path == null ? k : `${path}.${k}`, v); dive(v, p);
} }
}); });
}; };
dive(null, inc); dive(inc);
this.getCurrentStats('day', group).then(stats => { this.getCurrentStats('day', group).then(stats => {
this.collection.findOneAndUpdate({ this.collection.findOneAndUpdate({
@ -148,7 +155,7 @@ abstract class Chart<T> {
}); });
} }
public async getStats(span: Span, range: number, group?: Obj): Promise<T[]> { public async getStats(span: Span, range: number, group?: Obj): Promise<ArrayValue<T>> {
const chart: T[] = []; const chart: T[] = [];
const now = new Date(); const now = new Date();
@ -196,9 +203,45 @@ abstract class Chart<T> {
} }
} }
return chart; const res: ArrayValue<T> = {} as any;
/**
* [{
* x: 1,
* y: 5
* }, {
* x: 2,
* y: 6
* }, {
* x: 3,
* y: 7
* }]
*
*
*
* {
* x: [1, 2, 3],
* y: [5, 6, 7]
* }
*
*
*/
const dive = (x: Obj, path?: string) => {
Object.entries(x).forEach(([k, v]) => {
if (typeof v == 'object') {
dive(v, p);
} else {
nestedProperty.set(res, p, chart.map(s => nestedProperty.get(s, p)));
}
});
};
dive(chart[0]);
return res;
} }
} }
//#endregion
//#region Users stats //#region Users stats
/** /**