Skip to content

Commit

Permalink
Implement unique incremebt
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed Oct 21, 2018
1 parent be2cde1 commit 6cccd9d
Showing 1 changed file with 72 additions and 28 deletions.
100 changes: 72 additions & 28 deletions src/services/stats.ts
Expand Up @@ -3,6 +3,7 @@
*/

const nestedProperty = require('nested-property');
import autobind from 'autobind-decorator';
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import { INote } from '../models/note';
Expand Down Expand Up @@ -45,6 +46,11 @@ type ChartDocument<T extends Obj> = {
* データ
*/
data: T;

/**
* ユニークインクリメント用
*/
unique?: Obj;
};

/**
Expand All @@ -61,7 +67,28 @@ abstract class Chart<T> {
this.collection.createIndex('group');
}

protected async getCurrentStats(span: Span, group?: Obj): Promise<ChartDocument<T>> {
@autobind
private convertQuery(x: Obj, path: string): Obj {
const query: Obj = {};

const dive = (x: Obj, path: string) => {
Object.entries(x).forEach(([k, v]) => {
const p = path ? `${path}.${k}` : k;
if (typeof v === 'number') {
query[p] = v;
} else {
dive(v, p);
}
});
};

dive(x, path);

return query;
}

@autobind
private async getCurrentStats(span: Span, group?: Obj): Promise<ChartDocument<T>> {
const now = new Date();
const y = now.getFullYear();
const m = now.getMonth();
Expand Down Expand Up @@ -129,39 +156,42 @@ abstract class Chart<T> {
}
}

protected inc(inc: Partial<T>, group?: Obj): void {
const query: Obj = {};
@autobind
protected commit(query: Obj, group?: Obj, uniqueKey?: string, uniqueValue?: string): void {
const update = (stats: ChartDocument<T>) => {
// ユニークインクリメントの場合、指定のキーに指定の値が既に存在していたら弾く
if (uniqueKey && stats.unique && stats.unique[uniqueKey] && stats.unique[uniqueKey].includes(uniqueValue)) return;

const dive = (x: Obj, path?: string) => {
Object.entries(x).forEach(([k, v]) => {
const p = path ? `${path}.${k}` : k;
if (typeof v === 'number') {
query[`data.${p}`] = v;
} else {
dive(v, p);
}
});
if (uniqueKey) {
query['$push'] = {
[`unique.${uniqueKey}`]: uniqueValue
};
}

this.collection.update({
_id: stats._id
}, query);
};

dive(inc);
this.getCurrentStats('day', group).then(stats => update(stats));
this.getCurrentStats('hour', group).then(stats => update(stats));
}

this.getCurrentStats('day', group).then(stats => {
this.collection.findOneAndUpdate({
_id: stats._id
}, {
$inc: query
});
});
@autobind
protected inc(inc: Partial<T>, group?: Obj): void {
this.commit({
$inc: this.convertQuery(inc, 'data')
}, group);
}

this.getCurrentStats('hour', group).then(stats => {
this.collection.findOneAndUpdate({
_id: stats._id
}, {
$inc: query
});
});
@autobind
protected incIfUnique(inc: Partial<T>, key: string, value: string, group?: Obj): void {
this.commit({
$inc: this.convertQuery(inc, 'data')
}, group, key, value);
}

@autobind
public async getStats(span: Span, range: number, group?: Obj): Promise<ArrayValue<T>> {
const chart: T[] = [];

Expand Down Expand Up @@ -296,6 +326,7 @@ class UsersChart extends Chart<UsersStats> {
super('usersStats');
}

@autobind
protected generateInitialStats(): UsersStats {
return {
local: {
Expand All @@ -311,6 +342,7 @@ class UsersChart extends Chart<UsersStats> {
};
}

@autobind
protected generateEmptyStats(mostRecentStats: UsersStats): UsersStats {
return {
local: {
Expand All @@ -326,6 +358,7 @@ class UsersChart extends Chart<UsersStats> {
};
}

@autobind
public async update(user: IUser, isAdditional: boolean) {
const update: Obj = {};

Expand Down Expand Up @@ -424,6 +457,7 @@ class NotesChart extends Chart<NotesStats> {
super('notesStats');
}

@autobind
protected generateInitialStats(): NotesStats {
return {
local: {
Expand All @@ -449,6 +483,7 @@ class NotesChart extends Chart<NotesStats> {
};
}

@autobind
protected generateEmptyStats(mostRecentStats: NotesStats): NotesStats {
return {
local: {
Expand All @@ -474,8 +509,11 @@ class NotesChart extends Chart<NotesStats> {
};
}

@autobind
public async update(note: INote, isAdditional: boolean) {
const update: Obj = {};
const update: Obj = {
diffs: {}
};

update.total = isAdditional ? 1 : -1;

Expand Down Expand Up @@ -577,6 +615,7 @@ class DriveChart extends Chart<DriveStats> {
super('driveStats');
}

@autobind
protected generateInitialStats(): DriveStats {
return {
local: {
Expand All @@ -598,6 +637,7 @@ class DriveChart extends Chart<DriveStats> {
};
}

@autobind
protected generateEmptyStats(mostRecentStats: DriveStats): DriveStats {
return {
local: {
Expand All @@ -619,6 +659,7 @@ class DriveChart extends Chart<DriveStats> {
};
}

@autobind
public async update(file: IDriveFile, isAdditional: boolean) {
const update: Obj = {};

Expand Down Expand Up @@ -678,6 +719,7 @@ class NetworkChart extends Chart<NetworkStats> {
super('networkStats');
}

@autobind
protected generateInitialStats(): NetworkStats {
return {
incomingRequests: 0,
Expand All @@ -688,6 +730,7 @@ class NetworkChart extends Chart<NetworkStats> {
};
}

@autobind
protected generateEmptyStats(mostRecentStats: NetworkStats): NetworkStats {
return {
incomingRequests: 0,
Expand All @@ -698,6 +741,7 @@ class NetworkChart extends Chart<NetworkStats> {
};
}

@autobind
public async update(incomingRequests: number, time: number, incomingBytes: number, outgoingBytes: number) {
const inc: Partial<NetworkStats> = {
incomingRequests: incomingRequests,
Expand Down

0 comments on commit 6cccd9d

Please sign in to comment.