CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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