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/listings.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
/*
7
Table of directory listings.
8
*/
9
10
import { Table } from "./types";
11
12
export const WATCH_TIMEOUT_MS = 60000;
13
14
// Maximum number of paths to keep in listings tables for this project.
15
// NOTE: for now we're just using a limit query to only load this many
16
// when initializing things. We might have more in the database until
17
// synctable.delete gets fully and properly initialized. The main goal
18
// is to not waste bandwidth and memory in browsers.
19
export const MAX_PATHS = 50;
20
21
// Maximum number of entries in a directory listing. If this is exceeded
22
// we sort by last modification time, take only the first MAX_FILES_PER_PATH
23
// most recent entries, and set missing to the number that are missing.
24
// This was 100 for a long time -- I'm upping it to 200 since at a 100
25
// it has worked very well without any excessive load issues.
26
export const MAX_FILES_PER_PATH = 200;
27
28
import type { DirectoryListingEntry } from "@cocalc/util/types";
29
30
export interface Listing {
31
path: string;
32
project_id?: string;
33
compute_server_id?: number;
34
listing?: DirectoryListingEntry[];
35
time?: Date;
36
interest?: Date;
37
missing?: number;
38
error?: string;
39
deleted?: string[];
40
}
41
42
Table({
43
name: "listings",
44
fields: {
45
project_id: {
46
type: "uuid",
47
desc: "The project id.",
48
},
49
compute_server_id: {
50
type: "integer",
51
desc: "The compute server id. 0 or not given means 'the main project'.",
52
},
53
path: {
54
type: "string",
55
desc: "The directory that this is a listing of. Should not start or end with a slash and is relative to home directory of project.",
56
},
57
time: {
58
type: "timestamp",
59
desc: "When this directory listing was obtained.",
60
},
61
interest: {
62
type: "timestamp",
63
desc: "When a browser last said 'I care about contents of this directory'.",
64
},
65
listing: {
66
type: "array",
67
pg_type: "JSONB[]",
68
desc: "The directory listing itself.",
69
},
70
missing: {
71
type: "number",
72
desc: "If the listing is truncated due to being too large this is the number of missing entries. The oldest entries are missing.",
73
},
74
error: {
75
type: "string",
76
desc: "Set if there is an error computing the directory listing, e.g., if there is no directory this may happen. This will be cleared once the listing is successfully computed.",
77
},
78
deleted: {
79
type: "array",
80
pg_type: "TEXT[]",
81
desc: "Paths within this directory that have been explicitly deleted by a user",
82
},
83
},
84
rules: {
85
desc: "Directory listings in projects",
86
primary_key: ["project_id", "path", "compute_server_id"],
87
// this is necessary only for schema migration from befor we had compute_server_id as a column.
88
default_primary_key_value: { compute_server_id: 0 },
89
user_query: {
90
get: {
91
pg_where: ["projects"],
92
options: [{ order_by: "-interest" }, { limit: MAX_PATHS }],
93
fields: {
94
project_id: null,
95
compute_server_id: null,
96
path: null,
97
time: null,
98
listing: null,
99
missing: null,
100
interest: null,
101
error: null,
102
deleted: null,
103
},
104
},
105
set: {
106
// same privs as project, since compute servers are treated as a user. Plus listings isn't a
107
// security risk.
108
delete: true,
109
fields: {
110
project_id: "project_id",
111
compute_server_id: true,
112
path: true,
113
listing: true,
114
missing: true,
115
time: true,
116
interest: true,
117
error: true,
118
deleted: true,
119
},
120
},
121
},
122
123
project_query: {
124
get: {
125
pg_where: [{ "project_id = $::UUID": "project_id" }],
126
options: [{ order_by: "-interest" }, { limit: 3 }],
127
fields: {
128
project_id: null,
129
compute_server_id: null,
130
path: null,
131
time: null,
132
listing: null,
133
missing: null,
134
interest: null,
135
error: null,
136
deleted: null,
137
},
138
},
139
set: {
140
// delete=true, since project *IS* allowed to delete entries
141
// in this table (used for purging tracked listings).
142
delete: true,
143
fields: {
144
project_id: "project_id",
145
compute_server_id: true,
146
path: true,
147
listing: true,
148
missing: true,
149
time: true,
150
interest: true,
151
error: true,
152
deleted: true,
153
},
154
},
155
},
156
},
157
});
158
159