@opengis/bi
Version:
BI data visualization module
50 lines (45 loc) • 1.52 kB
JavaScript
function number({ metric, where, table, samples, fx }) {
const sql = `select ${fx || metric} from ${table} where ${where} ${samples ? 'limit 10' : ''}`;
return sql;
}
function table({ columns = [], table, where, samples }) {
const cols = Array.isArray(columns)
? columns.map((el) => `"${(el.name || el).replace(/'/g, "''")}"`).join(',')
: Object.keys(columns).map(key => `"${key.replace(/'/g, "''")}"`).join(',');
return `select ${cols || '*'} from ${table} where ${where} ${samples ? 'limit 10' : 'limit 20'} `;
}
function chart({
metric,
yType, // metric type
where,
table,
x,
groupby,
groupData,
order,
samples,
limit = 100,
xType,
fx, // agg function
}) {
const xCol = x && xType?.includes('[]') ? `unnest(${x})` : x;
const metricData =
groupData
?.filter(el => el.name)
?.map(
(el) =>
`${metric} filter (where '${el.name.toString().replace(/'/g, "''")}'=${yType?.includes('[]') ? `any(${groupby.replace(/'/g, "''")}::text[])` : groupby.replace(/'/g, "''")}) as "${el.name.toString().replace(/'/g, "''")}"`
)
.join(',') || `${fx || metric} as metric`;
const sql = `select ${xCol} ${x && xType?.includes('[]') ? `as ${x}` : ''}, ${metricData}
from ${table}
where ${where}
${xCol ? `group by ${xCol}` : ''}
${order || xCol ? `order by ${order || xCol}` : ''}
${samples ? 'limit 10' : `limit ${limit}`}`;
return sql;
}
function text() {
return undefined;
}
export default { number, chart, table };