UNPKG

@formant/ava

Version:

A framework for automated visual analytics.

87 lines (86 loc) 5.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateInsightsWithVisualizationSpec = exports.extractInsights = void 0; var tslib_1 = require("tslib"); var heap_js_1 = tslib_1.__importDefault(require("heap-js")); var constant_1 = require("../constant"); var aggregate_1 = require("../utils/aggregate"); var extract_1 = require("./extract"); var preprocess_1 = require("./preprocess"); var visualize_1 = require("./visualize"); var util_1 = require("./util"); function extractInsights(sourceData, options) { var _a; // get data columns infomations (column type, statistics, etc.) var data = sourceData.filter(function (item) { return !Object.values(item).some(function (v) { return v === null || v === undefined; }); }); var dataProps = (0, preprocess_1.dataToDataProps)(data, options === null || options === void 0 ? void 0 : options.dataProcessInfo); var fieldPropsMap = dataProps.reduce(function (acc, item) { acc[item.name] = item; return acc; }, {}); var impactMeasureReferences = (0, preprocess_1.calculateImpactMeasureReferenceValues)(data, options === null || options === void 0 ? void 0 : options.impactMeasures); var referenceInfo = { fieldPropsMap: fieldPropsMap, impactMeasureReferences: impactMeasureReferences, }; var measures = (options === null || options === void 0 ? void 0 : options.measures) || dataProps .filter(function (item) { return item.domainType === 'measure'; }) .map(function (item) { return ({ fieldName: item.name, method: 'SUM', }); }); var dimensions = ((_a = options === null || options === void 0 ? void 0 : options.dimensions) === null || _a === void 0 ? void 0 : _a.map(function (dimension) { return dimension.fieldName; })) || dataProps.filter(function (item) { return item.domainType === 'dimension'; }).map(function (item) { return item.name; }); // init insights storage var insightsHeap = new heap_js_1.default(util_1.insightPriorityComparator); var homogeneousInsightsHeap = new heap_js_1.default(util_1.homogeneousInsightPriorityComparator); var insightsLimit = (options === null || options === void 0 ? void 0 : options.limit) || constant_1.INSIGHT_DEFAULT_LIMIT; insightsHeap.limit = insightsLimit; insightsHeap.init([]); homogeneousInsightsHeap.init([]); (0, extract_1.enumerateInsights)(data, dimensions, measures, referenceInfo, insightsHeap, homogeneousInsightsHeap, options); // get top N results var insights = []; var heapSize = insightsHeap.size(); var insightsSize = heapSize > insightsLimit ? insightsLimit : heapSize; for (var i = 0; i < insightsSize; i += 1) { var top_1 = insightsHeap.pop(); insights.push(top_1); } var result = { insights: insights.reverse() }; if (options === null || options === void 0 ? void 0 : options.homogeneous) { var homogeneousInsightsResult = []; var homogeneousHeapSize = homogeneousInsightsHeap.size(); var homogeneousInsightsSize = homogeneousHeapSize > insightsLimit ? insightsLimit : homogeneousHeapSize; for (var i = 0; i < homogeneousInsightsSize; i += 1) { var top_2 = homogeneousInsightsHeap.pop(); homogeneousInsightsResult.push(top_2); } result.homogeneousInsights = homogeneousInsightsResult.reverse(); } return result; } exports.extractInsights = extractInsights; function generateInsightsWithVisualizationSpec(extraction, options) { var insights = extraction.insights, homogeneousInsights = extraction.homogeneousInsights; var insightsWithVis = insights.map(function (item) { return (tslib_1.__assign(tslib_1.__assign({}, item), { visualizationSpecs: (0, visualize_1.generateInsightVisualizationSpec)(item, options === null || options === void 0 ? void 0 : options.visualization) })); }); var result = { insights: insightsWithVis }; if (homogeneousInsights && (options === null || options === void 0 ? void 0 : options.homogeneous)) { var homogeneousInsightsWithVis = homogeneousInsights.map(function (item) { var visualizationSpecs = (0, visualize_1.generateHomogeneousInsightVisualizationSpec)(item, options.visualization); var data = item.data, measures = item.measures, dimensions = item.dimensions; var insight = tslib_1.__assign(tslib_1.__assign({}, item), { visualizationSpecs: visualizationSpecs }); if (measures.length > 1) { insight.data = (0, aggregate_1.aggregateWithMeasures)(data, dimensions[0].fieldName, measures); } else { insight.data = (0, aggregate_1.aggregateWithSeries)(data, dimensions[0].fieldName, measures[0], dimensions[1].fieldName); } return insight; }); result.homogeneousInsights = homogeneousInsightsWithVis; } return result; } exports.generateInsightsWithVisualizationSpec = generateInsightsWithVisualizationSpec;