All files / modules/cache index.js

97.44% Statements 76/78
91.67% Branches 33/36
100% Functions 8/8
97.44% Lines 76/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182      23x 23x 23x 23x         23x                 23x 12x               23x 17x               23x 5x             23x 1x           23x 1x 1x 1x           23x 3x             23x 2x   2x 2x 2x 2x 2x       2x   10x 10x 6x 6x 6x   6x 6x 6x   6x   6x   6x     6x         6x 3x 3x 3x       4x 1x 3x 1x   4x       2x 2x 2x     2x   2x             23x 4x 4x 4x 4x 4x   3x 3x 3x   3x   3x   3x     3x   3x 1x 1x 1x 2x 1x 1x 1x     4x           23x                    
/**
 * @module cache
 */
const NodeCache = require("node-cache");
const log = require("../../util/log");
const linkedinApi = require("../linkedin_learning");
const fs = require('fs');
 
/**
 * The main server cache.
 */
const modulesCache = new NodeCache({
    checkperiod: 0,
});
 
/**
 * Get a cached object.
 * @param {String} key
 * @return {Object} cached object
 */
const get = (key) => {
    return modulesCache.get(key);
};
 
/**
 * Cache an object.
 * @param {String} key
 * @param {String} value
 */
const set = (key, value) => {
    modulesCache.set(key, value);
};
 
/**
 * Return true if key exists in cache.
 * @param {String} key
 * @return {Boolean} true if exists
 */
const has = (key) => {
    return modulesCache.has(key);
};
 
/**
 * Remove an object from the cache.
 * @param {String} key
 */
const del = (key) => {
    modulesCache.del(key);
};
 
/**
 * Flush the cache.
 */
const flush = () => {
    log.info("About to flush CACHE, current stats: %s", modulesCache.getStats());
    modulesCache.flushAll();
    log.info("Flushed CACHE, stats reset: %s", modulesCache.getStats());
};
 
/**
 * Log the cache stats.
 */
const logStats = () => {
    log.info("CACHE stats: %s", modulesCache.getStats());
};
 
/**
 * Update all cached learning objects from LinkedIn Learning API.
 * Log the results and write out failed objects to a separate log file.
 */
const updateAllFromAPI = async () => {
    log.info("Attempting to update CACHE from LinkedIn-L API..");
 
    const courses = [];
    const videos = [];
    let successCount = 0;
    let errorCount = 0;
    const objectsWithoutURN = {
        courses: [],
        videos: [],
    };
    for await (const key of modulesCache.keys()) {
        // check if object has an LinkedIn Learning API ID
        const val = modulesCache.get(key);
        if (/^urn:li:/.test(val.urn)) {
            const learningObj = await linkedinApi.fetchLearningObject(val.urn);
            Eif (learningObj) {
                ++successCount;
                // update object fields
                val.title = learningObj.title.value;
                val.hyperlink = learningObj.details.urls.webLaunch;
                val.longDescription = learningObj.details.descriptionIncludingHtml ?
                    learningObj.details.descriptionIncludingHtml.value : null;
                val.shortDescription = learningObj.details.shortDescriptionIncludingHtml ?
                    learningObj.details.shortDescriptionIncludingHtml.value : null;
                val.picture = learningObj.details.images.primary ?
                    learningObj.details.images.primary : null;
                val.length = learningObj.details.timeToComplete ?
                    learningObj.details.timeToComplete.duration : null;
                // store updated object
                modulesCache.set(key, val);
            } else {
                ++errorCount;
            }
            // add object to collections despite update
            if (/^course-/.test(key)) {
                courses.push(modulesCache.get(key));
            } else Eif (/^video-/.test(key)) {
                videos.push(modulesCache.get(key));
            }
        } else {
            // log discarded object for not having a URN
            if (/^course-/.test(key)) {
                objectsWithoutURN.courses.push(val);
            } else if (/^video-/.test(key)) {
                objectsWithoutURN.videos.push(val);
            }
            modulesCache.del(key);
        }
    }
    // update collections
    modulesCache.set("courses", courses);
    modulesCache.set("videos", videos);
    errorCount === 0 ?
        log.info("CACHE update from LinkedIn-L API finished without errors: %s objects fetched successfully", successCount) :
        log.warn("CACHE update from LinkedIn-L API had at least one error: %s succeeded, %s failed", successCount, errorCount);
    logStats();
 
    fs.writeFileSync(`./log/objects_without_urn_${(new Date()).toUTCString()}`, JSON.stringify(objectsWithoutURN));
};
 
/**
 * Update a single cached object from LinkedIn Learning API.
 * @param {String} key
 */
const updateFromAPI = async (key) => {
    log.info("Attempting to update CACHE(%s) from LinkedIn-L API..", key);
    try {
        const val = modulesCache.get(key);
        const learningObj = await linkedinApi.fetchLearningObject(val.urn);
        if (learningObj) {
            // update object fields
            val.title = learningObj.title.value;
            val.hyperlink = learningObj.details.urls.webLaunch;
            val.longDescription = learningObj.details.descriptionIncludingHtml ?
                learningObj.details.descriptionIncludingHtml.value : null;
            val.shortDescription = learningObj.details.shortDescriptionIncludingHtml ?
                learningObj.details.shortDescriptionIncludingHtml.value : null;
            val.picture = learningObj.details.images.primary ?
                learningObj.details.images.primary : null;
            val.length = learningObj.details.timeToComplete ?
                learningObj.details.timeToComplete.duration : null;
            // store updated object
            modulesCache.set(key, val);
            // add object to collections if updated
            if (/^course-/.test(key)) {
                const courses = modulesCache.get("courses");
                courses.push(modulesCache.get(key));
                modulesCache.set("courses", courses);
            } else if (/^video-/.test(key)) {
                const videos = modulesCache.get("videos");
                videos.push(modulesCache.get(key));
                modulesCache.set("videos", videos);
            }
        }
        log.info("Successfully updated CACHE(%s) from LinkedIn-L API..", key);
    } catch (error) {
        log.error("Failed to update CACHE(%s from LinkedIn-L API.., err: " + error.message, key);
    }
};
 
module.exports = {
    get,
    set,
    has,
    del,
    flush,
    logStats,
    updateAllFromAPI,
    updateFromAPI,
};