@opengis/bi 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bi.js +1 -1
- package/dist/bi.umd.cjs +42 -42
- package/dist/import-file-D8jh74Dz.js +3543 -0
- package/dist/{vs-funnel-bar-C_TceUrc.js → vs-funnel-bar-T330oJNS.js} +3 -3
- package/dist/{vs-list-DyhLUIPb.js → vs-list-DeHF_Oaf.js} +109 -109
- package/dist/{vs-map-BtQJNN4L.js → vs-map-Skt608pM.js} +8 -8
- package/dist/{vs-map-cluster-BbPUosvt.js → vs-map-cluster-BRUiY_90.js} +21 -21
- package/dist/{vs-number-D2GkU586.js → vs-number-Dd_21nn-.js} +3 -3
- package/dist/{vs-table-D_Yn9QqB.js → vs-table-BwC29Zyc.js} +6 -6
- package/dist/{vs-text-BivVd6cY.js → vs-text-DEJjWxDu.js} +32 -39
- package/package.json +77 -76
- package/plugin.js +22 -0
- package/server/helpers/mdToHTML.js +17 -0
- package/server/migrations/bi.dataset.sql +46 -0
- package/server/migrations/bi.sql +112 -0
- package/server/plugins/docs.js +48 -0
- package/server/plugins/hook.js +89 -0
- package/server/plugins/vite.js +69 -0
- package/server/routes/dashboard/controllers/dashboard.import.js +103 -0
- package/server/routes/dashboard/controllers/dashboard.js +157 -0
- package/server/routes/dashboard/controllers/dashboard.list.js +40 -0
- package/server/routes/dashboard/controllers/utils/yaml.js +11 -0
- package/server/routes/dashboard/index.mjs +26 -0
- package/server/routes/data/controllers/data.js +230 -0
- package/server/routes/data/controllers/util/chartSQL.js +49 -0
- package/server/routes/data/controllers/util/normalizeData.js +65 -0
- package/server/routes/data/index.mjs +32 -0
- package/server/routes/dataset/controllers/bi.dataset.list.js +29 -0
- package/server/routes/dataset/controllers/bi.db.list.js +19 -0
- package/server/routes/dataset/controllers/comment.js +55 -0
- package/server/routes/dataset/controllers/createDatasetPost.js +134 -0
- package/server/routes/dataset/controllers/data.js +149 -0
- package/server/routes/dataset/controllers/dbTablePreview.js +58 -0
- package/server/routes/dataset/controllers/dbTables.js +34 -0
- package/server/routes/dataset/controllers/delete.js +40 -0
- package/server/routes/dataset/controllers/deleteDataset.js +52 -0
- package/server/routes/dataset/controllers/editDataset.js +90 -0
- package/server/routes/dataset/controllers/export.js +214 -0
- package/server/routes/dataset/controllers/form.js +99 -0
- package/server/routes/dataset/controllers/format.js +46 -0
- package/server/routes/dataset/controllers/insert.js +47 -0
- package/server/routes/dataset/controllers/table.js +68 -0
- package/server/routes/dataset/controllers/update.js +43 -0
- package/server/routes/dataset/index.mjs +132 -0
- package/server/routes/dataset/utils/convertJSONToCSV.js +17 -0
- package/server/routes/dataset/utils/convertJSONToXls.js +47 -0
- package/server/routes/dataset/utils/createTableQuery.js +59 -0
- package/server/routes/dataset/utils/datasetForms.js +1 -0
- package/server/routes/dataset/utils/descriptionList.js +46 -0
- package/server/routes/dataset/utils/downloadRemoteFile.js +58 -0
- package/server/routes/dataset/utils/executeQuery.js +46 -0
- package/server/routes/dataset/utils/getLayersData.js +107 -0
- package/server/routes/dataset/utils/getTableData.js +47 -0
- package/server/routes/dataset/utils/insertDataQuery.js +12 -0
- package/server/routes/dataset/utils/metaFormat.js +24 -0
- package/server/routes/edit/controllers/dashboard.add.js +36 -0
- package/server/routes/edit/controllers/dashboard.delete.js +39 -0
- package/server/routes/edit/controllers/dashboard.edit.js +61 -0
- package/server/routes/edit/controllers/widget.add.js +78 -0
- package/server/routes/edit/controllers/widget.del.js +58 -0
- package/server/routes/edit/controllers/widget.edit.js +106 -0
- package/server/routes/edit/index.mjs +33 -0
- package/server/routes/map/controllers/cluster.js +125 -0
- package/server/routes/map/controllers/clusterVtile.js +166 -0
- package/server/routes/map/controllers/geojson.js +127 -0
- package/server/routes/map/controllers/heatmap.js +118 -0
- package/server/routes/map/controllers/map.js +69 -0
- package/server/routes/map/controllers/utils/downloadClusterData.js +45 -0
- package/server/routes/map/controllers/vtile.js +183 -0
- package/server/routes/map/index.mjs +32 -0
- package/server/templates/page/login.html +59 -0
- package/server/utils/getWidget.js +117 -0
- package/utils.js +12 -0
- package/dist/import-file-Bx4xpxVb.js +0 -3493
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
+
|
|
3
|
+
import insertDataQuery from './insertDataQuery.js';
|
|
4
|
+
|
|
5
|
+
const insertDataset = `insert into bi.dataset
|
|
6
|
+
(name, table_name, dataset_file_path, column_list, pk, data_source, uid)
|
|
7
|
+
values($1,$2,$3,$4,$5,$6,$7) returning dataset_id`;
|
|
8
|
+
|
|
9
|
+
const updateAppeal = 'update datasets_appeal.appeal set dataset_id=$1, data_key=$2 where ap_id=$3';
|
|
10
|
+
|
|
11
|
+
export default async function executeQuery({
|
|
12
|
+
pg = pgClients.client, sql, data, name, id, table, relPath, columns, pkey, source, user, url, dataKey,
|
|
13
|
+
}) {
|
|
14
|
+
const client = await pg.connect();
|
|
15
|
+
try {
|
|
16
|
+
await client.query('BEGIN');
|
|
17
|
+
|
|
18
|
+
// create table
|
|
19
|
+
await client.query(sql);
|
|
20
|
+
|
|
21
|
+
// insert data
|
|
22
|
+
if (data) {
|
|
23
|
+
const insertData = insertDataQuery(columns, data, table);
|
|
24
|
+
await client.query(insertData);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// create dataset
|
|
28
|
+
const args = [name || id, table, relPath, columns ? JSON.stringify(columns) : null, pkey, JSON.stringify({ type: source, appeal: id, url }), user?.uid];
|
|
29
|
+
const datasetId = await client.query(insertDataset, args).then(el => el.rows?.[0]?.dataset_id);
|
|
30
|
+
|
|
31
|
+
if (id) {
|
|
32
|
+
// update appeal for public only, admin w/o appeal
|
|
33
|
+
await client.query(updateAppeal, [datasetId, dataKey, id]);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
await client.query('COMMIT');
|
|
37
|
+
return { datasetId };
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
await client.query('ROLLBACK');
|
|
41
|
+
return { error: err.toString(), status: 500 };
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
client.release();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import descriptionList from "./descriptionList.js";
|
|
2
|
+
import metaFormat from "./metaFormat.js";
|
|
3
|
+
|
|
4
|
+
async function getDataByLatLng({ pg, layer, table, id, lat, lng, time = [] }) {
|
|
5
|
+
// const rclient = getRedis();
|
|
6
|
+
// const redisKey = `${pg.options.database}:gis-format:${layer}:${lat}:${lng}`;
|
|
7
|
+
// const cacheData = await rclient.get(redisKey);
|
|
8
|
+
// if (cacheData && !config.local) return JSON.parse(cacheData);
|
|
9
|
+
|
|
10
|
+
const { rows: styles = [] } = await pg.query(`select style_id as id, dataset
|
|
11
|
+
from gis.style where style_id=any($1::text[])
|
|
12
|
+
union all
|
|
13
|
+
select dataset_id as id, json_build_array(json_build_object('pk', pk, 'table', table_name, 'query', query, 'card', setting->>'card', 'columns', column_list)) as dataset
|
|
14
|
+
from bi.dataset a where dataset_id=any($1::text[])`, [layer.split(',')]);
|
|
15
|
+
time.push(Date.now());
|
|
16
|
+
|
|
17
|
+
if (!styles?.length) {
|
|
18
|
+
return { message: `style not found: ${layer}`, status: 400 };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const { srids } = await pg.queryCache?.('select json_agg(srid) as srids from spatial_ref_sys').then((res1) => res1.rows?.[0] || {}) || {};
|
|
22
|
+
|
|
23
|
+
const layers = styles.reduce((acc, curr) => {
|
|
24
|
+
curr.dataset?.filter((el) => el.table && (el.pk || pg.pk?.[el.table]))
|
|
25
|
+
?.forEach((el) => acc.push({
|
|
26
|
+
layer: curr.id,
|
|
27
|
+
pk: el.pk || pg.pk?.[el.table],
|
|
28
|
+
key: el.key,
|
|
29
|
+
table: el.table,
|
|
30
|
+
query: el.query,
|
|
31
|
+
template: el.card,
|
|
32
|
+
column_list: el.column_list,
|
|
33
|
+
columns: el.columns || el.column_list?.map((el) => el.name)?.join(','),
|
|
34
|
+
srid: el.srid || '4326',
|
|
35
|
+
geom: el.geom || 'geom',
|
|
36
|
+
step: srids?.includes(el.srid - 0) && el.srid !== '4326' ? 10 : 0.0002,
|
|
37
|
+
}));
|
|
38
|
+
return acc;
|
|
39
|
+
}, [])
|
|
40
|
+
.filter((el) => el.pk && el.layer)
|
|
41
|
+
.filter((el) => table ? el.table === table : true);
|
|
42
|
+
|
|
43
|
+
if (!layers.length) {
|
|
44
|
+
return { message: 'data not found', status: 404 };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const point = `srid=4326;point(${lng} ${lat})`;
|
|
48
|
+
|
|
49
|
+
// ${el.columns?.split?.filter?.((el) => el) ? `,${el.columns?.split?.filter?.((el) => el) || ''}` : ''}
|
|
50
|
+
|
|
51
|
+
const q = layers.map((el) => `select ${el.pk}, '${el.layer}' as layer,
|
|
52
|
+
row_to_json(t) as data,
|
|
53
|
+
st_asgeojson(${el.geom})::json as geom from ${el.table} t
|
|
54
|
+
where ${el.query || '1=1'} and ${id ? `${el.pk}::text='${id.replace(/'/g, "''")}'` : '1=1'}
|
|
55
|
+
and case
|
|
56
|
+
when ST_GeometryType(geom) in ('ST_Polygon','ST_MultiPolygon')
|
|
57
|
+
then st_intersects(geom,st_buffer('${point}',${el.step}))
|
|
58
|
+
|
|
59
|
+
when ST_GeometryType(geom) in ('ST_Line','ST_MultiLineString')
|
|
60
|
+
then st_distance(geom,'${point}') < ${el.step}
|
|
61
|
+
else false end`).join(' union all ');
|
|
62
|
+
|
|
63
|
+
// if (config.local && config.debug) console.log(q);
|
|
64
|
+
const { rows: objects = [] } = await pg.query(q) || {};
|
|
65
|
+
time.push(Date.now());
|
|
66
|
+
|
|
67
|
+
const rows = objects.map((row) => {
|
|
68
|
+
const layerData = layers.find((el) => el.layer === row?.layer) || {};
|
|
69
|
+
const rowData = layerData?.columns?.split?.filter?.((el) => el) ? layerData?.columns?.split?.filter?.((el) => el).reduce((acc, curr) => Object.assign(acc, { [curr]: row.data?.[curr] }), {}) : row.data;
|
|
70
|
+
return {
|
|
71
|
+
...rowData,
|
|
72
|
+
...row,
|
|
73
|
+
...layerData,
|
|
74
|
+
data: undefined,
|
|
75
|
+
id: layerData.pk ? row?.[layerData.pk] : undefined,
|
|
76
|
+
geom: row.geom,
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const { pk, layer: layerId, card, template } = rows[0] || {};
|
|
81
|
+
|
|
82
|
+
const dataset = layers.find((el) => el.layer === rows?.[0]?.layer) || {};
|
|
83
|
+
|
|
84
|
+
const columnList = (dataset.column_list || dataset.columns)?.reduce?.((acc, curr) => Object.assign(acc, { [curr.name]: curr.title || curr.ua || curr.name }), {}) || {};
|
|
85
|
+
|
|
86
|
+
const columns = card
|
|
87
|
+
? Object.keys(card).map(el => `${el}| ${card[el]}`).join('|')
|
|
88
|
+
: Object.keys(columnList)?.map((key) => `${columnList[key]}| ${key}`).join('|');
|
|
89
|
+
|
|
90
|
+
const html = columns && rows?.[0] ? await descriptionList(rows[0], { hash: { columns } }) : undefined;
|
|
91
|
+
time.push(Date.now());
|
|
92
|
+
|
|
93
|
+
await Promise.all(rows.map(async () => {
|
|
94
|
+
const layerData = layers.find((el) => el.layer === rows?.[0]?.layer) || {};
|
|
95
|
+
const columns = layerData.column_list || layerData.columns;
|
|
96
|
+
if (columns?.length && Array.isArray(columns)) {
|
|
97
|
+
await metaFormat({ rows, columns });
|
|
98
|
+
}
|
|
99
|
+
}));
|
|
100
|
+
time.push(Date.now());
|
|
101
|
+
|
|
102
|
+
const resp = { id: rows?.[0]?.[pk], layers: layerId, template, data: rows?.[0], rows, html };
|
|
103
|
+
|
|
104
|
+
// await rclient.set(redisKey, JSON.stringify(resp), 'EX', 5 * 60);
|
|
105
|
+
return resp;
|
|
106
|
+
}
|
|
107
|
+
export default getDataByLatLng
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { handlebars } from "@opengis/fastify-table/utils.js";
|
|
2
|
+
|
|
3
|
+
import descriptionList from "./descriptionList.js";
|
|
4
|
+
// import metaFormat from "./metaFormat.js";
|
|
5
|
+
|
|
6
|
+
async function getDataById({ pg, table, id, time = [] }) {
|
|
7
|
+
const dataset = await pg.query(`select dataset_id as id, pk, query, table_name, column_list, setting
|
|
8
|
+
from bi.dataset where dataset_id=$1`, [table]).then((res) => res.rows[0] || {});
|
|
9
|
+
time.push(Date.now());
|
|
10
|
+
|
|
11
|
+
if (!dataset?.id) {
|
|
12
|
+
return { message: `dataset not found: ${table}`, status: 400 };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (!dataset?.table_name) {
|
|
16
|
+
return { message: 'invalid dataset: empty params table', status: 400 };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const pk = dataset.pk || pg.pk?.[dataset.table_name];
|
|
20
|
+
|
|
21
|
+
const { rows = [] } = pk ? await pg.query(`select *, ${pk}, geom::json from ${dataset.table_name} where ${dataset.query || '1=1'} and ${pk}=$1`, [id]) || {} : {};
|
|
22
|
+
if (pk) rows.forEach((row) => Object.assign(row, { id: row?.[pk] }));
|
|
23
|
+
time.push(Date.now());
|
|
24
|
+
|
|
25
|
+
// sql columns
|
|
26
|
+
const sql = `select attname as name, pg_catalog.col_description(attrelid,attnum) as title, atttypid::regtype as type from pg_catalog.pg_attribute a
|
|
27
|
+
where attrelid='${dataset.table_name}'::regclass and attnum>0
|
|
28
|
+
and attname not in ('editor_id','editor_date','cdate','geom','id','uid','cdate')
|
|
29
|
+
and atttypid::regtype not in ('json','geometry')`;
|
|
30
|
+
|
|
31
|
+
// card or auto
|
|
32
|
+
const type = dataset.setting?.card_type || 'auto';
|
|
33
|
+
const list = dataset.setting?.card_list || [];
|
|
34
|
+
const { rows: all } = await pg.query(sql).then(el => el);
|
|
35
|
+
const columns = all.filter(el => type === 'list' ? list.includes(el.name) : true)
|
|
36
|
+
.map(el => `${el.title || el.name}| ${el.name}`).join('|')
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
const { body } = type === 'html' && dataset?.setting?.card ? await pg.query(`select body->>'body' as body from admin.doc_template where title=$1`, [dataset?.setting?.card]).then(el => el.rows[0] || {}) : {}
|
|
40
|
+
|
|
41
|
+
const html = body ? await handlebars.compile(body)(rows[0]) : await descriptionList(rows[0], { hash: { columns } });
|
|
42
|
+
time.push(Date.now());
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
return { id: rows?.[0]?.id, template: dataset?.setting?.card, rows, html, body };
|
|
46
|
+
}
|
|
47
|
+
export default getDataById
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export default function insertDataQuery(columns, data, table) {
|
|
2
|
+
const columnList = columns?.map((el) => el.name)?.join(',');
|
|
3
|
+
const features = data?.features || [];
|
|
4
|
+
if (!features?.length) return null;
|
|
5
|
+
|
|
6
|
+
const insertData = `INSERT into ${table} (geom ${columns?.length ? ','.concat(columnList) : ''}) SELECT public.ST_GeomFromGeoJSON((features->>'geometry')::json) AS geom
|
|
7
|
+
${columns?.length ? ','.concat(columns?.map((el) => `features->'properties'->>'${el.title}'`)?.join(',')) : ''} FROM
|
|
8
|
+
(SELECT json_array_elements('${JSON.stringify(features).replace(/'/g, "''")}'::json) AS features)q`;
|
|
9
|
+
|
|
10
|
+
// console.log(insertData);
|
|
11
|
+
return insertData;
|
|
12
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getSelectVal } from '@opengis/fastify-table/utils.js';
|
|
2
|
+
|
|
3
|
+
export default async function metaFormat({ rows, columns }) {
|
|
4
|
+
const selectCols = columns?.filter?.((el) => el.data);
|
|
5
|
+
if (!selectCols?.length) return rows;
|
|
6
|
+
|
|
7
|
+
await Promise.all(selectCols.map(async (attr) => {
|
|
8
|
+
const values = [...new Set(rows?.map((el) => el[attr.name]).flat())].filter((el) => (typeof el === 'boolean' ? true : el));
|
|
9
|
+
if (!values.length) return null;
|
|
10
|
+
|
|
11
|
+
const cls = await getSelectVal({ name: attr.data, values });
|
|
12
|
+
if (!cls) return null;
|
|
13
|
+
|
|
14
|
+
rows.forEach(el => {
|
|
15
|
+
const val = el[attr.name]?.map?.(c => cls[c.toString()] || cls[c] || c) || cls[el[attr.name]?.toString()] || cls[el[attr.name]] || el[attr.name];
|
|
16
|
+
if (!val) return;
|
|
17
|
+
Object.assign(el, { [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: (val.color ? val : val.text || val) });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return null;
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
return rows;
|
|
24
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { pgClients, dataInsert, getPGAsync } from "@opengis/fastify-table/utils.js";
|
|
2
|
+
|
|
3
|
+
export default async function dashboardAdd(req, reply) {
|
|
4
|
+
const {
|
|
5
|
+
pg = pgClients.client,
|
|
6
|
+
body = {},
|
|
7
|
+
} = req;
|
|
8
|
+
const time = Date.now();
|
|
9
|
+
|
|
10
|
+
if (body?.db) {
|
|
11
|
+
try {
|
|
12
|
+
const pg1 = await getPGAsync(body.db);
|
|
13
|
+
await pg1.query('select 1');
|
|
14
|
+
} catch (err) {
|
|
15
|
+
return reply.status(400).send('Некоректна база даних');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const res = await dataInsert({
|
|
20
|
+
pg,
|
|
21
|
+
table: 'bi.dashboard',
|
|
22
|
+
data: body,
|
|
23
|
+
}).then(el => el.rows?.[0] || {});
|
|
24
|
+
|
|
25
|
+
if (res.dashboard_id && body.dataset) {
|
|
26
|
+
await pg.query(`update bi.dataset set dashboard_list=coalesce(dashboard_list, '{}'::text[]) || array[$1]
|
|
27
|
+
where dataset_id=$2`, [res.dashboard_id, body.dataset]);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
time: Date.now() - time,
|
|
32
|
+
id: res.dashboard_id,
|
|
33
|
+
title: res.title,
|
|
34
|
+
name: res.name,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { existsSync, readdirSync } from 'node:fs';
|
|
3
|
+
|
|
4
|
+
import { pgClients, dataDelete } from '@opengis/fastify-table/utils.js';
|
|
5
|
+
|
|
6
|
+
const cwd = process.cwd();
|
|
7
|
+
const dashboardDir = path.join(cwd, 'server/templates/dashboard');
|
|
8
|
+
|
|
9
|
+
export default async function dashboardDelete({ pg = pgClients.client, params = {}, user }, reply) {
|
|
10
|
+
if (!params?.id) {
|
|
11
|
+
return reply.status(400).send('not enough params: id');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const dirContent = existsSync(dashboardDir)
|
|
15
|
+
? readdirSync(dashboardDir)
|
|
16
|
+
: [];
|
|
17
|
+
|
|
18
|
+
if (dirContent.includes(params.id)) {
|
|
19
|
+
return reply.status(403).send(`access restricted: ${params.id}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const { rowCount = 0 } = pg.pk?.['bi.dashboard']
|
|
23
|
+
? await pg.query('select * from bi.dashboard where $1 in (dashboard_id,name)', [params.id])
|
|
24
|
+
: {};
|
|
25
|
+
|
|
26
|
+
if (rowCount === 0) {
|
|
27
|
+
return reply.status(404).send(`not found ${params.id}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await pg.query(`delete from bi.widget where $1 in (dashboard_id)`, [params.id]);
|
|
31
|
+
await dataDelete({
|
|
32
|
+
pg,
|
|
33
|
+
table: 'bi.dashboard',
|
|
34
|
+
id: params.id,
|
|
35
|
+
uid: user?.uid,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return reply.status(200).send('successfully deleted');
|
|
39
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { dataUpdate, pgClients, getPGAsync } from "@opengis/fastify-table/utils.js";
|
|
2
|
+
|
|
3
|
+
export default async function dashboardEdit({
|
|
4
|
+
pg = pgClients.client, params = {}, body = {},
|
|
5
|
+
}, reply) {
|
|
6
|
+
const { name: dashboardName } = params;
|
|
7
|
+
const { panels, widgets, table_name: tableName } = body;
|
|
8
|
+
|
|
9
|
+
if (body?.db) {
|
|
10
|
+
try {
|
|
11
|
+
const pg1 = await getPGAsync(body.db);
|
|
12
|
+
await pg1.query('select 1');
|
|
13
|
+
} catch (err) {
|
|
14
|
+
return reply.status(400).send('Некоректна база даних');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (!dashboardName) {
|
|
19
|
+
return reply.status(400).send('not enough params: name');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!tableName) {
|
|
23
|
+
return reply.status(400).send('not enough params: table_name');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const row = await pg.query('select dashboard_id, widgets, panels from bi.dashboard where $1 in (dashboard_id, name)', [dashboardName])
|
|
27
|
+
.then(el => el.rows?.[0] || {});
|
|
28
|
+
const { dashboard_id: dashboardId } = row;
|
|
29
|
+
|
|
30
|
+
if (!dashboardId) {
|
|
31
|
+
return reply.status(400).send('dashboard not found');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (panels?.length && !widgets?.length) {
|
|
35
|
+
row.widgets.forEach((el) => {
|
|
36
|
+
const { title } = panels?.find?.(item => item.widget === el.name) || {};
|
|
37
|
+
if (el.data) {
|
|
38
|
+
// console.log('el.title', el.title, 'title', title);
|
|
39
|
+
el.data.title = title;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
Object.assign(body, { widgets: row.widgets });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const res = await dataUpdate({
|
|
46
|
+
pg,
|
|
47
|
+
table: 'bi.dashboard',
|
|
48
|
+
id: dashboardId,
|
|
49
|
+
data: body,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
if (!Object.keys(res)?.length) {
|
|
53
|
+
return reply.status(404).send('not found data');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
message: `updated ${dashboardName}`,
|
|
58
|
+
status: 200,
|
|
59
|
+
rows: res,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { dataInsert, dataUpdate, getPGAsync, pgClients } from "@opengis/fastify-table/utils.js";
|
|
3
|
+
|
|
4
|
+
/* eslint-disable import/extensions */
|
|
5
|
+
import { yamlSafe } from '../../../../utils.js';
|
|
6
|
+
|
|
7
|
+
function generateUniqueName(prefix = 'bar') {
|
|
8
|
+
const randomPart = Math.floor(Math.random() * 10000);
|
|
9
|
+
const timestamp = Date.now();
|
|
10
|
+
return `${prefix}_${randomPart}_${timestamp}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default async function widgetAdd({ pg = pgClients.client, params = {}, body = {} }) {
|
|
14
|
+
const { name: dashboardName } = params;
|
|
15
|
+
if (!dashboardName) {
|
|
16
|
+
return { message: 'not enough params: id', status: 400 };
|
|
17
|
+
}
|
|
18
|
+
const data = body.yml ? yamlSafe.load(body.yml) : body;
|
|
19
|
+
|
|
20
|
+
const row = await pg.query(`select dashboard_id, widgets, panels, table_name, db from bi.dashboard
|
|
21
|
+
where $1 in (dashboard_id,name)`, [dashboardName]).then(el => el.rows?.[0] || {});
|
|
22
|
+
|
|
23
|
+
const pg1 = row?.db ? await getPGAsync(row.db) : pg;
|
|
24
|
+
|
|
25
|
+
const tableName = data.data?.table
|
|
26
|
+
|| data?.table
|
|
27
|
+
|| data.table_name
|
|
28
|
+
|| row.table_name;
|
|
29
|
+
|
|
30
|
+
const loadTemplate = pg1.pk?.['admin.doc_template'] ? await pg1.query(
|
|
31
|
+
'select body from admin.doc_template where doc_type=5 and title=$1',
|
|
32
|
+
[tableName]
|
|
33
|
+
).then(el => el.rows?.[0]?.body) : null;
|
|
34
|
+
|
|
35
|
+
if (!tableName || !(pg.pk?.[loadTemplate?.table || tableName] || pgClients[data.db || row.db || 'client']?.pk?.[loadTemplate?.table || tableName])) {
|
|
36
|
+
return { message: 'bad params: table', status: 400 };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const { dashboard_id: dashboardId } = row;
|
|
40
|
+
|
|
41
|
+
const metric = data.data?.metrics || data.data?.metric || data?.metrics || data?.metric;
|
|
42
|
+
Object.assign(data, {
|
|
43
|
+
name: generateUniqueName(data.type),
|
|
44
|
+
table_name: tableName,
|
|
45
|
+
metrics: Array.isArray(metric) ? metric[0] : metric,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
data.widget_id = createHash('md5').update([data?.name, dashboardId].join()).digest('hex').substr(0, 10);
|
|
49
|
+
|
|
50
|
+
const res = await dataUpdate({
|
|
51
|
+
pg,
|
|
52
|
+
table: 'bi.dashboard',
|
|
53
|
+
id: dashboardId,
|
|
54
|
+
data: {
|
|
55
|
+
widgets: [data].concat(row.widgets || []),
|
|
56
|
+
panels: [{ widget: data.name, col: data.col || 3, height: data.data?.height }].concat(
|
|
57
|
+
row.panels || []
|
|
58
|
+
),
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const widgetData = { ...data, data, dashboard_id: dashboardId };
|
|
63
|
+
if (body?.yml) Object.assign(widgetData, { yml: body.yml });
|
|
64
|
+
|
|
65
|
+
await dataInsert({
|
|
66
|
+
pg,
|
|
67
|
+
table: 'bi.widget',
|
|
68
|
+
data: widgetData,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
dashboard: dashboardName,
|
|
73
|
+
widgetId: data.widget_id,
|
|
74
|
+
widgetName: data.name,
|
|
75
|
+
status: 200,
|
|
76
|
+
rows: res,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { dataDelete, dataUpdate, pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
+
|
|
3
|
+
export default async function widgetDel({ pg = pgClients.client, params = {} }) {
|
|
4
|
+
const { widget: widgetName, name: dashboardName } = params;
|
|
5
|
+
|
|
6
|
+
if (!widgetName || !dashboardName) {
|
|
7
|
+
return {
|
|
8
|
+
message: 'not enough params: dashboard and widget name',
|
|
9
|
+
status: 400,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const row = await pg.query(`select a.widget_id, b.dashboard_id from bi.widget a, bi.dashboard b
|
|
14
|
+
where $2 in (a.widget_id, a.name) and $1 in (b.dashboard_id, b.name) order by 1,2`, [dashboardName, widgetName])
|
|
15
|
+
.then(el => el.rows?.[0] || {});
|
|
16
|
+
|
|
17
|
+
const { widget_id: widgetId, dashboard_id: dashboardId } = row || {};
|
|
18
|
+
|
|
19
|
+
if (!widgetId) {
|
|
20
|
+
return { message: `widget not found ${widgetName}`, status: 404 };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
await dataDelete({
|
|
24
|
+
pg,
|
|
25
|
+
table: 'bi.widget',
|
|
26
|
+
id: widgetId,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const currentDashboard = await pg.query('select * from bi.dashboard where $1 in (dashboard_id, name)', [dashboardName])
|
|
30
|
+
.then(el => el.rows?.[0] || {});
|
|
31
|
+
|
|
32
|
+
const body = currentDashboard;
|
|
33
|
+
|
|
34
|
+
if (!currentDashboard) {
|
|
35
|
+
return { message: `dashboard not found ${dashboardName}`, status: 404 };
|
|
36
|
+
}
|
|
37
|
+
body.panels =
|
|
38
|
+
Array.isArray(body.panels) && body.panels?.length
|
|
39
|
+
? body.panels?.filter((panel) => panel.widget !== widgetName)
|
|
40
|
+
: undefined;
|
|
41
|
+
body.widgets =
|
|
42
|
+
Array.isArray(body.widgets) && body?.widgets?.length
|
|
43
|
+
? body.widgets?.filter((widget) => widget.name !== widgetName)
|
|
44
|
+
: undefined;
|
|
45
|
+
|
|
46
|
+
const res1 = await dataUpdate({
|
|
47
|
+
pg,
|
|
48
|
+
table: 'bi.dashboard',
|
|
49
|
+
id: dashboardId,
|
|
50
|
+
data: body,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
message: `Deleted widget ${widgetName}`,
|
|
55
|
+
status: 200,
|
|
56
|
+
rows: res1,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { dataUpdate, pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
+
|
|
3
|
+
import { getWidget, yamlSafe } from '../../../../utils.js';
|
|
4
|
+
|
|
5
|
+
export default async function widgetEdit({ pg = pgClients.client, body, params }) {
|
|
6
|
+
const { widget: widgetName, name: dashboardName } = params;
|
|
7
|
+
const data = body.yml && !body.style ? yamlSafe.load(body.yml) : body;
|
|
8
|
+
const yml = body.yml ?? yamlSafe.dump(body.data);
|
|
9
|
+
// console.log(body.data, data, body.yml, yml);
|
|
10
|
+
|
|
11
|
+
if (!widgetName || !dashboardName) {
|
|
12
|
+
return {
|
|
13
|
+
message: 'not enough params: dashboard and widget name',
|
|
14
|
+
status: 400,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const { widget_id: widgetId, dashboard_id: dashboardId, widgets } = await pg.query(
|
|
19
|
+
`select a.widget_id , b.dashboard_id, b.widgets from bi.widget a, bi.dashboard b
|
|
20
|
+
where $2 in (a.widget_id, a.name) and $1 in (b.dashboard_id, b.name) order by 1,2`,
|
|
21
|
+
[dashboardName, widgetName]).then(res1 => res1.rows?.[0] || {});
|
|
22
|
+
|
|
23
|
+
// get Data
|
|
24
|
+
const templateData = await getWidget({ pg, dashboard: dashboardName, widget: widgetName });
|
|
25
|
+
if (!templateData) {
|
|
26
|
+
return { message: `widget not found ${widgetName}`, status: 404 };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (data?.data) {
|
|
30
|
+
Object.assign(data, {
|
|
31
|
+
data: Object.keys(data.data || {})
|
|
32
|
+
?.reduce?.((acc, curr) => ({ ...acc, [curr]: data.data[curr] === 'null' ? null : data.data[curr] }), {})
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
const widgetData = ['style', 'data', 'type', 'title', 'controls', 'query', 'cls', 'x']
|
|
36
|
+
.filter(el => data[el])
|
|
37
|
+
.reduce((p, el) => ({ ...p, [el]: data[el] }), {});
|
|
38
|
+
|
|
39
|
+
// get table
|
|
40
|
+
const tableName = body.table
|
|
41
|
+
|| widgetData.data?.table_name
|
|
42
|
+
|| widgetData.data?.table
|
|
43
|
+
|| widgetData?.table_name
|
|
44
|
+
|| templateData.table;
|
|
45
|
+
|
|
46
|
+
const loadTemplate = pg.pk?.['admin.doc_template'] ? await pg.query(
|
|
47
|
+
'select body from admin.doc_template where doc_type=5 and title=$1',
|
|
48
|
+
[tableName]
|
|
49
|
+
).then(el => el.rows?.[0]?.body) : null;
|
|
50
|
+
|
|
51
|
+
if (!tableName || !(pg.pk?.[loadTemplate?.table || tableName] || pgClients[widgetData?.data?.db || templateData?.db || 'client']?.pk?.[loadTemplate?.table || tableName])) {
|
|
52
|
+
return { message: 'bad params: table ' + tableName, status: 400 };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (yml) {
|
|
56
|
+
Object.assign(widgetData, { yml })
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
Object.assign(widgetData, { table_name: tableName, data: { ...widgetData?.data || {}, table_name: tableName } });
|
|
60
|
+
console.log(widgetData);
|
|
61
|
+
const rows = await dataUpdate({
|
|
62
|
+
pg,
|
|
63
|
+
table: 'bi.widget',
|
|
64
|
+
id: widgetId,
|
|
65
|
+
data: widgetData,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const idx = widgets?.findIndex?.(el => el.name === widgetName)?.toString?.() || -1;
|
|
69
|
+
if (widgetData.hasOwnProperty('table_name') && idx > -1) {
|
|
70
|
+
widgets[idx].table_name = tableName;
|
|
71
|
+
widgets[idx].table = tableName;
|
|
72
|
+
}
|
|
73
|
+
if (widgetData.hasOwnProperty('data') && idx > -1) {
|
|
74
|
+
widgets[idx].data = widgetData.data
|
|
75
|
+
}
|
|
76
|
+
if (widgetData.hasOwnProperty('yml') && idx > -1) {
|
|
77
|
+
widgets[idx].yml = widgetData.yml;
|
|
78
|
+
}
|
|
79
|
+
if (widgetData.hasOwnProperty('style') && idx > -1) {
|
|
80
|
+
widgets[idx].style = widgetData.style;
|
|
81
|
+
}
|
|
82
|
+
if (widgetData.hasOwnProperty('type') && idx > -1) {
|
|
83
|
+
widgets[idx].type = widgetData.type;
|
|
84
|
+
}
|
|
85
|
+
if (widgetData.data?.type === 'text' && idx > -1) {
|
|
86
|
+
widgets[idx].data.text = widgetData.data?.text;
|
|
87
|
+
}
|
|
88
|
+
if (typeof widgetData?.query === 'string' && idx > -1) {
|
|
89
|
+
widgets[idx].data.query = widgetData.query;
|
|
90
|
+
}
|
|
91
|
+
if (typeof widgetData?.cls === 'string' && idx > -1) {
|
|
92
|
+
widgets[idx].data.cls = widgetData.cls;
|
|
93
|
+
}
|
|
94
|
+
if (typeof widgetData?.x === 'string' && idx > -1) {
|
|
95
|
+
widgets[idx].data.x = widgetData.x;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
await dataUpdate({
|
|
99
|
+
pg,
|
|
100
|
+
table: 'bi.dashboard',
|
|
101
|
+
id: dashboardId,
|
|
102
|
+
data: { widgets },
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
return { message: rows, status: 200 };
|
|
106
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/* eslint-disable import/extensions */
|
|
2
|
+
import widgetAdd from './controllers/widget.add.js';
|
|
3
|
+
import widgetEdit from './controllers/widget.edit.js';
|
|
4
|
+
import widgetDel from './controllers/widget.del.js';
|
|
5
|
+
import dashboardEdit from './controllers/dashboard.edit.js';
|
|
6
|
+
import dashboardAdd from './controllers/dashboard.add.js';
|
|
7
|
+
import dashboardDelete from './controllers/dashboard.delete.js';
|
|
8
|
+
|
|
9
|
+
const biSchema = {
|
|
10
|
+
type: 'object',
|
|
11
|
+
properties: {
|
|
12
|
+
querystring: {
|
|
13
|
+
widget: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
14
|
+
dashboard: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
15
|
+
list: { type: 'string', pattern: '^([\\d])$' },
|
|
16
|
+
sql: { type: 'string', pattern: '^([\\d])$' },
|
|
17
|
+
},
|
|
18
|
+
params: {
|
|
19
|
+
name: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
20
|
+
widget: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default async function route(fastify) {
|
|
26
|
+
fastify.post(`/bi-dashboard/:name`, { schema: biSchema }, widgetAdd);
|
|
27
|
+
fastify.put(`/bi-dashboard/:name/:widget`, { schema: biSchema }, widgetEdit);
|
|
28
|
+
fastify.delete(`/bi-dashboard/:name/:widget`, { schema: biSchema }, widgetDel);
|
|
29
|
+
|
|
30
|
+
fastify.post(`/bi-dashboard`, { schema: biSchema }, dashboardAdd);
|
|
31
|
+
fastify.put(`/bi-dashboard/:name`, { schema: biSchema }, dashboardEdit);
|
|
32
|
+
fastify.delete(`/bi-dashboard/:id`, {}, dashboardDelete);
|
|
33
|
+
}
|