-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Connecte un datamart externe pour servir les résultats de c…
- Loading branch information
Showing
20 changed files
with
382 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const datamartBuffer = { | ||
objectsToInsert: [], | ||
|
||
pushInsertable({ tableName, values }) { | ||
this.objectsToInsert.push({ tableName, values }); | ||
|
||
return values; | ||
}, | ||
|
||
purge() { | ||
this.objectsToInsert = []; | ||
}, | ||
}; | ||
|
||
export { datamartBuffer }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { datamartBuffer } from './datamart-buffer.js'; | ||
import { factory } from './factory/index.js'; | ||
|
||
/** | ||
* @class DatamartBuilder | ||
* @property {Factory} factory | ||
*/ | ||
class DatamartBuilder { | ||
constructor({ knex }) { | ||
this.knex = knex; | ||
this.datamartBuffer = datamartBuffer; | ||
this.factory = factory; | ||
} | ||
|
||
static async create({ knex }) { | ||
const datamartBuilder = new DatamartBuilder({ knex }); | ||
|
||
try { | ||
await datamartBuilder._init(); | ||
} catch (_) { | ||
// Error thrown only with unit tests | ||
} | ||
|
||
return datamartBuilder; | ||
} | ||
|
||
async commit() { | ||
try { | ||
const trx = await this.knex.transaction(); | ||
for (const objectToInsert of this.datamartBuffer.objectsToInsert) { | ||
await trx(objectToInsert.tableName).insert(objectToInsert.values); | ||
} | ||
await trx.commit(); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Erreur dans datamartBuilder.commit() : ${err}`); | ||
throw err; | ||
} finally { | ||
this.datamartBuffer.purge(); | ||
} | ||
} | ||
|
||
async clean() { | ||
let rawQuery = ''; | ||
|
||
['data_export_parcoursup_certif_result'].forEach((tableName) => { | ||
rawQuery += `DELETE FROM ${tableName};`; | ||
}); | ||
|
||
try { | ||
await this.knex.raw(rawQuery); | ||
} catch { | ||
// ignore error | ||
} | ||
} | ||
} | ||
|
||
export { DatamartBuilder }; |
18 changes: 18 additions & 0 deletions
18
api/datamart/datamart-builder/factory/build-certification-result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { datamartBuffer } from '../datamart-buffer.js'; | ||
|
||
const buildCertificationResult = function ({ nationalStudentId } = {}) { | ||
const values = { | ||
national_student_id: nationalStudentId, | ||
}; | ||
|
||
datamartBuffer.pushInsertable({ | ||
tableName: 'data_export_parcoursup_certif_result', | ||
values, | ||
}); | ||
|
||
return { | ||
nationalStudentId, | ||
}; | ||
}; | ||
|
||
export { buildCertificationResult }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { dirname, join } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
import { importNamedExportsFromDirectory } from '../../../src/shared/infrastructure/utils/import-named-exports-from-directory.js'; | ||
|
||
const path = dirname(fileURLToPath(import.meta.url)); | ||
const unwantedFiles = ['index.js']; | ||
|
||
const datamartBuilders = await importNamedExportsFromDirectory({ | ||
path: join(path, './'), | ||
ignoredFileNames: unwantedFiles, | ||
}); | ||
|
||
export const factory = { | ||
...datamartBuilders, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as url from 'node:url'; | ||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); | ||
import * as dotenv from 'dotenv'; | ||
|
||
import { buildPostgresEnvironment } from '../db/utils/build-postgres-environment.js'; | ||
dotenv.config({ path: `${__dirname}/../.env` }); | ||
|
||
const baseConfiguration = { | ||
migrationsDirectory: './migrations/', | ||
seedsDirectory: './seeds/', | ||
databaseUrl: process.env.DATAMART_DATABASE_URL, | ||
}; | ||
|
||
const environments = { | ||
development: buildPostgresEnvironment(baseConfiguration), | ||
|
||
test: buildPostgresEnvironment({ ...baseConfiguration, databaseUrl: process.env.TEST_DATAMART_DATABASE_URL }), | ||
|
||
production: buildPostgresEnvironment(baseConfiguration), | ||
}; | ||
|
||
export default environments; |
14 changes: 14 additions & 0 deletions
14
api/datamart/migrations/20241224080125-create-certification-results-table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const TABLE_NAME = 'data_export_parcoursup_certif_result'; | ||
|
||
const up = async function (knex) { | ||
await knex.schema.createTable(TABLE_NAME, function (table) { | ||
table.string('national_student_id'); | ||
table.index('national_student_id'); | ||
}); | ||
}; | ||
|
||
const down = async function (knex) { | ||
await knex.schema.dropTable(TABLE_NAME); | ||
}; | ||
|
||
export { down, up }; |
2 changes: 2 additions & 0 deletions
2
api/datamart/seeds/csv/data_export_parcoursup_certif_result.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
national_student_id | ||
123456789OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { readdirSync } from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
import { parseCsvWithHeader } from '../../scripts/helpers/csvHelpers.js'; | ||
import { logger } from '../../src/shared/infrastructure/utils/logger.js'; | ||
|
||
// data are populated from exported data in csv files | ||
// csv files shall be named with the table name they represent | ||
const csvFolder = path.join(import.meta.dirname, 'csv'); | ||
|
||
export async function seed(knex) { | ||
const csvFilesToImport = readdirSync(csvFolder); | ||
for (const file of csvFilesToImport) { | ||
await insertDataFromFile(path.join(csvFolder, file), knex); | ||
} | ||
} | ||
|
||
async function insertDataFromFile(file, knex) { | ||
const tableName = path.basename(file, '.csv'); | ||
logger.info(`Inserting data from ${file} to ${tableName}`); | ||
|
||
const data = await parseCsvWithHeader(file); | ||
await knex(tableName).truncate(); | ||
await knex.batchInsert(tableName, data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,29 @@ | ||
import * as url from 'node:url'; | ||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); | ||
import * as dotenv from 'dotenv'; | ||
|
||
import { buildPostgresEnvironment } from './utils/build-postgres-environment.js'; | ||
dotenv.config({ path: `${__dirname}/../.env` }); | ||
|
||
function localPostgresEnv(databaseUrl, knexAsyncStacktraceEnabled) { | ||
return { | ||
client: 'postgresql', | ||
connection: databaseUrl, | ||
pool: { | ||
min: 1, | ||
max: 4, | ||
}, | ||
migrations: { | ||
tableName: 'knex_migrations', | ||
directory: './migrations', | ||
loadExtensions: ['.js'], | ||
}, | ||
seeds: { | ||
directory: './seeds', | ||
loadExtensions: ['.js'], | ||
}, | ||
asyncStackTraces: knexAsyncStacktraceEnabled !== 'false', | ||
}; | ||
} | ||
const environments = { | ||
development: localPostgresEnv(process.env.DATABASE_URL, process.env.KNEX_ASYNC_STACKTRACE_ENABLED), | ||
const baseConfiguration = { | ||
migrationsDirectory: './migrations/', | ||
seedsDirectory: './seeds/', | ||
databaseUrl: process.env.DATABASE_URL, | ||
}; | ||
|
||
export default { | ||
development: buildPostgresEnvironment(baseConfiguration), | ||
|
||
test: localPostgresEnv(process.env.TEST_DATABASE_URL, process.env.KNEX_ASYNC_STACKTRACE_ENABLED), | ||
test: buildPostgresEnvironment({ | ||
...baseConfiguration, | ||
databaseUrl: process.env.TEST_DATABASE_URL, | ||
}), | ||
|
||
production: { | ||
client: 'postgresql', | ||
connection: process.env.DATABASE_URL, | ||
production: buildPostgresEnvironment({ | ||
...baseConfiguration, | ||
pool: { | ||
min: parseInt(process.env.DATABASE_CONNECTION_POOL_MIN_SIZE, 10) || 1, | ||
max: parseInt(process.env.DATABASE_CONNECTION_POOL_MAX_SIZE, 10) || 4, | ||
}, | ||
migrations: { | ||
tableName: 'knex_migrations', | ||
directory: './migrations', | ||
loadExtensions: ['.js'], | ||
min: parseInt(process.env.DATABASE_CONNECTION_POOL_MIN_SIZE, 10), | ||
max: parseInt(process.env.DATABASE_CONNECTION_POOL_MAX_SIZE, 10), | ||
}, | ||
seeds: { | ||
directory: './seeds', | ||
loadExtensions: ['.js'], | ||
}, | ||
asyncStackTraces: process.env.KNEX_ASYNC_STACKTRACE_ENABLED !== 'false', | ||
}, | ||
}), | ||
}; | ||
|
||
export default environments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export function buildPostgresEnvironment({ databaseUrl, pool, migrationsDirectory, seedsDirectory }) { | ||
return { | ||
client: 'postgresql', | ||
connection: databaseUrl, | ||
pool: { | ||
min: pool?.min || 1, | ||
max: pool?.max || 4, | ||
}, | ||
migrations: { | ||
tableName: 'knex_migrations', | ||
directory: migrationsDirectory, | ||
loadExtensions: ['.js'], | ||
}, | ||
seeds: { | ||
directory: seedsDirectory, | ||
loadExtensions: ['.js'], | ||
}, | ||
asyncStackTraces: process.env.KNEX_ASYNC_STACKTRACE_ENABLED !== 'false', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.