2016-03-19 00:22:02 +08:00
|
|
|
var _ = require('underscore');
|
2016-03-22 20:10:42 +08:00
|
|
|
var BaseWidget = require('./base');
|
2016-03-19 00:22:02 +08:00
|
|
|
var debug = require('debug')('windshaft:dataview:histogram');
|
|
|
|
|
|
|
|
var dot = require('dot');
|
|
|
|
dot.templateSettings.strip = false;
|
|
|
|
|
2017-08-01 23:15:45 +08:00
|
|
|
var columnCastTpl = dot.template("date_part('epoch', {{=it.column}})");
|
|
|
|
|
2017-09-05 00:42:30 +08:00
|
|
|
const dateIntervalQueryTpl = ctx => `
|
|
|
|
WITH
|
|
|
|
__cdb_dates AS (
|
|
|
|
SELECT
|
2017-09-05 17:26:27 +08:00
|
|
|
MAX(${ctx.column}::timestamp) AS __cdb_end,
|
2017-09-05 00:42:30 +08:00
|
|
|
MIN(${ctx.column}::timestamp) AS __cdb_start
|
|
|
|
FROM (${ctx.query}) __cdb_source
|
|
|
|
),
|
|
|
|
__cdb_interval_in_days AS (
|
|
|
|
SELECT
|
2017-09-05 17:26:27 +08:00
|
|
|
DATE_PART('day', __cdb_end - __cdb_start) AS __cdb_days
|
2017-09-05 00:42:30 +08:00
|
|
|
FROM __cdb_dates
|
|
|
|
),
|
|
|
|
__cdb_interval_in_hours AS (
|
|
|
|
SELECT
|
2017-09-05 17:26:27 +08:00
|
|
|
__cdb_days * 24 + DATE_PART('hour', __cdb_end - __cdb_start) AS __cdb_hours
|
|
|
|
FROM __cdb_interval_in_days, __cdb_dates
|
2017-09-05 00:42:30 +08:00
|
|
|
),
|
|
|
|
__cdb_interval_in_minutes AS (
|
|
|
|
SELECT
|
2017-09-05 17:26:27 +08:00
|
|
|
__cdb_hours * 60 + DATE_PART('minute', __cdb_end - __cdb_start) AS __cdb_minutes
|
|
|
|
FROM __cdb_interval_in_hours, __cdb_dates
|
2017-09-05 00:42:30 +08:00
|
|
|
),
|
|
|
|
__cdb_interval_in_seconds AS (
|
|
|
|
SELECT
|
2017-09-05 17:26:27 +08:00
|
|
|
__cdb_minutes * 60 + DATE_PART('second', __cdb_end - __cdb_start) AS __cdb_seconds
|
|
|
|
FROM __cdb_interval_in_minutes, __cdb_dates
|
|
|
|
)
|
2017-09-05 00:42:30 +08:00
|
|
|
SELECT
|
2017-09-05 17:26:27 +08:00
|
|
|
ROUND(__cdb_days / 365) AS year,
|
|
|
|
ROUND(__cdb_days / 90) AS quarter,
|
|
|
|
ROUND(__cdb_days / 30) AS month,
|
|
|
|
ROUND(__cdb_days / 7) AS week,
|
|
|
|
__cdb_days AS day,
|
|
|
|
__cdb_hours AS hour,
|
|
|
|
__cdb_minutes AS minute,
|
2017-09-05 00:42:30 +08:00
|
|
|
__cdb_seconds AS second
|
2017-09-05 17:26:27 +08:00
|
|
|
FROM __cdb_interval_in_days, __cdb_interval_in_hours, __cdb_interval_in_minutes, __cdb_interval_in_seconds
|
2017-09-05 00:42:30 +08:00
|
|
|
`;
|
2017-06-29 01:58:45 +08:00
|
|
|
|
|
|
|
var MAX_INTERVAL_VALUE = 366;
|
2016-03-19 00:22:02 +08:00
|
|
|
var BIN_MIN_NUMBER = 6;
|
|
|
|
var BIN_MAX_NUMBER = 48;
|
|
|
|
|
2017-09-05 16:36:18 +08:00
|
|
|
const filterOutSpecialNumericValues = ctx => `
|
|
|
|
${ctx._column} != 'infinity'::float
|
|
|
|
AND
|
|
|
|
${ctx._column} != '-infinity'::float
|
|
|
|
AND
|
|
|
|
${ctx._column} != 'NaN'::float
|
|
|
|
`;
|
|
|
|
|
2017-09-05 01:01:58 +08:00
|
|
|
const filteredQueryTpl = ctx => `
|
|
|
|
__cdb_filtered_source AS (
|
|
|
|
SELECT *
|
|
|
|
FROM (${ctx._query}) __cdb_filtered_source_query
|
2017-09-05 16:36:18 +08:00
|
|
|
WHERE ${ctx._column} IS NOT NULL
|
|
|
|
${ctx._isFloatColumn ? `AND ${filterOutSpecialNumericValues(ctx)}` : ''}
|
2017-09-05 01:01:58 +08:00
|
|
|
)
|
2017-09-05 16:36:18 +08:00
|
|
|
`;
|
|
|
|
|
2017-09-05 16:41:48 +08:00
|
|
|
const basicsQueryTpl = ctx => `
|
|
|
|
__cdb_basics AS (
|
|
|
|
SELECT
|
|
|
|
max(${ctx._column}) AS __cdb_max_val, min(${ctx._column}) AS __cdb_min_val,
|
|
|
|
avg(${ctx._column}) AS __cdb_avg_val, count(1) AS __cdb_total_rows
|
|
|
|
FROM __cdb_filtered_source
|
|
|
|
)
|
|
|
|
`;
|
2016-03-19 00:22:02 +08:00
|
|
|
|
2017-09-05 17:26:38 +08:00
|
|
|
const overrideBasicsQueryTpl = ctx => `
|
|
|
|
__cdb_basics AS (
|
|
|
|
SELECT
|
|
|
|
max(${ctx._end}) AS __cdb_max_val, min(${ctx._start}) AS __cdb_min_val,
|
|
|
|
avg(${ctx._column}) AS __cdb_avg_val, count(1) AS __cdb_total_rows
|
|
|
|
FROM __cdb_filtered_source
|
|
|
|
)
|
|
|
|
`;
|
2016-03-19 00:22:02 +08:00
|
|
|
|
|
|
|
var iqrQueryTpl = dot.template([
|
2017-08-08 16:03:40 +08:00
|
|
|
'__cdb_iqrange AS (',
|
|
|
|
' SELECT max(quartile_max) - min(quartile_max) AS __cdb_iqr',
|
2016-03-19 00:22:02 +08:00
|
|
|
' FROM (',
|
|
|
|
' SELECT quartile, max(_cdb_iqr_column) AS quartile_max from (',
|
|
|
|
' SELECT {{=it._column}} AS _cdb_iqr_column, ntile(4) over (order by {{=it._column}}',
|
|
|
|
' ) AS quartile',
|
2017-08-08 15:36:23 +08:00
|
|
|
' FROM __cdb_filtered_source) _cdb_quartiles',
|
2016-03-19 00:22:02 +08:00
|
|
|
' WHERE quartile = 1 or quartile = 3',
|
|
|
|
' GROUP BY quartile',
|
2017-08-08 16:20:36 +08:00
|
|
|
' ) __cdb_iqr',
|
2016-03-19 00:22:02 +08:00
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var binsQueryTpl = dot.template([
|
2017-08-08 15:12:52 +08:00
|
|
|
'__cdb_bins AS (',
|
2017-08-08 16:11:35 +08:00
|
|
|
' SELECT CASE WHEN __cdb_total_rows = 0 OR __cdb_iqr = 0',
|
2016-03-19 00:22:02 +08:00
|
|
|
' THEN 1',
|
|
|
|
' ELSE GREATEST(',
|
2017-08-08 16:11:35 +08:00
|
|
|
' LEAST({{=it._minBins}}, CAST(__cdb_total_rows AS INT)),',
|
2016-03-19 00:22:02 +08:00
|
|
|
' LEAST(',
|
2017-08-08 16:11:35 +08:00
|
|
|
' CAST(((__cdb_max_val - __cdb_min_val) / (2 * __cdb_iqr * power(__cdb_total_rows, 1/3))) AS INT),',
|
2016-03-19 00:22:02 +08:00
|
|
|
' {{=it._maxBins}}',
|
|
|
|
' )',
|
|
|
|
' )',
|
2017-08-08 14:33:26 +08:00
|
|
|
' END AS __cdb_bins_number',
|
2017-08-08 16:03:40 +08:00
|
|
|
' FROM __cdb_basics, __cdb_iqrange, __cdb_filtered_source',
|
2016-03-19 00:22:02 +08:00
|
|
|
' LIMIT 1',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var overrideBinsQueryTpl = dot.template([
|
2017-08-08 15:12:52 +08:00
|
|
|
'__cdb_bins AS (',
|
2017-08-08 14:33:26 +08:00
|
|
|
' SELECT {{=it._bins}} AS __cdb_bins_number',
|
2016-03-19 00:22:02 +08:00
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var nullsQueryTpl = dot.template([
|
2017-08-08 15:32:13 +08:00
|
|
|
'__cdb_nulls AS (',
|
2016-03-19 00:22:02 +08:00
|
|
|
' SELECT',
|
2017-08-08 14:33:26 +08:00
|
|
|
' count(*) AS __cdb_nulls_count',
|
2017-08-08 16:20:36 +08:00
|
|
|
' FROM ({{=it._query}}) __cdb_histogram_nulls',
|
2016-03-19 00:22:02 +08:00
|
|
|
' WHERE {{=it._column}} IS NULL',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
2017-06-08 22:01:41 +08:00
|
|
|
var infinitiesQueryTpl = dot.template([
|
2017-08-08 15:43:31 +08:00
|
|
|
'__cdb_infinities AS (',
|
2017-06-08 22:01:41 +08:00
|
|
|
' SELECT',
|
2017-08-08 15:54:49 +08:00
|
|
|
' count(*) AS __cdb_infinities_count',
|
2017-08-08 15:46:34 +08:00
|
|
|
' FROM ({{=it._query}}) __cdb_infinities_query',
|
2017-06-08 22:01:41 +08:00
|
|
|
' WHERE',
|
|
|
|
' {{=it._column}} = \'infinity\'::float',
|
|
|
|
' OR',
|
|
|
|
' {{=it._column}} = \'-infinity\'::float',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var nansQueryTpl = dot.template([
|
2017-08-08 15:54:49 +08:00
|
|
|
'__cdb_nans AS (',
|
2017-06-08 22:01:41 +08:00
|
|
|
' SELECT',
|
2017-08-08 15:54:49 +08:00
|
|
|
' count(*) AS __cdb_nans_count',
|
2017-08-08 15:46:34 +08:00
|
|
|
' FROM ({{=it._query}}) __cdb_nans_query',
|
2017-06-08 22:01:41 +08:00
|
|
|
' WHERE {{=it._column}} = \'NaN\'::float',
|
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
2016-03-19 00:22:02 +08:00
|
|
|
var histogramQueryTpl = dot.template([
|
|
|
|
'SELECT',
|
2017-08-08 16:11:35 +08:00
|
|
|
' (__cdb_max_val - __cdb_min_val) / cast(__cdb_bins_number as float) AS bin_width,',
|
2017-08-08 14:33:26 +08:00
|
|
|
' __cdb_bins_number AS bins_number,',
|
|
|
|
' __cdb_nulls_count AS nulls_count,',
|
2017-08-08 15:54:49 +08:00
|
|
|
' {{?it._isFloatColumn}}__cdb_infinities_count AS infinities_count,',
|
|
|
|
' __cdb_nans_count AS nans_count,{{?}}',
|
2017-08-08 16:11:35 +08:00
|
|
|
' __cdb_avg_val AS avg_val,',
|
|
|
|
' CASE WHEN __cdb_min_val = __cdb_max_val',
|
2016-03-19 00:22:02 +08:00
|
|
|
' THEN 0',
|
2017-08-08 15:06:36 +08:00
|
|
|
' ELSE GREATEST(',
|
|
|
|
' 1,',
|
|
|
|
' LEAST(',
|
2017-08-08 16:11:35 +08:00
|
|
|
' WIDTH_BUCKET({{=it._column}}, __cdb_min_val, __cdb_max_val, __cdb_bins_number),',
|
2017-08-08 15:06:36 +08:00
|
|
|
' __cdb_bins_number',
|
|
|
|
' )',
|
|
|
|
' ) - 1',
|
2016-03-19 00:22:02 +08:00
|
|
|
' END AS bin,',
|
|
|
|
' min({{=it._column}})::numeric AS min,',
|
|
|
|
' max({{=it._column}})::numeric AS max,',
|
|
|
|
' avg({{=it._column}})::numeric AS avg,',
|
|
|
|
' count(*) AS freq',
|
2017-08-08 15:57:12 +08:00
|
|
|
'FROM __cdb_filtered_source, __cdb_basics, __cdb_nulls,',
|
|
|
|
' __cdb_bins{{?it._isFloatColumn}}, __cdb_infinities, __cdb_nans{{?}}',
|
2017-08-08 15:39:51 +08:00
|
|
|
'GROUP BY bin, bins_number, bin_width, nulls_count,',
|
2017-06-16 18:57:46 +08:00
|
|
|
' avg_val{{?it._isFloatColumn}}, infinities_count, nans_count{{?}}',
|
2016-03-19 00:22:02 +08:00
|
|
|
'ORDER BY bin'
|
|
|
|
].join('\n'));
|
|
|
|
|
2017-06-02 02:07:46 +08:00
|
|
|
var dateBasicsQueryTpl = dot.template([
|
2017-08-08 15:06:36 +08:00
|
|
|
'__cdb_basics AS (',
|
2017-06-02 02:07:46 +08:00
|
|
|
' SELECT',
|
2017-08-08 01:03:49 +08:00
|
|
|
' max(date_part(\'epoch\', {{=it._column}})) AS __cdb_max_val,',
|
|
|
|
' min(date_part(\'epoch\', {{=it._column}})) AS __cdb_min_val,',
|
|
|
|
' avg(date_part(\'epoch\', {{=it._column}})) AS __cdb_avg_val,',
|
2017-06-03 00:37:49 +08:00
|
|
|
' min(date_trunc(',
|
2017-08-08 00:27:24 +08:00
|
|
|
' \'{{=it._aggregation}}\', {{=it._column}}::timestamp AT TIME ZONE \'{{=it._offset}}\'',
|
2017-08-08 01:03:49 +08:00
|
|
|
' )) AS __cdb_start_date,',
|
|
|
|
' max({{=it._column}}::timestamp AT TIME ZONE \'{{=it._offset}}\') AS __cdb_end_date,',
|
|
|
|
' count(1) AS __cdb_total_rows',
|
2017-08-08 15:06:36 +08:00
|
|
|
' FROM ({{=it._query}}) __cdb_basics_query',
|
2017-06-02 02:07:46 +08:00
|
|
|
')'
|
|
|
|
].join(' \n'));
|
|
|
|
|
2017-06-02 18:15:43 +08:00
|
|
|
var dateOverrideBasicsQueryTpl = dot.template([
|
2017-08-08 15:06:36 +08:00
|
|
|
'__cdb_basics AS (',
|
2017-06-02 18:15:43 +08:00
|
|
|
' SELECT',
|
2017-08-08 01:03:49 +08:00
|
|
|
' max({{=it._end}}) AS __cdb_max_val,',
|
|
|
|
' min({{=it._start}}) AS __cdb_min_val,',
|
|
|
|
' avg(date_part(\'epoch\', {{=it._column}})) AS __cdb_avg_val,',
|
2017-07-12 21:10:39 +08:00
|
|
|
' min(',
|
|
|
|
' date_trunc(',
|
|
|
|
' \'{{=it._aggregation}}\',',
|
2017-07-15 00:30:36 +08:00
|
|
|
' TO_TIMESTAMP({{=it._start}})::timestamp AT TIME ZONE \'{{=it._offset}}\'',
|
2017-07-12 21:10:39 +08:00
|
|
|
' )',
|
2017-08-08 01:03:49 +08:00
|
|
|
' ) AS __cdb_start_date,',
|
2017-07-12 21:10:39 +08:00
|
|
|
' max(',
|
2017-07-15 00:30:36 +08:00
|
|
|
' TO_TIMESTAMP({{=it._end}})::timestamp AT TIME ZONE \'{{=it._offset}}\'',
|
2017-08-08 01:03:49 +08:00
|
|
|
' ) AS __cdb_end_date,',
|
|
|
|
' count(1) AS __cdb_total_rows',
|
2017-08-08 15:06:36 +08:00
|
|
|
' FROM ({{=it._query}}) __cdb_basics_query',
|
2017-06-02 18:15:43 +08:00
|
|
|
')'
|
|
|
|
].join(' \n'));
|
|
|
|
|
2017-06-02 02:07:46 +08:00
|
|
|
var dateBinsQueryTpl = dot.template([
|
2017-08-08 15:12:52 +08:00
|
|
|
'__cdb_bins AS (',
|
2017-06-02 02:07:46 +08:00
|
|
|
' SELECT',
|
2017-08-08 14:41:22 +08:00
|
|
|
' __cdb_bins_array,',
|
|
|
|
' ARRAY_LENGTH(__cdb_bins_array, 1) AS __cdb_bins_number',
|
2017-06-02 02:07:46 +08:00
|
|
|
' FROM (',
|
|
|
|
' SELECT',
|
|
|
|
' ARRAY(',
|
2017-06-23 00:04:23 +08:00
|
|
|
' SELECT GENERATE_SERIES(',
|
2017-08-08 01:03:49 +08:00
|
|
|
' __cdb_start_date::timestamptz,',
|
|
|
|
' __cdb_end_date::timestamptz,',
|
2017-07-12 21:10:39 +08:00
|
|
|
' {{?it._aggregation==="quarter"}}\'3 month\'{{??}}\'1 {{=it._aggregation}}\'{{?}}::interval',
|
2017-06-23 00:04:23 +08:00
|
|
|
' )',
|
2017-08-08 14:41:22 +08:00
|
|
|
' ) AS __cdb_bins_array',
|
2017-08-08 15:06:36 +08:00
|
|
|
' FROM __cdb_basics',
|
2017-08-08 16:20:36 +08:00
|
|
|
' ) __cdb_bins_array_query',
|
2017-06-02 02:07:46 +08:00
|
|
|
')'
|
|
|
|
].join('\n'));
|
|
|
|
|
|
|
|
var dateHistogramQueryTpl = dot.template([
|
|
|
|
'SELECT',
|
2017-08-08 14:33:26 +08:00
|
|
|
' (__cdb_max_val - __cdb_min_val) / cast(__cdb_bins_number as float) AS bin_width,',
|
|
|
|
' __cdb_bins_number AS bins_number,',
|
|
|
|
' __cdb_nulls_count AS nulls_count,',
|
2017-08-08 01:03:49 +08:00
|
|
|
' CASE WHEN __cdb_min_val = __cdb_max_val',
|
2017-06-02 02:07:46 +08:00
|
|
|
' THEN 0',
|
2017-06-03 00:37:49 +08:00
|
|
|
' ELSE GREATEST(1, LEAST(',
|
2017-06-22 02:19:02 +08:00
|
|
|
' WIDTH_BUCKET(',
|
2017-07-15 00:30:36 +08:00
|
|
|
' {{=it._column}}::timestamp AT TIME ZONE \'{{=it._offset}}\',',
|
2017-08-08 14:41:22 +08:00
|
|
|
' __cdb_bins_array',
|
2017-06-22 02:19:02 +08:00
|
|
|
' ),',
|
2017-08-08 14:33:26 +08:00
|
|
|
' __cdb_bins_number',
|
2017-06-03 00:37:49 +08:00
|
|
|
' )) - 1',
|
2017-06-02 02:07:46 +08:00
|
|
|
' END AS bin,',
|
2017-07-12 21:10:39 +08:00
|
|
|
' min(',
|
|
|
|
' date_part(',
|
|
|
|
' \'epoch\', ',
|
|
|
|
' date_trunc(',
|
2017-08-07 22:53:08 +08:00
|
|
|
' \'{{=it._aggregation}}\', {{=it._column}}::timestamp AT TIME ZONE \'{{=it._offset}}\'',
|
2017-07-15 00:30:36 +08:00
|
|
|
' ) AT TIME ZONE \'{{=it._offset}}\'',
|
2017-07-12 21:10:39 +08:00
|
|
|
' )',
|
|
|
|
' )::numeric AS timestamp,',
|
2017-08-08 01:03:49 +08:00
|
|
|
' date_part(\'epoch\', __cdb_start_date)::numeric AS timestamp_start,',
|
2017-06-05 22:26:29 +08:00
|
|
|
' min(date_part(\'epoch\', {{=it._column}}))::numeric AS min,',
|
2017-06-02 02:07:46 +08:00
|
|
|
' max(date_part(\'epoch\', {{=it._column}}))::numeric AS max,',
|
|
|
|
' avg(date_part(\'epoch\', {{=it._column}}))::numeric AS avg,',
|
|
|
|
' count(*) AS freq',
|
2017-08-08 15:32:13 +08:00
|
|
|
'FROM ({{=it._query}}) __cdb_histogram, __cdb_basics, __cdb_bins, __cdb_nulls',
|
2017-06-02 02:07:46 +08:00
|
|
|
'WHERE date_part(\'epoch\', {{=it._column}}) IS NOT NULL',
|
2017-08-08 14:46:26 +08:00
|
|
|
'GROUP BY bin, bins_number, bin_width, nulls_count, timestamp_start',
|
2017-06-02 02:07:46 +08:00
|
|
|
'ORDER BY bin'
|
|
|
|
].join('\n'));
|
2016-03-19 00:22:02 +08:00
|
|
|
|
|
|
|
var TYPE = 'histogram';
|
|
|
|
|
|
|
|
/**
|
2017-06-07 21:54:19 +08:00
|
|
|
Numeric histogram:
|
|
|
|
{
|
|
|
|
type: 'histogram',
|
|
|
|
options: {
|
|
|
|
column: 'name', // column data type: numeric
|
|
|
|
bins: 10 // OPTIONAL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Time series:
|
|
|
|
{
|
|
|
|
type: 'histogram',
|
|
|
|
options: {
|
|
|
|
column: 'date', // column data type: date
|
|
|
|
aggregation: 'day' // OPTIONAL (if undefined then it'll be built as numeric)
|
2017-07-15 00:30:36 +08:00
|
|
|
offset: -7200 // OPTIONAL (UTC offset in seconds)
|
2017-06-07 21:54:19 +08:00
|
|
|
}
|
2016-03-19 00:22:02 +08:00
|
|
|
}
|
|
|
|
*/
|
2017-03-14 01:40:29 +08:00
|
|
|
function Histogram(query, options, queries) {
|
2016-03-19 00:22:02 +08:00
|
|
|
if (!_.isString(options.column)) {
|
|
|
|
throw new Error('Histogram expects `column` in widget options');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.query = query;
|
2017-03-14 01:40:29 +08:00
|
|
|
this.queries = queries;
|
2016-03-19 00:22:02 +08:00
|
|
|
this.column = options.column;
|
|
|
|
this.bins = options.bins;
|
2017-06-02 02:07:46 +08:00
|
|
|
this.aggregation = options.aggregation;
|
2017-07-15 00:30:36 +08:00
|
|
|
this.offset = options.offset;
|
2016-03-19 00:22:02 +08:00
|
|
|
|
|
|
|
this._columnType = null;
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:10:42 +08:00
|
|
|
Histogram.prototype = new BaseWidget();
|
|
|
|
Histogram.prototype.constructor = Histogram;
|
|
|
|
|
2016-03-19 00:22:02 +08:00
|
|
|
module.exports = Histogram;
|
|
|
|
|
|
|
|
Histogram.prototype.sql = function(psql, override, callback) {
|
2017-06-16 18:57:46 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-03-19 00:22:02 +08:00
|
|
|
if (!callback) {
|
|
|
|
callback = override;
|
|
|
|
override = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._columnType === null) {
|
2017-06-16 18:57:46 +08:00
|
|
|
this.getColumnType(psql, this.column, this.queries.no_filters, function (err, type) {
|
2016-03-19 00:22:02 +08:00
|
|
|
// assume numeric, will fail later
|
|
|
|
self._columnType = 'numeric';
|
2017-06-16 18:57:46 +08:00
|
|
|
if (!err && !!type) {
|
|
|
|
self._columnType = Object.keys(type).find(function (key) {
|
|
|
|
return type[key];
|
|
|
|
});
|
2016-03-19 00:22:02 +08:00
|
|
|
}
|
|
|
|
self.sql(psql, override, callback);
|
|
|
|
}, true); // use read-only transaction
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-08-01 22:07:27 +08:00
|
|
|
this._buildQuery(psql, override, callback);
|
2017-06-16 18:57:46 +08:00
|
|
|
};
|
2016-03-19 00:22:02 +08:00
|
|
|
|
2017-08-08 01:24:15 +08:00
|
|
|
Histogram.prototype.isDateHistogram = function (override) {
|
|
|
|
return this._columnType === 'date' && (this.aggregation !== undefined || override.aggregation !== undefined);
|
|
|
|
};
|
|
|
|
|
2017-08-01 22:07:27 +08:00
|
|
|
Histogram.prototype._buildQuery = function (psql, override, callback) {
|
2017-06-08 22:01:41 +08:00
|
|
|
var filteredQuery, basicsQuery, binsQuery;
|
2017-06-16 18:57:46 +08:00
|
|
|
var _column = this.column;
|
|
|
|
var _query = this.query;
|
2016-03-19 00:22:02 +08:00
|
|
|
|
2017-08-08 01:24:15 +08:00
|
|
|
if (this.isDateHistogram(override)) {
|
2017-08-01 22:07:27 +08:00
|
|
|
return this._buildDateHistogramQuery(psql, override, callback);
|
2017-06-02 18:15:43 +08:00
|
|
|
}
|
2016-03-19 00:22:02 +08:00
|
|
|
|
2017-08-01 23:15:45 +08:00
|
|
|
if (this._columnType === 'date') {
|
|
|
|
_column = columnCastTpl({column: _column});
|
|
|
|
}
|
|
|
|
|
2017-06-08 22:01:41 +08:00
|
|
|
filteredQuery = filteredQueryTpl({
|
2017-06-16 18:57:46 +08:00
|
|
|
_isFloatColumn: this._columnType === 'float',
|
2017-06-08 22:01:41 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: _column
|
|
|
|
});
|
2016-03-19 00:22:02 +08:00
|
|
|
|
2017-06-16 18:57:46 +08:00
|
|
|
if (this._shouldOverride(override)) {
|
2016-03-19 00:22:02 +08:00
|
|
|
debug('overriding with %j', override);
|
|
|
|
basicsQuery = overrideBasicsQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: _column,
|
2016-06-06 23:10:52 +08:00
|
|
|
_start: getBinStart(override),
|
|
|
|
_end: getBinEnd(override)
|
2016-03-19 00:22:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
binsQuery = [
|
|
|
|
overrideBinsQueryTpl({
|
|
|
|
_bins: override.bins
|
|
|
|
})
|
|
|
|
].join(',\n');
|
|
|
|
} else {
|
|
|
|
basicsQuery = basicsQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: _column
|
|
|
|
});
|
|
|
|
|
2017-06-16 18:57:46 +08:00
|
|
|
if (this._shouldOverrideBins(override)) {
|
2016-03-19 00:22:02 +08:00
|
|
|
binsQuery = [
|
|
|
|
overrideBinsQueryTpl({
|
|
|
|
_bins: override.bins
|
|
|
|
})
|
|
|
|
].join(',\n');
|
|
|
|
} else {
|
|
|
|
binsQuery = [
|
|
|
|
iqrQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: _column
|
|
|
|
}),
|
|
|
|
binsQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_minBins: BIN_MIN_NUMBER,
|
|
|
|
_maxBins: BIN_MAX_NUMBER
|
|
|
|
})
|
|
|
|
].join(',\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 18:57:46 +08:00
|
|
|
var cteSql = [
|
|
|
|
filteredQuery,
|
|
|
|
basicsQuery,
|
|
|
|
binsQuery,
|
|
|
|
nullsQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: _column
|
|
|
|
})
|
|
|
|
];
|
2016-03-19 00:22:02 +08:00
|
|
|
|
2017-06-16 18:57:46 +08:00
|
|
|
if (this._columnType === 'float') {
|
|
|
|
cteSql.push(
|
2017-06-08 22:01:41 +08:00
|
|
|
infinitiesQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: _column
|
|
|
|
}),
|
|
|
|
nansQueryTpl({
|
2016-03-19 00:22:02 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: _column
|
|
|
|
})
|
2017-06-16 18:57:46 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
var histogramSql = [
|
|
|
|
"WITH",
|
|
|
|
cteSql.join(',\n'),
|
2016-03-19 00:22:02 +08:00
|
|
|
histogramQueryTpl({
|
2017-06-16 18:57:46 +08:00
|
|
|
_isFloatColumn: this._columnType === 'float',
|
2016-03-19 00:22:02 +08:00
|
|
|
_query: _query,
|
|
|
|
_column: _column
|
|
|
|
})
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
debug(histogramSql);
|
|
|
|
|
|
|
|
return callback(null, histogramSql);
|
|
|
|
};
|
|
|
|
|
2017-06-16 18:57:46 +08:00
|
|
|
Histogram.prototype._shouldOverride = function (override) {
|
|
|
|
return override && _.has(override, 'start') && _.has(override, 'end') && _.has(override, 'bins');
|
|
|
|
};
|
|
|
|
|
|
|
|
Histogram.prototype._shouldOverrideBins = function (override) {
|
|
|
|
return override && _.has(override, 'bins');
|
2016-03-19 00:22:02 +08:00
|
|
|
};
|
|
|
|
|
2017-08-03 18:24:05 +08:00
|
|
|
var DATE_AGGREGATIONS = {
|
|
|
|
'auto': true,
|
|
|
|
'minute': true,
|
|
|
|
'hour': true,
|
|
|
|
'day': true,
|
|
|
|
'week': true,
|
|
|
|
'month': true,
|
|
|
|
'quarter': true,
|
|
|
|
'year': true
|
|
|
|
};
|
|
|
|
|
2017-06-29 01:58:45 +08:00
|
|
|
Histogram.prototype._buildDateHistogramQuery = function (psql, override, callback) {
|
2017-06-02 02:07:46 +08:00
|
|
|
var _column = this.column;
|
|
|
|
var _query = this.query;
|
|
|
|
var _aggregation = override && override.aggregation ? override.aggregation : this.aggregation;
|
2017-07-15 00:30:36 +08:00
|
|
|
var _offset = override && Number.isFinite(override.offset) ? override.offset : this.offset;
|
2017-06-02 02:07:46 +08:00
|
|
|
|
2017-08-03 18:24:05 +08:00
|
|
|
if (!DATE_AGGREGATIONS.hasOwnProperty(_aggregation)) {
|
|
|
|
return callback(new Error('Invalid aggregation value. Valid ones: ' +
|
|
|
|
Object.keys(DATE_AGGREGATIONS).join(', ')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:15:45 +08:00
|
|
|
if (_aggregation === 'auto') {
|
2017-06-29 01:58:45 +08:00
|
|
|
this.getAutomaticAggregation(psql, function (err, aggregation) {
|
|
|
|
if (err || aggregation === 'none') {
|
|
|
|
this.aggregation = 'day';
|
|
|
|
} else {
|
|
|
|
this.aggregation = aggregation;
|
|
|
|
}
|
2017-08-02 18:06:10 +08:00
|
|
|
override.aggregation = this.aggregation;
|
2017-06-29 01:58:45 +08:00
|
|
|
this._buildDateHistogramQuery(psql, override, callback);
|
|
|
|
}.bind(this));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-02 18:15:43 +08:00
|
|
|
var dateBasicsQuery;
|
|
|
|
|
|
|
|
if (override && _.has(override, 'start') && _.has(override, 'end')) {
|
|
|
|
dateBasicsQuery = dateOverrideBasicsQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: _column,
|
|
|
|
_aggregation: _aggregation,
|
|
|
|
_start: getBinStart(override),
|
2017-06-03 00:37:49 +08:00
|
|
|
_end: getBinEnd(override),
|
2017-07-15 00:38:13 +08:00
|
|
|
_offset: parseOffset(_offset, _aggregation)
|
2017-06-02 18:15:43 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dateBasicsQuery = dateBasicsQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: _column,
|
2017-06-03 00:37:49 +08:00
|
|
|
_aggregation: _aggregation,
|
2017-07-15 00:38:13 +08:00
|
|
|
_offset: parseOffset(_offset, _aggregation)
|
2017-06-02 18:15:43 +08:00
|
|
|
});
|
|
|
|
}
|
2017-06-02 02:07:46 +08:00
|
|
|
|
|
|
|
var dateBinsQuery = [
|
|
|
|
dateBinsQueryTpl({
|
|
|
|
_aggregation: _aggregation
|
|
|
|
})
|
|
|
|
].join(',\n');
|
|
|
|
|
|
|
|
var nullsQuery = nullsQueryTpl({
|
|
|
|
_query: _query,
|
|
|
|
_column: _column
|
|
|
|
});
|
|
|
|
|
|
|
|
var dateHistogramQuery = dateHistogramQueryTpl({
|
|
|
|
_query: _query,
|
2017-06-03 00:37:49 +08:00
|
|
|
_column: _column,
|
|
|
|
_aggregation: _aggregation,
|
2017-07-15 00:38:13 +08:00
|
|
|
_offset: parseOffset(_offset, _aggregation)
|
2017-06-02 02:07:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
var histogramSql = [
|
|
|
|
"WITH",
|
|
|
|
[
|
|
|
|
dateBasicsQuery,
|
|
|
|
dateBinsQuery,
|
|
|
|
nullsQuery
|
|
|
|
].join(',\n'),
|
|
|
|
dateHistogramQuery
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
debug(histogramSql);
|
|
|
|
|
|
|
|
return callback(null, histogramSql);
|
|
|
|
};
|
|
|
|
|
2017-06-29 01:58:45 +08:00
|
|
|
Histogram.prototype.getAutomaticAggregation = function (psql, callback) {
|
|
|
|
var dateIntervalQuery = dateIntervalQueryTpl({
|
|
|
|
query: this.query,
|
|
|
|
column: this.column
|
|
|
|
});
|
|
|
|
|
2017-06-29 22:53:52 +08:00
|
|
|
debug(dateIntervalQuery);
|
|
|
|
|
2017-06-29 01:58:45 +08:00
|
|
|
psql.query(dateIntervalQuery, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var aggegations = result.rows[0];
|
|
|
|
var aggregation = Object.keys(aggegations)
|
|
|
|
.map(function (key) {
|
|
|
|
return {
|
|
|
|
name: key,
|
|
|
|
value: aggegations[key]
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.reduce(function (closer, current) {
|
|
|
|
if (current.value > MAX_INTERVAL_VALUE) {
|
|
|
|
return closer;
|
|
|
|
}
|
|
|
|
|
|
|
|
var closerDiff = MAX_INTERVAL_VALUE - closer.value;
|
|
|
|
var currentDiff = MAX_INTERVAL_VALUE - current.value;
|
|
|
|
|
|
|
|
if (Number.isFinite(current.value) && closerDiff > currentDiff) {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
return closer;
|
|
|
|
}, { name: 'none', value: -1 });
|
|
|
|
|
|
|
|
callback(null, aggregation.name);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-03-19 00:22:02 +08:00
|
|
|
Histogram.prototype.format = function(result, override) {
|
|
|
|
override = override || {};
|
|
|
|
var buckets = [];
|
|
|
|
|
|
|
|
var binsCount = getBinsCount(override);
|
|
|
|
var width = getWidth(override);
|
|
|
|
var binsStart = getBinStart(override);
|
|
|
|
var nulls = 0;
|
2017-06-08 22:01:41 +08:00
|
|
|
var infinities = 0;
|
|
|
|
var nans = 0;
|
2016-03-19 00:22:02 +08:00
|
|
|
var avg;
|
2017-07-15 00:22:05 +08:00
|
|
|
var timestampStart;
|
2017-08-01 22:07:27 +08:00
|
|
|
var aggregation;
|
|
|
|
var offset;
|
2016-03-19 00:22:02 +08:00
|
|
|
|
|
|
|
if (result.rows.length) {
|
|
|
|
var firstRow = result.rows[0];
|
|
|
|
binsCount = firstRow.bins_number;
|
|
|
|
width = firstRow.bin_width || width;
|
|
|
|
avg = firstRow.avg_val;
|
|
|
|
nulls = firstRow.nulls_count;
|
2017-07-15 00:22:05 +08:00
|
|
|
timestampStart = firstRow.timestamp_start;
|
2017-06-08 22:01:41 +08:00
|
|
|
infinities = firstRow.infinities_count;
|
|
|
|
nans = firstRow.nans_count;
|
2017-08-01 22:07:27 +08:00
|
|
|
binsStart = populateBinStart(override, firstRow);
|
|
|
|
|
2017-08-04 00:23:55 +08:00
|
|
|
if (Number.isFinite(timestampStart)) {
|
2017-08-01 22:07:27 +08:00
|
|
|
aggregation = getAggregation(override, this.aggregation);
|
|
|
|
offset = getOffset(override, this.offset);
|
|
|
|
}
|
2016-03-19 00:22:02 +08:00
|
|
|
|
|
|
|
buckets = result.rows.map(function(row) {
|
2017-08-01 22:07:27 +08:00
|
|
|
return _.omit(
|
|
|
|
row,
|
|
|
|
'bins_number',
|
|
|
|
'bin_width',
|
|
|
|
'nulls_count',
|
|
|
|
'infinities_count',
|
|
|
|
'nans_count',
|
|
|
|
'avg_val',
|
|
|
|
'timestamp_start'
|
|
|
|
);
|
2016-03-19 00:22:02 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2017-06-29 01:58:45 +08:00
|
|
|
aggregation: aggregation,
|
2017-07-15 00:30:36 +08:00
|
|
|
offset: offset,
|
2017-07-15 00:22:05 +08:00
|
|
|
timestamp_start: timestampStart,
|
2016-03-19 00:22:02 +08:00
|
|
|
bin_width: width,
|
|
|
|
bins_count: binsCount,
|
|
|
|
bins_start: binsStart,
|
|
|
|
nulls: nulls,
|
2017-06-08 22:01:41 +08:00
|
|
|
infinities: infinities,
|
|
|
|
nans: nans,
|
2016-03-19 00:22:02 +08:00
|
|
|
avg: avg,
|
|
|
|
bins: buckets
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-07-14 01:42:22 +08:00
|
|
|
function getAggregation(override, aggregation) {
|
|
|
|
return override && override.aggregation ? override.aggregation : aggregation;
|
|
|
|
}
|
|
|
|
|
2017-07-15 00:38:13 +08:00
|
|
|
function getOffset(override, offset) {
|
2017-07-15 00:30:36 +08:00
|
|
|
if (override && override.offset) {
|
|
|
|
return override.offset;
|
2017-07-14 01:42:22 +08:00
|
|
|
}
|
2017-07-15 00:30:36 +08:00
|
|
|
if (offset) {
|
|
|
|
return offset;
|
2017-07-14 01:42:22 +08:00
|
|
|
}
|
2017-07-15 00:22:05 +08:00
|
|
|
|
2017-07-14 01:42:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-19 00:22:02 +08:00
|
|
|
function getBinStart(override) {
|
2016-06-06 23:10:52 +08:00
|
|
|
if (override.hasOwnProperty('start') && override.hasOwnProperty('end')) {
|
|
|
|
return Math.min(override.start, override.end);
|
|
|
|
}
|
2016-03-19 00:22:02 +08:00
|
|
|
return override.start || 0;
|
|
|
|
}
|
|
|
|
|
2016-06-06 23:10:52 +08:00
|
|
|
function getBinEnd(override) {
|
|
|
|
if (override.hasOwnProperty('start') && override.hasOwnProperty('end')) {
|
|
|
|
return Math.max(override.start, override.end);
|
|
|
|
}
|
|
|
|
return override.end || 0;
|
|
|
|
}
|
|
|
|
|
2016-03-19 00:22:02 +08:00
|
|
|
function getBinsCount(override) {
|
|
|
|
return override.bins || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getWidth(override) {
|
|
|
|
var width = 0;
|
|
|
|
var binsCount = override.bins;
|
|
|
|
|
|
|
|
if (binsCount && Number.isFinite(override.start) && Number.isFinite(override.end)) {
|
|
|
|
width = (override.end - override.start) / binsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2017-07-15 00:38:13 +08:00
|
|
|
function parseOffset(offset, aggregation) {
|
2017-07-15 00:30:36 +08:00
|
|
|
if (!offset) {
|
2017-06-03 00:37:49 +08:00
|
|
|
return '0';
|
|
|
|
}
|
2017-07-12 23:08:55 +08:00
|
|
|
if (aggregation === 'hour' || aggregation === 'minute') {
|
|
|
|
return '0';
|
|
|
|
}
|
|
|
|
|
2017-07-15 00:30:36 +08:00
|
|
|
var offsetInHours = Math.ceil(offset / 3600);
|
|
|
|
return '' + offsetInHours;
|
2017-06-03 00:37:49 +08:00
|
|
|
}
|
|
|
|
|
2017-06-07 22:11:09 +08:00
|
|
|
function populateBinStart(override, firstRow) {
|
|
|
|
var binStart;
|
|
|
|
|
2017-07-12 21:14:41 +08:00
|
|
|
if (firstRow.hasOwnProperty('timestamp')) {
|
2017-06-07 22:11:09 +08:00
|
|
|
binStart = firstRow.timestamp;
|
2017-07-12 21:14:41 +08:00
|
|
|
} else if (override.hasOwnProperty('start')) {
|
|
|
|
binStart = getBinStart(override);
|
2017-06-07 22:11:09 +08:00
|
|
|
} else {
|
|
|
|
binStart = firstRow.min;
|
|
|
|
}
|
|
|
|
|
|
|
|
return binStart;
|
|
|
|
}
|
|
|
|
|
2016-03-19 00:22:02 +08:00
|
|
|
Histogram.prototype.getType = function() {
|
|
|
|
return TYPE;
|
|
|
|
};
|
|
|
|
|
|
|
|
Histogram.prototype.toString = function() {
|
|
|
|
return JSON.stringify({
|
|
|
|
_type: TYPE,
|
|
|
|
_column: this.column,
|
|
|
|
_query: this.query
|
|
|
|
});
|
|
|
|
};
|