Skip to content

Commit

Permalink
Merge pull request #216 from PEM-Humboldt/release/1.9.0
Browse files Browse the repository at this point in the history
Release 1.9.0
  • Loading branch information
erikasv committed Aug 26, 2021
2 parents 7a8d047 + 9ca4804 commit 0350f0b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 129 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "biomodelos-api",
"version": "1.8.0",
"version": "1.9.0",
"description": "RESTful server API for Biomodels.",
"main": "index.js",
"author": "Valentina Grajales Olarte - https://github.com/valegrajales",
Expand Down
202 changes: 102 additions & 100 deletions src/api/models/model.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,17 @@ export async function read(req, res) {
'Published',
{
taxID: req.params.taxID,
isActive: true,
published: true,
isActive: true
}
],
[
'Statistic',
{
taxID: req.params.taxID,
modelStatus: { $in: ['Statistic'] },
isActive: true
}
]
]);
try {
Expand Down Expand Up @@ -664,9 +671,11 @@ export async function occurrenceCoversStatsModel(req, res) {
* type: number
* validModels:
* type: number
* publishedModels:
* type: number
* developingModels:
* type: number
* pendingValidation:
* statisticModels:
* type: number
* features:
* type: array
Expand All @@ -678,132 +687,125 @@ export async function occurrenceCoversStatsModel(req, res) {
* $ref: "#/definitions/ErrorResponse"
*/
export async function generalModelStats(req, res) {
const totalStats: Array<{
taxonomicGroup: string,
totalSpecies: number,
developingModels?: number,
validModels?: number,
pendingValidation?: number
}> = [
{
taxonomicGroup: 'mamiferos',
totalSpecies: 492
},
{ taxonomicGroup: 'aves', totalSpecies: 1921 },
{ taxonomicGroup: 'reptiles', totalSpecies: 537 },
{ taxonomicGroup: 'anfibios', totalSpecies: 803 },
{ taxonomicGroup: 'peces', totalSpecies: 1435 },
{ taxonomicGroup: 'invertebrados', totalSpecies: 19312 },
{ taxonomicGroup: 'plantas', totalSpecies: 22840 }
];
let groups = [];
try {
const docs = await Specie.aggregate([
groups = await Specie.aggregate([
{
$match: {
$and: [
{
$or: [
{
bmClass: {
$in: [
'mamiferos',
'aves',
'reptiles',
'anfibios',
'peces',
'invertebrados',
'plantas'
]
}
}
]
}
]
$group: {
_id: {
taxonomyClass: '$bmClass'
},
taxes: { $push: '$taxID' },
total: { $sum: 1 }
}
},
}
]);
} catch (err) {
log.error(err);
res.send('There was an error getting the statistics');
}

const stats = groups.map(group =>
Model.aggregate([
{
$lookup: {
localField: 'taxID',
from: 'models',
foreignField: 'taxID',
as: 'models'
$match: {
taxID: { $in: group.taxes },
isActive: true,
modelLevel: 1
}
},
{ $unwind: '$models' },
{
$match: {
$and: [
{ 'models.isActive': { $in: [true] } },
{ 'models.modelLevel': { $in: [1] } }
]
$project: {
taxID: 1,
status: {
$switch: {
branches: [
{
case: { $eq: ['$modelStatus', 'Valid'] },
then: '0-Valid'
},
{
case: {
$and: [
{ $eq: ['$modelStatus', 'pendingValidation'] },
{ $eq: ['$published', true] }
]
},
then: '1-Published'
},
{
case: { $eq: ['$modelStatus', 'pendingValidation'] },
then: '2-Developing'
},
{
case: { $eq: ['$modelStatus', 'Statistic'] },
then: '3-Statistic'
}
],
default: '4-no-model'
}
}
}
},
{ $sort: { status: 1 } },
{
$group: {
_id: {
taxonomyClass: '$bmClass',
modelStatus: '$models.modelStatus',
taxID: '$taxID'
},
count: { $sum: 1 }
modelStatus: { $first: '$status' }
}
},
{
$group: {
_id: { taxonomyClass: '$_id.taxonomyClass', taxID: '$_id.taxID' },
modelStatus: {
$push: { status: '$_id.modelStatus', count: '$count' }
}
}
},
{ $unwind: '$modelStatus' },
{
$sort: { 'modelStatus.status': -1 }
},
{ $group: { _id: '$_id', modelStatus: { $first: '$modelStatus' } } },
{
$group: {
_id: {
taxonomyClass: '$_id.taxonomyClass',
modelStatus: '$modelStatus.status'
modelStatus: '$modelStatus'
},
count: { $sum: 1 }
}
},
{
$group: {
_id: '$_id.taxonomyClass',
modelStatus: {
$push: { status: '$_id.modelStatus', count: '$count' }
}
$project: {
modelStatus: '$_id.modelStatus',
count: 1,
_id: 0
}
}
]);
docs.map(elem => {
const obj = totalStats.find(x => x.taxonomicGroup === elem._id);
const index = totalStats.indexOf(obj);
elem.modelStatus.map(elem => {
switch (elem.status) {
case 'Developing':
// eslint-disable-next-line security/detect-object-injection
totalStats[index].developingModels = elem.count;
break;
case 'Valid':
// eslint-disable-next-line security/detect-object-injection
totalStats[index].validModels = elem.count;
break;
case 'pendingValidation':
// eslint-disable-next-line security/detect-object-injection
totalStats[index].pendingValidation = elem.count;
break;
}
])
);

return Promise.all(stats)
.then(response => {
const result = groups.map((group, index) => {
/* eslint-disable security/detect-object-injection */
const validModels = response[index].find(
e => e.modelStatus === '0-Valid'
);
const publishedModels = response[index].find(
e => e.modelStatus === '1-Published'
);
const developingModels = response[index].find(
e => e.modelStatus === '2-Developing'
);
const statisticModels = response[index].find(
e => e.modelStatus === '3-Statistic'
);
/* eslint-enable security/detect-object-injection */
return {
taxonomicGroup: group._id.taxonomyClass,
totalSpecies: group.total,
validModels: validModels ? validModels.count : 0,
publishedModels: publishedModels ? publishedModels.count : 0,
developingModels: developingModels ? developingModels.count : 0,
statisticModels: statisticModels ? statisticModels.count : 0
};
});
res.json(result);
})
.catch(err => {
log.error(err);
res.send('There was an error getting the statistics');
});
res.json(totalStats);
} catch (err) {
log.error(err);
res.send('There was an error getting the statistics');
}
}

/**
Expand Down
50 changes: 23 additions & 27 deletions src/api/species/specie.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import GeoJSON from 'geojson';
import { Record } from '../../models/record.model';
import Model from '../../models/model.model';
import Specie from '../../models/specie.model';
const log = require('../../config/log').logger();

Expand Down Expand Up @@ -321,31 +322,7 @@ export async function getAllSpecies(req, res) {
const query = constructQuery(req);
try {
let docs = [];
if (req.query.modelStatus) {
docs = await Specie.aggregate([
{
$match: query
},
{
$lookup: {
localField: 'taxID',
from: 'models',
foreignField: 'taxID',
as: 'models'
}
},
{
$match: { 'models.modelStatus': req.query.modelStatus }
},
{
$project: {
species: 1,
taxID: 1,
_id: 0
}
}
]).sort({ species: 1 });
} else if (req.query.speciesIn) {
if (req.query.speciesIn) {
docs = await Specie.aggregate([
{
$match: {
Expand All @@ -368,7 +345,26 @@ export async function getAllSpecies(req, res) {
{ $sort: { acceptedNameUsage: 1 } }
]);
} else {
docs = await Specie.find(query, {
let modelsFilter = {};
if (req.query.modelStatus) {
const taxIds = await Model.find(
{ modelStatus: req.query.modelStatus, isActive: true },
{ taxID: 1, _id: 0 }
);
modelsFilter = {
taxID: {
$in: taxIds.map(e => e.taxID)
}
};
} else if (!!req.query.withModel) {
const taxIds = await Model.distinct('taxID', { isActive: true });
modelsFilter = {
taxID: {
$in: taxIds
}
};
}
docs = await Specie.find(Object.assign(query, modelsFilter), {
species: 1,
taxID: 1,
_id: 0
Expand Down Expand Up @@ -523,7 +519,7 @@ export async function searchSpecies(req, res) {
fullAccess = scopes.includes('all');
}

let project = { _id: 0, species: 1, taxID: 1 }
let project = { _id: 0, species: 1, taxID: 1 };
if (fullAccess) {
project = {
_id: 0,
Expand Down

0 comments on commit 0350f0b

Please sign in to comment.