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/next/pages/api/v2/file-access.ts
Views: 687
1
/*
2
Get information about this user's activity to help them
3
better find things.
4
*/
5
6
import getAccountId from "lib/account/get-account";
7
import getPool from "@cocalc/database/pool";
8
import getParams from "lib/api/get-params";
9
10
export default async function handle(req, res) {
11
const account_id = await getAccountId(req);
12
if (account_id == null) {
13
// no usage to list, since not signed in.
14
res.json({ files: [] });
15
return;
16
}
17
18
let { interval } = getParams(req);
19
20
try {
21
const files = await fileAccess({ account_id, interval });
22
res.json({ files });
23
} catch (err) {
24
res.json({ error: `${err.message}` });
25
}
26
}
27
28
interface Access {
29
project_id: string;
30
title: string;
31
path: string;
32
}
33
34
async function fileAccess({ account_id, interval }): Promise<Access[]> {
35
const pool = getPool("medium");
36
const { rows } = await pool.query(
37
"SELECT DISTINCT file_access_log.filename AS path, file_access_log.project_id AS project_id, projects.title AS title FROM file_access_log, projects WHERE file_access_log.project_id=projects.project_id AND file_access_log.time >= NOW() - $2::interval AND file_access_log.account_id=$1 ORDER BY title,path",
38
[account_id, interval ? interval : "1 day"]
39
);
40
return rows;
41
}
42
43