@opengis/bi
Version:
BI data visualization module
45 lines (43 loc) • 2.04 kB
JavaScript
import { config, logger, pgClients } from '@opengis/fastify-table/utils.js';
export default async function downloadClusterData({ pg = pgClients.client, cluster }) {
if (!pg || !cluster) return null;
const res = await fetch(`https://cdn.softpro.ua/data/bi/${cluster}-ua.geojson`);
if (res?.status !== 200) {
return {
message: `cluster file not found: ${cluster}-ua.json`,
status: 404,
};
}
try {
const geojson = await res.json();
const features = geojson?.features?.filter(
(el, idx, arr) => el?.geometry &&
arr.map((item) => item.properties.name)
.indexOf(el.properties.name) === idx
); // unique
if (!features?.length) {
return {
message: `cluster file empty: ${cluster}-ua.json`,
status: 400,
};
}
const { count = 0 } = await pg.query(`select count(*)::int from bi.cluster where type=$1`, [cluster])
.then((res1) => res1.rows?.[0] || {});
if (count !== features?.length || config.debug) {
// await pg.query(`delete from bi.cluster where type=$1`, [cluster]);
const values = features?.map((el) => `('${el.properties.codifier?.replace(/'/g, "''") || ''}','${el.properties.name?.replace(/'/g, "''") || ''}', '${cluster}', ST_GeomFromGeoJSON('${JSON.stringify(el.geometry)}')::geometry)`).join(',');
const { rowCount } = await pg.query(`insert into bi.cluster (codifier,title,type,geom)
values ${values} on conflict(title,type) do update set codifier=excluded.codifier, geom=excluded.geom`);
logger.file('bi/clusterVtile', { cluster, rowCount });
}
} catch (err) {
logger.file('bi/clusterVtile/error', {
error: err.toString(),
filename: `${cluster}-ua.json`,
});
return {
message: `cluster file import error: ${cluster}-ua.json`,
status: 500,
};
}
}