Skip to content

Commit

Permalink
Merge pull request #72 from alexweltman/4.1.10_NM_updateToUseNewApiRo…
Browse files Browse the repository at this point in the history
…utes

Updated to use new Query Rules API
  • Loading branch information
alexweltman authored Jan 27, 2017
2 parents f7ef6b6 + e8148fc commit 9ec2e72
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ define(function (require) {
return config;
});
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
if (data && data.status === 'success') {
if ((data && data.status === 'success') || (response.status === 200)){
if (response.headers('Token')) {
localStorage.setItem('token', response.headers('Token'));
}
Expand Down
67 changes: 24 additions & 43 deletions src/kibana/netmon_libs/custom_modules/save_rule/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ define(function (require) {
$scope.ejs = ejsResource('https://' + window.location.hostname);
$scope.elasticSearchFields = elasticSearchFields;
$scope.validators = formValidators;

$scope.savableRule = {
enabled: true
};
$scope.modalState = {
state: 'unsaved'
};

if (query.query_string.query && query.query_string.query !== '*') {
$scope.savableRule.queryObj = query;
$scope.savableRule.query = query.query_string.query;
}
};
Expand All @@ -36,8 +37,8 @@ define(function (require) {
var from = moment().subtract('days', 1).toDate(),
to = moment().toDate();

$scope.savableRule.state = 'unconfirmed';
$scope.savableRule.loading = true;
$scope.modalState.state = 'unconfirmed';
$scope.modalState.loading = true;
form.$setPristine();

kbnIndex.indices(from, to, '[network_]YYYY_MM_DD', 'day')
Expand All @@ -59,60 +60,40 @@ define(function (require) {
.doSearch()
.then(
function(result) {
$scope.savableRule.loading = false;
$scope.modalState.loading = false;

if (result && result.facets && result.facets.rules && result.facets.rules.count !== undefined) {
$scope.savableRule.numMatches = result.facets.rules.count;
$scope.modalState.numMatches = result.facets.rules.count;
} else {
$scope.savableRule.state = 'error';
$scope.savableRule.error = 'There was a problem executing your search.';
$scope.modalState.state = 'error';
$scope.modalState.error = 'There was a problem executing your search.';
}
},
function() {
$scope.savableRule.loading = false;
$scope.savableRule.state = 'error';
$scope.savableRule.error = 'There was a problem executing your search.';
$scope.modalState.loading = false;
$scope.modalState.state = 'error';
$scope.modalState.error = 'There was a problem executing your search.';
}
);
},
function() {
$scope.savableRule.loading = false;
$scope.savableRule.state = 'error';
$scope.savableRule.error = 'There was a problem executing your search.';
$scope.modalState.loading = false;
$scope.modalState.state = 'error';
$scope.modalState.error = 'There was a problem executing your search.';
}
);
};

$scope.saveRule = function(rule) {
rule.query = $scope.elasticSearchFields.convertQuery(rule.query);

var query = $scope.ejs.QueryStringQuery(rule.query);

rule.loading = true;
Restangular
.all('rules/')
.post({
name: rule.name,
enabled: true,
severity: rule.severity,
query: angular.fromJson(query.toString())
})
.then(
function(data) {
if (!data.error){
$scope.savableRule = angular.copy(data);
$scope.savableRule.state = 'saved';
$scope.savableRule.loading = false;
} else{
$scope.savableRule = {
loading: false,
state: 'error',
error: data.message
};
}
},
function() {
$scope.savableRule = {
$scope.modalState.loading = true;
$http.put('/api/queryRules/' + rule.id, rule)
.then(function(data) {
$scope.savableRule = angular.copy(data);
$scope.modalState.state = 'saved';
$scope.modalState.loading = false;
},
function(error) {
$scope.modalState = {
loading: false,
state: 'error',
error: 'There was a problem saving your rule.'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<div ng-switch="savableRule.state">
<div ng-switch="modalState.state">

<!-- Confirm save -->
<div ng-switch-when="unconfirmed">
<div class="modal-header">
<h3>Create Rule</h3>
</div>
<div class="modal-body" ng-if="!!savableRule.loading">
<div class="modal-body" ng-if="!!modalState.loading">
<div class="loading">
<img ng-src="{{loadingIconPath}}" />
</div>
</div>
<div class="modal-body" ng-if="!savableRule.loading">
<p>In the last 24 hours, this rule would have triggered <strong>{{ savableRule.numMatches | number }}</strong> time(s).</p>
<p>Are you sure you want to create the rule {{ savableRule.name }}?</p>
<div class="modal-body" ng-if="!modalState.loading">
<p>In the last 24 hours, this rule would have triggered <strong>{{ modalState.numMatches | number }}</strong> time(s).</p>
<p>Are you sure you want to create the rule {{ savableRule.id }}?</p>
</div>
<div class="modal-footer">
<button class="btn btn-primary-orig" type="button" ng-click="saveRule(savableRule)" ng-if="!savableRule.id && !savableRule.loading">Confirm</button>
<button class="btn btn-primary-orig" type="button" ng-click="saveRule(savableRule)" ng-if="!modalState.loading">Confirm</button>
<button class="btn btn-default-orig" type="button" ng-click="cancel()">Cancel</button>
</div>
</div>
Expand All @@ -25,12 +25,12 @@ <h3>Create Rule</h3>
<div class="modal-header">
<h3>Save Complete</h3>
</div>
<div class="modal-body" ng-if="!!savableRule.loading">
<div class="modal-body" ng-if="!!modalState.loading">
<div class="loading">
<img ng-src="{{loadingIconPath}}" />
</div>
</div>
<div class="modal-body" ng-if="!savableRule.loading">
<div class="modal-body" ng-if="!modalState.loading">
Rule saved successfully.
</div>
<div class="modal-footer">
Expand All @@ -43,13 +43,13 @@ <h3>Save Complete</h3>
<div class="modal-header">
<h3>Error</h3>
</div>
<div class="modal-body" ng-if="!!savableRule.loading">
<div class="modal-body" ng-if="!!modalState.loading">
<div class="loading">
<img ng-src="{{loadingIconPath}}" />
</div>
</div>
<div class="modal-body" ng-if="!savableRule.loading">
{{ savableRule.error }}
<div class="modal-body" ng-if="!modalState.loading">
{{ modalState.error }}
</div>
<div class="modal-footer">
<button class="btn btn-default-orig" type="button" ng-click="cancel()">Close</button>
Expand All @@ -61,12 +61,12 @@ <h3>Error</h3>
<div class="modal-header">
<h3>Create Rule</h3>
</div>
<div class="modal-body" ng-if="!!savableRule.loading">
<div class="modal-body" ng-if="!!modalState.loading">
<div class="loading">
<img ng-src="{{loadingIconPath}}" />
</div>
</div>
<div class="modal-body" ng-if="!savableRule.loading">
<div class="modal-body" ng-if="!modalState.loading">
<form name="$parent.saveRuleForm" class="form-horizontal LR-rule-modal">
<input type="hidden" name="enabled" ng-model="savableRule.enabled" />
<div class="form-group">
Expand All @@ -80,7 +80,7 @@ <h3>Create Rule</h3>
name="name"
id="inputName"
class="form-control"
ng-model="savableRule.name"
ng-model="savableRule.id"
ui-validate="{ required : 'validators.required($value)', basicCharacters : 'validators.basicCharacters($value)', uniqueRule : 'validators.uniqueRule($value, savableRule.id)' }"
/>
<div class="formValidatorError" ng-show="validators.showError(saveRuleForm.name, 'required')">Name is required.</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
* between Kibana and NetMon
*/
define(function (require) {

var app = require('modules').get('app/dashboard');

app.factory('formValidators', function ($q, $timeout, $http, ejsResource, Restangular) {
var timeouts = {};
return {
hasErrors: function(formCtrl) {
return !formCtrl.$pristine && !formCtrl.$valid;
},
showError: function(formCtrl, field) {
showError: function(formCtrl, field) {
return !formCtrl.$pristine && !!formCtrl.$error[field];
},
required: function(value) {
Expand All @@ -32,18 +32,14 @@ define(function (require) {
$timeout.cancel(timeouts['uniqueRule']);
}
timeouts['uniqueRule'] = $timeout(function() {
Restangular
.one('rules/')
.get({ name: value })
$http.get('/api/queryRules/' + value)
.then(function(rule) {
if (!rule || rule.error || !rule.id) {
deferred.resolve();
} else {
deferred.reject();
}
deferred.reject();
}, function(error){
deferred.resolve();
});
}, 500);

return deferred.promise;
},
notAllQuery: function(value) {
Expand Down Expand Up @@ -82,4 +78,4 @@ define(function (require) {
}
};
});
});
});

0 comments on commit 9ec2e72

Please sign in to comment.