2021-05-12 06:35:00 +00:00
|
|
|
import { database 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';
|
2021-06-02 01:46:26 +00:00
|
|
|
import { Space } from './space';
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Stats';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'Stats';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export const prefix = 'sta';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = false;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = false;
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface BaseStats {
|
|
|
|
currentLaunch: number | null;
|
|
|
|
lastLaunch: number | null;
|
|
|
|
currentVersion: string | null;
|
|
|
|
lastVersion: string | null;
|
|
|
|
launches: number;
|
|
|
|
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
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function migrate(doc: Stats) {
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function create(patch: Partial<Stats> = {}) {
|
|
|
|
return db.docCreate<Stats>(type, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function update(patch: Partial<Stats>) {
|
2016-10-02 20:57:00 +00:00
|
|
|
const stats = await get();
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.docUpdate<Stats>(stats, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function get() {
|
|
|
|
const results = await db.all<Stats>(type) || [];
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
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,
|
2021-05-12 06:35:00 +00:00
|
|
|
}: Partial<Stats>) {
|
2020-12-09 16:39:53 +00:00
|
|
|
const stats = await get();
|
|
|
|
await update({
|
2021-05-12 06:35:00 +00:00
|
|
|
...(createdRequests && {
|
|
|
|
createdRequests: stats.createdRequests + createdRequests,
|
|
|
|
}),
|
|
|
|
...(deletedRequests && {
|
|
|
|
deletedRequests: stats.deletedRequests + deletedRequests,
|
|
|
|
}),
|
|
|
|
...(executedRequests && {
|
|
|
|
executedRequests: stats.executedRequests + executedRequests,
|
|
|
|
}),
|
2020-12-09 16:39:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function incrementCreatedRequests() {
|
2021-05-12 06:35:00 +00:00
|
|
|
await incrementRequestStats({
|
|
|
|
createdRequests: 1,
|
|
|
|
});
|
2020-12-09 16:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function incrementDeletedRequests() {
|
2021-05-12 06:35:00 +00:00
|
|
|
await incrementRequestStats({
|
|
|
|
deletedRequests: 1,
|
|
|
|
});
|
2020-12-09 16:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function incrementExecutedRequests() {
|
2021-05-12 06:35:00 +00:00
|
|
|
await incrementRequestStats({
|
|
|
|
executedRequests: 1,
|
|
|
|
});
|
2020-12-09 16:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function incrementCreatedRequestsForDescendents(doc: Workspace | RequestGroup) {
|
|
|
|
const docs = await db.withDescendants(doc);
|
|
|
|
const requests = docs.filter(doc => isRequest(doc) || isGrpcRequest(doc));
|
2021-05-12 06:35:00 +00:00
|
|
|
await incrementRequestStats({
|
|
|
|
createdRequests: requests.length,
|
|
|
|
});
|
2020-12-09 16:39:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 01:46:26 +00:00
|
|
|
export async function incrementDeletedRequestsForDescendents(doc: Workspace | RequestGroup | Space) {
|
2020-12-09 16:39:53 +00:00
|
|
|
const docs = await db.withDescendants(doc);
|
|
|
|
const requests = docs.filter(doc => isRequest(doc) || isGrpcRequest(doc));
|
2021-05-12 06:35:00 +00:00
|
|
|
await incrementRequestStats({
|
|
|
|
deletedRequests: requests.length,
|
|
|
|
});
|
2020-12-09 16:39:53 +00:00
|
|
|
}
|