Skip to content

Commit

Permalink
Merge pull request #13 from spenceralger/discover_skeleton
Browse files Browse the repository at this point in the history
Discovery Panel Skeleton
  • Loading branch information
spenceralger committed Feb 26, 2014
2 parents da9d6a1 + 7297c32 commit aa4d978
Show file tree
Hide file tree
Showing 22 changed files with 339 additions and 88 deletions.
31 changes: 20 additions & 11 deletions src/courier/courier.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ define(function (require) {
// execute a search right now
search: function (courier) {
if (courier._activeSearchRequest) {
return courier._error(new HastyRefresh());
// ensure that this happens async, otherwise listeners
// might miss error events
return nextTick(function () {
courier._error(new HastyRefresh());
});
}

courier._activeSearchRequest = SearchSource.fetch(
Expand Down Expand Up @@ -174,19 +178,24 @@ define(function (require) {
}, this);
};

// be default, the courier will throw an error if a fetch
// occurs before a previous fetch finishes. To prevent this, you
// should call abort before calling .fetch()
Courier.prototype.abort = function () {
if (this._activeSearchRequest) {
this._activeSearchRequest.abort();
this._activeSearchRequest = null;
}
};

// force a fetch of all datasources right now, optionally filter by type
Courier.prototype.fetch = function (onlyType) {
var courier = this;
// ensure that onFetch functions always run after the tick
// so that users can will be able to listen after or before the call to
// fetch and always get the same behavior (even if the onFetch runs synchronously)
nextTick(function () {
_.forOwn(onFetch, function (fn, type) {
if (onlyType && onlyType !== type) return;
if (courier._refs[type].length) fn(courier);
courier._refs[type].forEach(function (ref) {
ref.fetchCount ++;
});
_.forOwn(onFetch, function (fn, type) {
if (onlyType && onlyType !== type) return;
if (courier._refs[type].length) fn(courier);
courier._refs[type].forEach(function (ref) {
ref.fetchCount ++;
});
});
};
Expand Down
68 changes: 38 additions & 30 deletions src/courier/data_source/data_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ define(function (require) {
var _ = require('lodash');
var EventEmitter = require('utils/event_emitter');
var Mapper = require('courier/mapper');
var IndexPattern = require('courier/index_pattern');

function DataSource(courier, initialState) {
var state;
Expand Down Expand Up @@ -54,9 +53,6 @@ define(function (require) {
this._methods.forEach(function (name) {
this[name] = function (val) {
state[name] = val;
if (name === 'index' && arguments[1]) {
state.index = new IndexPattern(val, arguments[1]);
}
return this;
};
}, this);
Expand All @@ -81,8 +77,8 @@ define(function (require) {
* @callback {Error, Array} - calls cb with a possible error or an array of field names
* @todo
*/
DataSource.prototype.getFieldNames = function (cb) {
throw new Error('not implemented');
DataSource.prototype.getFields = function (cb) {
this._courier._mapper.getFields(this, this._wrapcb(cb));
};

/**
Expand Down Expand Up @@ -114,32 +110,33 @@ define(function (require) {
* @return {this} - chainable
*/
DataSource.prototype.$scope = function ($scope) {
var emitter = this;
var courier = this;

if (emitter._emitter$scope) {
emitter._emitter$scope = $scope;
if (courier._$scope) {
// simply change the scope that callbacks will point to
courier._$scope = $scope;
return this;
}

emitter._emitter$scope = $scope;
var origOn = emitter.on;

emitter.on = function (event, listener) {
var wrapped = function () {
var args = arguments;
// always use the stored ref so that it can be updated if needed
var $scope = emitter._emitter$scope;
$scope[$scope.$$phase ? '$eval' : '$apply'](function () {
listener.apply(emitter, args);
});
};
courier._$scope = $scope;

// wrap the 'on' method so that all listeners
// can be wrapped in calls to $scope.$apply
var origOn = courier.on;
courier.on = function (event, listener) {
var wrapped = courier._wrapcb(listener);
// set .listener so that it can be removed by
// .removeListener() using the original function
wrapped.listener = listener;
return origOn.call(emitter, event, wrapped);
return origOn.call(courier, event, wrapped);
};

emitter.on.restore = function () {
delete emitter._emitter$scope;
emitter.on = origOn;
// make sure the alias is still set
courier.addListener = courier.on;

courier.on.restore = function () {
delete courier._$scope;
courier.on = courier.addListener = origOn;
};

return this;
Expand Down Expand Up @@ -213,11 +210,22 @@ define(function (require) {
return flatState;
};

DataSource.prototype._resolveIndexPattern = function (start, end) {
if (this._state.indexInterval) {
throw new Error('Not implemented');
}
return this._state.index;
DataSource.prototype._wrapcb = function (cb) {
var courier = this;
var wrapped = function () {
var args = arguments;
// always use the stored ref so that it can be updated if needed
var $scope = courier._$scope;

// don't fall apart if we don't have a scope
if (!$scope) return cb.apply(courier, args);

// use angular's $apply or $eval functions for the given scope
$scope[$scope.$$phase ? '$eval' : '$apply'](function () {
cb.apply(courier, args);
});
};
return wrapped;
};

return DataSource;
Expand Down
7 changes: 4 additions & 3 deletions src/courier/data_source/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ define(function (require) {
var DataSource = require('courier/data_source/data_source');
var inherits = require('utils/inherits');
var nextTick = require('utils/next_tick');
var errors = require('courier/errors');
var VersionConflict = require('courier/errors').VersionConflict;
var FetchFailure = require('courier/errors').FetchFailure;
var listenerCount = require('utils/event_emitter').listenerCount;
var _ = require('lodash');

Expand Down Expand Up @@ -42,7 +43,7 @@ define(function (require) {
var ref = allRefs[i];
var source = ref.source;

if (resp.error) return source._error(new errors.DocFetchFailure(resp));
if (resp.error) return source._error(new FetchFailure(resp));
if (resp.found) {
if (ref.version === resp._version) return; // no change
ref.version = resp._version;
Expand Down Expand Up @@ -147,7 +148,7 @@ define(function (require) {
if (err) return cb(err);

if (resp && resp.status === 409) {
err = new errors.VersionConflict(resp);
err = new VersionConflict(resp);
if (listenerCount(source, 'conflict')) {
return source.emit('conflict', err);
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/courier/data_source/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ define(function (require) {
var DataSource = require('courier/data_source/data_source');
var inherits = require('utils/inherits');
var errors = require('courier/errors');
var FetchFailure = require('courier/errors').FetchFailure;
var _ = require('lodash');

function SearchSource(courier, initialState) {
Expand Down Expand Up @@ -44,7 +45,7 @@ define(function (require) {

_.each(resp.responses, function (resp, i) {
var source = allRefs[i];
if (resp.error) return errors.emit(source, courier, resp);
if (resp.error) return source._error(new FetchFailure(resp));
source.emit('results', resp);
});

Expand Down
8 changes: 4 additions & 4 deletions src/courier/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ define(function (require) {


/**
* DocFetchFailure Error - where there is an error getting a doc
* FetchFailure Error - where there is an error getting a doc
* @param {String} [msg] - An error message that will probably end up in a log.
*/
errors.DocFetchFailure = function DocFetchFailure(resp) {
errors.FetchFailure = function FetchFailure(resp) {
CourierError.call(this,
'Failed to get the doc: ' + JSON.stringify(resp),
errors.DocFetchFailure);
errors.FetchFailure);

this.resp = resp;
};
inherits(errors.DocFetchFailure, CourierError);
inherits(errors.FetchFailure, CourierError);


/**
Expand Down
23 changes: 0 additions & 23 deletions src/courier/index_pattern.js

This file was deleted.

12 changes: 5 additions & 7 deletions src/courier/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,15 @@ define(function (require) {

// TODO: Add week/month check
client.indices.getFieldMapping(params, function (err, response, status) {

// TODO: Add error message

var fields = {};

_.each(response, function (index) {
_.each(response, function (index, indexName) {
if (indexName === config.cacheIndex) return;
_.each(index.mappings, function (type) {
_.each(type, function (field, name) {
if (_.isUndefined(field.mapping) || name[0] === '_') return;
if (!_.isUndefined(fields[name]) && fields[name] !== field.mapping[_.keys(field.mapping)[0]])
return courier._error(new Error.MappingConflict());
if (_.size(field.mapping) === 0 || name[0] === '_') return;
if (!_.isUndefined(fields[name]) && fields[name].type !== field.mapping[_.keys(field.mapping)[0]].type)
return courier._error(new Error.MappingConflict(name));
fields[name] = field.mapping[_.keys(field.mapping)[0]];
});
});
Expand Down
Empty file.
44 changes: 43 additions & 1 deletion src/kibana/apps/discover/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
<h1>Discover</h1>
<div ng-controller="discover">
<h1>Discover</h1>
<div class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-2">Index</label>
<div class="col-sm-10">
<input class="form-control" ng-model="index">
</div>
</div>
<!-- <div class="form-group">
<label class="control-label col-sm-3">Repeat Interval</label>
<div class="col-sm-9">
<select
class="form-control"
ng-model="interval"
ng-options="i.display for i in intervalOptions">
</select>
</div>
</div> -->
<form class="form-group" ng-submit="reset()">
<label class="control-label col-sm-2">Query</label>
<div class="col-sm-10">
<div class="input-group">
<input class="form-control" ng-model="query" >
<span class="input-group-btn">
<button type="button" class="btn" ng-click="reset()">
<i class="glyphicon glyphicon-search"></i>
</button>
</span>
</div>
</div>
</form>
</div>
<div class="input-group">
<label class="control-label col-sm-2">Limit</label>
<select
class="form-control"
ng-model="size"
ng-options="size.display for size in sizeOptions">
</select>
</div>
<kbn-table rows="rows" columns="columns"></kbn-table>
</div>
Loading

0 comments on commit aa4d978

Please sign in to comment.