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

/*
 * site-monitor/services/sitesCache.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.sitesCache */

define(
    [
        "angular",
        "lodash",
        "cjt/util/locale",
        "cjt/io/uapi-request",
        "app/models/site",
        "app/models/monitor",
        "cjt/util/parse",
        "cjt/io/uapi",
        "cjt/io/api2",
        "cjt/modules",
        "cjt/services/APICatcher",

    ],
    function(angular, _, LOCALE, UAPIRequest, Site, Monitor, PARSE) {

        "use strict";

        var app = angular.module("cpanel.siteMonitor.sitesCacheService", []);

        var CAN_EDIT_DOCROOT = {
            documentRoot: true,
        };

        app.factory("sitesCache", [
            function() {

            /**
             * Caching service for sites.
             *
             * @module sites
             * @example
             * SiteCache.add(new Site({domain: 'a.tld', docroot: 'public_html/a'}));
             * SiteCache.add(new Site({domain: 'b.tld', docroot: 'public_html/b'}));
             *
             * SiteCache.get().for((site) => {
             *   console.log(site);
             * });
             *
             * my siteA = SiteCache.findByDomainName('a.tld');
             * if (siteA) {
             *  console.log('found');
             * } else {
             *  console.log('not found');
             * }
             */

            var _flattenedSites = [];
            var _siteLookupMap = {};

            var SitesCache = function() {};

            /**
             * Cache a site.
             *
             * @private
             * @method add
             * @param {Site} site - the domain information about the site.
             * @returns Site - a Site object instantiated from the site param,
             *     regardless of whether the site was pre-existing or added to
             *     the cache with this call
             */
            SitesCache.prototype.add = function _add(site) {

                if (!_flattenedSites) {
                    _flattenedSites = [];
                }

                var site = new Site(site);
                if (_siteLookupMap[site.domain]) {
                    this.remove(site.domain);
                }

                _siteLookupMap[site.domain] = site;
                _flattenedSites.push(site);

                return site;
            };

            /**
             * Clear the cache completly.
             *
             * @method clear
             */
            SitesCache.prototype.clear = function _clear() {
                _flattenedSites = [];
                _siteLookupMap = {};
            };

            /**
             * Remove a previously cached Site object by the domain name.
             *
             * @private
             * @method remove
             * @param {string} domainName
             * @returns
             */
            SitesCache.prototype.remove = function _remove(domainName) {
                var self = this;

                var siteObject = self.findByDomainName(domainName);
                if (!siteObject) {
                    return false;
                }
                for (var i = _flattenedSites.length - 1; i >= 0; i--) {
                    if (_flattenedSites[i].domain === siteObject.domain) {
                        _flattenedSites.splice(i, 1);
                        delete _siteLookupMap[domainName];
                        return true;
                    }
                }

                return false;
            };

            /**
             * Returns the cached sites list.
             *
             * @public
             * @method get
             * @returns {Site[]}
             */
            SitesCache.prototype.get = function get() {
                return _flattenedSites;
            };

            /**
             * Find a site object by the domain name
             *
             * @method findByDomainName
             * @public
             * @param  {String} domainName - domain name for the site as a whole
             * @return {Site} returns the site object if found
             */
            SitesCache.prototype.findByDomainName = function _findByDomainName(domainName) {
                return _siteLookupMap[domainName];
            };

            /**
             * Lookup an existing monitor on a site if it exists.
             *
             * @param {String} domainName - domain name for the site as a whole
             * @param {String} url - the url for the monitor
             * @returns {Monitor?} returns the monitor if one is found matching the url in the site with the domainName.
             */
            SitesCache.prototype.findMonitor = function _findMonitor(domainName, url) {
                var site = this.findByDomainName(domainName);
                if (!site) {
                    return;
                }
                return site.monitors.find(function(monitor) {
                    return monitor.url() === url;
                });
            };

            return new SitesCache();
        }]);
    }
);
Back to Directory File Manager