Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/util/db-schema/project-log.ts
1447 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { deep_copy, uuid } from "../misc";
7
import { SCHEMA as schema } from "./index";
8
import { Table } from "./types";
9
10
const DEFAULT_LIMIT = 750;
11
12
Table({
13
name: "project_log",
14
rules: {
15
primary_key: "id",
16
// db_standby feels too slow for this, since the user only
17
// does this query when they actually click to show the log.
18
//db_standby: "unsafe",
19
durability: "soft", // dropping a log entry (e.g., "foo opened a file") wouldn't matter much
20
21
pg_indexes: ["project_id", "time", "account_id"],
22
23
user_query: {
24
get: {
25
pg_where: ["time >= NOW() - interval '2 months'", "projects"],
26
pg_changefeed: "projects",
27
options: [{ order_by: "-time" }, { limit: DEFAULT_LIMIT }],
28
throttle_changes: 2000,
29
fields: {
30
id: null,
31
project_id: null,
32
time: null,
33
account_id: null,
34
event: null,
35
},
36
},
37
set: {
38
fields: {
39
id(obj) {
40
return obj.id != null ? obj.id : uuid();
41
},
42
project_id: "project_write",
43
account_id: "account_id",
44
time: true,
45
event: true,
46
},
47
},
48
},
49
},
50
fields: {
51
id: {
52
type: "uuid",
53
desc: "which",
54
},
55
project_id: {
56
type: "uuid",
57
desc: "where",
58
render: { type: "project_link" },
59
},
60
time: {
61
type: "timestamp",
62
desc: "when",
63
},
64
account_id: {
65
type: "uuid",
66
desc: "who",
67
render: { type: "account" },
68
},
69
event: {
70
type: "map",
71
desc: "what",
72
render: { type: "json" },
73
},
74
},
75
});
76
77
// project_log_all -- exactly like project_log, but loads up
78
// to the most recent **many** log entries (so a LOT).
79
schema.project_log_all = deep_copy(schema.project_log);
80
// This happens rarely, and user is prepared to wait.
81
schema.project_log_all.db_standby = "unsafe";
82
schema.project_log_all.virtual = "project_log";
83
// no time constraint:
84
if (schema.project_log_all.user_query?.get == null) {
85
throw Error("make typescript happy");
86
}
87
schema.project_log_all.user_query.get.pg_where = ["projects"];
88
schema.project_log_all.user_query.get.options = [
89
{ order_by: "-time" },
90
{ limit: 7500 },
91
];
92
93
Table({
94
name: "crm_project_log",
95
rules: {
96
virtual: "project_log",
97
primary_key: "id",
98
user_query: {
99
get: {
100
admin: true, // only admins can do get queries on this table
101
fields: schema.project_log.user_query?.get?.fields ?? {},
102
},
103
},
104
},
105
fields: schema.project_log.fields,
106
});
107
108