@opengis/bi
Version:
BI data visualization module
61 lines (46 loc) • 2.16 kB
JavaScript
import { pgClients, getTemplatePath, getTemplate, getTemplateSync } from '@opengis/fastify-table/utils.js';
const dirContent = getTemplatePath('dashboard').map(([filename, filepath]) => {
const obj = getTemplateSync('dashboard', filename);
const index = obj?.find?.((el) => el[0] === 'index.yml')?.[1];
const { table_name, description, title } = index || {};
return { name: filename, path: filepath, type: 'file', title, description, table_name };
});
const maxLimit = 100;
export default async function dashboardList({
pg = pgClients.client, query = {}, user = {}
}) {
const time = Date.now();
const { type = 'file', page, search, sql } = query;
const limit = Math.min(maxLimit, +(query.limit || 15));
const offset = page && page > 0 ? (page - 1) * limit : 0;
const dir = type === 'file' ? dirContent : [];
const where = [search ? `lower(title) ~ $1` : null, type === 'viewer' ? `left(table_name,5)='demo.'` : null].filter(Boolean).join(' and ') || 'true';
const q1 = `select count(*)::int as total, count(*) filter(where ${where})::int as filtered from bi.dashboard`;
const q = `select dashboard_id as name, 'db' as type, title, description, table_name, words, public, updated_at as last_update from bi.dashboard where ${where} limit ${limit} offset ${offset}`;
if (user?.user_type?.includes('admin') && ['db', 'viewer'].includes(type) && sql) {
return `${q1};${q}`;
}
const args = [search].filter(Boolean);
const { total = 0, filtered = 0 } = ['db', 'viewer'].includes(type) ? await pg.query(q1, args).then(el => el.rows?.[0] || {}) : {};
const rows = ['db', 'viewer'].includes(type) ? await pg.query(q, args).then(el => el.rows || []) : [];
if (type === 'viewer') {
return {
time: Date.now() - time,
db: pg.options?.database,
total,
filtered,
count: rows.length,
rows,
};
}
const list = (user?.user_type?.includes('admin') ? dir : dir.map(el => ({ ...el, path: undefined }))).concat(rows);
const res = {
time: Date.now() - time,
db: pg.options?.database,
total,
filtered,
count: rows.length,
rows: list,
};
return res;
}