-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #502 from EverythingMe/feature/alerts
Feature: alerts on query results
- Loading branch information
Showing
22 changed files
with
1,447 additions
and
856 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from redash.models import db, Alert, AlertSubscription | ||
|
||
if __name__ == '__main__': | ||
with db.database.transaction(): | ||
Alert.create_table() | ||
AlertSubscription.create_table() | ||
|
||
db.close_db(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
"trailing": true, | ||
"smarttabs": true, | ||
"globals": { | ||
"angular": false | ||
"angular": false, | ||
"_": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
(function() { | ||
|
||
var AlertsCtrl = function($scope, Events, Alert) { | ||
Events.record(currentUser, "view", "page", "alerts"); | ||
$scope.$parent.pageTitle = "Alerts"; | ||
|
||
$scope.alerts = [] | ||
Alert.query(function(alerts) { | ||
var stateClass = { | ||
'ok': 'label label-success', | ||
'triggered': 'label label-danger', | ||
'unknown': 'label label-warning' | ||
}; | ||
_.each(alerts, function(alert) { | ||
alert.class = stateClass[alert.state]; | ||
}) | ||
$scope.alerts = alerts; | ||
|
||
}); | ||
|
||
$scope.gridConfig = { | ||
isPaginationEnabled: true, | ||
itemsByPage: 50, | ||
maxSize: 8, | ||
}; | ||
|
||
|
||
$scope.gridColumns = [ | ||
{ | ||
"label": "Name", | ||
"map": "name", | ||
"cellTemplate": '<a href="/alerts/{{dataRow.id}}">{{dataRow.name}}</a> (<a href="/queries/{{dataRow.query.id}}">query</a>)' | ||
}, | ||
{ | ||
'label': 'Created By', | ||
'map': 'user.name' | ||
}, | ||
{ | ||
'label': 'State', | ||
'cellTemplate': '<span ng-class="dataRow.class">{{dataRow.state | uppercase}}</span> since <span am-time-ago="dataRow.updated_at"></span>' | ||
}, | ||
{ | ||
'label': 'Created At', | ||
'cellTemplate': '<span am-time-ago="dataRow.created_at"></span>' | ||
} | ||
]; | ||
}; | ||
|
||
var AlertCtrl = function($scope, $routeParams, growl, Query, Events, Alert) { | ||
$scope.$parent.pageTitle = "Alerts"; | ||
|
||
$scope.alertId = $routeParams.alertId; | ||
if ($scope.alertId === "new") { | ||
Events.record(currentUser, 'view', 'page', 'alerts/new'); | ||
} else { | ||
Events.record(currentUser, 'view', 'alert', $scope.alertId); | ||
} | ||
|
||
$scope.onQuerySelected = function(item) { | ||
$scope.selectedQuery = item; | ||
item.getQueryResultPromise().then(function(result) { | ||
$scope.queryResult = result; | ||
$scope.alert.options.column = result.getColumnNames()[0]; | ||
}); | ||
}; | ||
|
||
if ($scope.alertId === "new") { | ||
$scope.alert = new Alert({options: {}}); | ||
} else { | ||
$scope.alert = Alert.get({id: $scope.alertId}, function(alert) { | ||
$scope.onQuerySelected(new Query($scope.alert.query)); | ||
}); | ||
} | ||
|
||
$scope.ops = ['greater than', 'less than', 'equals']; | ||
$scope.selectedQuery = null; | ||
|
||
$scope.getDefaultName = function() { | ||
if (!$scope.alert.query) { | ||
return undefined; | ||
} | ||
return _.template("<%= query.name %>: <%= options.column %> <%= options.op %> <%= options.value %>", $scope.alert); | ||
}; | ||
|
||
$scope.searchQueries = function (term) { | ||
if (!term || term.length < 3) { | ||
return; | ||
} | ||
|
||
Query.search({q: term}, function(results) { | ||
$scope.queries = results; | ||
}); | ||
}; | ||
|
||
$scope.saveChanges = function() { | ||
if ($scope.alert.name === undefined || $scope.alert.name === '') { | ||
$scope.alert.name = $scope.getDefaultName(); | ||
} | ||
|
||
$scope.alert.$save(function() { | ||
growl.addSuccessMessage("Saved."); | ||
}, function() { | ||
growl.addErrorMessage("Failed saving alert."); | ||
}); | ||
}; | ||
}; | ||
|
||
angular.module('redash.directives').directive('alertSubscribers', ['AlertSubscription', function (AlertSubscription) { | ||
return { | ||
restrict: 'E', | ||
replace: true, | ||
templateUrl: '/views/alerts/subscribers.html', | ||
scope: { | ||
'alertId': '=' | ||
}, | ||
controller: function ($scope) { | ||
$scope.subscribers = AlertSubscription.query({alertId: $scope.alertId}); | ||
} | ||
} | ||
}]); | ||
|
||
angular.module('redash.directives').directive('subscribeButton', ['AlertSubscription', 'growl', function (AlertSubscription, growl) { | ||
return { | ||
restrict: 'E', | ||
replace: true, | ||
template: '<button class="btn btn-default btn-xs" ng-click="toggleSubscription()"><i ng-class="class"></i></button>', | ||
controller: function ($scope) { | ||
var updateClass = function() { | ||
if ($scope.subscription) { | ||
$scope.class = "fa fa-eye-slash"; | ||
} else { | ||
$scope.class = "fa fa-eye"; | ||
} | ||
} | ||
|
||
$scope.subscribers.$promise.then(function() { | ||
$scope.subscription = _.find($scope.subscribers, function(subscription) { | ||
return (subscription.user.email == currentUser.email); | ||
}); | ||
|
||
updateClass(); | ||
}); | ||
|
||
$scope.toggleSubscription = function() { | ||
if ($scope.subscription) { | ||
$scope.subscription.$delete(function() { | ||
$scope.subscribers = _.without($scope.subscribers, $scope.subscription); | ||
$scope.subscription = undefined; | ||
updateClass(); | ||
}, function() { | ||
growl.addErrorMessage("Failed saving subscription."); | ||
}); | ||
} else { | ||
$scope.subscription = new AlertSubscription({alert_id: $scope.alertId}); | ||
$scope.subscription.$save(function() { | ||
$scope.subscribers.push($scope.subscription); | ||
updateClass(); | ||
}, function() { | ||
growl.addErrorMessage("Unsubscription failed."); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}]); | ||
|
||
angular.module('redash.controllers') | ||
.controller('AlertsCtrl', ['$scope', 'Events', 'Alert', AlertsCtrl]) | ||
.controller('AlertCtrl', ['$scope', '$routeParams', 'growl', 'Query', 'Events', 'Alert', AlertCtrl]) | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.