Skip to content

Commit

Permalink
feat(atlas): amélioration de l'admin avec l'ajout des constantes serveur
Browse files Browse the repository at this point in the history
  • Loading branch information
jerem1508 committed Feb 4, 2025
1 parent 60d31b0 commit 033594b
Show file tree
Hide file tree
Showing 8 changed files with 861 additions and 455 deletions.
345 changes: 265 additions & 80 deletions client/src/pages/admin/dashboard.tsx

Large diffs are not rendered by default.

File renamed without changes.
2 changes: 1 addition & 1 deletion client/src/pages/admin/routes.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route, Routes } from "react-router-dom";

import Home from "./index.tsx";
import Home from "./home.tsx";
import { Layout } from "../../layout/Layout.tsx";
import Dashboard from "./dashboard.tsx";

Expand Down
2 changes: 1 addition & 1 deletion server/src/routes/admin/api-tests/list-indexes.http
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GET http://localhost:3000/api/admin/list-indexes?collectionId=fr-esr-all-projects-synthese
GET http://localhost:3000/api/admin/list-indexes?collectionId=atlas2024
115 changes: 115 additions & 0 deletions server/src/routes/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from "path";
import { db } from "../../services/mongo.js";
import { checkQuery } from "./utils.js";
import uploadVersionRoute from "./upload/upload-version.js";
import { create } from "domain";

const router = new express.Router();

Expand Down Expand Up @@ -60,6 +61,30 @@ router.route("/admin/set-current-version").post(async (req, res) => {
res.json(ret);
});

// update constant
router.route("/admin/update-constant").post(async (req, res) => {
const filters = checkQuery(req.body, ["dashboardId", "key", "value"], res);
const { dashboardId, key, value } = filters;

const response = await db
.collection("boards")
.updateOne(
{ id: dashboardId },
{ $set: { "constants.$[elem].value": value } },
{
arrayFilters: [{ "elem.key": key }]
}
);

if (response.matchedCount === 0) {
return res.status(404).json({ error: "No document found" });
}

// return current data
const ret = await db.collection("boards").findOne({ id: dashboardId });
res.json(ret);
});

// list all uploaded files in files folder
router.route("/admin/list-uploaded-files").get((req, res) => {
fs.readdir("../files", (err, files) => {
Expand Down Expand Up @@ -153,4 +178,94 @@ router.route("/admin/delete-uploaded-files").delete((req, res) => {
});
});

// Update a collection from another collection with script execution
router.route('/admin/update-version-from-dependent-collection').post(async (req, res) => {
const filters = checkQuery(req.body, ["dashboardId", "collectionId"], res);
const { dashboardId, collectionId } = filters;

const board = await db.collection("boards").findOne({ id: dashboardId });
if (!board) {
return res.status(404).json({ error: "No document found" });
}

const dependsOf = board.data.find((d) => d.id === collectionId).dependsOf;
if (!dependsOf) {
return res.status(400).json({ error: "No dependency found" });
}

// get the current version of the dependent collection - ex:atlas2024
const dependentCollectionId = board.data.find((d) => d.id === dependsOf).current;


async function run({dependentCollectionId}) {
try {
const atlasCollection = db.collection(dependentCollectionId);
const newCollectionId = 'similar-elements_' + Date.now();
const similarElementsCollection = db.collection(newCollectionId);

// await similarElementsCollection.deleteMany({});

const years = await atlasCollection.distinct("annee_universitaire");
const geoIds = await atlasCollection.distinct("geo_id");

// for (let i = 0; i < years.length; i++) {
for (let i = years.length-1; i < years.length; i++) { // TODO: remove this line
for (let j = 0; j < geoIds.length; j++) {
const query = { geo_id: geoIds[j], annee_universitaire: years[i], regroupement: 'TOTAL' };
const data = await atlasCollection.find(query).toArray();

const effectifPR = data.filter((item) => item.secteur === 'PR').reduce((acc, item) => acc + item.effectif, 0);
const effectifPU = data.filter((item) => item.secteur === 'PU').reduce((acc, item) => acc + item.effectif, 0);
const pctPR = effectifPR / (effectifPR + effectifPU) * 100;
const pctPU = effectifPU / (effectifPR + effectifPU) * 100;

const effectifF = data.filter((item) => item.sexe === '2').reduce((acc, item) => acc + item.effectif, 0);
const effectifM = data.filter((item) => item.sexe === '1').reduce((acc, item) => acc + item.effectif, 0);
const pctF = effectifF / (effectifF + effectifM) * 100;
const pctM = effectifM / (effectifF + effectifM) * 100;

const similarElement = {
geo_id: geoIds[j],
annee_universitaire: years[i],
niveau_geo: data[0]?.niveau_geo,
geo_nom: data[0]?.geo_nom,
effectifPR: effectifPR,
effectifPU: effectifPU,
pctPR: pctPR,
pctPU: pctPU,
effectifF: effectifF,
effectifM: effectifM,
pctF: pctF,
pctM: pctM,
};

const result = await similarElementsCollection.insertOne(similarElement);

// insert dans la collection board de la nouvelle version
const response = await db.collection("boards").updateOne(
{ id: dashboardId, "data.id": collectionId },
{
$push: {
"data.$.versions": {
id: newCollectionId,
createdAt: new Date().toISOString().split('T')[0], // yyyy-mm-dd
from: dependentCollectionId,
}
}
}
);
};
};
} finally {
await db.close();
res.status(200).json({ message: "Update done" });
}
}

await run({dependentCollectionId}).catch(console.dir);

console.log('------------------------- fin ----------------------');

});

export default router;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET http://localhost:3000/api/atlas/get-geo-ids-from-search?q=lyon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET http://localhost:3000/api/atlas/get-indexes
Loading

0 comments on commit 033594b

Please sign in to comment.