@opengis/bi 1.0.47 → 1.0.48

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.47",
3
+ "version": "1.0.48",
4
4
  "description": "BI data visualization module",
5
5
  "main": "dist/bi.js",
6
6
  "browser": "dist/bi.umd.cjs",
@@ -4,6 +4,7 @@ import {
4
4
  config,
5
5
  autoIndex,
6
6
  pgClients,
7
+ getSelect,
7
8
  getSelectVal,
8
9
  getFilterSQL,
9
10
  getMeta,
@@ -43,7 +44,7 @@ export default async function dataAPI(req, reply) {
43
44
  }));
44
45
 
45
46
  // data param
46
- const { x, cls, metric, table, where, tableSQL, groupby, xName, yName, xType, yType, error } =
47
+ const { x, cls, groupbyCls, metric, table, where, tableSQL, groupby, xName, yName, xType, yType, error } =
47
48
  normalizeData(widgetData, query, columnTypes);
48
49
 
49
50
  if (error) { return reply.status(400).send(error); }
@@ -106,7 +107,7 @@ export default async function dataAPI(req, reply) {
106
107
  }
107
108
 
108
109
  const sql = widgetData.sql ? optimizedSQL : (chartSQL[type] || chartSQL.chart)({
109
- where,
110
+ where: config.local && user?.user_type === 'superadmin' ? 'true' : where, // test
110
111
  metric,
111
112
  yType, // metric type
112
113
  columns: widgetData.columns,
@@ -147,6 +148,21 @@ export default async function dataAPI(req, reply) {
147
148
  return { errorSql: err.toString() };
148
149
  }); // test with limit
149
150
 
151
+ if (groupbyCls) {
152
+ const { arr = [] } = await getSelect(groupbyCls, pg) || {};
153
+ if (arr.length) {
154
+ const ids = arr.map(el => el.id);
155
+ const text = arr.reduce((acc, curr) => ({ ...acc, [curr.id]: curr.text }), {});
156
+ rows.forEach(row => {
157
+ ids.reduce((acc, curr) => {
158
+ Object.assign(row, { [text[curr]]: row[curr] });
159
+ delete row[curr];
160
+ return acc;
161
+ }, {});
162
+ });
163
+ }
164
+ }
165
+
150
166
  if (cls) {
151
167
  const values = rows
152
168
  .map((row) => row[x])
@@ -45,7 +45,7 @@ function normalizeData(data, query = {}, columnTypes = []) {
45
45
  const yName = metrics?.[0]?.name || metrics?.[0];
46
46
  const yType = columnTypes.find((el) => el.name == yName)?.type;
47
47
 
48
- const { cls, table, filterCustom } = data;
48
+ const { cls, groupbyCls, table, filterCustom } = data;
49
49
  const groupby = (query.groupby || data.groupby) === 'null' ? null : (query.groupby || data.groupby);
50
50
  // const orderby = query.orderby || data.orderby || 'count(*)';
51
51
 
@@ -60,6 +60,6 @@ function normalizeData(data, query = {}, columnTypes = []) {
60
60
  ? `(select * from ${data?.table} t ${data.tableSQL || ''} where ${where})q`
61
61
  : undefined;
62
62
 
63
- return { x, cls, metric, table, where, tableSQL, groupby, xName, xType, yName, yType, error: skip.length ? skip.join(',') : undefined };
63
+ return { x, cls, groupbyCls, metric, table, where, tableSQL, groupby, xName, xType, yName, yType, error: skip.length ? skip.join(',') : undefined };
64
64
  }
65
65
  export default normalizeData;
@@ -85,18 +85,22 @@ export default async function cluster(req, reply) {
85
85
  const { columns = [] } = await getMeta({ pg, table });
86
86
  const columnList = columns.map(el => el.name);
87
87
 
88
- if (query.metric && query.metric !== 'count' && !columnList.includes(query.metric)) {
89
- return reply.status(404).send(`metric column not found: ${query.metric}`);
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
+ }
90
93
  }
91
94
 
92
- const metricFunc = (columnList.includes(query.metric) ? `sum(${query.metric.replace(/'/g, "''")})::float` : null)
93
- || ({ count: 'count(*)' }[query.metric])
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
94
98
  || `${clusterTable?.operator || 'sum'}("${metrics[0]}")::float`;
95
99
 
96
- const q = `select b.*, ${metricFunc} as metric
100
+ const q = `select b.*, ${metricFunc} ${multipleMetrics ? '' : 'as metric'}
97
101
  from ${optimizedSQL ? `(${optimizedSQL})` : table} q
98
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
99
- where ${where} group by b.id, b.name, b.title order by ${metricFunc} desc`;
103
+ where ${where} group by b.id, b.name, b.title order by ${multipleMetricsOrder || metricFunc} desc`;
100
104
 
101
105
  if (query.sql === '1') return q;
102
106
 
@@ -6,7 +6,7 @@ export default async function map(req) {
6
6
  const { query = {} } = req;
7
7
  const { dashboard, widget } = query;
8
8
 
9
- const { pg = req.pg || pgClients.client, data, type, layers } = await getWidget({ pg: req.pg, dashboard, widget });
9
+ const { pg = req.pg || pgClients.client, data, type, layers, style, controls } = await getWidget({ pg: req.pg, dashboard, widget });
10
10
 
11
11
  if (!['map'].includes(type)) {
12
12
  return { message: 'access restricted: invalid widget type', status: 403 };
@@ -57,6 +57,8 @@ export default async function map(req) {
57
57
 
58
58
  Object.assign(res, {
59
59
  layers,
60
+ style,
61
+ controls,
60
62
  columns: data.columns,
61
63
  bounds, // Map bounds
62
64
  extent,