Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into infra-opti
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian committed Nov 7, 2023
2 parents e692aa4 + c5c8501 commit a786983
Show file tree
Hide file tree
Showing 30 changed files with 836 additions and 390 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
gzip on;
gzip_types *;
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ server {
proxy_set_header X-Forwarded-Proto $scheme;

proxy_http_version 1.1;
proxy_buffering off;
proxy_buffering on;
proxy_cache off;
proxy_connect_timeout 60s;
proxy_read_timeout 36000s;
Expand Down
21 changes: 20 additions & 1 deletion server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cli.command("create-migration").action(() =>
`import { Kysely } from "kysely";
export const up = async (db: Kysely<unknown>) => {};
export const down = async (db: Kysely<unknown>) => {};
`
)
Expand Down Expand Up @@ -90,6 +90,25 @@ cli
}
});

cli
.command("create-user")
.requiredOption("--email <string>")
.requiredOption("--firstname <string>")
.requiredOption("--lastname <string>")
.requiredOption("--role <string>")
.option("--codeRegion <string>")
.action(
async (options: {
email: string;
firstname: string;
lastname: string;
role: string;
codeRegion?: string;
}) => {
await createUser(options);
}
);

cli
.command("importFiles")
.argument("[filename]")
Expand Down
20 changes: 15 additions & 5 deletions server/src/modules/data/queries/utils/tauxDevenirFavorable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,32 @@ export const selectTauxDevenirFavorableAgg = (
end
`;

export function withDevenirFavorableReg({
type EbRef<EB extends ExpressionBuilder<DB, never>> = Parameters<EB["ref"]>[0];

export function withTauxDevenirFavorableReg<
EB extends ExpressionBuilder<DB, never>,
>({
eb,
millesimeSortie,
cfdRef,
dispositifIdRef,
codeRegionRef,
}: {
eb: ExpressionBuilder<DB, "formationEtablissement" | "etablissement">;
eb: EB;
millesimeSortie: string;
cfdRef: EbRef<EB>;
dispositifIdRef: EbRef<EB>;
codeRegionRef: EbRef<EB>;
}) {
return eb
.selectFrom("indicateurRegionSortie as subIRS")
.whereRef("subIRS.cfd", "=", "formationEtablissement.cfd")
.whereRef("subIRS.dispositifId", "=", "formationEtablissement.dispositifId")
.whereRef("subIRS.cfd", "=", cfdRef)
.whereRef("subIRS.dispositifId", "=", dispositifIdRef)
.where("subIRS.millesimeSortie", "=", millesimeSortie)
.whereRef(
"subIRS.codeRegion",
"=",
sql`ANY(array_agg(${eb.ref("etablissement.codeRegion")}))`
sql`ANY(array_agg(${eb.ref(codeRegionRef)}))`
)
.select([selectTauxDevenirFavorableAgg("subIRS").as("sa")])
.groupBy(["subIRS.cfd", "subIRS.dispositifId"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export const pilotageTransformationRoutes = ({
preHandler: hasPermissionHandler("pilotage-intentions/lecture"),
},
async (request, response) => {
const { ...filters } = request.query;
const { order, orderBy, ...filters } = request.query;

const stats = await getTransformationStats({
...filters,
orderBy: order && orderBy ? { order, column: orderBy } : undefined,
});
response.status(200).send(stats);
}
Expand All @@ -33,7 +34,11 @@ export const pilotageTransformationRoutes = ({
preHandler: hasPermissionHandler("pilotage-intentions/lecture"),
},
async (request, response) => {
const stats = await getFormationsTransformationStats(request.query);
const { order, orderBy, ...filters } = request.query;
const stats = await getFormationsTransformationStats({
...filters,
orderBy: order && orderBy ? { order, column: orderBy } : undefined,
});
response.status(200).send(stats);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { kdb } from "../../../../db/db";
import { cleanNull } from "../../../../utils/noNull";
import { effectifAnnee } from "../../queries/utils/effectifAnnee";
import { hasContinuum } from "../../queries/utils/hasContinuum";
import { withDevenirFavorableReg } from "../../queries/utils/tauxDevenirFavorable";
import { withTauxDevenirFavorableReg } from "../../queries/utils/tauxDevenirFavorable";
import { withInsertionReg } from "../../queries/utils/tauxInsertion6mois";
import { withPoursuiteReg } from "../../queries/utils/tauxPoursuite";
import { selectTauxPressionAgg } from "../../queries/utils/tauxPression";
Expand Down Expand Up @@ -122,9 +122,13 @@ export const queryFormations = async ({
codeRegionRef: "etablissement.codeRegion",
}).as("continuum"),
(eb) =>
withDevenirFavorableReg({ eb, millesimeSortie }).as(
"tauxDevenirFavorable"
),
withTauxDevenirFavorableReg({
eb,
millesimeSortie,
cfdRef: "formationEtablissement.cfd",
dispositifIdRef: "formationEtablissement.dispositifId",
codeRegionRef: "etablissement.codeRegion",
}).as("tauxDevenirFavorable"),
])
.$narrowType<{
tauxInsertion6mois: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { kdb } from "../../../../db/db";
import { DB } from "../../../../db/schema";
import { cleanNull } from "../../../../utils/noNull";
import { hasContinuum } from "../../queries/utils/hasContinuum";
import { withTauxDevenirFavorableReg } from "../../queries/utils/tauxDevenirFavorable";
import { withInsertionReg } from "../../queries/utils/tauxInsertion6mois";
import { withPoursuiteReg } from "../../queries/utils/tauxPoursuite";
import { withTauxPressionReg } from "../../queries/utils/tauxPression";
Expand All @@ -30,17 +31,25 @@ const selectDifferencePlaces = (
- ${eb.ref("capaciteApprentissageActuelle")})`;
};

