Skip to content

Commit

Permalink
feat(apis): init aggregate ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 28, 2020
1 parent b3c254a commit 1b19c03
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
15 changes: 15 additions & 0 deletions modules/apis/controllers/apis.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions modules/apis/policies/apis.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ exports.invokeRolesPolicies = () => {
}, {
resources: '/api/apis/data/:apiId',
permissions: '*',
}, {
resources: '/api/apis/aggregate/:apiId',
permissions: '*',
}],
}]);
};
86 changes: 85 additions & 1 deletion modules/apis/repositories/apis.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 } },
// },
// ]);
3 changes: 3 additions & 0 deletions modules/apis/routes/apis.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
10 changes: 10 additions & 0 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit 1b19c03

Please sign in to comment.