Skip to content

Commit

Permalink
removing vis dependency from AggConfigs/AggConfig/AggTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Aug 27, 2018
1 parent cd4d53f commit 45880bb
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 75 deletions.
12 changes: 6 additions & 6 deletions src/ui/public/agg_response/point_series/_fake_x_aspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import { AggConfig } from '../../vis/agg_config';
import { AggType } from '../../agg_types/agg_type';

export function PointSeriesFakeXAxisProvider() {
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/ui/public/agg_response/point_series/_get_aspects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
Expand All @@ -70,7 +70,7 @@ export function PointSeriesGetAspectsProvider(Private) {
.value();

if (!aspects.x) {
aspects.x = fakeXAspect(vis);
aspects.x = fakeXAspect();
}

return aspects;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/public/agg_response/point_series/_init_x_axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/ui/public/agg_response/point_series/point_series.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
});
Expand Down
15 changes: 7 additions & 8 deletions src/ui/public/agg_types/buckets/date_histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}

Expand Down Expand Up @@ -102,7 +97,11 @@ export const dateHistogramBucketAgg = new BucketAggType({
setBounds(agg, true);
}
},

{
name: 'timeRange',
default: null,
write: _.noop,
},
{
name: 'interval',
type: 'optioned',
Expand Down
7 changes: 3 additions & 4 deletions src/ui/public/agg_types/buckets/geo_hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/public/agg_types/buckets/terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand All @@ -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;
},
Expand Down
22 changes: 11 additions & 11 deletions src/ui/public/vis/agg_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -305,7 +305,7 @@ class AggConfig {
}

fieldIsTimeField() {
const timeFieldName = this.vis.indexPattern.timeFieldName;
const timeFieldName = this._indexPattern.timeFieldName;
return timeFieldName && this.fieldName() === timeFieldName;
}

Expand Down Expand Up @@ -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;
Expand Down
60 changes: 33 additions & 27 deletions src/ui/public/vis/agg_configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,50 @@ function parseParentAggs(dslLvlCursor, dsl) {
}

class AggConfigs extends IndexedArray {
constructor(vis, configStates = []) {
constructor(indexPattern, configStates = [], schemas) {
configStates = AggConfig.ensureIds(configStates);

super({
index: ['id'],
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;
}

/**
Expand All @@ -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';
})
Expand Down
7 changes: 7 additions & 0 deletions src/ui/public/vis/request_handlers/courier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/public/vis/response_handlers/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 45880bb

Please sign in to comment.