/*
* site-monitor/services/monitoring.js Copyright 2021 cPanel, L.L.C.
* All rights reserved.
* copyright@cpanel.net http://cpanel.net
* This code is subject to the cPanel license. Unauthorized copying is prohibited
*/
/* global define, PAGE */
/** @namespace cpanel.siteMonitor.services.monitoring */
define(
[
"angular",
"lodash",
"cjt/util/locale",
"cjt/io/uapi-request",
"cjt/io/uapi",
"cjt/modules",
"cjt/services/APICatcher",
],
function(angular, _, LOCALE, UAPIRequest) {
"use strict";
var app = angular.module("cpanel.siteMonitor.monitoringService", [
"cjt2.services.apicatcher",
]);
app.factory("monitoring", [
"$q",
"APICatcher",
"PAGE",
function($q, APICatcher, PAGE) {
var MonitorAPI = function() {};
MonitorAPI.prototype = Object.create(APICatcher);
/**
* Fake list method to make the build work.
*
* @async
* @method get
* @returns {IMonitorItem[]} - list of all the monitors available from the nixstats engine for this user.
*/
MonitorAPI.prototype.list = function list() {
var self = this;
var apiCall = new UAPIRequest.Class();
apiCall.initialize("Monitoring", "get_all_site_monitors");
return self.promise(apiCall).then(function(result) {
return result.data.response.monitors;
});
}
/**
* Get the monitor by its uniqe id
*
* @async
* @method get
* @params {String} id - the id of the monitor
* @returns {IMonitorDetails} The details of the requested monitor.
*/
MonitorAPI.prototype.get = function get(id) {
var self = this;
if (typeof(id) !== 'string' || id === '') {
throw new Error('The `id` argument is required.')
}
var apiCall = new UAPIRequest.Class();
apiCall.initialize("Monitoring", "get_site_monitor");
apiCall.addArgument("id", id);
return self.promise(apiCall).then(function(result) {
return result.data.response;
});
};
/**
* Enable monitoring for the site
*
* @async
* @method enable
* @params {String} site - the url to monitor. Example: domain.com/api/list?p=1
* @params {String} [ name ] - optional name of the monitor, will default to the domain from the url if not provided
* @returns {boolean} returns true if successful, false otherwise.
*/
MonitorAPI.prototype.enable = function enable(site, name, emails) {
var self = this;
if (typeof(site) !== 'string' || site === '') {
throw new Error('The `domain` argument is required.')
}
if (emails && !Array.isArray(emails)) {
throw new Error('The `emails` argument must be an array of email addresses.');
}
var apiCall = new UAPIRequest.Class();
apiCall.initialize("Monitoring", "create_site_monitor");
apiCall.addArgument("site", site);
apiCall.addArgument("name", name || site);
apiCall.addArgument("email", emails);
return self.promise(apiCall).then(function(result) {
if (result.data.http_code === "200") {
return result.data.response.id;
} else {
throw new Error(LOCALE.maketext("The system could not create the monitor for [_1] with the error: [_2]", site, result.data.response.error))
}
});
};
/**
* Disable an existing site monitor
*
* @async
* @method disable
* @params {String} id - the unique id of the monitor
* @params {String} url - the url for the monitor. Only used in messages.
* @returns {boolean} returns true if successful, false otherwise.
*/
MonitorAPI.prototype.disable = function disable(id, url) {
var self = this;
if (typeof(id) !== 'string' || id === '') {
throw new Error('The `id` argument is required.')
}
var apiCall = new UAPIRequest.Class();
apiCall.initialize("Monitoring", "remove_site_monitor");
apiCall.addArgument("id", id);
return self.promise(apiCall).then(function(result) {
if (result.data.http_code === "204") {
return true;
} else {
throw new Error(LOCALE.maketext("The system could not remove the monitor for [_1] with the error: [_2]", url, result.data.response.error))
}
});
};
return new MonitorAPI();
}
]);
}
);