Skip to content

Commit

Permalink
temorarily improved labels for the query results, moved global time f…
Browse files Browse the repository at this point in the history
…ilter into the rootSearch, so that it can be easilyt excluded. Closes #32. Closes #68
  • Loading branch information
Spencer Alger committed Apr 24, 2014
1 parent d25f260 commit 97e0a4e
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 88 deletions.
4 changes: 4 additions & 0 deletions src/kibana/apps/settings/services/index_patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ define(function (require) {

module.service('indexPatterns', function (config, es, courier) {
this.getFields = function (id) {
if (typeof id === 'object' && typeof id.get === 'function') {
id = id.get('index');
}

return es.get({
index: config.file.kibanaIndex,
type: 'mapping',
Expand Down
25 changes: 20 additions & 5 deletions src/kibana/apps/visualize/saved_visualizations/_aggs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,38 @@ define(function (require) {
aggs.metricAggs = [
{
name: 'count',
display: 'Count'
display: 'Count',
makeLabel: function (params) {
return 'Count of documents';
}
},
{
name: 'avg',
display: 'Average'
display: 'Average',
makeLabel: function (params) {
return 'Average ' + params.field;
}
},
{
name: 'sum',
display: 'Sum'
display: 'Sum',
makeLabel: function (params) {
return 'Sum of ' + params.field;
}
},
{
name: 'min',
display: 'Min'
display: 'Min',
makeLabel: function (params) {
return 'Min ' + params.field;
}
},
{
name: 'max',
display: 'Max'
display: 'Max',
makeLabel: function (params) {
return 'Max ' + params.field;
}
}
];
aggs.metricAggsByName = _.indexBy(aggs.metricAggs, 'name');
Expand Down
41 changes: 28 additions & 13 deletions src/kibana/apps/visualize/saved_visualizations/_build_chart_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ define(function (require) {

var converters = require('./resp_converters/index');
var aggs = require('./_aggs');
var _ = require('lodash');

// private functionality for Vis.buildChartDataFromResp()
return function (createNotifier) {
Expand All @@ -20,17 +21,7 @@ define(function (require) {

// the list of configs that make up the aggs and eventually
// splits and columns, label added
var configs = vis.getConfig().map(function (col) {
var agg = aggs.byName[col.agg];
if (!agg) {
col.label = col.agg;
} else if (agg.makeLabel) {
col.label = aggs.byName[col.agg].makeLabel(col.aggParams);
} else {
col.label = agg.display || agg.name;
}
return col;
});
var configs = vis.getConfig();

var lastCol = configs[configs.length - 1];

Expand All @@ -43,7 +34,25 @@ define(function (require) {

// all chart data will be written here or to child chartData
// formatted objects
var chartData = {};
var chartData = {
label: _.reduce(configs, function (label, col) {
var agg = aggs.byName[col.agg];

if (!agg) {
col.label = col.agg;
} else if (agg.makeLabel) {
col.label = aggs.byName[col.agg].makeLabel(col.aggParams);
} else {
col.label = agg.display || agg.name;
}

if (label) {
return label + ' > ' + col.label;
} else {
return col.label;
}
}, '')
};

var writeRow = function (rows, bucket) {
// collect the count and bail, free metric!!
Expand Down Expand Up @@ -84,13 +93,19 @@ define(function (require) {
// pick the key for the split's groups
var groupsKey = col.row ? 'rows' : 'columns';

// if (chartData.label) {
// chartData.label = col.label + ' > ' + chartData.label;
// } else {
// chartData.label = col.label;
// }

// the groups will be written here
chartData[groupsKey] = [];

result.buckets.forEach(function (bucket) {
// create a new group for each bucket
var group = {
label: col.label
label: bucket.key
};
chartData[groupsKey].push(group);

Expand Down
66 changes: 32 additions & 34 deletions src/kibana/components/courier/data_source/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ define(function (require) {

var module = require('modules').get('kibana/courier');

module.factory('CouriersSourceAbstract', function (couriersFetch, Promise, timefilter, $q) {
module.factory('CouriersSourceAbstract', function (couriersFetch, Promise, timefilter) {

function SourceAbstract(courier, initialState) {
this._state = (function () {
Expand Down Expand Up @@ -198,44 +198,47 @@ define(function (require) {
/**
* Walk the inheritance chain of a source and return it's
* flat representaion (taking into account merging rules)
* @return {object} - the flat state of the SourceAbstract
* @returns {Promise}
* @resolved {Object|null} - the flat state of the SourceAbstract
*/
SourceAbstract.prototype._flatten = function () {
var type = this._getType();

// the merged state of this dataSource and it's ancestors
var flatState = {};

var collectProp = _.partial(this._mergeProp, flatState);
// function used to write each property from each state object in the chain to flat state
var root = this;

// walk the chain and merge each property
// start the chain at this source
var current = this;
while (current) {
// stop processing if this or one of it's parents is disabled
if (current._fetchDisabled) return;
// merge the properties from the state into the flattened copy
_.forOwn(current._state, collectProp);
// move to this sources parent
current = current._parent;
}
current = null;

// call the ittr and return it's promise
return (function ittr(resolve, reject) {

// TODO: This is a hacky way of getting the timefield, this should be stored in mapping metadata
// and we should check if the data source is time based in the first place;
if (type === 'search') {
// stop processing if current is disabled, returns nothing
if (current._fetchDisabled) return Promise.resolved();

return this._courier._mapper.getCachedFieldsFor(this).then(function (indexPattern) {
// itterate the _state object (not array) and
// pass each key:value pair to collect prop.
return Promise.all(_.map(current._state, function (value, key) {
return root._mergeProp(flatState, value, key);
}))
.then(function () {
// move to this sources parent
current = current._parent;

// keep calling until we reach the top parent
if (current) return ittr();
});
}())
.then(function () {
if (type === 'search') {
// defaults for the query
_.forOwn({
query: {
if (!flatState.body.query) {
flatState.body.query = {
'match_all': {}
}
}, collectProp);

var filter = timefilter.get(indexPattern);
if (!!filter) {
flatState.filters = flatState.filters || [];
flatState.filters.push(filter);
};
}

// switch to filtered query if there are filters
Expand All @@ -254,15 +257,10 @@ define(function (require) {
}
delete flatState.filters;
}
}

return flatState;
});
} else {
var p = $q.defer();
p.resolve(flatState);
return p.promise;
}

return flatState;
});
};

return SourceAbstract;
Expand Down
11 changes: 10 additions & 1 deletion src/kibana/components/courier/data_source/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ define(function (require) {

var module = require('modules').get('kibana/courier');

module.factory('CouriersSearchSource', function (couriersErrors, CouriersSourceAbstract) {
module.factory('CouriersSearchSource', function (couriersErrors, CouriersSourceAbstract, Promise) {
var FetchFailure = couriersErrors.FetchFailure;
var RequestFailure = couriersErrors.RequestFailure;

Expand Down Expand Up @@ -89,6 +89,15 @@ define(function (require) {
* @return {undefined}
*/
SearchSource.prototype._mergeProp = function (state, val, key) {
if (typeof val === 'function') {
var source = this;
return Promise.cast(val(source)).then(function (res) {
return source._mergeProp(state, res, key);
});
}

if (val == null) return;

switch (key) {
case 'filter':
state.filters = state.filters || [];
Expand Down
39 changes: 6 additions & 33 deletions src/kibana/components/courier/fetch/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ define(function (require) {
location: 'Courier Fetch'
});

var flattenRequest = function (req) {
return req.source._flatten();
};

function RequestErrorHandler(courier) { this._courier = courier; }
RequestErrorHandler.prototype.handle = function (req, error) {
if (!this._courier) return req.defer.reject(error);
Expand All @@ -36,11 +32,12 @@ define(function (require) {

all = requests.splice(0);

var allRequests = all.map(flattenRequest);

return $q.all(allRequests).then(function (reqs) {
return Promise.map(all, function (req) {
return req.source._flatten();
})
.then(function (reqs) {
body = strategy.requestStatesToBody(reqs);

return es[strategy.clientMethod]({
body: body
})
Expand All @@ -60,31 +57,7 @@ define(function (require) {
});
throw err;
});

});


/*
return es[strategy.clientMethod]({
body: body
})
.then(function (resp) {
strategy.getResponses(resp).forEach(function (resp) {
var req = all.shift();
if (resp.error) return reqErrHandler.handle(req, new couriersErrors.FetchFailure(resp));
else strategy.resolveRequest(req, resp);
});
// pass the response along to the next promise
return resp;
})
.catch(function (err) {
all.forEach(function (req) {
reqErrHandler.handle(req, err);
});
throw err;
});
*/
}, notify.fatal);
};

var fetchPending = function (strategy, courier) {
Expand Down
3 changes: 3 additions & 0 deletions src/kibana/services/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ define(function (require) {
cb(void 0, val);
}, cb);
};
Promise.map = function (arr, fn) {
return Promise.all(arr.map(fn));
};

/**
* Create a promise that uses our "event" like pattern.
Expand Down
11 changes: 9 additions & 2 deletions src/kibana/services/root_search.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
define(function (require) {
var module = require('modules').get('kibana/services');

module.service('rootSearch', function (courier, config, $rootScope) {
module.service('rootSearch', function (courier, config, timefilter, indexPatterns) {
return courier.createSource('search')
.index(config.get('defaultIndex'));
.index(config.get('defaultIndex'))
.filter(function (source) {
return source.getFields()
.then(function (fields) {
// dynamic time filter will be called in the _flatten phase of things
return timefilter.get(fields);
});
});
});
});

0 comments on commit 97e0a4e

Please sign in to comment.