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-times.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";67/*8This is a virtual table to make it simple from clients to9query about all usage ever of a specific file or directory10tree by a given user.1112The usage times are the union of times both when patches13and times when the file was accessed. Thus this would be14useful to see if a user opened a PDF file, say, even though15they didn't edit it.16*/1718const LIMIT = 1000;1920Table({21name: "file_use_times",22fields: {23project_id: {24type: "uuid",25desc: "id of a project",26},27account_id: {28type: "uuid",29desc: "id of a user",30},31path: {32type: "string",33desc: "path to a specific file in the project",34},35edit_times: {36type: "array",37desc: `array of times (as ms since epoch) when the file was edited by the given account_id, sorted from newest to oldest. At most ${LIMIT} values are returned.`,38},39access_times: {40type: "array",41desc: `array of times (as ms since epoch) when the file was accessed by the given account_id, sorted from newest to oldest. At most ${LIMIT} values are returned.`,42},43},44rules: {45virtual: true, // don't make an actual table46desc: "File usage information.",47anonymous: false,48primary_key: ["project_id", "path"],49user_query: {50get: {51options: [{ limit: LIMIT }], // todo -- add an option to trim the number of results by lowering resolution?52fields: {53project_id: null,54account_id: null,55path: null,56access_times: null,57edit_times: null,58},59// Actual query is implemented using this code below rather than an actual query directly.60async instead_of_query(database, opts, cb): Promise<void> {61const obj: any = Object.assign({}, opts.query);62const { project_id, account_id, path } = obj;63if (project_id == null || account_id == null || path == null) {64cb("project_id, account_id, and path must all be specified");65return;66}67const edit_times = obj.edit_times === null;68const access_times = obj.access_times === null;69if (!edit_times && !access_times) {70// dumb -- nothing to do; but let's make this edge case work, of course.71cb(undefined, obj);72return;73}74let limit = LIMIT;75if (opts.options && opts.options[0] && opts.options[0].limit) {76// hackishly only support limit option.77limit = opts.options[0].limit;78}79try {80const x = await database.file_use_times({81project_id,82account_id,83user_account_id: opts.account_id,84path,85limit,86access_times,87edit_times,88});89if (access_times) {90obj.access_times = x.access_times;91}92if (edit_times) {93obj.edit_times = x.edit_times;94}95cb(undefined, obj);96} catch (err) {97cb(err);98}99},100},101},102},103});104105106