diff --git a/modules/apis/controllers/apis.controller.js b/modules/apis/controllers/apis.controller.js index e2c243d8b..7167e173b 100644 --- a/modules/apis/controllers/apis.controller.js +++ b/modules/apis/controllers/apis.controller.js @@ -125,6 +125,21 @@ exports.getApi = async (req, res) => { } }; +/** + * @desc Endpoint to getData Aggregated saved from load on apis + * @param {Object} req - Express request object + * @param {Object} res - Express response object + */ +exports.getAggregateApi = async (req, res) => { + // TODO if (req.scrap && req.user && req.scrap.user && req.scrap.user.id === req.user.id) next(); + try { + const data = await ApisService.getAggregateApi(req.api, req.body); + responses.success(res, 'api getData')(data); + } catch (err) { + responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err); + } +}; + /** * @desc MiddleWare to ask the service the api for this id diff --git a/modules/apis/policies/apis.policy.js b/modules/apis/policies/apis.policy.js index 678379fd5..d5a035f02 100644 --- a/modules/apis/policies/apis.policy.js +++ b/modules/apis/policies/apis.policy.js @@ -23,6 +23,9 @@ exports.invokeRolesPolicies = () => { }, { resources: '/api/apis/data/:apiId', permissions: '*', + }, { + resources: '/api/apis/aggregate/:apiId', + permissions: '*', }], }]); }; diff --git a/modules/apis/repositories/apis.repository.js b/modules/apis/repositories/apis.repository.js index 27812fd5a..7e10bb217 100644 --- a/modules/apis/repositories/apis.repository.js +++ b/modules/apis/repositories/apis.repository.js @@ -99,7 +99,6 @@ exports.listApi = (collection) => { * @return {Object} locations */ exports.getApi = (collection, filters) => { - console.log(collection, filters); const _schema = new mongoose.Schema({}, { collection, strict: false, @@ -115,3 +114,88 @@ exports.getApi = (collection, filters) => { return model.findOne(filters).exec(); }; + +/** + * @desc Function to ask for api data in db + * @param {Object} locations + * @return {Object} locations + */ +exports.getAggregateApi = (collection) => { + const _schema = new mongoose.Schema({}, { + collection, + strict: false, + timestamps: true, + }); + + let model; + try { + model = mongoose.model(collection); + } catch (error) { + model = mongoose.model(collection, _schema); + } + + return model.aggregate([ + { + $match: { '@date': '2020-03-26T00:00:00+01:00' }, + }, + { + $unwind: { path: '$weathers' }, + }, + { + $project: { + status: { $arrayElemAt: ['$weathers.status.value', 0] }, + temp: { $arrayElemAt: ['$weathers.temp.value', 0] }, + }, + }, + { + $group: { + _id: '$_id', + status: { $avg: '$status' }, + temp: { $avg: '$temp' }, + }, + }, + ]).sort('-updatedAt').exec(); +}; + + +// db.meteoFrance.aggregate([ +// { +// $match: { '@date': '2020-03-26T00:00:00+01:00' }, +// }, +// { +// $unwind: { path: '$weathers' }, +// }, +// { +// $project: { +// status: { $arrayElemAt: ['$weathers.status.value', 0] }, +// temp: { $arrayElemAt: ['$weathers.temp.value', 0] }, +// }, +// }, +// { +// $group: { +// _id: '$_id', +// status: { $avg: '$status' }, +// temp: { $avg: '$temp' }, +// }, +// }, +// ]); + + +// db.mareeInfo.aggregate([ +// { +// $match: { '@date': '2020-04-04T00:00:00+02:00' }, +// }, +// { +// $unwind: { path: '$coeffs' }, +// }, +// { +// $project: { +// coeff: { $arrayElemAt: ['$coeffs.coeff.value', 0] }, +// height: { $arrayElemAt: ['$coeffs.height.value', 0] }, +// hour: { $arrayElemAt: ['$coeffs.hour.value', 0] }, +// }, +// }, +// { +// $match: { coeff: { $gt: 60 } }, +// }, +// ]); diff --git a/modules/apis/routes/apis.routes.js b/modules/apis/routes/apis.routes.js index 6c1f09c43..080568929 100644 --- a/modules/apis/routes/apis.routes.js +++ b/modules/apis/routes/apis.routes.js @@ -31,6 +31,9 @@ module.exports = (app) => { .get(apis.listApi) .post(apis.getApi); + app.route('/api/apis/aggregate/:apiId') + .post(apis.getAggregateApi); + // Finish by binding the api middleware app.param('apiId', apis.apiByID); }; diff --git a/modules/apis/services/apis.service.js b/modules/apis/services/apis.service.js index 5973af815..ab109eb37 100644 --- a/modules/apis/services/apis.service.js +++ b/modules/apis/services/apis.service.js @@ -155,3 +155,13 @@ exports.getApi = async (api, body) => { const result = await ApisRepository.getApi(api.slug, body); return Promise.resolve(result); }; + +/** + * @desc Functio to ask repository to get data Aggregated saved from apis request + * @param {Object} scrap - original scrap + * @return {Promise} scrap + */ +exports.getAggregateApi = async (api, body) => { + const result = await ApisRepository.getAggregateApi(api.slug, body); + return Promise.resolve(result); +};