Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/db-schema/file-use.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Table } from "./types";6import { minutes_ago } from "../misc";7import { SCHEMA as schema } from "./index";89/* TODO: for postgres rewrite after done we MIGHT completely redo file_use to eliminate10the id field, use project_id, path as a compound primary key, and maybe put users in11another table with a relation. There is also expert discussion about this table in the12Hacker News discussion of my PostgreSQL vs ... blog post.13*/1415Table({16name: "file_use",17fields: {18id: {19type: "string",20pg_type: "CHAR(40)",21},22project_id: {23type: "uuid",24},25path: {26type: "string",27},28users: {29type: "map",30desc: "{account_id1: {action1: timestamp1, action2:timestamp2}, account_id2: {...}}",31date: "all",32},33last_edited: {34type: "timestamp",35},36},37rules: {38primary_key: "id",39durability: "soft", // loss of some log data not serious, since used only for showing notifications40unique_writes: true, // there is no reason for a user to write the same record twice41db_standby: "safer", // allow doing the initial read part of the query from a standby node.42pg_indexes: ["project_id", "last_edited"],4344// CRITICAL! At scale, this query45// 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;46// 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 scan47// We disable the indes scan for this query, which gets rid of the extra workers and runs fine.48pg_indexscan: false,4950// I put a time limit in pg_where below of to just give genuinely recent notifications,51// and massively reduce server load. The obvious todo list is to make another file_use52// virtual table that lets you get older entries.53user_query: {54get: {55pg_where: ["last_edited >= NOW() - interval '21 days'", "projects"],56pg_where_load: [57"last_edited >= NOW() - interval '10 days'",58"projects",59],60pg_changefeed: "projects",61options: [{ order_by: "-last_edited" }, { limit: 200 }], // limit is arbitrary62options_load: [{ order_by: "-last_edited" }, { limit: 70 }], // limit is arbitrary63throttle_changes: 2000,64fields: {65id: null,66project_id: null,67path: null,68users: null,69last_edited: null,70},71},72set: {73fields: {74id(obj, db) {75return db.sha1(obj.project_id, obj.path);76},77project_id: "project_write",78path: true,79users: true,80last_edited: true,81},82required_fields: {83id: true,84project_id: true,85path: true,86},87check_hook(db, obj, account_id, _project_id, cb) {88// hook to note that project is being used (CRITICAL: do not pass path89// into db.touch since that would cause another write to the file_use table!)90// CRITICAL: Only do this if what edit or chat for this user is very recent.91// Otherwise we touch the project just for seeing notifications or opening92// the file, which is confusing and wastes a lot of resources.93const x = obj.users != null ? obj.users[account_id] : undefined;94const recent = minutes_ago(3);95if (96x != null &&97(x.edit >= recent || x.chat >= recent || x.open >= recent)98) {99db.touch({ project_id: obj.project_id, account_id });100// Also log that this particular file is being used/accessed; this101// is mainly only for longterm analytics but also the file_use_times102// virtual table queries this. Note that log_file_access103// is throttled.104db.log_file_access({105project_id: obj.project_id,106account_id,107filename: obj.path,108});109}110cb();111},112},113},114},115});116117Table({118name: "crm_file_use",119rules: {120virtual: "file_use",121primary_key: "id",122user_query: {123get: {124admin: true, // only admins can do get queries on this table125fields: schema.file_use.user_query?.get?.fields ?? {},126},127},128},129fields: schema.file_use.fields,130});131132133