Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pilotage intention v2 #138

Merged
merged 8 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions server/src/modules/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { cadranRoutes } from "./routes/panorama.routes";
import { pilotageReformeRoutes } from "./routes/pilotageReforme.routes";
import { pilotageTransformationRoutes } from "./routes/pilotageTransformation.routes";
import { regionsRoutes } from "./routes/regions.routes";
import { restitutionIntentionsRoutes } from "./routes/restitutionIntentions.routes";

export const registerFormationModule = ({ server }: { server: Server }) => {
formationsRoutes({ server });
Expand All @@ -13,4 +14,5 @@ export const registerFormationModule = ({ server }: { server: Server }) => {
regionsRoutes({ server });
pilotageReformeRoutes({ server });
pilotageTransformationRoutes({ server });
restitutionIntentionsRoutes({ server });
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { kdb } from "../../../../db/db";
import { cleanNull } from "../../../../utils/noNull";
import { hasContinuum } from "../utils/hasContinuum";
import { notHistorique } from "../utils/notHistorique";
import { withPositionCadran } from "../utils/positionCadran";
import { withInsertionReg } from "../utils/tauxInsertion6mois";
import { withPoursuiteReg } from "../utils/tauxPoursuite";
import { selectTauxPression } from "../utils/tauxPression";
Expand Down Expand Up @@ -106,6 +107,13 @@ export const getEtablissement = async ({
dispositifIdRef: "formationEtablissement.dispositifId",
codeRegionRef: "etablissement.codeRegion",
}).as("tauxPoursuiteEtudes"),
withPositionCadran({
eb,
millesimeSortie,
cfdRef: "formationEtablissement.cfd",
dispositifIdRef: "formationEtablissement.dispositifId",
codeRegionRef: "etablissement.codeRegion",
}).as("positionCadran"),
])
.where(notHistorique)
.whereRef("formationEtablissement.UAI", "=", "etablissement.UAI")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { kdb } from "../../../../db/db";
import { notHistoriqueIndicateurRegionSortie } from "../../queries/utils/notHistorique";
import { selectTauxInsertion6moisAgg } from "../../queries/utils/tauxInsertion6mois";
import { selectTauxPoursuiteAgg } from "../../queries/utils/tauxPoursuite";

export const getFormationsTransformationRegionStats = async ({
codeRegion,
codeAcademie,
codeDepartement,
codeNiveauDiplome,
millesimeSortie = "2020_2021",
}: {
codeRegion?: string;
codeAcademie?: string;
codeDepartement?: string;
millesimeSortie?: string;
codeNiveauDiplome?: string[];
}) => {
const statsSortie = await kdb
.selectFrom("indicateurRegionSortie")
.innerJoin(
"formation",
"formation.codeFormationDiplome",
"indicateurRegionSortie.cfd"
)
.where((w) => {
if (!codeRegion) return w.val(true);
return w("indicateurRegionSortie.codeRegion", "=", codeRegion);
})
.$call((q) => {
if (!codeDepartement && !codeAcademie) {
return q;
}
return q
.innerJoin(
"departement",
"departement.codeRegion",
"indicateurRegionSortie.codeRegion"
)
.where((w) => {
if (!codeAcademie) return w.val(true);
return w("departement.codeAcademie", "=", codeAcademie);
})
.where((w) => {
if (!codeDepartement) return w.val(true);
return w("departement.codeDepartement", "=", codeDepartement);
});
})
.$call((q) => {
if (!codeNiveauDiplome?.length) return q;
return q.where("formation.codeNiveauDiplome", "in", codeNiveauDiplome);
})
.where("indicateurRegionSortie.millesimeSortie", "=", millesimeSortie)
.where(notHistoriqueIndicateurRegionSortie)
.select([
selectTauxInsertion6moisAgg("indicateurRegionSortie").as("tauxInsertion"),
selectTauxPoursuiteAgg("indicateurRegionSortie").as("tauxPoursuite"),
])
.executeTakeFirstOrThrow();

return statsSortie;
};
134 changes: 134 additions & 0 deletions server/src/modules/data/queries/utils/positionCadran.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { ExpressionBuilder, sql } from "kysely";

import { DB } from "../../../../db/schema";
import { notHistoriqueIndicateurRegionSortie } from "./notHistorique";
import {
selectTauxInsertion6mois,
selectTauxInsertion6moisAgg,
withInsertionReg,
} from "./tauxInsertion6mois";
import {
selectTauxPoursuite,
selectTauxPoursuiteAgg,
withPoursuiteReg,
} from "./tauxPoursuite";

