Skip to content

Instantly share code, notes, and snippets.

Created June 14, 2016 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0c68fbd87574d61377b54981a09f66e5 to your computer and use it in GitHub Desktop.
Save anonymous/0c68fbd87574d61377b54981a09f66e5 to your computer and use it in GitHub Desktop.
function getImpactLevel(alert) {
var impact = alert.impact.trim().toLowerCase();
if (impact === "major") {
return 0;
} else if (impact === "significant") {
return 1;
} else if (impact === "minor") {
return 2;
} else if (impact === "none") {
return 3;
} else {
return 4;
}
}
angular.module("cmat.controllers")
.controller('watchbox.alerts', function ($scope, $rootScope, $location, $http, AlertsService, WatchListService, $filter, $log, OwfService) {
$scope.smallNavbar = OwfService.isRunningInOWF;
$scope.toggleNavbar = function () { $scope.smallNavbar = !$scope.smallNavbar; };
$scope.sortOptions = [
{title: "Date/Time", attribute: "timeStart", reverse: true},
{title: "Impact Level", attribute: getImpactLevel, reverse: false}
];
$scope.sortOption = $scope.sortOptions[0];
$scope.isActive = function (location) {
return location === $location.path();
};
$scope.setSort = function(option) {
$log.debug("sort by " + option.title);
$scope.sortOption = option;
};
$scope.clearAll = function () {
_.each(AlertsService.alerts, function (alert) {
alert.message = "Deleting...";
alert.error = null;
});
var url = "api/rest/alerts";
$http.delete(url).then(function () {
_.each(AlertsService.alerts, function (alert) {
alert.message = "Alert queued for deletion.";
});
}).catch(function (e) {
_.each(AlertsService.alerts, function (alert) {
alert.error = e.status;
alert.message = "Could not delete alert!";
});
});
};
$scope.clear = function (alert) {
alert.message = "Deleting...";
alert.error = null;
var url = "api/watchbox/alert/" + alert.id;
$http.delete(url).then(function () {
alert.message = "Alert queued for deletion...";
}).catch(function (e) {
alert.error = e.status;
alert.message = "Could not delete alert!";
});
};
$scope.anyImpact = function (impact) {
return AlertsService.anyImpact(AlertsService.filtered(), impact);
};
$scope.countImpact = function (impact) {
return AlertsService.countImpact(AlertsService.filtered(), impact);
};
$scope.filtered = AlertsService.filtered;
$scope.generate = false;
$scope.flyTo = function(id) {
id = id[0];
$scope.$parent.flyTo = id;
$rootScope.flyTo = id;
};
$scope.flyToAll = function (alert) {
$scope.flyTo(_.pluck(alert.impacted, "id"));
};
$scope.clear = function (alert) {
alert.message = "Deleting...";
alert.error = null;
var url = "api/watchbox/alert/" + alert.id;
$http.delete(url).then(function () {
alert.message = "Alert queued for deletion...";
}).catch(function (e) {
alert.error = e.status;
alert.message = "Could not delete alert!";
});
};
$scope.restart = function(){
$http.put("api/watchbox/restart").then(function(){
$http.get("api/watchbox/alerts");
});
};
$scope.changeView = function() {
if ($scope.generate) {
return 'src/html/watchbox/generate.html';
} else {
return 'src/html/watchbox/alerts.html';
}
}
})
.controller('watchbox.generate', function ($scope, $log, $http, $timeout, GenerateService, $rootScope) {
$scope.service = GenerateService;
$scope.addImpact = function (alert, impact) {
if (impact) {
$log.debug(impact);
alert.impacts.push(impact);
alert.dirtyImpact = "";
}
};
$scope.removeImpact = function (alert, impact) {
alert.impacts = _.without(alert.impacts, impact);
};
// $scope.generateAll = function () {
// _.each(GenerateService.demo, function (alert, idx) {
// $timeout(function () { $scope.generate(alert); }, idx * 1000);
// });
// };
$scope.generate = function (alert) {
$log.debug("generating alert " + alert.alert + " (" + alert.impacts.join(",") + ")");
alert.message = "Generating alert...";
alert.error = null;
alert.timestamp = null;
var url = "api/watchbox/generate?"
+ "alert=" + encodeURIComponent(alert.alert)
+ "&impact=" + encodeURIComponent(alert.impact)
+ "&detail=" + encodeURIComponent(alert.detail)
+ "&networkNodeName=" + encodeURIComponent(alert.networkNodeName);
+ "&networkNodeId=" + encodeURIComponent(alert.networkNodeId);
_.each(alert.impacts, function (impact) {
url += "&impacts=" + encodeURIComponent(impact);
});
$http.put(url).success(function () {
$log.debug("PUT " + url + " success");
alert.message = null;
alert.timestamp = new Date();
alert.editMode = false;
$rootScope.$broadcast('alert', alert);
}).error(function (e, status) {
$log.error("could not generate alert");
alert.message = "Could not generate alert!";
alert.error = status;
});
};
$scope.launchScenario = function (scenarioNumber) {
$http.get("test/json/alerts/demo" + scenarioNumber + ".json").then(
function(response) {
console.log("Demo Script " + scenarioNumber + " Data Fetched");
_.each(response.data, function (alert) {
$scope.generate(alert);
});
});
};
$scope.deleteAlert = function (alert) {
GenerateService.demo = _.without(GenerateService.demo, alert);
};
// $scope.newAlert = function () {
// GenerateService.demo.unshift({
// alert: "New Alert",
// impact: "major",
// impacts: [],
// editMode: true
// });
// }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment