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/file-use.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 { Table } from "./types";
7
import { minutes_ago } from "../misc";
8
import { SCHEMA as schema } from "./index";
9
10
/* TODO: for postgres rewrite after done we MIGHT completely redo file_use to eliminate
11
the id field, use project_id, path as a compound primary key, and maybe put users in
12
another table with a relation. There is also expert discussion about this table in the
13
Hacker News discussion of my PostgreSQL vs ... blog post.
14
*/
15
16
Table({
17
name: "file_use",
18
fields: {
19
id: {
20
type: "string",
21
pg_type: "CHAR(40)",
22
},
23
project_id: {
24
type: "uuid",
25
},
26
path: {
27
type: "string",
28
},
29
users: {
30
type: "map",
31
desc: "{account_id1: {action1: timestamp1, action2:timestamp2}, account_id2: {...}}",
32
date: "all",
33
},
34
last_edited: {
35
type: "timestamp",
36
},
37
},
38
rules: {
39
primary_key: "id",
40
durability: "soft", // loss of some log data not serious, since used only for showing notifications
41
unique_writes: true, // there is no reason for a user to write the same record twice
42
db_standby: "safer", // allow doing the initial read part of the query from a standby node.
43
pg_indexes: ["project_id", "last_edited"],
44
45
// CRITICAL! At scale, this query
46
// SELECT * FROM file_use WHERE project_id = any(select project_id from projects where users ? '25e2cae4-05c7-4c28-ae22-1e6d3d2e8bb3') ORDER BY last_edited DESC limit 100;
47
// will take forever due to the query planner being off with its estimation (its the case where there is no such user or no data) and also uses several workers to do an index scan
48
// We disable the indes scan for this query, which gets rid of the extra workers and runs fine.
49
pg_indexscan: false,
50
51
// I put a time limit in pg_where below of to just give genuinely recent notifications,
52
// and massively reduce server load. The obvious todo list is to make another file_use
53
// virtual table that lets you get older entries.
54
user_query: {
55
get: {
56
pg_where: ["last_edited >= NOW() - interval '21 days'", "projects"],
57
pg_where_load: [
58
"last_edited >= NOW() - interval '10 days'",
59
"projects",
60
],
61
pg_changefeed: "projects",
62
options: [{ order_by: "-last_edited" }, { limit: 200 }], // limit is arbitrary
63
options_load: [{ order_by: "-last_edited" }, { limit: 70 }], // limit is arbitrary
64
throttle_changes: 2000,
65
fields: {
66
id: null,
67
project_id: null,
68
path: null,
69
users: null,
70
last_edited: null,
71
},
72
},
73
set: {
74
fields: {
75
id(obj, db) {
76
return db.sha1(obj.project_id, obj.path);
77
},
78
project_id: "project_write",
79
path: true,
80
users: true,
81
last_edited: true,
82
},
83
required_fields: {
84
id: true,
85
project_id: true,
86
path: true,
87
},
88
check_hook(db, obj, account_id, _project_id, cb) {
89
// hook to note that project is being used (CRITICAL: do not pass path
90
// into db.touch since that would cause another write to the file_use table!)
91
// CRITICAL: Only do this if what edit or chat for this user is very recent.
92
// Otherwise we touch the project just for seeing notifications or opening
93
// the file, which is confusing and wastes a lot of resources.
94
const x = obj.users != null ? obj.users[account_id] : undefined;
95
const recent = minutes_ago(3);
96
if (
97
x != null &&
98
(x.edit >= recent || x.chat >= recent || x.open >= recent)
99
) {
100
db.touch({ project_id: obj.project_id, account_id });
101
// Also log that this particular file is being used/accessed; this
102
// is mainly only for longterm analytics but also the file_use_times
103
// virtual table queries this. Note that log_file_access
104
// is throttled.
105
db.log_file_access({
106
project_id: obj.project_id,
107
account_id,
108
filename: obj.path,
109
});
110
}
111
cb();
112
},
113
},
114
},
115
},
116
});
117
118
Table({
119
name: "crm_file_use",
120
rules: {
121
virtual: "file_use",
122
primary_key: "id",
123
user_query: {
124
get: {
125
admin: true, // only admins can do get queries on this table
126
fields: schema.file_use.user_query?.get?.fields ?? {},
127
},
128
},
129
},
130
fields: schema.file_use.fields,
131
});
132
133