Viewing File: /usr/local/cpanel/base/frontend/jupiter/site-monitor/models/site.js

/*
 * site-monitor/models/site.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.siteMoniter.models.site */

define([
        "lodash",
        "app/models/monitor",
        "app/models/domain-type.enum",
    ],
    function(_, Monitor, DomainType) {

        "use strict";

        /**
         * @typedef Site
         * @property {string} documentRoot - the file system path to where the sites files are stored, if any.
         * @property {string} domain - the domain the site is hosted under.
         * @property {boolean} hasValidHTTPSAliases - ???
         * @property {string} homedir - the homedir for the user that owns the site, if any.
         * @property {boolean} isHttpsRedirecting - true when the domain is always redirect to HTTPS.
         * @property {Monitor[]} monitors - list of monitors attached to the site.
         * @property {boolean} fetchFailed - true if the fetch failed, false otherwise.
         * @property {boolean} nonHTTPS - false if the site is HTTPS, true otherwise.
         * @property {string} protocol - one of: http, https
         * @property {string} realRootDomain - the main domain for the account.
         * @property {string} redirectsTo - the url the site redirects to, if any.
         * @property {string} rootDomain - the root domain the site is hosted on.
         * @property {DomainType} type - the type of the domain.
         */


        function Site(site) {
            var self = this;
            self.monitors = site.monitors || [];
            self.fetchFailed = site.fetchFailed || false;

            Object.keys(site).forEach(function(key) {
                self[key] = site[key];
            });

            self.type = DomainType[DomainType.fromString(site.type)];
            self.protocol = self.isHttpsRedirecting ? "https" : "http";
        }

        /**
         * Generate a default match function if one is not provided. By default we match by name.
         *
         * @param {Partial<Monitor>} value - The monitor to find. Only needs the fields consumed by the match function
         * @param {Function} match - The match function, will match by name if not provided.
         * @returns
         */
        Site.prototype._makeMatcher = function _makeMatcher(partial, match) {
            if (!match || typeof match !== 'function') {
                match = function(m) {
                    return m.name === partial.name;
                };
            }
            else {
                // Generate the matcher from the factory function
                match = match(partial);
            }
            return match;
        }

        /**
         * Find matching monitors in the collection.
         *
         * @param {Partial<Monitor>} partial - The monitor to find. Only needs the fields consumed by the match function
         * @param {Function} match - The match function, will match by name if not provided.
         * @returns {Monitor[]} The list of matching monitors.
         */
        Site.prototype.findMonitor = function findMonitor(partial, match) {
            match = this._makeMatcher(partial, match);
            return this.monitors.filter(match);
        }

        /**
         * Find first matching monitor in the collection.
         *
         * @param {Partial<Monitor>} partial - The monitor to find. Only needs the fields consumed by the match function
         * @param {Function} match - The match function, will match by name if not provided.
         * @returns {Monitor} The first matching monitor in the list
         */
         Site.prototype.findFirstMonitor = function findFirstMonitor(partial, match) {
            match = this._makeMatcher(partial, match);
            return this.monitors.find(match);
        }

        /**
         * Add a monitor to the list of monitors. It will overwrite any montiors that match.
         * If match is a string, its the name of the monitor. If its a function, its a predicate
         * used to compare monitors for equality
         *
         * @method addMonitor
         * @param {Monitor} monitor - The monitor to add.
         * @param {Function} match - The match function, will match by name field if not provided.
         */
         Site.prototype.addMonitor = function addMonitor(monitor, match) {
            match = this._makeMatcher(monitor, match);
            var index = _.findIndex(this.monitors, match);
            if (index !== -1) {
                // Replace existing monitor
                this.monitors[index] = monitor;
            } else {
                this.monitors.push(monitor);
            }
        };

        /**
         * Remove a monitor from the list of monitors.
         *
         * @method removeMonitor
         * @param {Monitor} monitor - The monitor to remove
         * @param {Function} match - The match function, will match by name field if not provided.
         */
        Site.prototype.removeMonitor = function removeMonitor(monitor, match) {
            match = this._makeMatcher(monitor, match);
            _.remove(this.monitors, match);
        }

        /**
         * Check to see if there are any monitors for a giving site.
         *
         * @returns true when there are enabled monitors and false when there are no enabled monitors.
         */
        Site.prototype.anyEnabled = function anyEnabled() {
            return this.monitors.length > 0;
        }

        return Site;
    }
);
Back to Directory File Manager