@opengis/bi 1.0.42 → 1.0.44

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/bi",
3
- "version": "1.0.42",
3
+ "version": "1.0.44",
4
4
  "description": "BI data visualization module",
5
5
  "main": "dist/bi.js",
6
6
  "browser": "dist/bi.umd.cjs",
@@ -93,6 +93,7 @@ alter table bi.cluster alter column cluster_id set default next_id();
93
93
  alter table bi.cluster alter column cluster_id set default next_id();
94
94
  ALTER TABLE bi.cluster ADD COLUMN IF NOT EXISTS title text;
95
95
  ALTER TABLE bi.cluster ADD COLUMN IF NOT EXISTS type text;
96
+ ALTER TABLE bi.cluster ADD COLUMN IF NOT EXISTS codifier text;
96
97
  ALTER TABLE bi.cluster ADD COLUMN IF NOT EXISTS geom geometry(MultiPolygon,4326);
97
98
 
98
99
  ALTER TABLE bi.cluster ADD COLUMN IF NOT EXISTS uid text;
@@ -1,4 +1,4 @@
1
- import { getFilterSQL, logger, pgClients } from '@opengis/fastify-table/utils.js';
1
+ import { getFilterSQL, logger, pgClients, getMeta } from '@opengis/fastify-table/utils.js';
2
2
 
3
3
  import { getWidget } from '../../../../utils.js';
4
4
 
@@ -6,7 +6,7 @@ import downloadClusterData from './utils/downloadClusterData.js';
6
6
 
7
7
  const clusterExists = {};
8
8
 
9
- export default async function cluster(req) {
9
+ export default async function cluster(req, reply) {
10
10
  const { query = {} } = req;
11
11
  const { widget, filter, dashboard, search } = query;
12
12
 
@@ -14,7 +14,7 @@ export default async function cluster(req) {
14
14
  return { message: 'not enough params: widget', status: 400 };
15
15
  }
16
16
 
17
- const { pg = req.pg || pgClients.client, data } = await getWidget({ pg: req.pg, dashboard, widget });
17
+ const { pg = req.pg || pgClients.client, data, style, controls } = await getWidget({ pg: req.pg, dashboard, widget });
18
18
 
19
19
  const pkey = pg.pk?.[data?.table];
20
20
 
@@ -82,10 +82,21 @@ export default async function cluster(req) {
82
82
  ? await getFilterSQL({ pg, table, filter, search })
83
83
  : {};
84
84
 
85
- const q = `select b.*, ${clusterTable?.operator || 'sum'}("${metrics[0]}")::float as metric
85
+ const { columns = [] } = await getMeta({ pg, table });
86
+ const columnList = columns.map(el => el.name);
87
+
88
+ if (query.metric && query.metric !== 'count' && !columnList.includes(query.metric)) {
89
+ return reply.status(404).send(`metric column not found: ${query.metric}`);
90
+ }
91
+
92
+ const metricFunc = (columnList.includes(query.metric) ? `sum(${query.metric.replace(/'/g, "''")})::float` : null)
93
+ || ({ count: 'count(*)' }[query.metric])
94
+ || `${clusterTable?.operator || 'sum'}("${metrics[0]}")::float`;
95
+
96
+ const q = `select b.*, ${metricFunc} as metric
86
97
  from ${optimizedSQL ? `(${optimizedSQL})` : table} q
87
98
  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
88
- where ${where} group by b.id, b.name, b.title order by ${clusterTable?.operator || 'sum'}("${metrics[0]}")::float desc`;
99
+ where ${where} group by b.id, b.name, b.title order by ${metricFunc} desc`;
89
100
 
90
101
  if (query.sql === '1') return q;
91
102
 
@@ -102,7 +113,7 @@ export default async function cluster(req) {
102
113
  vals[Math.floor(len * 0.75)],
103
114
  vals[len - 1],
104
115
  ];
105
- return { sizes, rows, bounds, extent, count: rows.length, total: rows?.reduce((acc, curr) => (curr.metric || 0) + acc, 0) };
116
+ return { sizes, style, controls, rows, bounds, extent, count: rows.length, total: rows?.reduce((acc, curr) => (curr.metric || 0) + acc, 0) };
106
117
  } catch (err) {
107
118
  logger.file('bi/cluster/error', { error: err.toString(), query });
108
119
  return { error: err.toString(), status: 500 };
@@ -1,4 +1,4 @@
1
- import { logger, pgClients } from '@opengis/fastify-table/utils.js';
1
+ import { config, logger, pgClients } from '@opengis/fastify-table/utils.js';
2
2
 
3
3
  export default async function downloadClusterData({ pg = pgClients.client, cluster }) {
4
4
  if (!pg || !cluster) return null;
@@ -24,12 +24,12 @@ export default async function downloadClusterData({ pg = pgClients.client, clust
24
24
  }
25
25
  const { count = 0 } = await pg.query(`select count(*)::int from bi.cluster where type=$1`, [cluster])
26
26
  .then((res1) => res1.rows?.[0] || {});
27
- if (count !== features?.length) {
27
+ if (count !== features?.length || config.debug) {
28
28
  // await pg.query(`delete from bi.cluster where type=$1`, [cluster]);
29
- const values = features?.map((el) => `('${el.properties.name?.replace(/'/g, "''") || ''}', '${cluster}', ST_GeomFromGeoJSON('${JSON.stringify(el.geometry)}')::geometry)`).join(',');
29
+ const values = features?.map((el) => `('${el.properties.codifier?.replace(/'/g, "''") || ''}','${el.properties.name?.replace(/'/g, "''") || ''}', '${cluster}', ST_GeomFromGeoJSON('${JSON.stringify(el.geometry)}')::geometry)`).join(',');
30
30
 
31
- const { rowCount } = await pg.query(`insert into bi.cluster (title,type,geom)
32
- values ${values} on conflict(title,type) do update set geom=excluded.geom`);
31
+ const { rowCount } = await pg.query(`insert into bi.cluster (codifier,title,type,geom)
32
+ values ${values} on conflict(title,type) do update set codifier=excluded.codifier, geom=excluded.geom`);
33
33
  logger.file('bi/clusterVtile', { cluster, rowCount });
34
34
  }
35
35
  } catch (err) {