@opengis/bi 1.0.43 → 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,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
|
|
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 ${
|
|
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 };
|