export const getPositionCadran = (
indicateurRegionSortieAlias: string,
indicateurRegionSortieRegAlias: string
) => {
const tauxPoursuite = sql<number>`${selectTauxPoursuite(
indicateurRegionSortieAlias
)}`;
const tauxInsertion = sql<number>`${selectTauxInsertion6mois(
indicateurRegionSortieAlias
)}`;
const tauxPoursuiteReg = sql<number>`${selectTauxPoursuiteAgg(
indicateurRegionSortieRegAlias
)}`;
const tauxInsertionReg = sql<number>`${selectTauxInsertion6moisAgg(
indicateurRegionSortieRegAlias
)}`;

return sql<string>`
CASE
WHEN (${tauxInsertion} >= ${tauxInsertionReg} AND ${tauxPoursuite} > ${tauxPoursuiteReg}) THEN 'Q1'
WHEN (${tauxInsertion} >= ${tauxInsertionReg} AND ${tauxPoursuite} < ${tauxPoursuiteReg}) THEN 'Q2'
WHEN (${tauxInsertion} < ${tauxInsertionReg} AND ${tauxPoursuite} >= ${tauxPoursuiteReg}) THEN 'Q3'
WHEN (${tauxInsertion} < ${tauxInsertionReg} AND ${tauxPoursuite} < ${tauxPoursuiteReg}) THEN 'Q4'
ELSE 'Hors cadran'
END
`;
};

type EbRef<EB extends ExpressionBuilder<DB, never>> = Parameters<EB["ref"]>[0];

export function withPositionCadran<EB extends ExpressionBuilder<DB, never>>({
eb,
cfdRef,
dispositifIdRef,
codeRegionRef,
millesimeSortie,
codeNiveauDiplomeRef,
}: {
eb: EB;
cfdRef: EbRef<EB>;
dispositifIdRef: EbRef<EB>;
codeRegionRef: EbRef<EB>;
millesimeSortie: string;
codeNiveauDiplomeRef?: EbRef<EB>;
}) {
const tauxInsertionReg = withInsertionReg({
eb,
cfdRef,
dispositifIdRef,
codeRegionRef,
millesimeSortie,
});

const tauxPoursuiteReg = withPoursuiteReg({
eb,
cfdRef,
dispositifIdRef,
codeRegionRef,
millesimeSortie,
});

return eb
.selectFrom("indicateurRegionSortie")
.whereRef(
"indicateurRegionSortie.codeRegion",
"=",
sql`ANY(array_agg(${eb.ref(codeRegionRef)}))`
)
.where("indicateurRegionSortie.millesimeSortie", "=", millesimeSortie)
.where(notHistoriqueIndicateurRegionSortie)
.innerJoin(
"formation",
"formation.codeFormationDiplome",
"indicateurRegionSortie.cfd"
)
.$call((eb) => {
if (codeNiveauDiplomeRef)
return eb.whereRef(
"formation.codeNiveauDiplome",
"=",
codeNiveauDiplomeRef
);
return eb;
})
.select([
sql<string>`
CASE
WHEN (${tauxInsertionReg} >= ${selectTauxInsertion6moisAgg(
"indicateurRegionSortie"
)}
AND ${tauxPoursuiteReg} > ${selectTauxPoursuiteAgg(
"indicateurRegionSortie"
)})
THEN 'Q1'
WHEN (${tauxInsertionReg} >= ${selectTauxInsertion6moisAgg(
"indicateurRegionSortie"
)}
AND ${tauxPoursuiteReg} < ${selectTauxPoursuiteAgg(
"indicateurRegionSortie"
)})
THEN 'Q2'
WHEN (${tauxInsertionReg} < ${selectTauxInsertion6moisAgg(
"indicateurRegionSortie"
)}
AND ${tauxPoursuiteReg} >= ${selectTauxPoursuiteAgg(
"indicateurRegionSortie"
)})
THEN 'Q3'
WHEN (${tauxInsertionReg} < ${selectTauxInsertion6moisAgg(
"indicateurRegionSortie"
)}
AND ${tauxPoursuiteReg} < ${selectTauxPoursuiteAgg(
"indicateurRegionSortie"
)})
THEN 'Q4'
ELSE 'Hors cadran'
END`.as("positionCadran"),
]);
}
20 changes: 10 additions & 10 deletions server/src/modules/data/queries/utils/tauxInsertion6mois.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ export const selectDenominateurInsertion6moisAgg = (
indicateurSortieAlias: string
) => sql<number>`
SUM(
case when
${sql.table(indicateurSortieAlias)}."nbInsertion6mois" is not null
then ${sql.table(indicateurSortieAlias)}."nbSortants"
case when
${sql.table(indicateurSortieAlias)}."nbInsertion6mois" is not null
then ${sql.table(indicateurSortieAlias)}."nbSortants"
end
)`;

