Path: blob/master/src/packages/util/db-schema/file-use.ts
5800 views
/*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// Helper function to normalize dates for comparison10function getTime(date: any): number {11if (!date) return 0;12if (date instanceof Date) return date.getTime();13if (typeof date === "string") return new Date(date).getTime();14return 0;15}1617/* TODO: for postgres rewrite after done we MIGHT completely redo file_use to eliminate18the id field, use project_id, path as a compound primary key, and maybe put users in19another table with a relation. There is also expert discussion about this table in the20Hacker News discussion of my PostgreSQL vs ... blog post.21*/2223Table({24name: "file_use",25fields: {26id: {27type: "string",28pg_type: "CHAR(40)",29},30project_id: {31type: "uuid",32},33path: {34type: "string",35},36users: {37type: "map",38desc: "{account_id1: {action1: timestamp1, action2:timestamp2}, account_id2: {...}}",39date: "all",40},41last_edited: {42type: "timestamp",43},44},45rules: {46primary_key: "id",47durability: "soft", // loss of some log data not serious, since used only for showing notifications48unique_writes: true, // there is no reason for a user to write the same record twice49db_standby: "safer", // allow doing the initial read part of the query from a standby node.50pg_indexes: ["project_id", "last_edited"],5152// CRITICAL! At scale, this query53// 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;54// 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 scan55// We disable the indes scan for this query, which gets rid of the extra workers and runs fine.56pg_indexscan: false,5758// I put a time limit in pg_where below of to just give genuinely recent notifications,59// and massively reduce server load. The obvious todo list is to make another file_use60// virtual table that lets you get older entries.61user_query: {62get: {63pg_where: ["last_edited >= NOW() - interval '21 days'", "projects"],64pg_where_load: [65"last_edited >= NOW() - interval '10 days'",66"projects",67],68pg_changefeed: "projects",69options: [{ order_by: "-last_edited" }, { limit: 200 }], // limit is arbitrary70options_load: [{ order_by: "-last_edited" }, { limit: 70 }], // limit is arbitrary71throttle_changes: 2000,72fields: {73id: null,74project_id: null,75path: null,76users: null,77last_edited: null,78},79},80set: {81fields: {82id(obj, db) {83return db.sha1(obj.project_id, obj.path);84},85project_id: "project_write",86path: true,87users: true,88last_edited: true,89},90required_fields: {91id: true,92project_id: true,93path: true,94},95check_hook(db, obj, account_id, _project_id, cb) {96// hook to note that project is being used (CRITICAL: do not pass path97// into db.touch since that would cause another write to the file_use table!)98// CRITICAL: Only do this if what edit or chat for this user is very recent.99// Otherwise we touch the project just for seeing notifications or opening100// the file, which is confusing and wastes a lot of resources.101const x = obj.users != null ? obj.users[account_id] : undefined;102// edit/chat/open fields may be strings or Date objects depending on how they're processed103const recentTime = minutes_ago(3).getTime();104105if (106x != null &&107(getTime(x.edit) >= recentTime ||108getTime(x.chat) >= recentTime ||109getTime(x.open) >= recentTime)110) {111db.touch({ project_id: obj.project_id, account_id });112// Also log that this particular file is being used/accessed; this113// is mainly only for longterm analytics but also the file_use_times114// virtual table queries this. Note that log_file_access115// is throttled.116db.log_file_access({117project_id: obj.project_id,118account_id,119filename: obj.path,120});121}122cb();123},124},125},126},127});128129Table({130name: "crm_file_use",131rules: {132virtual: "file_use",133primary_key: "id",134user_query: {135get: {136admin: true, // only admins can do get queries on this table137fields: schema.file_use.user_query?.get?.fields ?? {},138},139},140},141fields: schema.file_use.fields,142});143144145