@opengis/bi 1.2.28 → 1.2.29

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.
@@ -1,4 +1,4 @@
1
- import { _ as l, c as h, a as c, b as d, d as p } from "./import-file-CeeuNrOS.js";
1
+ import { _ as l, c as h, a as c, b as d, d as p } from "./import-file-C1s2X9Kt.js";
2
2
  import { createElementBlock as u, openBlock as m } from "vue";
3
3
  const o = typeof window < "u" ? window.echarts : null, f = {
4
4
  name: "VsFunnelBar",
@@ -1,5 +1,5 @@
1
1
  import { createBlock as b, openBlock as l, unref as L, mergeProps as v, createElementBlock as d, createElementVNode as c, withDirectives as F, toDisplayString as S, vShow as R, Fragment as P, renderList as V, normalizeClass as D, resolveComponent as A, createVNode as H } from "vue";
2
- import { e as x, X as J, _ as w } from "./import-file-CeeuNrOS.js";
2
+ import { e as x, X as J, _ as w } from "./import-file-C1s2X9Kt.js";
3
3
  /**
4
4
  * @license lucide-vue-next v0.577.0 - ISC
5
5
  *
@@ -1,5 +1,5 @@
1
- import { _ as A, a as F, p as T, c as G, V as P, b as Z, d as q, e as D, l as K } from "./vs-list-BlFV_Syw.js";
2
- import { e as W, _ as H, V as J, c as Q, g as U, h as X, i as Y } from "./import-file-CeeuNrOS.js";
1
+ import { _ as A, a as F, p as T, c as G, V as P, b as Z, d as q, e as D, l as K } from "./vs-list-uhEudqNr.js";
2
+ import { e as W, _ as H, V as J, c as Q, g as U, h as X, i as Y } from "./import-file-C1s2X9Kt.js";
3
3
  import { createElementBlock as g, createCommentVNode as S, openBlock as p, createElementVNode as l, normalizeClass as V, Fragment as R, renderList as N, toDisplayString as O, normalizeStyle as j, createBlock as z, unref as $, mergeProps as ee, resolveComponent as m, withDirectives as M, resolveDynamicComponent as te, createVNode as w, vShow as C } from "vue";
4
4
  /**
5
5
  * @license lucide-vue-next v0.577.0 - ISC
@@ -1,5 +1,5 @@
1
- import { _ as E, a as L, p as $, V as T, b as B, c as H, d as O, l as F, e as N } from "./vs-list-BlFV_Syw.js";
2
- import { _ as V, c as P } from "./import-file-CeeuNrOS.js";
1
+ import { _ as E, a as L, p as $, V as T, b as B, c as H, d as O, l as F, e as N } from "./vs-list-uhEudqNr.js";
2
+ import { _ as V, c as P } from "./import-file-C1s2X9Kt.js";
3
3
  import { resolveComponent as u, createElementBlock as c, openBlock as i, Fragment as w, createElementVNode as r, createBlock as C, createCommentVNode as m, createVNode as x, Teleport as R, toDisplayString as g, renderList as S, normalizeStyle as k, normalizeClass as I } from "vue";
4
4
  const A = {
5
5
  components: { legendIcon: L, closeIcon: E },
@@ -1,4 +1,4 @@
1
- import { _ as c, c as o, f as n } from "./import-file-CeeuNrOS.js";
1
+ import { _ as c, c as o, f as n } from "./import-file-C1s2X9Kt.js";
2
2
  import { createElementBlock as i, openBlock as m, toDisplayString as s } from "vue";
3
3
  const u = {
4
4
  name: "VsNumber",
@@ -1,4 +1,4 @@
1
- import { _ as m, c as f, b as _, d as b } from "./import-file-CeeuNrOS.js";
1
+ import { _ as m, c as f, b as _, d as b } from "./import-file-C1s2X9Kt.js";
2
2
  import { createElementBlock as a, openBlock as r, createElementVNode as s, Fragment as n, renderList as c, toDisplayString as d } from "vue";
3
3
  const x = {
4
4
  name: "VsTable",
@@ -1,4 +1,4 @@
1
- import { _ as s, c as a, m as d } from "./import-file-CeeuNrOS.js";
1
+ import { _ as s, c as a, m as d } from "./import-file-C1s2X9Kt.js";
2
2
  import { createElementBlock as n, openBlock as o, createCommentVNode as c } from "vue";
3
3
  const u = {
4
4
  name: "VsText",
package/package.json CHANGED
@@ -1,75 +1,75 @@
1
- {
2
- "name": "@opengis/bi",
3
- "version": "1.2.28",
4
- "description": "BI data visualization module",
5
- "main": "dist/bi.js",
6
- "browser": "dist/bi.umd.cjs",
7
- "type": "module",
8
- "files": [
9
- "dist",
10
- "server",
11
- "plugin.js",
12
- "utils.js"
13
- ],
14
- "scripts": {
15
- "patch": "npm version patch && git push && npm publish",
16
- "prepublishOnly": "npm run build:lib",
17
- "debug": "node --watch-path=server server",
18
- "dev": "vite",
19
- "type-check": "vue-tsc --noEmit",
20
- "build": "vite build",
21
- "build:lib": "cross-env APP=true vite build",
22
- "build-app": "vite build",
23
- "lint": "eslint .",
24
- "format": "prettier --write .",
25
- "test": "node --test",
26
- "test21": "node --test ./test/plugins/*",
27
- "start": "node server.js",
28
- "prod": "NODE_ENV=production npm run start",
29
- "docs:i": "npm install --prefix ./docs",
30
- "docs:dev": "npm run --prefix ./docs docs:dev",
31
- "docs:build": "npm run --prefix ./docs docs:build",
32
- "docs:preview": "npm run --prefix ./docs docs:preview",
33
- "adduser": "node script/adduser.js"
34
- },
35
- "keywords": [
36
- "bi",
37
- "data visualization",
38
- "chart",
39
- "opengis",
40
- "vue",
41
- "fastify"
42
- ],
43
- "author": "Softpro",
44
- "license": "ISC",
45
- "dependencies": {
46
- "@mapbox/sphericalmercator": "^1.2.0",
47
- "@opengis/table": "^0.0.30",
48
- "lucide-vue-next": "^0.577.0"
49
- },
50
- "devDependencies": {
51
- "@opengis/core": "^0.0.33",
52
- "@opengis/fastify-table": "^2.0.33",
53
- "@opengis/form": "0.0.137",
54
- "@turf/turf": "^7.1.0",
55
- "@types/node": "^22.7.5",
56
- "@typescript-eslint/eslint-plugin": "^6.21.0",
57
- "@typescript-eslint/parser": "^6.21.0",
58
- "@vitejs/plugin-vue": "^5.1.4",
59
- "cross-env": "^10.1.0",
60
- "eslint": "^8.57.1",
61
- "eslint-config-airbnb-base": "^15.0.0",
62
- "eslint-config-prettier": "^9.1.0",
63
- "eslint-plugin-vue": "^9.29.0",
64
- "globals": "^15.10.0",
65
- "marked": "^14.1.2",
66
- "prettier": "^3.3.3",
67
- "rollup-plugin-visualizer": "^7.0.0",
68
- "sass-embedded": "1.89.2",
69
- "typescript": "^5.4.5",
70
- "vite": "^5.1.5",
71
- "vue": "^3.4.27",
72
- "vue-router": "^4.4.3",
73
- "vue-tsc": "^2.1.6"
74
- }
75
- }
1
+ {
2
+ "name": "@opengis/bi",
3
+ "version": "1.2.29",
4
+ "description": "BI data visualization module",
5
+ "main": "dist/bi.js",
6
+ "browser": "dist/bi.umd.cjs",
7
+ "type": "module",
8
+ "files": [
9
+ "dist",
10
+ "server",
11
+ "plugin.js",
12
+ "utils.js"
13
+ ],
14
+ "scripts": {
15
+ "patch": "npm version patch && git push && npm publish",
16
+ "prepublishOnly": "npm run build:lib",
17
+ "debug": "node --watch-path=server server",
18
+ "dev": "vite",
19
+ "type-check": "vue-tsc --noEmit",
20
+ "build": "vite build",
21
+ "build:lib": "cross-env APP=true vite build",
22
+ "build-app": "vite build",
23
+ "lint": "eslint .",
24
+ "format": "prettier --write .",
25
+ "test": "node --test",
26
+ "test21": "node --test ./test/plugins/*",
27
+ "start": "node server.js",
28
+ "prod": "NODE_ENV=production npm run start",
29
+ "docs:i": "npm install --prefix ./docs",
30
+ "docs:dev": "npm run --prefix ./docs docs:dev",
31
+ "docs:build": "npm run --prefix ./docs docs:build",
32
+ "docs:preview": "npm run --prefix ./docs docs:preview",
33
+ "adduser": "node script/adduser.js"
34
+ },
35
+ "keywords": [
36
+ "bi",
37
+ "data visualization",
38
+ "chart",
39
+ "opengis",
40
+ "vue",
41
+ "fastify"
42
+ ],
43
+ "author": "Softpro",
44
+ "license": "ISC",
45
+ "dependencies": {
46
+ "@mapbox/sphericalmercator": "^1.2.0",
47
+ "@opengis/table": "^0.0.30",
48
+ "lucide-vue-next": "^0.577.0"
49
+ },
50
+ "devDependencies": {
51
+ "@opengis/core": "^0.0.33",
52
+ "@opengis/fastify-table": "^2.0.33",
53
+ "@opengis/form": "0.0.137",
54
+ "@turf/turf": "^7.1.0",
55
+ "@types/node": "^22.7.5",
56
+ "@typescript-eslint/eslint-plugin": "^6.21.0",
57
+ "@typescript-eslint/parser": "^6.21.0",
58
+ "@vitejs/plugin-vue": "^5.1.4",
59
+ "cross-env": "^10.1.0",
60
+ "eslint": "^8.57.1",
61
+ "eslint-config-airbnb-base": "^15.0.0",
62
+ "eslint-config-prettier": "^9.1.0",
63
+ "eslint-plugin-vue": "^9.29.0",
64
+ "globals": "^15.10.0",
65
+ "marked": "^14.1.2",
66
+ "prettier": "^3.3.3",
67
+ "rollup-plugin-visualizer": "^7.0.0",
68
+ "sass-embedded": "1.89.2",
69
+ "typescript": "^5.4.5",
70
+ "vite": "^5.1.5",
71
+ "vue": "^3.4.27",
72
+ "vue-router": "^4.4.3",
73
+ "vue-tsc": "^2.1.6"
74
+ }
75
+ }
@@ -1,39 +1,59 @@
1
- import { pgClients, getTemplatePath, getTemplate } from '@opengis/fastify-table/utils.js';
1
+ import { pgClients, getTemplatePath, getTemplate, getTemplateSync } from '@opengis/fastify-table/utils.js';
2
2
 
3
- const q = `select dashboard_id as name, 'db' as type, title, description, table_name, words, public from bi.dashboard`;
3
+ const dirContent = getTemplatePath('dashboard').map(([filename, filepath]) => {
4
+ const obj = getTemplateSync('dashboard', filename);
5
+ const index = obj?.find?.((el) => el[0] === 'index.yml')?.[1];
6
+ const { table_name, description, title } = index || {};
4
7
 
5
- export default async function data({
8
+ return { name: filename, path: filepath, type: 'file', title, description, table_name };
9
+ });
10
+
11
+ const maxLimit = 100;
12
+
13
+ export default async function dashboardList({
6
14
  pg = pgClients.client, query = {}, user = {}
7
15
  }) {
8
16
  const time = Date.now();
9
- const { type = 'file' } = query;
17
+ const { type = 'file', page, search, sql } = query;
18
+
19
+ const limit = Math.min(maxLimit, +(query.limit || 15));
20
+ const offset = page && page > 0 ? (page - 1) * limit : 0;
21
+
22
+ const dir = type === 'file' ? dirContent : [];
10
23
 
11
- const data = getTemplatePath('dashboard');
12
- const dir = type === 'file' ? await Promise.all(
13
- data.map(async ([filename, filepath]) => {
14
- const obj = await getTemplate('dashboard', filename);
15
- const index = obj?.find?.((el) => el[0] === 'index.yml')?.[1];
16
- const { table_name, description, title } = index || {};
24
+ const where = [search ? `lower(title) ~ $1` : null, type === 'viewer' ? `left(table_name,5)='demo.'` : null].filter(Boolean).join(' and ') || 'true';
25
+ const q1 = `select count(*)::int as total, count(*) filter(where ${where})::int as filtered from bi.dashboard`;
26
+
27
+ const q = `select dashboard_id as name, 'db' as type, title, description, table_name, words, public from bi.dashboard where ${where} limit ${limit} offset ${offset}`;
28
+
29
+ if (user?.user_type?.includes('admin') && ['db', 'viewer'].includes(type) && sql) {
30
+ return `${q1};${q}`;
31
+ }
17
32
 
18
- return { name: filename, path: user?.user_type?.includes('admin') ? filepath : undefined, type: 'file', title, description, table_name };
19
- })
20
- ) : [];
33
+ const args = [search].filter(Boolean);
34
+ const { total = 0, filtered = 0 } = ['db', 'viewer'].includes(type) ? await pg.query(q1, args).then(el => el.rows?.[0] || {}) : {};
21
35
 
22
- const { rows = [] } = ['db', 'viewer'].includes(type) ? await pg.query(q) : {};
36
+ const rows = ['db', 'viewer'].includes(type) ? await pg.query(q, args).then(el => el.rows || []) : [];
23
37
 
24
38
  if (type === 'viewer') {
25
39
  return {
26
40
  time: Date.now() - time,
27
41
  db: pg.options?.database,
28
- rows: rows.filter(el => el.table_name && !el.table_name.startsWith('demo.')),
42
+ total,
43
+ filtered,
44
+ count: rows.length,
45
+ rows,
29
46
  };
30
47
  }
31
48
 
32
- const list = dir.concat(rows);
49
+ const list = (user?.user_type?.includes('admin') ? dir : dir.map(el => ({ ...el, path: undefined }))).concat(rows);
33
50
 
34
51
  const res = {
35
52
  time: Date.now() - time,
36
53
  db: pg.options?.database,
54
+ total,
55
+ filtered,
56
+ count: rows.length,
37
57
  rows: list,
38
58
  };
39
59
  return res;
@@ -1,11 +1,11 @@
1
- import yaml from 'js-yaml';
2
-
3
- yaml.loadSafe = (yml) => {
4
- try {
5
- return yaml.load(yml);
6
- } catch (err) {
7
- return { error: err.toString() };
8
- }
9
- };
10
-
11
- export default yaml;
1
+ import yaml from 'js-yaml';
2
+
3
+ yaml.loadSafe = (yml) => {
4
+ try {
5
+ return yaml.load(yml);
6
+ } catch (err) {
7
+ return { error: err.toString() };
8
+ }
9
+ };
10
+
11
+ export default yaml;
@@ -2,17 +2,17 @@ import { dataUpdate, pgClients } from '@opengis/fastify-table/utils.js';
2
2
 
3
3
  import { getWidget, yamlSafe } from '../../../../utils.js';
4
4
 
5
- export default async function widgetEdit({ pg = pgClients.client, body, params }) {
5
+ export default async function widgetEdit({ pg = pgClients.client, body, params }, reply) {
6
6
  const { widget: widgetName, name: dashboardName } = params;
7
7
  const data = body.yml && !body.style ? yamlSafe.load(body.yml) : body;
8
8
  const yml = body.yml ?? yamlSafe.dump(body.data);
9
9
  // console.log(body.data, data, body.yml, yml);
10
10
 
11
11
  if (!widgetName || !dashboardName) {
12
- return {
13
- message: 'not enough params: dashboard and widget name',
14
- status: 400,
15
- };
12
+ return reply.status(400).send({
13
+ error: 'not enough params: dashboard and widget name',
14
+ code: 400,
15
+ });
16
16
  }
17
17
 
18
18
  const { widget_id: widgetId, dashboard_id: dashboardId, widgets } = await pg.query(
@@ -23,7 +23,10 @@ export default async function widgetEdit({ pg = pgClients.client, body, params }
23
23
  // get Data
24
24
  const templateData = await getWidget({ pg, dashboard: dashboardName, widget: widgetName });
25
25
  if (!templateData) {
26
- return { message: `widget not found ${widgetName}`, status: 404 };
26
+ return reply.status(404).send({
27
+ error: `widget not found ${widgetName}`,
28
+ code: 404,
29
+ });
27
30
  }
28
31
 
29
32
  if (data?.data) {
@@ -43,22 +46,28 @@ export default async function widgetEdit({ pg = pgClients.client, body, params }
43
46
  || widgetData?.table_name
44
47
  || templateData.table;
45
48
 
46
- const loadTemplate = pg.pk?.['admin.doc_template'] ? await pg.query(
49
+ const loadTemplate = pg?.pk?.['admin.doc_template'] ? await pg.query(
47
50
  'select body from admin.doc_template where doc_type=5 and title=$1',
48
51
  [tableName]
49
52
  ).then(el => el.rows?.[0]?.body) : null;
50
53
 
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 };
54
+ if (!pg || !tableName || !(pg.pk?.[loadTemplate?.table || tableName] || pgClients[widgetData?.data?.db || templateData?.db || 'client']?.pk?.[loadTemplate?.table || tableName])) {
55
+ return reply.status(400).send({
56
+ error: 'bad params: table ' + tableName,
57
+ code: 400,
58
+ });
53
59
  }
54
60
 
55
61
  if (yml) {
56
62
  Object.assign(widgetData, { yml })
57
63
  }
58
64
 
59
- Object.assign(widgetData, { table_name: tableName, data: { ...widgetData?.data || {}, table_name: tableName } });
60
- console.log(widgetData);
61
- const rows = await dataUpdate({
65
+ if (widgetData?.data) {
66
+ Object.assign(widgetData, { data: { ...widgetData?.data || {}, table_name: tableName } });
67
+ }
68
+ Object.assign(widgetData, { table_name: tableName });
69
+ // console.log(widgetData);
70
+ const row = await dataUpdate({
62
71
  pg,
63
72
  table: 'bi.widget',
64
73
  id: widgetId,
@@ -102,5 +111,5 @@ export default async function widgetEdit({ pg = pgClients.client, body, params }
102
111
  data: { widgets },
103
112
  });
104
113
 
105
- return { message: rows, status: 200 };
114
+ return row;
106
115
  }
@@ -1,125 +1,125 @@
1
- import { getFilterSQL, logger, pgClients, getMeta } from '@opengis/fastify-table/utils.js';
2
-
3
- import { getWidget } from '../../../../utils.js';
4
-
5
- import downloadClusterData from './utils/downloadClusterData.js';
6
-
7
- const clusterExists = {};
8
-
9
- export default async function cluster(req, reply) {
10
- const { query = {} } = req;
11
- const { widget, filter, dashboard, search } = query;
12
-
13
- if (!widget) {
14
- return { message: 'not enough params: widget', status: 400 };
15
- }
16
-
17
- const { pg = req.pg || pgClients.client, data, style, controls } = await getWidget({ pg: req.pg, dashboard, widget });
18
-
19
- const pkey = pg.pk?.[data?.table];
20
-
21
- if (!pkey) {
22
- return {
23
- message: `invalid ${widget ? 'widget' : 'dashboard'}: table pk not found (${data?.table})`,
24
- status: 400,
25
- };
26
- }
27
-
28
- // data param
29
- const {
30
- table,
31
- query: where = '1=1',
32
- metrics = [],
33
- cluster,
34
- clusterTable = {},
35
- } = data;
36
-
37
- if (!cluster) {
38
- return {
39
- message: `invalid ${widget ? 'widget' : 'dashboard'}: cluster column not specified`,
40
- status: 400,
41
- };
42
- }
43
-
44
- if (!metrics.length) {
45
- return {
46
- message: `invalid ${widget ? 'widget' : 'dashboard'}: metric columns not found`,
47
- status: 400,
48
- };
49
- }
50
-
51
- if (!clusterTable?.name) {
52
- Object.assign(clusterTable, {
53
- name: 'bi.cluster',
54
- title: 'title',
55
- query: `type='${cluster}'`,
56
- });
57
- }
58
-
59
- try {
60
- if (cluster && !clusterExists[cluster]) {
61
- const res = await downloadClusterData({ pg, cluster });
62
- if (res) return res;
63
- clusterExists[cluster] = 1;
64
- }
65
-
66
- if (clusterTable?.name && !pg.pk?.[clusterTable?.name]) {
67
- return {
68
- message: 'invalid widget params: clusterTable pkey not found',
69
- status: 404,
70
- };
71
- }
72
-
73
- const { bounds, extentStr } = await pg.query(`select count(*),
74
- st_asgeojson(st_extent(geom))::json as bounds,
75
- replace(regexp_replace(st_extent(geom)::box2d::text,'BOX\\(|\\)','','g'),' ',',') as "extentStr"
76
- from ${table} where ${where || '1=1'}`).then((res) => res.rows?.[0] || {});
77
- const extent = extentStr ? extentStr.split(',') : undefined;
78
-
79
- // get sql
80
- const { optimizedSQL } =
81
- filter || search
82
- ? await getFilterSQL({ pg, table, filter, search })
83
- : {};
84
-
85
- const { columns = [] } = await getMeta({ pg, table });
86
- const columnList = columns.map(el => el.name);
87
-
88
- if (query.metric && typeof query.metric === 'string') {
89
- const checkInvalid = query.metric.split(',').find(el => !columnList.includes(el) && el !== 'count');
90
- if (checkInvalid) {
91
- return reply.status(404).send(`invalid query metric value: ${checkInvalid}`);
92
- }
93
- }
94
-
95
- const multipleMetrics = query.metric ? query.metric.split(',').map(el => el === 'count' ? 'count(*)' : `sum(${el.replace(/'/g, "''")})::float as ${el}`).join(',') : null;
96
- const multipleMetricsOrder = query.metric ? query.metric.split(',').map(el => el === 'count' ? 'count(*)' : `sum(${el.replace(/'/g, "''")})::float`).join(',') : null;
97
- const metricFunc = multipleMetrics
98
- || `${clusterTable?.operator || 'sum'}("${metrics[0]}")::float`;
99
-
100
- const q = `select b.*, ${metricFunc} ${multipleMetrics ? '' : 'as metric'}
101
- from ${optimizedSQL ? `(${optimizedSQL})` : table} q
102
- left join lateral (select "${pg.pk?.[clusterTable?.name]}" as id, ${clusterTable?.column || cluster} as name, ${clusterTable?.title} as title from ${clusterTable?.name} where ${clusterTable?.codifierColumn || 'codifier'}=q."${clusterTable?.column || cluster}" limit 1)b on 1=1
103
- where ${where} group by b.id, b.name, b.title order by ${multipleMetricsOrder || metricFunc} desc`;
104
-
105
- if (query.sql === '1') return q;
106
-
107
- // auto Index
108
- // autoIndex({ table, columns: (metrics || []).concat([cluster]) });
109
-
110
- const { rows = [] } = await pg.query(q);
111
- const vals = rows.map((el) => el.metric - 0).sort((a, b) => a - b);
112
- const len = vals.length;
113
- const sizes = [
114
- vals[0],
115
- vals[Math.floor(len / 4)],
116
- vals[Math.floor(len / 2)],
117
- vals[Math.floor(len * 0.75)],
118
- vals[len - 1],
119
- ];
120
- return { sizes, style, controls, metrics, rows, columns: columns.map(({ name, title, dataTypeID }) => ({ name, title, type: pg.pgType[dataTypeID] })), bounds, extent, count: rows.length, total: rows?.reduce((acc, curr) => (curr.metric || 0) + acc, 0) };
121
- } catch (err) {
122
- logger.file('bi/cluster/error', { error: err.toString(), query });
123
- return { error: err.toString(), status: 500 };
124
- }
125
- }
1
+ import { getFilterSQL, logger, pgClients, getMeta } from '@opengis/fastify-table/utils.js';
2
+
3
+ import { getWidget } from '../../../../utils.js';
4
+
5
+ import downloadClusterData from './utils/downloadClusterData.js';
6
+
7
+ const clusterExists = {};
8
+
9
+ export default async function cluster(req, reply) {
10
+ const { query = {} } = req;
11
+ const { widget, filter, dashboard, search } = query;
12
+
13
+ if (!widget) {
14
+ return { message: 'not enough params: widget', status: 400 };
15
+ }
16
+
17
+ const { pg = req.pg || pgClients.client, data, style, controls } = await getWidget({ pg: req.pg, dashboard, widget });
18
+
19
+ const pkey = pg.pk?.[data?.table];
20
+
21
+ if (!pkey) {
22
+ return {
23
+ message: `invalid ${widget ? 'widget' : 'dashboard'}: table pk not found (${data?.table})`,
24
+ status: 400,
25
+ };
26
+ }
27
+
28
+ // data param
29
+ const {
30
+ table,
31
+ query: where = '1=1',
32
+ metrics = [],
33
+ cluster,
34
+ clusterTable = {},
35
+ } = data;
36
+
37
+ if (!cluster) {
38
+ return {
39
+ message: `invalid ${widget ? 'widget' : 'dashboard'}: cluster column not specified`,
40
+ status: 400,
41
+ };
42
+ }
43
+
44
+ if (!metrics.length) {
45
+ return {
46
+ message: `invalid ${widget ? 'widget' : 'dashboard'}: metric columns not found`,
47
+ status: 400,
48
+ };
49
+ }
50
+
51
+ if (!clusterTable?.name) {
52
+ Object.assign(clusterTable, {
53
+ name: 'bi.cluster',
54
+ title: 'title',
55
+ query: `type='${cluster}'`,
56
+ });
57
+ }
58
+
59
+ try {
60
+ if (cluster && !clusterExists[cluster]) {
61
+ const res = await downloadClusterData({ pg, cluster });
62
+ if (res) return res;
63
+ clusterExists[cluster] = 1;
64
+ }
65
+
66
+ if (clusterTable?.name && !pg.pk?.[clusterTable?.name]) {
67
+ return {
68
+ message: 'invalid widget params: clusterTable pkey not found',
69
+ status: 404,
70
+ };
71
+ }
72
+
73
+ const { bounds, extentStr } = await pg.query(`select count(*),
74
+ st_asgeojson(st_extent(geom))::json as bounds,
75
+ replace(regexp_replace(st_extent(geom)::box2d::text,'BOX\\(|\\)','','g'),' ',',') as "extentStr"
76
+ from ${table} where ${where || '1=1'}`).then((res) => res.rows?.[0] || {});
77
+ const extent = extentStr ? extentStr.split(',') : undefined;
78
+
79
+ // get sql
80
+ const { optimizedSQL } =
81
+ filter || search
82
+ ? await getFilterSQL({ pg, table, filter, search })
83
+ : {};
84
+
85
+ const { columns = [] } = await getMeta({ pg, table });
86
+ const columnList = columns.map(el => el.name);
87
+
88
+ if (query.metric && typeof query.metric === 'string') {
89
+ const checkInvalid = query.metric.split(',').find(el => !columnList.includes(el) && el !== 'count');
90
+ if (checkInvalid) {
91
+ return reply.status(404).send(`invalid query metric value: ${checkInvalid}`);
92
+ }
93
+ }
94
+
95
+ const multipleMetrics = query.metric ? query.metric.split(',').map(el => el === 'count' ? 'count(*)' : `sum(${el.replace(/'/g, "''")})::float as ${el}`).join(',') : null;
96
+ const multipleMetricsOrder = query.metric ? query.metric.split(',').map(el => el === 'count' ? 'count(*)' : `sum(${el.replace(/'/g, "''")})::float`).join(',') : null;
97
+ const metricFunc = multipleMetrics
98
+ || `${clusterTable?.operator || 'sum'}("${metrics[0]}")::float`;
99
+
100
+ const q = `select b.*, ${metricFunc} ${multipleMetrics ? '' : 'as metric'}
101
+ from ${optimizedSQL ? `(${optimizedSQL})` : table} q
102
+ left join lateral (select "${pg.pk?.[clusterTable?.name]}" as id, ${clusterTable?.column || cluster} as name, ${clusterTable?.title} as title from ${clusterTable?.name} where ${clusterTable?.codifierColumn || 'codifier'}=q."${clusterTable?.column || cluster}" limit 1)b on 1=1
103
+ where ${where} group by b.id, b.name, b.title order by ${multipleMetricsOrder || metricFunc} desc`;
104
+
105
+ if (query.sql === '1') return q;
106
+
107
+ // auto Index
108
+ // autoIndex({ table, columns: (metrics || []).concat([cluster]) });
109
+
110
+ const { rows = [] } = await pg.query(q);
111
+ const vals = rows.map((el) => el.metric - 0).sort((a, b) => a - b);
112
+ const len = vals.length;
113
+ const sizes = [
114
+ vals[0],
115
+ vals[Math.floor(len / 4)],
116
+ vals[Math.floor(len / 2)],
117
+ vals[Math.floor(len * 0.75)],
118
+ vals[len - 1],
119
+ ];
120
+ return { sizes, style, controls, metrics, rows, columns: columns.map(({ name, title, dataTypeID }) => ({ name, title, type: pg.pgType[dataTypeID] })), bounds, extent, count: rows.length, total: rows?.reduce((acc, curr) => (curr.metric || 0) + acc, 0) };
121
+ } catch (err) {
122
+ logger.file('bi/cluster/error', { error: err.toString(), query });
123
+ return { error: err.toString(), status: 500 };
124
+ }
125
+ }