Skip to content

Commit

Permalink
add job for laureates
Browse files Browse the repository at this point in the history
  • Loading branch information
folland87 committed Jun 13, 2024
1 parent 6ad7f98 commit e22c2e4
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 34 deletions.
74 changes: 40 additions & 34 deletions src/api/jobs/jobs.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,25 @@ router.route('/jobs')
data: [{ $match: filters }, { $sort: parseSortParams(sort) }, { $skip: skip }, { $limit: limit }],
activity: [
{ $match: statusFilter },
{ $addFields: {
duration: {
$round: [
{
$divide: [
{
$subtract: [
'$lastFinishedAt',
'$lastRunAt',
],
},
1000,
],
},
],
},
} },
{
$addFields: {
duration: {
$round: [
{
$divide: [
{
$subtract: [
'$lastFinishedAt',
'$lastRunAt',
],
},
1000,
],
},
],
},
}
},
{ $set: { duration: { $cond: [{ $eq: ['$status', 'scheduled'] }, 0, '$duration'] } } },
{ $set: { duration: { $cond: [{ $eq: ['$status', 'running'] }, 0, '$duration'] } } },
{
Expand All @@ -84,12 +86,14 @@ router.route('/jobs')
},
{ $match: { date: { $gte: getDateXDaysBefore(7) } } },
{ $match: { date: { $lte: getDateXDaysAfter(1) } } },
{ $project: {
status: 1,
date: { $toLong: '$date' },
duration: { $ifNull: ['$duration', 0] },
name: 1,
} },
{
$project: {
status: 1,
date: { $toLong: '$date' },
duration: { $ifNull: ['$duration', 0] },
name: 1,
}
},
],
status: [
{ $match: statusFilter },
Expand All @@ -101,17 +105,19 @@ router.route('/jobs')
},
},
{ $set: { totalCount: { $arrayElemAt: ['$totalCount', 0] } } },
{ $project: {
totalCount: { $ifNull: ['$totalCount.totalCount', 0] },
data: 1,
statuses: 1,
aggregations: {
definitions: Object.keys(agenda._definitions).filter((def) => !SKIP_JOBS.includes(def)),
activity: '$activity',
byStatus: '$status',
byName: '$name',
},
} },
{
$project: {
totalCount: { $ifNull: ['$totalCount.totalCount', 0] },
data: 1,
statuses: 1,
aggregations: {
definitions: Object.keys(agenda._definitions).filter((def) => !SKIP_JOBS.includes(def)),
activity: '$activity',
byStatus: '$status',
byName: '$name',
},
}
},
]).toArray();
return res.json(stats?.[0] || {});
},
Expand Down
2 changes: 2 additions & 0 deletions src/jobs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
exportFrEsrPersonIdentifiers,
exportFrEsrStructureWebsites,
exportFrEsrPrizes,
exportFrEsrPaysageLaureatAll,
} from './opendata';
import synchronizeAnnuaireCollection from './synchronize/annuaire-collection';
import synchronizeCuriexploreActors from './synchronize/curiexplore-actors';
Expand All @@ -45,6 +46,7 @@ agenda.define('export fr-esr-paysage_personnes_identifiants', { shouldSaveResult
agenda.define('export fr-esr-paysage-fonctions-gourvernance', { shouldSaveResult: true }, exportFrEsrPaysageFonctionsGourvernance);
agenda.define('export fr-esr-annelis-paysage-etablissements', { shouldSaveResult: true }, exportFrEsrAnnelisPaysageEtablissements);
agenda.define('export fr-esr-paysage_structures_websites', { shouldSaveResult: true }, exportFrEsrStructureWebsites);
agenda.define('export fr_esr_paysage_laureat_all', { shouldSaveResult: true }, exportFrEsrPaysageLaureatAll);
agenda.define('synchronize fr-esr-referentiel-geographique', { shouldSaveResult: true }, synchronizeFrEsrReferentielGeographique);
agenda.define('synchronize curiexplore actors', { shouldSaveResult: true }, synchronizeCuriexploreActors);
agenda.define('ask for email revalidation with otp', { shouldSaveResult: true }, askForEmailRevalidation);
Expand Down
124 changes: 124 additions & 0 deletions src/jobs/opendata/fr_esr_paysage_laureats_all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { client, db } from '../../services/mongo.service';
import readQuery from '../../api/commons/queries/relations.query';
import structuresLightQuery from '../../api/commons/queries/structures.light.query';
import categoriesLightQuery from '../../api/commons/queries/categories.light.query';

const dataset = 'fr_esr_paysage_laureats_all';

export default async function exportFrEsrPaysageLaureatAll() {
const data = await db.collection('relationships').aggregate([
{ $match: { relationTag: 'laureat' } },
...readQuery,
{
$lookup: {
from: 'relationships',
localField: 'resourceId',
foreignField: 'resourceId',
as: 'porteurs',
pipeline: [
{ $match: { relationTag: 'prix-porteur' } },
{ $project: { _id: 0, relatedObjectId: 1 } },
],
},
},
{
$lookup: {
from: 'structures',
localField: 'porteurs.relatedObjectId',
foreignField: 'id',
as: 'porteurs',
pipeline: structuresLightQuery,
},
},
{
$lookup: {
from: 'relationships',
localField: 'resourceId',
foreignField: 'resourceId',
as: 'categories',
pipeline: [
{ $match: { relationTag: 'prix-categorie' } },
{ $project: { _id: 0, relatedObjectId: 1 } },
],
},
},
{
$lookup: {
from: 'categories',
localField: 'categories.relatedObjectId',
foreignField: 'id',
as: 'categories',
pipeline: categoriesLightQuery,
},
},
{
$lookup: {
from: 'weblinks',
localField: 'resourceId',
foreignField: 'resourceId',
as: 'websites',
pipeline: [
{ $match: { type: 'website' } },
],
},
},

]).toArray();
// console.log(`Exporting ${data.length} relations for dataset ${dataset}`, data[0].websites);
const json = data.map(e => {
const wikidata = e.relatedObject.identifiers?.filter((i) => (i.type === 'wikidata'))
.sort((a, b) => a?.startDate?.localeCompare(b?.startDate)).map((i) => i.value).join('|') || null;
const otherAssociatedObjectIdentifiers = e.otherAssociatedObjects?.flatMap((i) => i.identifiers)
return {
prix_laureat_identifiant: e.id,
prix_annee: e.startDate,
prix_identifiant: e.resourceId,
prix_libelle: e.resource.displayName,
prix_detail: e.resource.descriptionFr,
prix_libelle_francais: e.resource.nameFr,
prix_libelle_anglais: e.resource.nameEn,
prix_categories: e.categories?.map((c) => c.usualNameFr),
prix_site_internet: e.websites?.map((w) => w?.url).join(';'),
prix_identifiant_wikidata: wikidata,
prix_element_wikidata: wikidata ? `https://www.wikidata.org/entity/${wikidata}` : null,
prix_porteurs_identifiant: e.porteurs?.map((p) => p.id).join(';'),
prix_porteurs_libelle: e.porteurs?.map((p) => p.displayName).join(';'),
laureat_type: e.relatedObject?.collection === 'persons' ? 'Personne' : 'Structure',
laureat_identifiant: e.relatedObject.id,
laureat_libelle: e.relatedObject.displayName,
laureat_personne_prenom: e.relatedObject.firstName,
laureat_personne_nom: e.relatedObject.lastName,
laureat_personne_genre: e.relatedObject.gender,
laureat_structures_associees_identifiant: e.otherAssociatedObjectIds,
laureat_structures_associees_libelle: e.otherAssociatedObjects?.map((i) => (i.displayName)),
laureat_identifiant_wikidata: e.relatedObject.identifiers?.filter((i) => (i.type === 'wikidata')).map((i) => i.value) || null,
laureat_identifiant_idref: e.relatedObject.identifiers?.filter((i) => (i.type === 'idref')).map((i) => i.value) || null,
laureat_identifiant_orcid: e.relatedObject.identifiers?.filter((i) => (i.type === 'orcid')).map((i) => i.value) || null,
laureat_identifiant_uai: e.relatedObject.identifiers?.filter((i) => (i.type === 'uai')).map((i) => i.value) || null,
laureat_identifiant_siret: e.relatedObject.identifiers?.filter((i) => (i.type === 'siret')).map((i) => i.value) || null,
laureat_identifiant_rnsr: e.relatedObject.identifiers?.filter((i) => (i.type === 'rnsr')).map((i) => i.value) || null,
laureat_identifiant_cnrs: e.relatedObject.identifiers?.filter((i) => (i.type === 'cnrs')).map((i) => i.value) || null,
laureat_identifiant_ed: e.relatedObject.identifiers?.filter((i) => (i.type === 'ed')).map((i) => i.value) || null,
laureat_identifiant_ror: e.relatedObject.identifiers?.filter((i) => (i.type === 'ror')).map((i) => i.value) || null,
laureat_structures_associees_identifiant_wikidata: otherAssociatedObjectIdentifiers.filter((i) => (i.type === 'wikidata')).map((i) => i.value),
laureat_structures_associees_identifiant_idref: otherAssociatedObjectIdentifiers.filter((i) => (i.type === 'idref')).map((i) => i.value),
laureat_structures_associees_identifiant_orcid: otherAssociatedObjectIdentifiers.filter((i) => (i.type === 'orcid')).map((i) => i.value),
laureat_structures_associees_identifiant_uai: otherAssociatedObjectIdentifiers.filter((i) => (i.type === 'uai')).map((i) => i.value),
laureat_structures_associees_identifiant_siret: otherAssociatedObjectIdentifiers.filter((i) => (i.type === 'siret')).map((i) => i.value),
laureat_structures_associees_identifiant_rnsr: otherAssociatedObjectIdentifiers.filter((i) => (i.type === 'rnsr')).map((i) => i.value),
laureat_structures_associees_identifiant_cnrs: otherAssociatedObjectIdentifiers.filter((i) => (i.type === 'cnrs')).map((i) => i.value),
laureat_structures_associees_identifiant_ed: otherAssociatedObjectIdentifiers.filter((i) => (i.type === 'ed')).map((i) => i.value),
laureat_structures_associees_identifiant_ror: otherAssociatedObjectIdentifiers.filter((i) => (i.type === 'ror')).map((i) => i.value),

}
});
console.log(json[0]);
// const session = client.startSession();
// await session.withTransaction(async () => {
// await db.collection('opendata').deleteMany({ dataset });
// await db.collection('opendata').insertMany(json);
// await session.endSession();
// });

return { status: 'success', location: `/opendata/${dataset}` };
}
1 change: 1 addition & 0 deletions src/jobs/opendata/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as exportFrEsrStructureIdentifiers } from './fr-esr-paysage_str
export { default as exportFrEsrPersonIdentifiers } from './fr-esr-paysage_personnes_identifiants';
export { default as exportFrEsrStructureWebsites } from './fr-esr-paysage_structures_websites';
export { default as exportFrEsrPrizes } from './fr-esr-paysage_prix';
export { default as exportFrEsrPaysageLaureatAll } from './fr_esr_paysage_laureats_all';

0 comments on commit e22c2e4

Please sign in to comment.