Merge pull request #3164 from rashidkpc/feature/save-time-with-dashboard

Add option to save time with dashboard
This commit is contained in:
Rashid Khan 2015-04-23 16:14:26 -07:00
commit 4b5d401892
7 changed files with 59 additions and 20 deletions

View file

@ -43,6 +43,7 @@ define(function (require) {
// the id of the document
self.id = config.id || void 0;
self.defaults = config.defaults;
/**
* Asynchronously initialize this object - will only run
@ -185,14 +186,12 @@ define(function (require) {
});
}
/**
* Save this object
* Serialize this object
*
* @return {Promise}
* @resolved {String} - The id of the doc
* @return {Object}
*/
self.save = function () {
self.serialize = function () {
var body = {};
_.forOwn(mapping, function (fieldMapping, fieldName) {
@ -209,6 +208,18 @@ define(function (require) {
};
}
return body;
};
/**
* Save this object
*
* @return {Promise}
* @resolved {String} - The id of the doc
*/
self.save = function () {
var body = self.serialize();
// Slugify the object id
self.id = slugifyId(self.id);

View file

@ -1,19 +1,23 @@
define(function (require) {
var _ = require('lodash');
var modules = require('modules');
var urlParam = '_a';
function AppStateProvider(Private, $rootScope, getAppState) {
var State = Private(require('components/state_management/state'));
_(AppState).inherits(State);
function AppState(defaults) {
AppState.Super.call(this, '_a', defaults);
AppState.Super.call(this, urlParam, defaults);
getAppState._set(this);
}
// if the url param is missing, write it back
AppState.prototype._persistAcrossApps = false;
AppState.prototype.destroy = function () {
AppState.Super.prototype.destroy.call(this);
getAppState._set(null);
@ -26,13 +30,19 @@ define(function (require) {
.factory('AppState', function (Private) {
return Private(AppStateProvider);
})
.service('getAppState', function () {
.service('getAppState', function ($location) {
var currentAppState;
function get() {
return currentAppState;
}
// Checks to see if the appState might already exist, even if it hasn't been newed up
get.previouslyStored = function () {
var search = $location.search();
return search[urlParam] ? true : false;
};
get._set = function (current) {
currentAppState = current;
};

View file

@ -49,7 +49,7 @@ define(function (require) {
app.directive('dashboardApp', function (Notifier, courier, AppState, timefilter, kbnUrl) {
return {
controller: function ($scope, $route, $routeParams, $location, configFile, Private) {
controller: function ($scope, $route, $routeParams, $location, configFile, Private, getAppState) {
var filterBarWatchFilters = Private(require('components/filter_bar/lib/watchFilters'));
var notify = new Notifier({
@ -57,6 +57,12 @@ define(function (require) {
});
var dash = $scope.dash = $route.current.locals.dash;
if (dash.timeRestore && dash.timeTo && dash.timeFrom && !getAppState.previouslyStored()) {
timefilter.time.to = dash.timeTo;
timefilter.time.from = dash.timeFrom;
}
$scope.$on('$destroy', dash.destroy);
var matchQueryFilter = function (filter) {
@ -135,6 +141,8 @@ define(function (require) {
$state.title = dash.id = dash.title;
$state.save();
dash.panelsJSON = angular.toJson($state.panels);
dash.timeFrom = dash.timeRestore ? timefilter.time.from : undefined;
dash.timeTo = dash.timeRestore ? timefilter.time.to : undefined;
dash.save()
.then(function (id) {

View file

@ -3,5 +3,11 @@
<label for="dashboardTitle">Save As</label>
<input id="dashboardTitle" type="text" ng-model="opts.dashboard.title" class="form-control" placeholder="Dashboard title" input-focus="select">
</div>
<div class="form-group">
<label>
<input type="checkbox" ng-model="opts.dashboard.timeRestore" ng-checked="opts.dashboard.timeRestore">
Store time with dashboard <i class="fa fa-info-circle ng-scope" tooltip="Change the time filter to the currently selected time each time this dashboard is loaded" tooltip-placement="" tooltip-popup-delay="250"></i>
</label>
</div>
<button type="submit" ng-disabled="!opts.dashboard.title" class="btn btn-primary" aria-label="Save dashboard">Save</button>
</form>

View file

@ -1,6 +1,7 @@
define(function (require) {
var module = require('modules').get('app/dashboard');
var _ = require('lodash');
var moment = require('moment');
// Used only by the savedDashboards service, usually no reason to change this
module.factory('SavedDashboard', function (courier) {
@ -23,7 +24,10 @@ define(function (require) {
hits: 'integer',
description: 'string',
panelsJSON: 'string',
version: 'integer'
version: 'integer',
timeRestore: 'boolean',
timeTo: 'string',
timeFrom: 'string'
},
// defeult values to assign to the doc
@ -32,7 +36,10 @@ define(function (require) {
hits: 0,
description: '',
panelsJSON: '[]',
version: 1
version: 1,
timeRestore: false,
timeTo: undefined,
timeFrom: undefined
},
searchSource: true,

View file

@ -24,6 +24,7 @@
<textarea rows="1" msd-elastic ng-if="field.type === 'text'" ng-model="field.value" class="form-control span12"/>
<input ng-if="field.type === 'number'" type="number" ng-model="field.value" class="form-control span12"/>
<div ng-if="field.type === 'json' || field.type === 'array'" ui-ace="{ onLoad: aceLoaded, mode: 'json' }" id="{{field.name}}" ng-model="field.value" class="form-control"></div>
<input ng-if="field.type === 'boolean'" type="checkbox" ng-model="field.value" ng-checked="field.value">
</div>
</form>
<div class="form-group">

View file

@ -55,11 +55,12 @@ define(function (require) {
} else if (_.isArray(field.value)) {
field.type = 'array';
field.value = angular.toJson(field.value, true);
} else if (_.isBoolean(field.value)) {
field.type = 'boolean';
field.value = field.value;
} else if (_.isPlainObject(field.value)) {
// do something recursive
return _.reduce(field.value, _.partialRight(createField, parents), memo);
} else {
return;
}
memo.push(field);
@ -74,15 +75,10 @@ define(function (require) {
$scope.title = inflection.singularize(serviceObj.title);
es.get({
index: config.file.kibana_index,
type: service.type,
id: $routeParams.id
})
.then(function (obj) {
service.get($routeParams.id).then(function (obj) {
$scope.obj = obj;
$scope.link = service.urlFor(obj._id);
$scope.fields = _.reduce(obj._source, createField, []);
$scope.link = service.urlFor(obj.id);
$scope.fields = _.reduce(_.defaults(obj.serialize(), obj.defaults), createField, []);
})
.catch(notify.fatal);