From 45880bbbe2b16f6be594472ca4b0ed60a72173b9 Mon Sep 17 00:00:00 2001 From: ppisljar Date: Thu, 9 Aug 2018 08:20:54 +0200 Subject: [PATCH] removing vis dependency from AggConfigs/AggConfig/AggTypes --- .../point_series/_fake_x_aspect.js | 12 ++-- .../agg_response/point_series/_get_aspects.js | 4 +- .../agg_response/point_series/_init_x_axis.js | 2 +- .../point_series/_ordered_date_axis.js | 2 +- .../agg_response/point_series/point_series.js | 6 +- .../buckets/_terms_other_bucket_helper.js | 3 +- .../agg_types/buckets/date_histogram.js | 15 +++-- src/ui/public/agg_types/buckets/geo_hash.js | 7 +-- src/ui/public/agg_types/buckets/terms.js | 2 +- .../metrics/lib/parent_pipeline_agg_helper.js | 2 +- .../lib/sibling_pipeline_agg_helper.js | 4 +- src/ui/public/vis/agg_config.js | 22 +++---- src/ui/public/vis/agg_configs.js | 60 ++++++++++--------- src/ui/public/vis/request_handlers/courier.js | 7 +++ src/ui/public/vis/response_handlers/basic.js | 2 +- src/ui/public/vis/vis.js | 7 ++- .../visualize/loader/visualize_data_loader.ts | 2 - 17 files changed, 84 insertions(+), 75 deletions(-) diff --git a/src/ui/public/agg_response/point_series/_fake_x_aspect.js b/src/ui/public/agg_response/point_series/_fake_x_aspect.js index a6088ec45a71da4..7d0bf25772969f0 100644 --- a/src/ui/public/agg_response/point_series/_fake_x_aspect.js +++ b/src/ui/public/agg_response/point_series/_fake_x_aspect.js @@ -17,7 +17,6 @@ * under the License. */ -import { AggConfig } from '../../vis/agg_config'; import { AggType } from '../../agg_types/agg_type'; export function PointSeriesFakeXAxisProvider() { @@ -29,11 +28,12 @@ export function PointSeriesFakeXAxisProvider() { hasNoDsl: true }); - return function makeFakeXAxis(vis) { - const fake = new AggConfig(vis, { - type: allAgg, - schema: vis.type.schemas.all.byName.segment - }); + return function makeFakeXAxis() { + const fake = { + makeLabel: () => 'all', + fieldFormatter: () => '', + type: allAgg + }; return { i: -1, diff --git a/src/ui/public/agg_response/point_series/_get_aspects.js b/src/ui/public/agg_response/point_series/_get_aspects.js index b1f8539618686c7..f5bedc889ceee1a 100644 --- a/src/ui/public/agg_response/point_series/_get_aspects.js +++ b/src/ui/public/agg_response/point_series/_get_aspects.js @@ -55,7 +55,7 @@ export function PointSeriesGetAspectsProvider(Private) { * @return {object} - an object with a key for each aspect (see map). The values * may be undefined, a single aspect, or an array of aspects. */ - return function getAspects(vis, table) { + return function getAspects(table) { const aspects = _(table.columns) // write each column into the aspects under it's group .transform(columnToAspect, {}) @@ -70,7 +70,7 @@ export function PointSeriesGetAspectsProvider(Private) { .value(); if (!aspects.x) { - aspects.x = fakeXAspect(vis); + aspects.x = fakeXAspect(); } return aspects; diff --git a/src/ui/public/agg_response/point_series/_init_x_axis.js b/src/ui/public/agg_response/point_series/_init_x_axis.js index d07cbc5ba49c377..7859b47512e51ec 100644 --- a/src/ui/public/agg_response/point_series/_init_x_axis.js +++ b/src/ui/public/agg_response/point_series/_init_x_axis.js @@ -26,7 +26,7 @@ export function PointSeriesInitXAxisProvider() { if (!x.agg || !x.agg.type.ordered) return; - chart.indexPattern = x.agg.vis.indexPattern; + chart.indexPattern = x.agg._indexPattern; chart.xAxisField = x.agg.params.field; chart.ordered = {}; diff --git a/src/ui/public/agg_response/point_series/_ordered_date_axis.js b/src/ui/public/agg_response/point_series/_ordered_date_axis.js index e5d5f46883ec010..487745c4faf10cf 100644 --- a/src/ui/public/agg_response/point_series/_ordered_date_axis.js +++ b/src/ui/public/agg_response/point_series/_ordered_date_axis.js @@ -21,7 +21,7 @@ import moment from 'moment'; export function PointSeriesOrderedDateAxisProvider() { - return function orderedDateAxis(vis, chart) { + return function orderedDateAxis(chart) { const xAgg = chart.aspects.x.agg; const buckets = xAgg.buckets; const format = buckets.getScaledDateFormat(); diff --git a/src/ui/public/agg_response/point_series/point_series.js b/src/ui/public/agg_response/point_series/point_series.js index 6dce54ee9b3c949..c76efe078875cb8 100644 --- a/src/ui/public/agg_response/point_series/point_series.js +++ b/src/ui/public/agg_response/point_series/point_series.js @@ -33,9 +33,9 @@ export function AggResponsePointSeriesProvider(Private) { const setupOrderedDateXAxis = Private(PointSeriesOrderedDateAxisProvider); const tooltipFormatter = Private(PointSeriesTooltipFormatter); - return function pointSeriesChartDataFromTable(vis, table) { + return function pointSeriesChartDataFromTable(table) { const chart = {}; - const aspects = chart.aspects = getAspects(vis, table); + const aspects = chart.aspects = getAspects(table); chart.tooltipFormatter = tooltipFormatter; @@ -44,7 +44,7 @@ export function AggResponsePointSeriesProvider(Private) { const datedX = aspects.x.agg.type.ordered && aspects.x.agg.type.ordered.date; if (datedX) { - setupOrderedDateXAxis(vis, chart); + setupOrderedDateXAxis(chart); } chart.series = getSeries(table.rows, chart); diff --git a/src/ui/public/agg_types/buckets/_terms_other_bucket_helper.js b/src/ui/public/agg_types/buckets/_terms_other_bucket_helper.js index 338a427674329ae..d5738a576f12fa3 100644 --- a/src/ui/public/agg_types/buckets/_terms_other_bucket_helper.js +++ b/src/ui/public/agg_types/buckets/_terms_other_bucket_helper.js @@ -18,7 +18,6 @@ */ import _ from 'lodash'; -import { AggConfig } from '../../vis/agg_config'; import { buildExistsFilter } from '../../filter_manager/lib/exists'; import { buildPhrasesFilter } from '../../filter_manager/lib/phrases'; import { buildQueryFromFilters } from '../../courier'; @@ -110,7 +109,7 @@ const buildOtherBucketAgg = (aggConfigs, aggWithOtherBucket, response) => { const indexPattern = aggWithOtherBucket.params.field.indexPattern; // create filters aggregation - const filterAgg = new AggConfig(aggConfigs[index].vis, { + const filterAgg = this.parent.createAggregation({ type: 'filters', id: 'other', }); diff --git a/src/ui/public/agg_types/buckets/date_histogram.js b/src/ui/public/agg_types/buckets/date_histogram.js index fe93fe41f28e9dc..8e467a2b29406bd 100644 --- a/src/ui/public/agg_types/buckets/date_histogram.js +++ b/src/ui/public/agg_types/buckets/date_histogram.js @@ -28,6 +28,7 @@ import { TimeBuckets } from '../../time_buckets'; import { createFilterDateHistogram } from './create_filter/date_histogram'; import { intervalOptions } from './_interval_options'; import intervalTemplate from '../controls/time_interval.html'; +import { timefilter } from '../../timefilter'; const config = chrome.getUiSettingsClient(); const detectedTimezone = tzDetect.determine().name(); @@ -41,16 +42,10 @@ function getInterval(agg) { return interval; } -function getBounds(vis) { - if (vis.filters && vis.filters.timeRange) { - return vis.API.timeFilter.calculateBounds(vis.filters.timeRange); - } -} - function setBounds(agg, force) { if (agg.buckets._alreadySet && !force) return; agg.buckets._alreadySet = true; - const bounds = getBounds(agg.vis); + const bounds = agg.params.timeRange ? timefilter.calculateBounds(agg.params.timeRange) : null; agg.buckets.setBounds(agg.fieldIsTimeField() && bounds); } @@ -102,7 +97,11 @@ export const dateHistogramBucketAgg = new BucketAggType({ setBounds(agg, true); } }, - + { + name: 'timeRange', + default: null, + write: _.noop, + }, { name: 'interval', type: 'optioned', diff --git a/src/ui/public/agg_types/buckets/geo_hash.js b/src/ui/public/agg_types/buckets/geo_hash.js index 3cc5facd3ae3966..c9aecd0ffa2c1e3 100644 --- a/src/ui/public/agg_types/buckets/geo_hash.js +++ b/src/ui/public/agg_types/buckets/geo_hash.js @@ -20,7 +20,6 @@ import _ from 'lodash'; import chrome from '../../chrome'; import { BucketAggType } from './_bucket_agg_type'; -import { AggConfig } from '../../vis/agg_config'; import precisionTemplate from '../controls/precision.html'; import { geohashColumns } from '../../utils/decode_geo_hash'; import { geoContains, scaleBounds } from '../../utils/geo_utils'; @@ -117,7 +116,7 @@ export const geoHashBucketAgg = new BucketAggType({ ], getRequestAggs: function (agg) { const aggs = []; - const { vis, params } = agg; + const params = agg.params; if (params.isFilteredByCollar && agg.getField()) { const { mapBounds, mapZoom } = params; @@ -137,7 +136,7 @@ export const geoHashBucketAgg = new BucketAggType({ bottom_right: mapCollar.bottom_right } }; - aggs.push(new AggConfig(vis, { + aggs.push(agg.parent.createAggConfig({ type: 'filter', id: 'filter_agg', enabled: true, @@ -154,7 +153,7 @@ export const geoHashBucketAgg = new BucketAggType({ aggs.push(agg); if (params.useGeocentroid) { - aggs.push(new AggConfig(vis, { + aggs.push(agg.parent.createAggConfig({ type: 'geo_centroid', enabled: true, params: { diff --git a/src/ui/public/agg_types/buckets/terms.js b/src/ui/public/agg_types/buckets/terms.js index 0c1f6b71f4651bd..e336c3d4ea4b997 100644 --- a/src/ui/public/agg_types/buckets/terms.js +++ b/src/ui/public/agg_types/buckets/terms.js @@ -147,7 +147,7 @@ export const termsBucketAgg = new BucketAggType({ makeOrderAgg: function (termsAgg, state) { state = state || {}; state.schema = orderAggSchema; - const orderAgg = new AggConfig(termsAgg.vis, state); + const orderAgg = this.parent.createAggregation(state); orderAgg.id = termsAgg.id + '-orderAgg'; return orderAgg; }, diff --git a/src/ui/public/agg_types/metrics/lib/parent_pipeline_agg_helper.js b/src/ui/public/agg_types/metrics/lib/parent_pipeline_agg_helper.js index c9687b1c4903f11..8bf59680e369e15 100644 --- a/src/ui/public/agg_types/metrics/lib/parent_pipeline_agg_helper.js +++ b/src/ui/public/agg_types/metrics/lib/parent_pipeline_agg_helper.js @@ -54,7 +54,7 @@ const parentPipelineAggHelper = { makeAgg: function (termsAgg, state) { state = state || { type: 'count' }; state.schema = metricAggSchema; - const metricAgg = new AggConfig(termsAgg.vis, state); + const metricAgg = this.parent.createAggregation(termsAgg.vis, state); metricAgg.id = termsAgg.id + '-metric'; return metricAgg; }, diff --git a/src/ui/public/agg_types/metrics/lib/sibling_pipeline_agg_helper.js b/src/ui/public/agg_types/metrics/lib/sibling_pipeline_agg_helper.js index befc8c0c95fe268..8bdad1e21a37f27 100644 --- a/src/ui/public/agg_types/metrics/lib/sibling_pipeline_agg_helper.js +++ b/src/ui/public/agg_types/metrics/lib/sibling_pipeline_agg_helper.js @@ -68,7 +68,7 @@ const siblingPipelineAggHelper = { makeAgg: function (agg, state) { state = state || { type: 'date_histogram' }; state.schema = bucketAggSchema; - const orderAgg = new AggConfig(agg.vis, state); + const orderAgg = this.parent.createAggregation(state); orderAgg.id = agg.id + '-bucket'; return orderAgg; }, @@ -90,7 +90,7 @@ const siblingPipelineAggHelper = { makeAgg: function (agg, state) { state = state || { type: 'count' }; state.schema = metricAggSchema; - const orderAgg = new AggConfig(agg.vis, state); + const orderAgg = this.parent.createAggregation(state); orderAgg.id = agg.id + '-metric'; return orderAgg; }, diff --git a/src/ui/public/vis/agg_config.js b/src/ui/public/vis/agg_config.js index 1df28693e1b5202..536b83c0b6e2ad1 100644 --- a/src/ui/public/vis/agg_config.js +++ b/src/ui/public/vis/agg_config.js @@ -62,11 +62,11 @@ class AggConfig { }, 0); } - constructor(vis, opts = {}, aggs) { - this.id = String(opts.id || AggConfig.nextId(vis.aggs)); - this.vis = vis; - this._indexPattern = vis.indexPattern; - this._aggs = aggs || vis.aggs; + constructor(parent, opts = {}) { + this.parent = parent; + this.id = String(opts.id || AggConfig.nextId(parent.aggs)); + this._indexPattern = parent.indexPattern; + this._aggs = parent.aggs; this._opts = opts; this.enabled = typeof opts.enabled === 'boolean' ? opts.enabled : true; @@ -261,18 +261,18 @@ class AggConfig { return this.params.field; } - makeLabel() { + makeLabel(percentageMode = false) { if (this.params.customLabel) { return this.params.customLabel; } if (!this.type) return ''; - let pre = (_.get(this.vis, 'params.mode') === 'percentage') ? 'Percentage of ' : ''; + let pre = percentageMode ? 'Percentage of ' : ''; return pre += this.type.makeLabel(this); } getIndexPattern() { - return this.vis.indexPattern; + return this._indexPattern; } getFieldOptions() { @@ -305,7 +305,7 @@ class AggConfig { } fieldIsTimeField() { - const timeFieldName = this.vis.indexPattern.timeFieldName; + const timeFieldName = this._indexPattern.timeFieldName; return timeFieldName && this.fieldName() === timeFieldName; } @@ -348,8 +348,8 @@ class AggConfig { } set schema(schema) { - if (_.isString(schema)) { - schema = this.vis.type.schemas.all.byName[schema]; + if (_.isString(schema) && this.parent.schemas) { + schema = this.parent.schemas.byName[schema]; } this.__schema = schema; diff --git a/src/ui/public/vis/agg_configs.js b/src/ui/public/vis/agg_configs.js index de7e805158641a7..f9728d581170537 100644 --- a/src/ui/public/vis/agg_configs.js +++ b/src/ui/public/vis/agg_configs.js @@ -47,7 +47,7 @@ function parseParentAggs(dslLvlCursor, dsl) { } class AggConfigs extends IndexedArray { - constructor(vis, configStates = []) { + constructor(indexPattern, configStates = [], schemas) { configStates = AggConfig.ensureIds(configStates); super({ @@ -55,36 +55,42 @@ class AggConfigs extends IndexedArray { group: ['schema.group', 'type.name', 'schema.name'], }); - this.push(...configStates.map(aggConfigState => { - if (aggConfigState instanceof AggConfig) { - return aggConfigState; - } - return new AggConfig(vis, aggConfigState, this); - })); + this.indexPattern = indexPattern; + this.schemas = schemas; + + configStates.forEach(params => this.createAggregation(params)); - this.vis = vis; + if (this.schemas) { + this.initializeDefaultsFromSchemas(schemas); + } + } + initializeDefaultsFromSchemas(schemas) { // Set the defaults for any schema which has them. If the defaults // for some reason has more then the max only set the max number // of defaults (not sure why a someone define more... // but whatever). Also if a schema.name is already set then don't // set anything. - if (vis && vis.type && vis.type.schemas && vis.type.schemas.all) { - _(vis.type.schemas.all) - .filter(schema => { - return Array.isArray(schema.defaults) && schema.defaults.length > 0; - }) - .each(schema => { - if (!this.bySchemaName[schema.name]) { - const defaults = schema.defaults.slice(0, schema.max); - _.each(defaults, defaultState => { - const state = _.defaults({ id: AggConfig.nextId(this) }, defaultState); - this.push(new AggConfig(vis, state, this)); - }); - } - }) - .commit(); - } + _(schemas) + .filter(schema => { + return Array.isArray(schema.defaults) && schema.defaults.length > 0; + }) + .each(schema => { + if (!this.bySchemaName[schema.name]) { + const defaults = schema.defaults.slice(0, schema.max); + _.each(defaults, defaultState => { + const state = _.defaults({ id: AggConfig.nextId(this) }, defaultState); + this.push(new AggConfig(this, state)); + }); + } + }) + .commit(); + } + + createAggregation(params) { + const aggConfig = params instanceof AggConfig ? params : new AggConfig(this, params); + this.push(aggConfig); + return aggConfig; } /** @@ -104,14 +110,14 @@ class AggConfigs extends IndexedArray { return true; } - toDsl() { + toDsl(hierarchical = false) { const dslTopLvl = {}; let dslLvlCursor; let nestedMetrics; - if (this.vis.isHierarchical()) { + if (hierarchical) { // collect all metrics, and filter out the ones that we won't be copying - nestedMetrics = _(this.vis.aggs.bySchemaGroup.metrics) + nestedMetrics = _(this.bySchemaGroup.metrics) .filter(function (agg) { return agg.type.name !== 'count'; }) diff --git a/src/ui/public/vis/request_handlers/courier.js b/src/ui/public/vis/request_handlers/courier.js index eb9ff172d622658..1e5ffd422e0518c 100644 --- a/src/ui/public/vis/request_handlers/courier.js +++ b/src/ui/public/vis/request_handlers/courier.js @@ -85,6 +85,13 @@ const CourierRequestHandlerProvider = function () { const timeFilterSearchSource = searchSource.createChild({ callParentStartHandlers: true }); const requestSearchSource = timeFilterSearchSource.createChild({ callParentStartHandlers: true }); + // if date_histogram agg exists we need to set the timeRange parameter on it + aggs.forEach(agg => { + if (agg.type.name === 'date_histogram') { + agg.params.timeRange = timeRange; + } + }); + // For now we need to mirror the history of the passed search source, since // the spy panel wouldn't work otherwise. Object.defineProperty(requestSearchSource, 'history', { diff --git a/src/ui/public/vis/response_handlers/basic.js b/src/ui/public/vis/response_handlers/basic.js index d8ce9942a889838..de8c147b77155e6 100644 --- a/src/ui/public/vis/response_handlers/basic.js +++ b/src/ui/public/vis/response_handlers/basic.js @@ -59,7 +59,7 @@ const BasicResponseHandlerProvider = function (Private) { } function convertTable(vis, table) { - return vis.type.responseConverter ? vis.type.responseConverter(vis, table) : table; + return vis.type.responseConverter ? vis.type.responseConverter(table) : table; } return { diff --git a/src/ui/public/vis/vis.js b/src/ui/public/vis/vis.js index e00943d815a661c..2598e81c0b67e52 100644 --- a/src/ui/public/vis/vis.js +++ b/src/ui/public/vis/vis.js @@ -188,7 +188,7 @@ export function VisProvider(Private, indexPatterns, getAppState) { updateVisualizationConfig(state.params, this.params); - this.aggs = new AggConfigs(this, state.aggs); + this.aggs = new AggConfigs(this.indexPattern, state.aggs, this.type.schemas.all); } setState(state, updateCurrentState = true) { @@ -233,7 +233,7 @@ export function VisProvider(Private, indexPatterns, getAppState) { copyCurrentState(includeDisabled = false) { const state = this.getCurrentState(includeDisabled); - state.aggs = new AggConfigs(this, state.aggs); + state.aggs = new AggConfigs(this.indexPattern, state.aggs, this.type.schemas.all); return state; } @@ -252,7 +252,8 @@ export function VisProvider(Private, indexPatterns, getAppState) { } getAggConfig() { - return new AggConfigs(this, this.aggs.raw.filter(agg => agg.enabled)); + const aggConfigs = new AggConfigs(this.indexPattern, this.aggs.raw.filter(agg => agg.enabled), this.type.schemas.all); + return aggConfigs; } getState() { diff --git a/src/ui/public/visualize/loader/visualize_data_loader.ts b/src/ui/public/visualize/loader/visualize_data_loader.ts index 9acd88897b4f3fc..243686a45bcc6f9 100644 --- a/src/ui/public/visualize/loader/visualize_data_loader.ts +++ b/src/ui/public/visualize/loader/visualize_data_loader.ts @@ -68,8 +68,6 @@ export class VisualizeDataLoader { } public async fetch(params: RequestHandlerParams): Promise { - this.vis.filters = { timeRange: params.timeRange }; - try { // searchSource is only there for courier request handler const requestHandlerResponse = await this.requestHandler(this.vis, params);