export const selectTauxInsertion6moisAgg = (
indicateurSortieAlias: string
) => sql<number>`
case when
case when
${selectDenominateurInsertion6moisAgg(indicateurSortieAlias)} >= ${seuil}
then (100 * SUM(${sql.table(indicateurSortieAlias)}."nbInsertion6mois")
then (100 * SUM(${sql.table(indicateurSortieAlias)}."nbInsertion6mois")
/ ${selectDenominateurInsertion6moisAgg(indicateurSortieAlias)})
end
`;

export const selectDenominateurInsertion6mois = (
indicateurSortieAlias: string
) => sql<number>`
case when
${sql.table(indicateurSortieAlias)}."nbInsertion6mois" is not null
then ${sql.table(indicateurSortieAlias)}."nbSortants"
case when
${sql.table(indicateurSortieAlias)}."nbInsertion6mois" is not null
then ${sql.table(indicateurSortieAlias)}."nbSortants"
end`;

export const selectTauxInsertion6mois = (
indicateurSortieAlias: string
) => sql<number>`
case when
case when
${selectDenominateurInsertion6mois(indicateurSortieAlias)} >= ${seuil}
then (100 * ${sql.table(indicateurSortieAlias)}."nbInsertion6mois"
then (100 * ${sql.table(indicateurSortieAlias)}."nbInsertion6mois"
/ ${selectDenominateurInsertion6mois(indicateurSortieAlias)})
end
`;
Expand Down
46 changes: 46 additions & 0 deletions server/src/modules/data/routes/restitutionIntentions.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Boom from "@hapi/boom";
import { ROUTES_CONFIG } from "shared";

import { Server } from "../../../server";
import { hasPermissionHandler } from "../../core";
import { getCountRestitutionIntentionsStats } from "../usecases/getCountRestitutionIntentionsStats/getCountRestitutionIntentionsStats";
import { getRestitutionIntentionsStats } from "../usecases/getRestitutionIntentionsStats/getRestitutionIntentionsStats.usecase";

export const restitutionIntentionsRoutes = ({ server }: { server: Server }) => {
server.get(
"/intentions/stats",
{
schema: ROUTES_CONFIG.getRestitutionIntentionsStats,
preHandler: hasPermissionHandler("intentions/lecture"),
},
async (request, response) => {
const { order, orderBy, ...rest } = request.query;
if (!request.user) throw Boom.forbidden();

const result = await getRestitutionIntentionsStats({
...rest,
orderBy: order && orderBy ? { order, column: orderBy } : undefined,
user: request.user,
});
response.status(200).send(result);
}
);

server.get(
"/intentions/stats/count",
{
schema: ROUTES_CONFIG.countRestitutionIntentionsStats,
preHandler: hasPermissionHandler("intentions/lecture"),
},
async (request, response) => {
const { ...filters } = request.query;
if (!request.user) throw Boom.forbidden();

const result = await getCountRestitutionIntentionsStats({
...filters,
user: request.user,
});
response.status(200).send(result);
}
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
countOuvertures,
countOuverturesApprentissage,
countOuverturesSco,
} from "../../utils/countCapacite";
import { isStatsDemandeVisible } from "../../utils/isStatsDemandesVisible";
} from "../../../utils/countCapacite";
import { isIntentionVisible } from "../../../utils/isIntentionVisible";

const countStatsDemandesInDB = async ({
const countRestitutionIntentionsStatsInDB = async ({
status,
codeRegion,
rentreeScolaire,
Expand Down Expand Up @@ -249,12 +249,12 @@ const countStatsDemandesInDB = async ({
if (secteur) return eb.where("dataEtablissement.secteur", "=", secteur);
return eb;
})
.where(isStatsDemandeVisible({ user }))
.where(isIntentionVisible({ user }))
.executeTakeFirstOrThrow();

return countDemandes;
};

export const dependencies = {
countStatsDemandesInDB,
countRestitutionIntentionsStatsInDB,
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { RequestUser } from "../../../core/model/User";
import { dependencies } from "./dependencies";

const getCountStatsDemandesFactory =
({ countStatsDemandesInDB = dependencies.countStatsDemandesInDB }) =>
const getCountRestitutionIntentionsStatsFactory =
({
countRestitutionIntentionsStatsInDB = dependencies.countRestitutionIntentionsStatsInDB,
}) =>
async (activeFilters: {
status?: "draft" | "submitted";
codeRegion?: string[];
Expand All @@ -24,9 +26,11 @@ const getCountStatsDemandesFactory =
compensation?: string;
user: Pick<RequestUser, "id" | "role" | "codeRegion">;
}) => {
const countStatsDemandesPromise = countStatsDemandesInDB(activeFilters);
const countStatsDemandesPromise =
countRestitutionIntentionsStatsInDB(activeFilters);

return await countStatsDemandesPromise;
};

export const getCountStatsDemandes = getCountStatsDemandesFactory({});
export const getCountRestitutionIntentionsStats =
getCountRestitutionIntentionsStatsFactory({});
Loading
Loading