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/database/postgres/site-license/search.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { PostgreSQL } from "../types";6import {7is_valid_uuid_string,8search_match,9search_split,10} from "@cocalc/util/misc";11import { query } from "../query";1213// This works and does the entire search in the database.14// Unfortunately, it is unusably slow and I just don't have15// the time to do something better right now:16/*17export async function matching_site_licenses(18db: PostgreSQL,19search: string,20limit: number = 521): Promise<{ id: string }[]> {22const where: any[] = [];23const params: (string | number)[] = [];24let i = 1;25for (const s of search_split(search.toLowerCase())) {26where.push(27`(lower(site_licenses.title) LIKE $${i}::TEXT OR lower(site_licenses.description) LIKE $${i}::TEXT OR site_licenses.id::TEXT LIKE $${i}::TEXT OR lower(site_licenses.info::TEXT) LIKE $${i}::TEXT OR lower(accounts.first_name) LIKE $${i}::TEXT OR lower(accounts.last_name) LIKE $${i}::TEXT OR lower(accounts.email_address) LIKE $${i}::TEXT)`28);29params.push(`%${s}%`);30i += 1;31}32let query =33"SELECT DISTINCT(site_licenses.id) AS id, site_licenses.last_used FROM site_licenses, accounts WHERE accounts.account_id::TEXT = ANY(site_licenses.managers) AND";34query += ` (${where.join(" AND ")})`;3536// recently active licenses are much more relevant than old ones37query += " ORDER BY site_licenses.last_used DESC NULLS LAST";38query += ` LIMIT $${i}::INTEGER`;39params.push(limit);40i += 1;4142return (await db.async_query({ query, params })).rows;43}44*/4546// This is dumb but will be sufficiently fast up to probably 5K licenses.47// This is not user facing functionality. We could maybe restrict to last_used48// in the few months by default (optionally anything) and this would last49// much longer...50export async function matching_site_licenses(51db: PostgreSQL,52search: string,53limit: number = 554): Promise<{ id: string }[]> {55if (is_valid_uuid_string(search)) {56return (57await db.async_query<{id :string}>({58cache: true,59query: "SELECT id FROM site_licenses WHERE id=$1",60params: [search],61})62).rows;63}64// Get them all.65const licenses = (66await db.async_query({67query:68"SELECT id, id || ' ' || coalesce(lower(title),'') || ' ' || coalesce(lower(description),'') || ' ' || coalesce(lower(info::TEXT),'') AS info, managers FROM site_licenses ORDER BY last_used DESC NULLS LAST",69})70).rows;71// Replace manager account ids by name and email72const managers: Set<string> = new Set();73for (const x of licenses) {74if (x.managers != null) {75for (const account_id of x.managers) {76managers.add(account_id);77}78}79}80const accounts: { [account_id: string]: string } = {};81for (const row of (82await db.async_query({83cache: true,84query:85"SELECT account_id, coalesce(lower(first_name),'') || ' ' || coalesce(lower(last_name),'') || ' ' || coalesce(lower(email_address),'') AS info FROM accounts WHERE account_id=ANY($1)",86params: [Array.from(managers)],87})88).rows) {89accounts[row.account_id] = row.info;90}9192const v = search_split(search.toLowerCase());93const matches: { id: string }[] = [];94for (const license of licenses) {95let s = license.info;96if (license.managers) {97for (const account_id of license.managers) {98s += " " + accounts[account_id];99}100}101if (search_match(s, v)) {102matches.push({ id: license.id as string });103}104if (matches.length >= limit) break;105}106107return matches;108}109110export async function manager_site_licenses(111db: PostgreSQL,112account_id: string113): Promise<object[]> {114const query = "SELECT * FROM site_licenses WHERE $1=ANY(managers)";115const params = [account_id];116return (await db.async_query({ query, params })).rows;117}118119// Return true if the given user is a manager of any site licenses.120export async function is_a_site_license_manager(121db: PostgreSQL,122account_id: string123): Promise<boolean> {124return (125(126await query({127db,128query: "SELECT COUNT(*)::INT FROM site_licenses WHERE $1=ANY(managers)",129one: true,130params: [account_id],131})132).count > 0133);134}135136137