@opengis/bi 1.0.4 → 1.0.6

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.4",
3
+ "version": "1.0.6",
4
4
  "description": "BI data visualization module",
5
5
  "main": "dist/bi.js",
6
6
  "browser": "dist/bi.umd.cjs",
@@ -50,7 +50,7 @@ export default async function data({
50
50
  data.type = 'file';
51
51
  const { table } = data?.data || { table: data?.table_name };
52
52
  // console.log(fileData)
53
- const widgets = fileData.filter(el => el[0] !== 'index.yml').map(el => el[1]).map(el => el.data ? ({ name: el.name, type: el.type, text: el.text, style: el.style, data: el.data }) : { text: el });
53
+ const widgets = fileData.filter(el => el[0] !== 'index.yml').map(el => el[1].data ? ({ name: el[0].split('.')[0], type: el[1].type, text: el[1].text, style: el[1].style, data: el[1].data }) : { name: el[0].split('.')[0], text: el[1] });
54
54
 
55
55
  const { fields = [] } = table ? await pg.query(`select * from ${table} limit 1`) : {};
56
56
 
@@ -17,7 +17,7 @@ const biSchema = {
17
17
 
18
18
 
19
19
  export default async function route(fastify) {
20
- fastify.get(`/api/bi-dashboard/:id`, { schema: biSchema }, dashboard);
21
- fastify.get(`/api/bi-dashboard`, dashboardList);
22
- fastify.delete(`/api/bi-dashboard/:id`, dashboardDelete);
20
+ fastify.get(`/bi-dashboard/:id`, { schema: biSchema }, dashboard);
21
+ fastify.get(`/bi-dashboard`, dashboardList);
22
+ fastify.delete(`/bi-dashboard/:id`, dashboardDelete);
23
23
  }
@@ -1,17 +1,12 @@
1
- import path from 'path';
1
+
2
2
  // import getTemplate from '@opengis/fastify-table/table/controllers/utils/getTemplate.js';
3
3
  import autoIndex from '@opengis/fastify-table/pg/funcs/autoIndex.js';
4
4
  import pgClients from '@opengis/fastify-table/pg/pgClients.js';
5
5
  import chartSQL from './util/chartSQL.js';
6
6
  import normalizeData from './util/normalizeData.js';
7
7
 
8
- const cwd = process.cwd();
9
- const dashboardDir = path.join(cwd, 'server/templates/dashboard');
10
- const widgetDir = path.join(cwd, 'server/templates/widget');
11
-
12
- import { existsSync, readdirSync, readFileSync } from 'fs';
13
8
 
14
- import yaml from '../../dashboard/controllers/utils/yaml.js';
9
+ import { getTemplate } from '@opengis/fastify-table/utils.js';
15
10
 
16
11
  export default async function data({
17
12
  pg = pgClients.client, funcs = {}, query = {},
@@ -23,29 +18,32 @@ export default async function data({
23
18
  if (!widget && !dashboard) {
24
19
  return { message: 'not enough params: widget or dashboard required', status: 400 };
25
20
  }
21
+ const dashboardData = dashboard ? await getTemplate('dashboard', dashboard) : null;
26
22
 
27
- const fileList = dashboard && existsSync(path.join(dashboardDir, dashboard)) ? readdirSync(path.join(dashboardDir, dashboard)) : []
28
- const filePath = dashboard ? fileList.filter(el => el.includes(widget)).map(el => path.join(dashboardDir, dashboard, el))[0] : path.join(widgetDir, widget + '.yml')
23
+ const { id, tableName } = !dashboardData && pg.pk['bi.dashboard'] && dashboard ?
24
+ await pg.query(`select dashboard_id as id, table_name as "tableName" from bi.dashboard where $1 in (dashboard_id, name)`, [dashboard]).then((res1) => res1.rows?.[0] || {}) : {};
29
25
 
30
- const { id, tableName } = !existsSync(filePath) ? await pg.query(`select dashboard_id as id, table_name as "tableName" from bi.dashboard where $1 in (dashboard_id, name)`, [dashboard]).then((res1) => res1.rows?.[0] || {}) : {};
31
26
 
32
- if (!id && !existsSync(filePath)) {
33
- return { message: { root: config?.local ? dashboardDir : undefined, error: `not found`, widget, dashboard }, status: 404 };
34
- }
27
+ if (!dashboardData && dashboard && !id) return 'dashboard not found';
35
28
 
36
- const fileData = existsSync(filePath) ? readFileSync(filePath, 'utf-8') : '';
29
+ // return { t: 111, data: dashboardData, dashboard }
37
30
 
38
- const jsonData = filePath?.includes('.yml') ? yaml.loadSafe(fileData) : {};
31
+ const widgetData = dashboard ? dashboardData?.find(el => el[0]?.includes(widget))?.[1] : await getTemplate('widget', widget);
39
32
 
40
- const q = `select *, coalesce(data::jsonb, '{}'::jsonb) || jsonb_build_object('table', table_name) as data from bi.widget where dashboard_id=$1 and name=$2`;
41
- const { type, text, data, options, controls, style } = existsSync(filePath)
42
- ? jsonData
43
- : await pg.query(q, [id || dashboard, widget])
44
- .then((res1) => res1.rows?.[0] || {});
45
33
 
46
- if (type === 'text' || filePath?.includes('.md')) {
34
+ if (dashboardData && !widgetData) {
35
+ return widgetData
36
+ }
37
+
38
+ if (!id && !dashboardData && !widgetData) {
39
+ return { message: { root: config?.local ? dashboardDir : undefined, error: `not found`, widget, dashboard }, status: 404 };
40
+ }
41
+
42
+ const q = `select *, coalesce(data::jsonb, '{}'::jsonb) || jsonb_build_object('table', table_name) as data from bi.widget where dashboard_id=$1 and name=$2`;
43
+ const { type, text, data, controls, style, options } = widgetData || await pg.query(q, [id || dashboard, widget]).then((res1) => res1.rows?.[0] || {});
47
44
 
48
- return fileData;
45
+ if (typeof widgetData === 'string') {
46
+ return widgetData;
49
47
  }
50
48
 
51
49
  if (!data?.table && tableName) {
@@ -60,7 +58,9 @@ export default async function data({
60
58
  const { x, metric, table, where, groupby, xName } = normalizeData(data, query);
61
59
 
62
60
  // auto Index
63
- autoIndex({ table: data.table, columns: [data?.time].concat([xName]).concat([groupby]).filter(el => el) })
61
+ if (pg.pk[data.table]) {
62
+ autoIndex({ table: data.table, columns: [data?.time].concat([xName]).concat([groupby]).filter(el => el) }).catch(err => console.log(err))
63
+ }
64
64
 
65
65
  // get group
66
66
  const groupData = groupby ? await pg.query(`select ${groupby} as name ,count(*) from ${table} group by ${groupby} order by count(*) desc limit 20`).then(el => el.rows) : null;
@@ -19,7 +19,7 @@ export default async function route(fastify, opts) {
19
19
  const prefix = opts?.prefix || config.prefix || '/api';
20
20
  fastify.route({
21
21
  method: 'GET',
22
- url: '/api/bi-data',
22
+ url: '/bi-data',
23
23
  schema: biSchema,
24
24
  handler: data,
25
25
  });
@@ -19,9 +19,9 @@ const biSchema = {
19
19
 
20
20
 
21
21
  export default async function route(fastify) {
22
- fastify.post(`/api/bi-dashboard/:name`, {schema: biSchema}, widgetAdd);
23
- fastify.put(`/api/bi-dashboard/:name/:widget`, {schema: biSchema}, widgetEdit);
24
- fastify.delete(`/api/bi-dashboard/:name/:widget`, {schema: biSchema}, widgetDel);
25
- fastify.post(`/api/bi-dashboard`, {schema: biSchema}, dashboardAdd);
26
- fastify.put(`/api/bi-dashboard/:name`, {schema: biSchema}, dashboardEdit);
22
+ fastify.post(`/bi-dashboard/:name`, { schema: biSchema }, widgetAdd);
23
+ fastify.put(`/bi-dashboard/:name/:widget`, { schema: biSchema }, widgetEdit);
24
+ fastify.delete(`/bi-dashboard/:name/:widget`, { schema: biSchema }, widgetDel);
25
+ fastify.post(`/bi-dashboard`, { schema: biSchema }, dashboardAdd);
26
+ fastify.put(`/bi-dashboard/:name`, { schema: biSchema }, dashboardEdit);
27
27
  }
@@ -10,11 +10,11 @@ data:
10
10
  operator: sum
11
11
  title: sales
12
12
  x: product_line
13
- groupby: product_line
13
+ groupby1: product_line
14
14
  orderby: sales
15
15
 
16
16
  controls:
17
17
  style:
18
18
  donut: true
19
- innerRadius: 30
20
- outerRadius: 70
19
+ innerRadius: 30
20
+ outerRadius: 70