2017-11-22 21:10:34 +00:00
|
|
|
// @flow
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { BaseModel } from './index';
|
2020-12-09 16:39:53 +00:00
|
|
|
import type { Workspace } from './workspace';
|
|
|
|
import type { RequestGroup } from './request-group';
|
|
|
|
import { isRequest, isGrpcRequest } from './helpers/is-model';
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Stats';
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'Stats';
|
|
|
|
export const prefix = 'sta';
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = false;
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = false;
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2017-11-22 21:10:34 +00:00
|
|
|
type BaseStats = {
|
|
|
|
currentLaunch: number | null,
|
|
|
|
lastLaunch: number | null,
|
|
|
|
currentVersion: string | null,
|
|
|
|
lastVersion: string | null,
|
2018-12-12 17:36:11 +00:00
|
|
|
launches: number,
|
2020-12-09 16:39:53 +00:00
|
|
|
createdRequests: number,
|
|
|
|
deletedRequests: number,
|
|
|
|
executedRequests: number,
|
2017-11-22 21:10:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type Stats = BaseModel & BaseStats;
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function init(): BaseStats {
|
2016-11-10 01:15:27 +00:00
|
|
|
return {
|
2017-11-22 21:10:34 +00:00
|
|
|
currentLaunch: null,
|
|
|
|
lastLaunch: null,
|
|
|
|
currentVersion: null,
|
2016-10-02 20:57:00 +00:00
|
|
|
lastVersion: null,
|
2018-12-12 17:36:11 +00:00
|
|
|
launches: 0,
|
2020-12-09 16:39:53 +00:00
|
|
|
createdRequests: 0,
|
|
|
|
deletedRequests: 0,
|
|
|
|
executedRequests: 0,
|
2016-11-10 01:15:27 +00:00
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function migrate(doc: Stats): Stats {
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export function create(patch: $Shape<Stats> = {}): Promise<Stats> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export async function update(patch: $Shape<Stats>): Promise<Stats> {
|
2016-10-02 20:57:00 +00:00
|
|
|
const stats = await get();
|
|
|
|
return db.docUpdate(stats, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function get(): Promise<Stats> {
|
2016-10-02 20:57:00 +00:00
|
|
|
const results = await db.all(type);
|
|
|
|
if (results.length === 0) {
|
2017-11-20 16:07:36 +00:00
|
|
|
return create();
|
2016-10-02 20:57:00 +00:00
|
|
|
} else {
|
|
|
|
return results[0];
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 16:39:53 +00:00
|
|
|
|
|
|
|
export async function incrementRequestStats({
|
|
|
|
createdRequests,
|
|
|
|
deletedRequests,
|
|
|
|
executedRequests,
|
|
|
|
}: $Shape<Stats>) {
|
|
|
|
const stats = await get();
|
|
|
|
await update({
|
|
|
|
...(createdRequests && { createdRequests: stats.createdRequests + createdRequests }),
|
|
|
|
...(deletedRequests && { deletedRequests: stats.deletedRequests + deletedRequests }),
|
|
|
|
...(executedRequests && { executedRequests: stats.executedRequests + executedRequests }),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function incrementCreatedRequests() {
|
|
|
|
await incrementRequestStats({ createdRequests: 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function incrementDeletedRequests() {
|
|
|
|
await incrementRequestStats({ deletedRequests: 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function incrementExecutedRequests() {
|
|
|
|
await incrementRequestStats({ executedRequests: 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function incrementCreatedRequestsForDescendents(doc: Workspace | RequestGroup) {
|
|
|
|
const docs = await db.withDescendants(doc);
|
|
|
|
const requests = docs.filter(doc => isRequest(doc) || isGrpcRequest(doc));
|
|
|
|
await incrementRequestStats({ createdRequests: requests.length });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function incrementDeletedRequestsForDescendents(doc: Workspace | RequestGroup) {
|
|
|
|
const docs = await db.withDescendants(doc);
|
|
|
|
const requests = docs.filter(doc => isRequest(doc) || isGrpcRequest(doc));
|
|
|
|
await incrementRequestStats({ deletedRequests: requests.length });
|
|
|
|
}
|