const selectPlacesTransformees = (eb: ExpressionBuilder<DB, "demande">) =>
sql`GREATEST(${eb.ref("capaciteScolaire")}
- ${eb.ref("capaciteScolaireActuelle")}, 0)
+ GREATEST(${eb.ref("capaciteApprentissage")}
- ${eb.ref("capaciteApprentissageActuelle")}, 0)
+ GREATEST(${eb.ref("capaciteScolaireActuelle")}
- ${eb.ref("capaciteScolaire")}, 0)`;

const selectPlacesOuvertes = (eb: ExpressionBuilder<DB, "demande">) =>
sql`GREATEST(${eb.ref("capaciteScolaire")}
- ${eb.ref("capaciteScolaireActuelle")}, 0)
+ GREATEST(${eb.ref("capaciteApprentissage")}
- ${eb.ref("capaciteApprentissageActuelle")}, 0)`;
- ${eb.ref("capaciteScolaireActuelle")}, 0)
+ GREATEST(${eb.ref("capaciteApprentissage")}
- ${eb.ref("capaciteApprentissageActuelle")}, 0)`;

const selectPlacesFermees = (eb: ExpressionBuilder<DB, "demande">) =>
sql`GREATEST(${eb.ref("capaciteScolaireActuelle")}
- ${eb.ref("capaciteScolaire")}, 0)
+ GREATEST(${eb.ref("capaciteApprentissageActuelle")}
- ${eb.ref("capaciteApprentissage")}, 0)`;
- ${eb.ref("capaciteScolaire")}, 0)
+ GREATEST(${eb.ref("capaciteApprentissageActuelle")}
- ${eb.ref("capaciteApprentissage")}, 0)`;

const selectNbDemandes = (eb: ExpressionBuilder<DB, "demande">) =>
eb.fn.count<number>("demande.id").distinct();
Expand All @@ -59,6 +68,7 @@ export const getFormationsTransformationStatsQuery = ({
tauxPression,
codeNiveauDiplome,
filiere,
orderBy,
}: {
status?: "draft" | "submitted";
type?: "fermeture" | "ouverture";
Expand All @@ -69,6 +79,7 @@ export const getFormationsTransformationStatsQuery = ({
tauxPression?: "eleve" | "faible";
codeNiveauDiplome?: string[];
filiere?: string[];
orderBy?: { column: string; order: "asc" | "desc" };
}) => {
const partition = (() => {
if (codeDepartement) return ["dataEtablissement.codeDepartement"] as const;
Expand Down Expand Up @@ -119,13 +130,21 @@ export const getFormationsTransformationStatsQuery = ({
dispositifIdRef: "demande.dispositifId",
codeRegionRef: "dataEtablissement.codeRegion",
}).as("tauxPression"),
withTauxDevenirFavorableReg({
eb,
millesimeSortie: "2020_2021",
cfdRef: "demande.cfd",
dispositifIdRef: "demande.dispositifId",
codeRegionRef: "dataEtablissement.codeRegion",
}).as("tauxDevenirFavorable"),
selectNbDemandes(eb).as("nbDemandes"),
selectNbEtablissements(eb).as("nbEtablissements"),
sql<number>`ABS(${eb.fn.sum(selectDifferencePlaces(eb, type))})`.as(
"differencePlaces"
),
eb.fn.sum<number>(selectPlacesOuvertes(eb)).as("placesOuvertes"),
eb.fn.sum<number>(selectPlacesFermees(eb)).as("placesFermees"),
eb.fn.sum<number>(selectPlacesTransformees(eb)).as("placesTransformees"),
hasContinuum({
eb,
millesimeSortie: "2020_2021",
Expand Down Expand Up @@ -222,6 +241,14 @@ export const getFormationsTransformationStatsQuery = ({
if (!filiere?.length) return q;
return q.where("dataFormation.libelleFiliere", "in", filiere);
})
.$call((q) => {
if (!orderBy) return q;
return q.orderBy(
sql.ref(orderBy.column),
sql`${sql.raw(orderBy.order)} NULLS LAST`
);
})
.orderBy("tauxDevenirFavorable", "desc")
.execute()
.then(cleanNull);
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const [getFormationsTransformationStats] = inject(
tauxPression?: "eleve" | "faible";
codeNiveauDiplome?: string[];
filiere?: string[];
orderBy?: { column: string; order: "asc" | "desc" };
}) => {
const [stats, formations] = await Promise.all([
deps.getRegionStats({
Expand Down
Loading

0 comments on commit a786983

Please sign in to comment.