<commit_before>// Copyright 2014-2015, Renasar Technologies Inc.
/*jslint node: true */
'use strict';

var di = require('di'),
    _ = require('lodash'),
    express = require('express'),
    parser = require('body-parser'),
    router = express.Router();

module.exports = pollerRouterFactory;

di.annotate(pollerRouterFactory,
    new di.Inject(
        'Services.Waterline',
        'common-api-presenter',
        'Protocol.Task',
        'Constants',
        'Q',
        '_'
    )
);

/**
 * pollerRouterFactory extends the express router with the additional routes
 * specific to devices.  Uses the provided or injected dependencies to do so.
 * @private
 * @param {DomainService} domainService instance of the data service
 * @param {PollerService} pollerService instance of the poller service
 * @param presenter
 * @param {Q} Q
 * @returns {Router} returns the router that has been extended with our verbs
 */
function pollerRouterFactory (waterline, presenter, taskProtocol, Constants, Q, _) {
    var pollerLibrary = [
        {
            name: 'ipmi',
            node: true,
            config: [
                {
                    key: 'ip',
                    type: 'string',
                    ipv4: true
                },
                {
                    key: 'user',
                    type: 'string',
                    defaultsTo: 'admin'
                },
                {
                    key: 'password',
                    type: 'string',
                    defaultsTo: '<PASSWORD>'
                }
            ]
        },
        {
            name: 'snmp',
            config: [
                {
                    key: 'ip',
                    type: 'string',
                    required: true,
                    ipv4: true
                },
                {
                    key: 'communityString',
                    type: 'string',
                    required: true
                },
                {
                    key: 'extensionMibs',
                    type: 'string[]'
                }
            ]
        }
    ];

    /**
     * @api {get} /api/common/pollers/library GET /library
     * @apiDescription get list of possible pollers services
     * @apiName pollers-library-get
     * @apiGroup pollers
     */

    router.get('/pollers/library', presenter.middleware(pollerLibrary));

    /**
     * @api {get} /api/common/pollers/library/:identifier GET /library/:identifier
     * @apiDescription get a single OBM service
     * @apiName pollers-library-service-get
     * @apiGroup pollers
     */

    router.get('/pollers/library/:identifier', presenter.middleware(function (req) {
        return _.detect(pollerLibrary, { name: req.param('identifier') });
    }));

    var pollerWorkItems = {
        ipmi: Constants.WorkItems.Pollers.IPMI,
        snmp: Constants.WorkItems.Pollers.SNMP
    };

    function unpresenterMiddleware(func) {
        return function (req, res, next) {
            Q.resolve(func(req.body)).then(function (value) {
                req.body = value;
                next();
            }).catch(function (err) {
                return presenter(req, res).render(Q.reject(err));
            });
        };
    }

    function deserializePoller(poller) {
        poller = _.pick(poller, 'name', 'node', 'config', 'pollInterval');
        poller.name = pollerWorkItems[poller.name];
        return poller;
    }

    function serializePoller(poller) {
        poller.name = _.findKey(pollerWorkItems, function (workItem) {
            return workItem === poller.name;
        });
        return poller;
    }

    presenter.use('poller', function (value) {
        if (Array.isArray(value)) {
            return _.map(value, serializePoller);
        }
        return serializePoller(value);
    });

    /**
     * @api {get} /api/common/pollers/ GET /
     * @apiDescription get a list of all pollers
     * @apiName pollers-get
     * @apiGroup pollers
     */

    router.get('/pollers', presenter.middleware(function (req) {
        return waterline.workitems.findPollers(req.query);
    }, { serializer: 'poller' }));<commit_msg>fixing 500 error when poller api calls should have 404ed<commit_after>// Copyright 2014-2015, Renasar Technologies Inc.
/*jslint node: true */
'use strict';

var di = require('di'),
    _ = require('lodash'),
    express = require('express'),
    parser = require('body-parser'),
    router = express.Router();

module.exports = pollerRouterFactory;

di.annotate(pollerRouterFactory,
    new di.Inject(
        'Services.Waterline',
        'common-api-presenter',
        'Protocol.Task',
        'Constants',
        'Q',
        '_'
    )
);

/**
 * pollerRouterFactory extends the express router with the additional routes
 * specific to devices.  Uses the provided or injected dependencies to do so.
 * @private
 * @param {DomainService} domainService instance of the data service
 * @param {PollerService} pollerService instance of the poller service
 * @param presenter
 * @param {Q} Q
 * @returns {Router} returns the router that has been extended with our verbs
 */
function pollerRouterFactory (waterline, presenter, taskProtocol, Constants, Q, _) {
    var pollerLibrary = [
        {
            name: 'ipmi',
            node: true,
            config: [
                {
                    key: 'ip',
                    type: 'string',
                    ipv4: true
                },
                {
                    key: 'user',
                    type: 'string',
                    defaultsTo: 'admin'
                },
                {
                    key: 'password',
                    type: 'string',
                    defaultsTo: '<PASSWORD>'
                }
            ]
        },
        {
            name: 'snmp',
            config: [
                {
                    key: 'ip',
                    type: 'string',
                    required: true,
                    ipv4: true
                },
                {
                    key: 'communityString',
                    type: 'string',
                    required: true
                },
                {
                    key: 'extensionMibs',
                    type: 'string[]'
                }
            ]
        }
    ];

    /**
     * @api {get} /api/common/pollers/library GET /library
     * @apiDescription get list of possible pollers services
     * @apiName pollers-library-get
     * @apiGroup pollers
     */

    router.get('/pollers/library', presenter.middleware(pollerLibrary));

    /**
     * @api {get} /api/common/pollers/library/:identifier GET /library/:identifier
     * @apiDescription get a single OBM service
     * @apiName pollers-library-service-get
     * @apiGroup pollers
     */

    router.get('/pollers/library/:identifier', presenter.middleware(function (req) {
        return _.detect(pollerLibrary, { name: req.param('identifier') });
    }));

    var pollerWorkItems = {
        ipmi: Constants.WorkItems.Pollers.IPMI,
        snmp: Constants.WorkItems.Pollers.SNMP
    };

    function unpresenterMiddleware(func) {
        return function (req, res, next) {
            Q.resolve(func(req.body)).then(function (value) {
                req.body = value;
                next();
            }).catch(function (err) {
                return presenter(req, res).render(Q.reject(err));
            });
        };
    }

    function deserializePoller(poller) {
        if (poller) {
            poller = _.pick(poller, 'name', 'node', 'config', 'pollInterval');
            poller.name = pollerWorkItems[poller.name];
        }
        return poller;
    }

    function serializePoller(poller) {
        if (poller) {
            poller.name = _.findKey(pollerWorkItems, function (workItem) {
                return workItem === poller.name;
            });
        }
        return poller;
    }

    presenter.use('poller', function (value) {
        if (Array.isArray(value)) {
            return _.map(value, serializePoller);
        }
        return serializePoller(value);
    });

    /**
     * @api {get} /api/common/pollers/ GET /
     * @apiDescription get a list of all pollers
     * @apiName pollers-get
     * @apiGroup pollers
     */

    router.get('/pollers', presenter.middleware(function (req) {
        return waterline.workitems.findPollers(req.query);
    }, { serializer: 'poller' }));
{'fraction_non_alphanumeric': 0.1180514400853384, 'fraction_numerical': 0.0030816640986132513, 'mean_word_length': 1.5979064039408868, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2596276', 'n_tokens_mistral': 2398, 'n_tokens_neox': 2284, 'n_words': 527}