@opengis/bi 1.2.27 → 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.
- package/dist/bi.js +1 -1
- package/dist/bi.umd.cjs +23 -23
- package/dist/{import-file-DN5MrFOd.js → import-file-C1s2X9Kt.js} +539 -575
- package/dist/style.css +1 -1
- package/dist/{vs-funnel-bar-DwVkX7Q2.js → vs-funnel-bar-BuV_pQBq.js} +1 -1
- package/dist/{vs-list-BWyOIZbY.js → vs-list-uhEudqNr.js} +1 -1
- package/dist/{vs-map-cluster-ltvourI_.js → vs-map-cluster-CeJxLtZx.js} +2 -2
- package/dist/{vs-map-BeYJ2aj0.js → vs-map-qs7P1yfP.js} +2 -2
- package/dist/{vs-number-BvAcFzRT.js → vs-number-BNnFUzs3.js} +1 -1
- package/dist/{vs-table-CBFIS-pF.js → vs-table-BEvWIkoj.js} +1 -1
- package/dist/{vs-text-GIhpwEL4.js → vs-text-CPMz7TWB.js} +1 -1
- package/package.json +75 -75
- package/server/routes/dashboard/controllers/dashboard.list.js +36 -16
- package/server/routes/dashboard/controllers/utils/yaml.js +11 -11
- package/server/routes/edit/controllers/widget.edit.js +22 -13
- package/server/routes/map/controllers/cluster.js +125 -125
- package/server/routes/map/controllers/clusterVtile.js +166 -166
- package/server/routes/map/controllers/geojson.js +127 -127
- package/server/routes/map/controllers/map.js +69 -69
- package/server/routes/map/controllers/utils/downloadClusterData.js +44 -44
- package/server/routes/map/controllers/vtile.js +183 -183
- package/utils.js +12 -12
|
@@ -1,127 +1,127 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { createHash } from 'crypto';
|
|
3
|
-
import { writeFile, mkdir, readFile, stat } from 'fs/promises';
|
|
4
|
-
import { existsSync, /* readdirSync, */ readFileSync } from 'fs';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { getFolder, getFilterSQL, autoIndex, logger, pgClients } from '@opengis/fastify-table/utils.js';
|
|
8
|
-
|
|
9
|
-
import normalizeData from '../../data/controllers/util/normalizeData.js';
|
|
10
|
-
|
|
11
|
-
import { getWidget } from '../../../../utils.js';
|
|
12
|
-
|
|
13
|
-
const types = {
|
|
14
|
-
point: 'ST_Point' /* ,ST_MultiPoint */,
|
|
15
|
-
polygon: 'ST_Polygon,ST_MultiPolygon',
|
|
16
|
-
};
|
|
17
|
-
const hourMs = 3.6e6;
|
|
18
|
-
|
|
19
|
-
export default async function geojson(req, reply) {
|
|
20
|
-
const { query = {} } = req;
|
|
21
|
-
|
|
22
|
-
const {
|
|
23
|
-
filter,
|
|
24
|
-
widget,
|
|
25
|
-
sql,
|
|
26
|
-
type,
|
|
27
|
-
nocache,
|
|
28
|
-
id,
|
|
29
|
-
dashboard,
|
|
30
|
-
geom = 'geom',
|
|
31
|
-
pointZoom = 0,
|
|
32
|
-
} = query;
|
|
33
|
-
|
|
34
|
-
if (!widget && !dashboard) {
|
|
35
|
-
return { message: 'not enough params: widget', status: 400 };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const data = await getWidget({ pg: req.pg, dashboard, widget });
|
|
39
|
-
if (data.status) return data;
|
|
40
|
-
|
|
41
|
-
const pg = data.pg || req.pg || pgClients.client;
|
|
42
|
-
const hash = [pointZoom, filter].filter((el) => el).join();
|
|
43
|
-
|
|
44
|
-
const root = getFolder(req);
|
|
45
|
-
const file = path.join(
|
|
46
|
-
root,
|
|
47
|
-
`/map/geojson/${widget}/${hash ? `${createHash('sha1').update(hash).digest('base64')}/` : ''}.geojson`
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
if (existsSync(file)) {
|
|
51
|
-
const timeNow = Date.now();
|
|
52
|
-
const stats = await stat(file);
|
|
53
|
-
const birthTime = new Date(stats.birthtime).getTime();
|
|
54
|
-
if (!(birthTime - timeNow > hourMs * 24) && !nocache) {
|
|
55
|
-
const res = JSON.parse((await readFile(file, 'utf-8')) || {});
|
|
56
|
-
return res;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
try {
|
|
61
|
-
const pkey = pg.pk?.[data?.table];
|
|
62
|
-
if (!pkey) {
|
|
63
|
-
return {
|
|
64
|
-
message: `invalid ${widget ? 'widget' : 'dashboard'}: table pk not found (${data?.table})`,
|
|
65
|
-
status: 400,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// data param
|
|
70
|
-
const { table, where = '1=1', xName, x } = normalizeData(data, query);
|
|
71
|
-
|
|
72
|
-
if (!xName && !x) {
|
|
73
|
-
return {
|
|
74
|
-
message: `invalid ${widget ? 'widget' : 'dashboard'}: x axis column not specified`,
|
|
75
|
-
status: 400,
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// get sql
|
|
80
|
-
const filterQ = filter
|
|
81
|
-
? await getFilterSQL({ pg, table, filter, query })
|
|
82
|
-
: undefined;
|
|
83
|
-
const q = `select "${pkey}", "${xName || x}", /* st_asgeojson(geom)::json as */ ${geom} as geom from ${filterQ ? `(${filterQ})` : table} q where ${where}`;
|
|
84
|
-
|
|
85
|
-
if (sql === '1') return q;
|
|
86
|
-
|
|
87
|
-
const { st_geometrytype: geomType = 'point' } = await pg
|
|
88
|
-
.query(
|
|
89
|
-
`select st_geometrytype(${geom}), count(*) from ${table}
|
|
90
|
-
where ${where} group by st_geometrytype(${geom})`
|
|
91
|
-
)
|
|
92
|
-
.then((res) => res.rows?.[0] || {});
|
|
93
|
-
|
|
94
|
-
const q1 = `SELECT 'FeatureCollection' As type, json_agg(f) As features FROM (
|
|
95
|
-
SELECT 'Feature' As type, row_number() over() as id,
|
|
96
|
-
st_asgeojson(st_force2d(${query.srid
|
|
97
|
-
? `st_transform(${type === 'centroid' ? `st_centroid(${geom})` : geom},${query.srid})`
|
|
98
|
-
: `${type === 'centroid' || query.point || query.centroid ? `st_centroid(${geom})` : geom}`
|
|
99
|
-
}), 6, 0)::json as geometry,
|
|
100
|
-
(select row_to_json(tc) from (select ${'' ? `${''} as status, ` : ''}
|
|
101
|
-
${xName ? `${xName},` : ''}
|
|
102
|
-
${data.style?.colorAttr ? `${data.style.colorAttr},` : ''}
|
|
103
|
-
${pkey} as id,(select file_path from crm.files
|
|
104
|
-
where entity_id=q.${pkey}::text and file_status <>'3' and ext in ('png','jpg') limit 1) as image
|
|
105
|
-
)tc) as properties
|
|
106
|
-
from (${q})q where ${id && pkey ? ` ${pkey} = '${id}' and ` : ''} ${geom} is not null
|
|
107
|
-
${data.query ? ` and ${data.query}` : ''}
|
|
108
|
-
${query.extent ? `and ${geom} && 'BOX(${query.extent.split(',').reduce((p, el, i) => p + el + (i % 2 ? ',' : ' '), '')})'::box2d` : ''}
|
|
109
|
-
${types[type] ? ` and ST_GeometryType(${geom}) = any ('{ ${types[type]} }') ` : ''}
|
|
110
|
-
limit ${geomType?.toLowerCase()?.includes('point') ? '15000' : '2500'})f`;
|
|
111
|
-
|
|
112
|
-
if (sql === '2') return q1;
|
|
113
|
-
|
|
114
|
-
// auto Index
|
|
115
|
-
autoIndex({ table, columns: [xName] });
|
|
116
|
-
|
|
117
|
-
const res = await pg.query(q1).then((res) => res.rows?.[0] || {});
|
|
118
|
-
|
|
119
|
-
await mkdir(path.dirname(file), { recursive: true });
|
|
120
|
-
await writeFile(file, JSON.stringify(res));
|
|
121
|
-
|
|
122
|
-
return res;
|
|
123
|
-
} catch (err) {
|
|
124
|
-
logger.file('bi/geojson', { level: 'ERROR', error: err.toString(), query });
|
|
125
|
-
return { error: err.toString(), status: 500 };
|
|
126
|
-
}
|
|
127
|
-
}
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { createHash } from 'crypto';
|
|
3
|
+
import { writeFile, mkdir, readFile, stat } from 'fs/promises';
|
|
4
|
+
import { existsSync, /* readdirSync, */ readFileSync } from 'fs';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
import { getFolder, getFilterSQL, autoIndex, logger, pgClients } from '@opengis/fastify-table/utils.js';
|
|
8
|
+
|
|
9
|
+
import normalizeData from '../../data/controllers/util/normalizeData.js';
|
|
10
|
+
|
|
11
|
+
import { getWidget } from '../../../../utils.js';
|
|
12
|
+
|
|
13
|
+
const types = {
|
|
14
|
+
point: 'ST_Point' /* ,ST_MultiPoint */,
|
|
15
|
+
polygon: 'ST_Polygon,ST_MultiPolygon',
|
|
16
|
+
};
|
|
17
|
+
const hourMs = 3.6e6;
|
|
18
|
+
|
|
19
|
+
export default async function geojson(req, reply) {
|
|
20
|
+
const { query = {} } = req;
|
|
21
|
+
|
|
22
|
+
const {
|
|
23
|
+
filter,
|
|
24
|
+
widget,
|
|
25
|
+
sql,
|
|
26
|
+
type,
|
|
27
|
+
nocache,
|
|
28
|
+
id,
|
|
29
|
+
dashboard,
|
|
30
|
+
geom = 'geom',
|
|
31
|
+
pointZoom = 0,
|
|
32
|
+
} = query;
|
|
33
|
+
|
|
34
|
+
if (!widget && !dashboard) {
|
|
35
|
+
return { message: 'not enough params: widget', status: 400 };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const data = await getWidget({ pg: req.pg, dashboard, widget });
|
|
39
|
+
if (data.status) return data;
|
|
40
|
+
|
|
41
|
+
const pg = data.pg || req.pg || pgClients.client;
|
|
42
|
+
const hash = [pointZoom, filter].filter((el) => el).join();
|
|
43
|
+
|
|
44
|
+
const root = getFolder(req);
|
|
45
|
+
const file = path.join(
|
|
46
|
+
root,
|
|
47
|
+
`/map/geojson/${widget}/${hash ? `${createHash('sha1').update(hash).digest('base64')}/` : ''}.geojson`
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
if (existsSync(file)) {
|
|
51
|
+
const timeNow = Date.now();
|
|
52
|
+
const stats = await stat(file);
|
|
53
|
+
const birthTime = new Date(stats.birthtime).getTime();
|
|
54
|
+
if (!(birthTime - timeNow > hourMs * 24) && !nocache) {
|
|
55
|
+
const res = JSON.parse((await readFile(file, 'utf-8')) || {});
|
|
56
|
+
return res;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const pkey = pg.pk?.[data?.table];
|
|
62
|
+
if (!pkey) {
|
|
63
|
+
return {
|
|
64
|
+
message: `invalid ${widget ? 'widget' : 'dashboard'}: table pk not found (${data?.table})`,
|
|
65
|
+
status: 400,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// data param
|
|
70
|
+
const { table, where = '1=1', xName, x } = normalizeData(data, query);
|
|
71
|
+
|
|
72
|
+
if (!xName && !x) {
|
|
73
|
+
return {
|
|
74
|
+
message: `invalid ${widget ? 'widget' : 'dashboard'}: x axis column not specified`,
|
|
75
|
+
status: 400,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// get sql
|
|
80
|
+
const filterQ = filter
|
|
81
|
+
? await getFilterSQL({ pg, table, filter, query })
|
|
82
|
+
: undefined;
|
|
83
|
+
const q = `select "${pkey}", "${xName || x}", /* st_asgeojson(geom)::json as */ ${geom} as geom from ${filterQ ? `(${filterQ})` : table} q where ${where}`;
|
|
84
|
+
|
|
85
|
+
if (sql === '1') return q;
|
|
86
|
+
|
|
87
|
+
const { st_geometrytype: geomType = 'point' } = await pg
|
|
88
|
+
.query(
|
|
89
|
+
`select st_geometrytype(${geom}), count(*) from ${table}
|
|
90
|
+
where ${where} group by st_geometrytype(${geom})`
|
|
91
|
+
)
|
|
92
|
+
.then((res) => res.rows?.[0] || {});
|
|
93
|
+
|
|
94
|
+
const q1 = `SELECT 'FeatureCollection' As type, json_agg(f) As features FROM (
|
|
95
|
+
SELECT 'Feature' As type, row_number() over() as id,
|
|
96
|
+
st_asgeojson(st_force2d(${query.srid
|
|
97
|
+
? `st_transform(${type === 'centroid' ? `st_centroid(${geom})` : geom},${query.srid})`
|
|
98
|
+
: `${type === 'centroid' || query.point || query.centroid ? `st_centroid(${geom})` : geom}`
|
|
99
|
+
}), 6, 0)::json as geometry,
|
|
100
|
+
(select row_to_json(tc) from (select ${'' ? `${''} as status, ` : ''}
|
|
101
|
+
${xName ? `${xName},` : ''}
|
|
102
|
+
${data.style?.colorAttr ? `${data.style.colorAttr},` : ''}
|
|
103
|
+
${pkey} as id,(select file_path from crm.files
|
|
104
|
+
where entity_id=q.${pkey}::text and file_status <>'3' and ext in ('png','jpg') limit 1) as image
|
|
105
|
+
)tc) as properties
|
|
106
|
+
from (${q})q where ${id && pkey ? ` ${pkey} = '${id}' and ` : ''} ${geom} is not null
|
|
107
|
+
${data.query ? ` and ${data.query}` : ''}
|
|
108
|
+
${query.extent ? `and ${geom} && 'BOX(${query.extent.split(',').reduce((p, el, i) => p + el + (i % 2 ? ',' : ' '), '')})'::box2d` : ''}
|
|
109
|
+
${types[type] ? ` and ST_GeometryType(${geom}) = any ('{ ${types[type]} }') ` : ''}
|
|
110
|
+
limit ${geomType?.toLowerCase()?.includes('point') ? '15000' : '2500'})f`;
|
|
111
|
+
|
|
112
|
+
if (sql === '2') return q1;
|
|
113
|
+
|
|
114
|
+
// auto Index
|
|
115
|
+
autoIndex({ table, columns: [xName] });
|
|
116
|
+
|
|
117
|
+
const res = await pg.query(q1).then((res) => res.rows?.[0] || {});
|
|
118
|
+
|
|
119
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
120
|
+
await writeFile(file, JSON.stringify(res));
|
|
121
|
+
|
|
122
|
+
return res;
|
|
123
|
+
} catch (err) {
|
|
124
|
+
logger.file('bi/geojson', { level: 'ERROR', error: err.toString(), query });
|
|
125
|
+
return { error: err.toString(), status: 500 };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
import { pgClients, getFilterSQL, getSelectVal } from '@opengis/fastify-table/utils.js';
|
|
2
|
-
|
|
3
|
-
import { getWidget } from '../../../../utils.js';
|
|
4
|
-
|
|
5
|
-
export default async function map(req) {
|
|
6
|
-
const { query = {} } = req;
|
|
7
|
-
const { dashboard, widget } = query;
|
|
8
|
-
|
|
9
|
-
const { pg = req.pg || pgClients.client, data, type, layers, style, controls } = await getWidget({ pg: req.pg, dashboard, widget });
|
|
10
|
-
|
|
11
|
-
if (!['map'].includes(type)) {
|
|
12
|
-
return { message: 'access restricted: invalid widget type', status: 403 };
|
|
13
|
-
}
|
|
14
|
-
if (!data?.table) {
|
|
15
|
-
return { message: 'invalid widget: param table is required', status: 400 };
|
|
16
|
-
}
|
|
17
|
-
if (!pg.pk[data?.table]) {
|
|
18
|
-
return { message: 'invalid widget: table pkey not found', status: 400 };
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const { q = '' } = await getFilterSQL({
|
|
22
|
-
pg,
|
|
23
|
-
table: data?.table,
|
|
24
|
-
filter: query.filter,
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
const res = {};
|
|
28
|
-
if (data?.color) {
|
|
29
|
-
const { rows = [] } = await pg.query(
|
|
30
|
-
`select count(*), "${data.color}" as val from ${data.table} where ${data.query || '1=1'} group by "${data.color}"`
|
|
31
|
-
);
|
|
32
|
-
if (data?.cls) {
|
|
33
|
-
const vals = await getSelectVal({
|
|
34
|
-
pg, name: data.cls, values: rows.map(el => el.val), ar: true,
|
|
35
|
-
});
|
|
36
|
-
rows.forEach(row => Object.assign(row, { ...vals?.find?.(el => el.id === row.val) || { text: row.val } }));
|
|
37
|
-
}
|
|
38
|
-
Object.assign(res, { colors: rows }); // кольори для легенди
|
|
39
|
-
}
|
|
40
|
-
if (data?.metrics?.length) {
|
|
41
|
-
const metric = data?.metrics[0];
|
|
42
|
-
const q1 = `select PERCENTILE_CONT(0) WITHIN GROUP (ORDER BY "${metric}") as "0",
|
|
43
|
-
PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY "${metric}") as "25",
|
|
44
|
-
PERCENTILE_CONT(0.50) WITHIN GROUP (ORDER BY "${metric}") as "50",
|
|
45
|
-
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY "${metric}") as "75",
|
|
46
|
-
PERCENTILE_CONT(1) WITHIN GROUP (ORDER BY "${metric}") as "100" from ${data.table} where ${data.query || '1=1'} and ${q || '1=1'}`;
|
|
47
|
-
const sizes = await pg
|
|
48
|
-
.query(q1)
|
|
49
|
-
.then(el => Object.values(el.rows?.[0] || {}));
|
|
50
|
-
Object.assign(res, { sizes }); // розміри для легенди
|
|
51
|
-
}
|
|
52
|
-
const { bounds, extentStr } = await pg.query(`select count(*),
|
|
53
|
-
st_asgeojson(st_extent(geom))::json as bounds,
|
|
54
|
-
replace(regexp_replace(st_extent(geom)::box2d::text,'BOX\\(|\\)','','g'),' ',',') as "extentStr"
|
|
55
|
-
from ${data.table} where ${data.query || '1=1'}`).then(el => el.rows?.[0] || {});
|
|
56
|
-
const extent = extentStr ? extentStr.split(',') : undefined;
|
|
57
|
-
|
|
58
|
-
Object.assign(res, {
|
|
59
|
-
layers,
|
|
60
|
-
style,
|
|
61
|
-
controls,
|
|
62
|
-
columns: data.columns,
|
|
63
|
-
bounds, // Map bounds
|
|
64
|
-
extent,
|
|
65
|
-
top: [], // 10 найкращих
|
|
66
|
-
bottom: [], // 10 найгірших
|
|
67
|
-
});
|
|
68
|
-
return res;
|
|
69
|
-
}
|
|
1
|
+
import { pgClients, getFilterSQL, getSelectVal } from '@opengis/fastify-table/utils.js';
|
|
2
|
+
|
|
3
|
+
import { getWidget } from '../../../../utils.js';
|
|
4
|
+
|
|
5
|
+
export default async function map(req) {
|
|
6
|
+
const { query = {} } = req;
|
|
7
|
+
const { dashboard, widget } = query;
|
|
8
|
+
|
|
9
|
+
const { pg = req.pg || pgClients.client, data, type, layers, style, controls } = await getWidget({ pg: req.pg, dashboard, widget });
|
|
10
|
+
|
|
11
|
+
if (!['map'].includes(type)) {
|
|
12
|
+
return { message: 'access restricted: invalid widget type', status: 403 };
|
|
13
|
+
}
|
|
14
|
+
if (!data?.table) {
|
|
15
|
+
return { message: 'invalid widget: param table is required', status: 400 };
|
|
16
|
+
}
|
|
17
|
+
if (!pg.pk[data?.table]) {
|
|
18
|
+
return { message: 'invalid widget: table pkey not found', status: 400 };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const { q = '' } = await getFilterSQL({
|
|
22
|
+
pg,
|
|
23
|
+
table: data?.table,
|
|
24
|
+
filter: query.filter,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const res = {};
|
|
28
|
+
if (data?.color) {
|
|
29
|
+
const { rows = [] } = await pg.query(
|
|
30
|
+
`select count(*), "${data.color}" as val from ${data.table} where ${data.query || '1=1'} group by "${data.color}"`
|
|
31
|
+
);
|
|
32
|
+
if (data?.cls) {
|
|
33
|
+
const vals = await getSelectVal({
|
|
34
|
+
pg, name: data.cls, values: rows.map(el => el.val), ar: true,
|
|
35
|
+
});
|
|
36
|
+
rows.forEach(row => Object.assign(row, { ...vals?.find?.(el => el.id === row.val) || { text: row.val } }));
|
|
37
|
+
}
|
|
38
|
+
Object.assign(res, { colors: rows }); // кольори для легенди
|
|
39
|
+
}
|
|
40
|
+
if (data?.metrics?.length) {
|
|
41
|
+
const metric = data?.metrics[0];
|
|
42
|
+
const q1 = `select PERCENTILE_CONT(0) WITHIN GROUP (ORDER BY "${metric}") as "0",
|
|
43
|
+
PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY "${metric}") as "25",
|
|
44
|
+
PERCENTILE_CONT(0.50) WITHIN GROUP (ORDER BY "${metric}") as "50",
|
|
45
|
+
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY "${metric}") as "75",
|
|
46
|
+
PERCENTILE_CONT(1) WITHIN GROUP (ORDER BY "${metric}") as "100" from ${data.table} where ${data.query || '1=1'} and ${q || '1=1'}`;
|
|
47
|
+
const sizes = await pg
|
|
48
|
+
.query(q1)
|
|
49
|
+
.then(el => Object.values(el.rows?.[0] || {}));
|
|
50
|
+
Object.assign(res, { sizes }); // розміри для легенди
|
|
51
|
+
}
|
|
52
|
+
const { bounds, extentStr } = await pg.query(`select count(*),
|
|
53
|
+
st_asgeojson(st_extent(geom))::json as bounds,
|
|
54
|
+
replace(regexp_replace(st_extent(geom)::box2d::text,'BOX\\(|\\)','','g'),' ',',') as "extentStr"
|
|
55
|
+
from ${data.table} where ${data.query || '1=1'}`).then(el => el.rows?.[0] || {});
|
|
56
|
+
const extent = extentStr ? extentStr.split(',') : undefined;
|
|
57
|
+
|
|
58
|
+
Object.assign(res, {
|
|
59
|
+
layers,
|
|
60
|
+
style,
|
|
61
|
+
controls,
|
|
62
|
+
columns: data.columns,
|
|
63
|
+
bounds, // Map bounds
|
|
64
|
+
extent,
|
|
65
|
+
top: [], // 10 найкращих
|
|
66
|
+
bottom: [], // 10 найгірших
|
|
67
|
+
});
|
|
68
|
+
return res;
|
|
69
|
+
}
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import { config, logger, pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
-
|
|
3
|
-
export default async function downloadClusterData({ pg = pgClients.client, cluster }) {
|
|
4
|
-
if (!pg || !cluster) return null;
|
|
5
|
-
const res = await fetch(`https://cdn.softpro.ua/data/bi/${cluster}-ua.geojson`);
|
|
6
|
-
if (res?.status !== 200) {
|
|
7
|
-
return {
|
|
8
|
-
message: `cluster file not found: ${cluster}-ua.json`,
|
|
9
|
-
status: 404,
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
try {
|
|
13
|
-
const geojson = await res.json();
|
|
14
|
-
const features = geojson?.features?.filter(
|
|
15
|
-
(el, idx, arr) => el?.geometry &&
|
|
16
|
-
arr.map((item) => item.properties.name)
|
|
17
|
-
.indexOf(el.properties.name) === idx
|
|
18
|
-
); // unique
|
|
19
|
-
if (!features?.length) {
|
|
20
|
-
return {
|
|
21
|
-
message: `cluster file empty: ${cluster}-ua.json`,
|
|
22
|
-
status: 400,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
const { count = 0 } = await pg.query(`select count(*)::int from bi.cluster where type=$1`, [cluster])
|
|
26
|
-
.then((res1) => res1.rows?.[0] || {});
|
|
27
|
-
if (count !== features?.length || config.debug) {
|
|
28
|
-
// await pg.query(`delete from bi.cluster where type=$1`, [cluster]);
|
|
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
|
-
|
|
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
|
-
logger.file('bi/clusterVtile', { cluster, rowCount });
|
|
34
|
-
}
|
|
35
|
-
} catch (err) {
|
|
36
|
-
logger.file('bi/clusterVtile/error', {
|
|
37
|
-
error: err.toString(),
|
|
38
|
-
filename: `${cluster}-ua.json`,
|
|
39
|
-
});
|
|
40
|
-
return {
|
|
41
|
-
message: `cluster file import error: ${cluster}-ua.json`,
|
|
42
|
-
status: 500,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
1
|
+
import { config, logger, pgClients } from '@opengis/fastify-table/utils.js';
|
|
2
|
+
|
|
3
|
+
export default async function downloadClusterData({ pg = pgClients.client, cluster }) {
|
|
4
|
+
if (!pg || !cluster) return null;
|
|
5
|
+
const res = await fetch(`https://cdn.softpro.ua/data/bi/${cluster}-ua.geojson`);
|
|
6
|
+
if (res?.status !== 200) {
|
|
7
|
+
return {
|
|
8
|
+
message: `cluster file not found: ${cluster}-ua.json`,
|
|
9
|
+
status: 404,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
const geojson = await res.json();
|
|
14
|
+
const features = geojson?.features?.filter(
|
|
15
|
+
(el, idx, arr) => el?.geometry &&
|
|
16
|
+
arr.map((item) => item.properties.name)
|
|
17
|
+
.indexOf(el.properties.name) === idx
|
|
18
|
+
); // unique
|
|
19
|
+
if (!features?.length) {
|
|
20
|
+
return {
|
|
21
|
+
message: `cluster file empty: ${cluster}-ua.json`,
|
|
22
|
+
status: 400,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const { count = 0 } = await pg.query(`select count(*)::int from bi.cluster where type=$1`, [cluster])
|
|
26
|
+
.then((res1) => res1.rows?.[0] || {});
|
|
27
|
+
if (count !== features?.length || config.debug) {
|
|
28
|
+
// await pg.query(`delete from bi.cluster where type=$1`, [cluster]);
|
|
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
|
+
|
|
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
|
+
logger.file('bi/clusterVtile', { cluster, rowCount });
|
|
34
|
+
}
|
|
35
|
+
} catch (err) {
|
|
36
|
+
logger.file('bi/clusterVtile/error', {
|
|
37
|
+
error: err.toString(),
|
|
38
|
+
filename: `${cluster}-ua.json`,
|
|
39
|
+
});
|
|
40
|
+
return {
|
|
41
|
+
message: `cluster file import error: ${cluster}-ua.json`,
|
|
42
|
+
status: 500,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
45
|
}
|