All files / universal/gpii/node_modules/deviceReporter/src DeviceReporter.js

100% Statements 39/39
75% Branches 6/8
100% Functions 11/11
100% Lines 38/38

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 183                                    1x 1x 1x   1x 1x       1x       1x                                                 1x                         1x                 1x 120x           1x                                   1x 118x 118x 115x 115x     2x         2x       1x                                   1x 5x     5x 5x       1x 5x 5x 360x 84x 100x 100x 42x     84x 42x       5x     1x                 1x 5x                
/*!
 * Device Reporter
 *
 * Copyright 2012 Raising the Floor - International
 *
 * Licensed under the New BSD license. You may not use this file except in
 * compliance with this License.
 *
 * The research leading to these results has received funding from the European Union's
 * Seventh Framework Programme (FP7/2007-2013)
 * under grant agreement no. 289016.
 *
 * You may obtain a copy of the License at
 * https://github.com/GPII/universal/blob/master/LICENSE.txt
 */
 
"use strict";
 
var fluid = require("infusion"),
    os = require("os"),
    gpii = fluid.registerNamespace("gpii");
 
require("kettle");
require("./DeviceReporterUtilities.js");
 
// TODO: This module has *NO* unit tests
 
fluid.defaults("gpii.deviceReporter", {
    gradeNames: "fluid.component"
});
 
fluid.defaults("gpii.deviceReporter.base", {
    gradeNames: ["kettle.app", "gpii.deviceReporter"],
    requestHandlers: {
        deviceGet: {
            route: "/device",
            type: "gpii.deviceReporter.handlers.get",
            method: "get"
        }
    },
    components: {
        platformReporter: {
            type: "gpii.platformReporter.native"
        },
        nameResolver: {
            type: "gpii.deviceReporter.nameResolver"
        }
    },
    invokers: {
        fireResponse: {
            funcName: "gpii.deviceReporter.fireResponse",
            args: ["{arguments}.0", "{arguments}.1", "{platformReporter}"]
        }
    }
});
 
fluid.defaults("gpii.deviceReporter.handlers.get", {
    gradeNames: ["kettle.request.http"],
    invokers: {
        handleRequest: {
            func: "{deviceReporter}.get",
            args: [
                "{request}"
            ]
        }
    }
});
 
 
fluid.defaults("gpii.deviceReporter.nameResolver", {
    gradeNames: ["fluid.component"],
    invokers: {
        resolveName: {
            funcName: "fluid.identity"
        }
    }
});
 
gpii.deviceReporter.fireResponse = function (request, installedSolutions, platformReporter) {
    request.events.onSuccess.fire({
        solutions: installedSolutions,
        OS: platformReporter.reportPlatform()
    });
};
 
fluid.defaults("gpii.deviceReporter.static", {
    gradeNames: ["gpii.deviceReporter.base"],
    components: {
        installedSolutionsDataSource: {
            type: "kettle.dataSource.file",
            options: {
                gradeNames: "kettle.dataSource.file.moduleTerms"
            }
        }
    },
    invokers: {
        get: {
            funcName: "gpii.deviceReporter.static.get",
            args: ["{arguments}.0", "{deviceReporter}"]
        }
    }
});
 
gpii.deviceReporter["static"].get = function (request, deviceReporter) {
    var promise = deviceReporter.installedSolutionsDataSource.get();
    promise.then(function (solutions) {
        Eif (!fluid.isDestroyed(deviceReporter)) { // Fix failure for GPII-1227 tests which can receive this response after the deviceReporter is gone
            deviceReporter.fireResponse(request, solutions); // TODO: Remove such checks once FLUID-5790 is resolved
        }
    }, function (err) {
        var error = fluid.extend(err, {
            statusCode: 500,
            message: "Failed to read deviceReporter source: " + err.message,
            isError: true
        });
        request.events.onError.fire(error);
    });
};
 
fluid.defaults("gpii.deviceReporter.live", {
    gradeNames: ["gpii.deviceReporter.base"],
    components: {
        // currently in "gpii.config.all.development.dr.production" as {flowManager}.solutionsRegistryDataSource
        // solutionsRegistryDataSource:             // should be supplied by integrator
    },
    invokers: {
        get: {
            funcName: "gpii.deviceReporter.live.get",
            args: ["{arguments}.0", "{deviceReporter}", "{solutionsRegistryDataSource}"]
        },
        filterByInstalledSolutions: {
            funcName: "gpii.deviceReporter.filterByInstalledSolutions",
            args: [ "{arguments}.0", "{deviceReporter}" ]
        }
    }
});
 
gpii.deviceReporter.live.get = function (request, deviceReporter, solutionsRegistryDataSource) {
    solutionsRegistryDataSource.get({
        os: deviceReporter.platformReporter.reportPlatform().id
    }, function (entries) {
        var filteredSolutions = deviceReporter.filterByInstalledSolutions(entries);
        deviceReporter.fireResponse(request, filteredSolutions);
    }, request.events.onError.fire);
};
 
gpii.deviceReporter.filterByInstalledSolutions = function (entries, deviceReporter) {
    var installedSolutions = [];
    fluid.each(entries, function (entry, entryId) {
        Eif (!installedSolutions.some(function (s) { return s.id === entryId; })) {
            var foundEntryId = fluid.find(entry.isInstalled, function (installedSolutionsReporter) {
                var resolvedName = deviceReporter.nameResolver.resolveName(installedSolutionsReporter.type, "deviceReporter");
                if (fluid.invokeGradedFunction(resolvedName, installedSolutionsReporter)) {
                    return entryId;
                }
            }, null);
            if  (foundEntryId !== null) {
                installedSolutions.push({ "id": foundEntryId });
            }
        }
    });
    return installedSolutions;
};
 
fluid.defaults("gpii.platformReporter.native", {
    gradeNames: ["fluid.component"],
    invokers: {
        reportPlatform: {
            funcName: "gpii.platformReporter.native.reportPlatform"
        }
    }
});
 
gpii.platformReporter["native"].reportPlatform = function () { // "native" is a reserved word
    return {
        // TODO: need to report more details - windowmanager, etc.
        id: os.platform(),
        // TODO: Need a better strategy - Node semver fails horribly
        // in the face of the benign underscore (eg. x86_64).
        version: os.release().replace("_", "-")
    };
};