Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4efbbd3
using db API v2 with postgres
JeromeBu Aug 7, 2024
8c55c86
fix agent query to get declarations at the same time
JeromeBu Aug 9, 2024
2fb6050
fix frontend types
JeromeBu Aug 9, 2024
0ba491c
fix parentExternalId query
JeromeBu Aug 9, 2024
65511f7
fix userAndReferentCountByOrganization in get softwares
JeromeBu Aug 9, 2024
5fc9adb
fix sequences of ids when loading data in pg db from git repository
JeromeBu Aug 10, 2024
0f54550
handle case when trying to add a software with a name that already ex…
JeromeBu Aug 10, 2024
0082955
reference agent by Id in software and instances instead of by email
JeromeBu Aug 11, 2024
3a2571e
fix fetchExternalData
JeromeBu Oct 9, 2024
6bfaf6a
add some logs when fetching external data
JeromeBu Oct 9, 2024
dfd7d0a
use fetchExternalData in bootstrap
JeromeBu Oct 9, 2024
e4faf30
only update extra data for software that have not been updated for mo…
JeromeBu Oct 9, 2024
c1be676
configure app to run migration on app start
JeromeBu Oct 9, 2024
a97737c
add pre prod docker-compose
JeromeBu Oct 9, 2024
51f03d9
update docker-compose to restart PG if it crashes
JeromeBu Oct 18, 2024
03d2403
start incremental compilation for the first time after a delay
JeromeBu Oct 18, 2024
e049e52
fix loading logos from wikidata or comptoir du libre
JeromeBu Oct 18, 2024
e564abf
fix comptoire du libre schema to keep logo
JeromeBu Oct 18, 2024
27b6726
fix fetch comptoir du libre logo and keywords, and cache condition
JeromeBu Oct 18, 2024
4da3876
show private instances to connected users
JeromeBu Oct 25, 2024
b22b67b
clean unused files in backend
JeromeBu Oct 25, 2024
37166d0
remove getSoftwareFormAutoFillDataFromExternalAndOtherSources from redux
JeromeBu Oct 25, 2024
8710e3e
remove redux from backend
JeromeBu Oct 25, 2024
cf277e9
change instance publicUrl to instanceUrl, add boolean isPublic (in pr…
JeromeBu Oct 25, 2024
8a59770
test cleaning
JeromeBu Oct 25, 2024
acd322a
fixes on instances in front
JeromeBu Oct 25, 2024
12a68bf
possibility to add insanceUrl for private instances
JeromeBu Oct 25, 2024
558b510
update instance wording to handle plural
JeromeBu Oct 25, 2024
f67e61e
improve sill.json query, using primaryKey for compiled_softwares
JeromeBu Oct 29, 2024
710126f
bump version to 1.43.0 (first with postgres)
JeromeBu Oct 30, 2024
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
8 changes: 4 additions & 4 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"types": "dist/src/lib/index.d.ts",
"scripts": {
"migrate": "dotenv -e ../.env -- kysely migrate",
"db:up": "yarn migrate latest",
"prepare": "[ ! -f .env.local.sh ] && cp .env.sh .env.local.sh || true",
"test": "vitest --watch=false",
"test": "vitest --watch=false --no-file-parallelism",
"dev": "yarn build && yarn start",
"build": "tsc",
"start": "dotenv -e ../.env -- forever dist/src/main.js",
"start": "yarn db:up && dotenv -e ../.env -- forever dist/src/main.js",
"build-prod": "esbuild src/main.ts --bundle --platform=node --target=node20 --outfile=dist/index.js",
"start-prod": "dotenv -e ../.env -- forever dist/index.js",
"start-prod": "yarn db:up && dotenv -e ../.env -- forever dist/index.js",
"_format": "prettier \"**/*.{ts,tsx,json,md}\"",
"format": "yarn run _format --write",
"format:check": "yarn run _format --list-different",
Expand Down Expand Up @@ -67,7 +68,6 @@
"memoizee": "^0.4.15",
"node-fetch": "^2.6.7",
"prettier": "^2.8.2",
"redux-clean-architecture": "^4.1.0",
"run-exclusive": "^2.2.19",
"superjson": "^1.12.2",
"ts-node": "^10.9.1",
Expand Down
17 changes: 11 additions & 6 deletions api/scripts/compile-data.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { Kysely } from "kysely";
import { bootstrapCore } from "../src/core";
import type { Database } from "../src/core/adapters/dbApi/kysely/kysely.database";
import { createPgDialect } from "../src/core/adapters/dbApi/kysely/kysely.dialect";
import { env } from "../src/env";

(async () => {
const { core } = await bootstrapCore({
const kyselyDb = new Kysely<Database>({ dialect: createPgDialect(env.databaseUrl) });
const { useCases } = await bootstrapCore({
"keycloakUserApiParams": undefined,
"dbConfig": {
"dbKind": "git",
"dataRepoSshUrl": "[email protected]:codegouvfr/sill-data.git",
"sshPrivateKey": env.sshPrivateKeyForGit,
"sshPrivateKeyName": env.sshPrivateKeyForGitName
"dbKind": "kysely",
"kyselyDb": kyselyDb
// "dataRepoSshUrl": "[email protected]:codegouvfr/sill-data.git",
// "sshPrivateKey": env.sshPrivateKeyForGit,
// "sshPrivateKeyName": env.sshPrivateKeyForGitName
},
"githubPersonalAccessTokenForApiRateLimit": env.githubPersonalAccessTokenForApiRateLimit,
"doPerPerformPeriodicalCompilation": false,
"doPerformCacheInitialization": false,
"externalSoftwareDataOrigin": env.externalSoftwareDataOrigin
});

await core.functions.readWriteSillData.manuallyTriggerNonIncrementalCompilation();
await useCases.fetchAndSaveExternalDataForAllSoftwares();

process.exit(0);
})();
53 changes: 39 additions & 14 deletions api/scripts/load-git-repo-in-pg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { InsertObject, Kysely } from "kysely";
import { InsertObject, Kysely, sql } from "kysely";
import { z } from "zod";
import { createGitDbApi, GitDbApiParams } from "../src/core/adapters/dbApi/createGitDbApi";
import { makeGetAgentIdByEmail } from "../src/core/adapters/dbApi/kysely/createPgAgentRepository";
import { Database } from "../src/core/adapters/dbApi/kysely/kysely.database";
import { createPgDialect } from "../src/core/adapters/dbApi/kysely/kysely.dialect";
import { CompiledData } from "../src/core/ports/CompileData";
Expand All @@ -20,30 +21,36 @@ const saveGitDbInPostgres = async ({ pgConfig, gitDbConfig }: Params) => {

const { softwareRows, agentRows, softwareReferentRows, softwareUserRows, instanceRows } = await gitDbApi.fetchDb();

await insertSoftwares(softwareRows, pgDb);
await insertAgents(agentRows, pgDb);

const agentIdByEmail = await makeGetAgentIdByEmail(pgDb);

await insertSoftwares(softwareRows, agentIdByEmail, pgDb);
await insertSoftwareReferents({
softwareReferentRows: softwareReferentRows,
agentIdByEmail: agentIdByEmail,
agentIdByEmail,
db: pgDb
});
await insertSoftwareUsers({
softwareUserRows: softwareUserRows,
agentIdByEmail: agentIdByEmail,
agentIdByEmail,
db: pgDb
});
await insertInstances({
instanceRows: instanceRows,
agentIdByEmail,
db: pgDb
});

const compiledSoftwares = await gitDbApi.fetchCompiledData();
await insertCompiledSoftwaresAndSoftwareExternalData(compiledSoftwares, pgDb);
};

const insertSoftwares = async (softwareRows: SoftwareRow[], db: Kysely<Database>) => {
const insertSoftwares = async (
softwareRows: SoftwareRow[],
agentIdByEmail: Record<string, number>,
db: Kysely<Database>
) => {
console.info("Deleting than Inserting softwares");
console.info("Number of softwares to insert : ", softwareRows.length);
await db.transaction().execute(async trx => {
Expand All @@ -52,8 +59,9 @@ const insertSoftwares = async (softwareRows: SoftwareRow[], db: Kysely<Database>
await trx
.insertInto("softwares")
.values(
softwareRows.map(({ similarSoftwareExternalDataIds: _, ...row }) => ({
softwareRows.map(({ similarSoftwareExternalDataIds: _, addedByAgentEmail, ...row }) => ({
...row,
addedByAgentId: agentIdByEmail[addedByAgentEmail],
dereferencing: row.dereferencing ? JSON.stringify(row.dereferencing) : null,
softwareType: JSON.stringify(row.softwareType),
workshopUrls: JSON.stringify(row.workshopUrls),
Expand All @@ -63,6 +71,7 @@ const insertSoftwares = async (softwareRows: SoftwareRow[], db: Kysely<Database>
}))
)
.executeTakeFirst();
await sql`SELECT setval('softwares_id_seq', (SELECT MAX(id) FROM softwares))`.execute(trx);

await trx
.insertInto("softwares__similar_software_external_datas")
Expand All @@ -82,17 +91,14 @@ const insertAgents = async (agentRows: Db.AgentRow[], db: Kysely<Database>) => {
console.log("Deleting than Inserting agents");
console.info("Number of agents to insert : ", agentRows.length);
await db.transaction().execute(async trx => {
await trx.deleteFrom("instances").execute();
await trx.deleteFrom("softwares").execute();
await trx.deleteFrom("agents").execute();
await trx.insertInto("agents").values(agentRows).executeTakeFirst();
await sql`SELECT setval('agents_id_seq', (SELECT MAX(id) FROM agents))`.execute(trx);
});
};

const makeGetAgentIdByEmail = async (db: Kysely<Database>): Promise<Record<string, number>> => {
console.info("Fetching agents, to map email to id");
const agents = await db.selectFrom("agents").select(["email", "id"]).execute();
return agents.reduce((acc, agent) => ({ ...acc, [agent.email]: agent.id }), {});
};

const insertSoftwareReferents = async ({
softwareReferentRows,
agentIdByEmail,
Expand Down Expand Up @@ -143,12 +149,31 @@ const insertSoftwareUsers = async ({
});
};

const insertInstances = async ({ instanceRows, db }: { instanceRows: Db.InstanceRow[]; db: Kysely<Database> }) => {
const insertInstances = async ({
instanceRows,
agentIdByEmail,
db
}: {
instanceRows: Db.InstanceRow[];
agentIdByEmail: Record<string, number>;
db: Kysely<Database>;
}) => {
console.info("Deleting than Inserting instances");
console.info("Number of instances to insert : ", instanceRows.length);
await db.transaction().execute(async trx => {
await trx.deleteFrom("instances").execute();
await trx.insertInto("instances").values(instanceRows).executeTakeFirst();
await trx
.insertInto("instances")
.values(
instanceRows.map(({ addedByAgentEmail, publicUrl, ...instanceRow }) => ({
...instanceRow,
instanceUrl: publicUrl,
isPublic: !!publicUrl,
addedByAgentId: agentIdByEmail[addedByAgentEmail]
}))
)
.executeTakeFirst();
await sql`SELECT setval('instances_id_seq', (SELECT MAX(id) FROM instances))`.execute(trx);
});
};

Expand Down
3 changes: 3 additions & 0 deletions api/src/core/adapters/comptoirDuLibreApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const comptoirDuLibreApi: ComptoirDuLibreApi = {
"getComptoirDuLibre": memoize(
async () => {
try {
console.info("Fetching comptoir du libre");
const res = await fetch(url);

if (res.status !== 200) {
Expand All @@ -34,6 +35,7 @@ export const comptoirDuLibreApi: ComptoirDuLibreApi = {
let imgSrc: string | undefined;

try {
console.info(`Fetching comptoir du libre icon, for comptoirDuLibreId : ${comptoirDuLibreId}`);
const body = await fetch(`https://comptoir-du-libre.org/fr/softwares/${comptoirDuLibreId}`).then(r =>
r.text()
);
Expand All @@ -55,6 +57,7 @@ export const comptoirDuLibreApi: ComptoirDuLibreApi = {
let $: CheerioAPI;

try {
console.info(`Fetching comptoir du libre keywords, for comptoirDuLibreId : ${comptoirDuLibreId}`);
const body = await fetch(`https://comptoir-du-libre.org/fr/softwares/${comptoirDuLibreId}`).then(r =>
r.text()
);
Expand Down
60 changes: 0 additions & 60 deletions api/src/core/adapters/dbApi/InMemoryDbApi.ts

This file was deleted.

2 changes: 1 addition & 1 deletion api/src/core/adapters/dbApi/createGitDbApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type GitDbApiParams = {
sshPrivateKey: string;
};

export function createGitDbApi(params: GitDbApiParams): Db.DbApiAndInitializeCache {
export function createGitDbApi(params: GitDbApiParams): { dbApi: DbApi; initializeDbApiCache: () => Promise<void> } {
const { dataRepoSshUrl, sshPrivateKeyName, sshPrivateKey } = params;

const dbApi: DbApi = {
Expand Down
23 changes: 8 additions & 15 deletions api/src/core/adapters/dbApi/kysely/createGetCompiledData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CompiledData } from "../../../ports/CompileData";
import { Db } from "../../../ports/DbApi";
import { ParentSoftwareExternalData, SoftwareExternalData } from "../../../ports/GetSoftwareExternalData";
import { Database } from "./kysely.database";
import { convertNullValuesToUndefined, isNotNull, jsonBuildObject, jsonStripNulls } from "./kysely.utils";
import { stripNullOrUndefinedValues, isNotNull, jsonBuildObject, jsonStripNulls } from "./kysely.utils";

export const createGetCompiledData = (db: Kysely<Database>) => async (): Promise<CompiledData<"private">> => {
console.time("agentById query");
Expand Down Expand Up @@ -33,19 +33,10 @@ export const createGetCompiledData = (db: Kysely<Database>) => async (): Promise
"softwares__similar_software_external_datas.similarExternalId",
"similarExt.externalId"
)
.groupBy([
"s.id",
"csft.softwareId",
"csft.annuaireCnllServiceProviders",
"csft.comptoirDuLibreSoftware",
"csft.latestVersion",
"csft.serviceProviders",
"parentExt.externalId",
"ext.externalId"
])
.groupBy(["s.id", "csft.softwareId", "parentExt.externalId", "ext.externalId"])
.select([
"s.id",
"s.addedByAgentEmail",
"s.addedByAgentId",
"s.catalogNumeriqueGouvFrId",
"s.categories",
"s.dereferencing",
Expand Down Expand Up @@ -120,6 +111,7 @@ export const createGetCompiledData = (db: Kysely<Database>) => async (): Promise
console.time("software processing");
const processedSoftwares = results.map(
({
addedByAgentId,
externalDataSoftwareId,
annuaireCnllServiceProviders,
comptoirDuLibreSoftware,
Expand All @@ -138,7 +130,8 @@ export const createGetCompiledData = (db: Kysely<Database>) => async (): Promise
...software
}): CompiledData.Software<"private"> => {
return {
...convertNullValuesToUndefined(software),
...stripNullOrUndefinedValues(software),
addedByAgentEmail: agentById[addedByAgentId].email,
updateTime: new Date(+updateTime).getTime(),
referencedSinceTime: new Date(+referencedSinceTime).getTime(),
doRespectRgaa,
Expand Down Expand Up @@ -171,8 +164,8 @@ export const createGetCompiledData = (db: Kysely<Database>) => async (): Promise
id: instance.id!,
organization: instance.organization!,
targetAudience: instance.targetAudience!,
publicUrl: instance.publicUrl ?? undefined,
addedByAgentEmail: instance.addedByAgentEmail!,
publicUrl: instance.instanceUrl ?? undefined,
addedByAgentEmail: agentById[instance.addedByAgentId!].email,
otherWikidataSoftwares: []
}))
};
Expand Down
Loading