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

Feat/improve logs #113

Merged
merged 2 commits into from
Jul 2, 2024
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
40 changes: 38 additions & 2 deletions packages/generic/backend/src/lib/annotator/buildAnnotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ function buildAnnotator(
operationName: 'annotateDocumentsWithoutAnnotations',
msg: `Error while annotating document ${formatDocumentInfos(
currentDocumentToAnnotate,
)}. Setting the document to its previous status...`,
)}.`,
data: {
sourceId: currentDocumentToAnnotate.documentNumber,
sourceName: currentDocumentToAnnotate.source,
},
});
await documentService.updateDocumentStatus(
currentDocumentToAnnotate._id,
Expand Down Expand Up @@ -200,7 +204,8 @@ function buildAnnotator(
} = await annotatorConfig.fetchAnnotationOfDocument(settings, document);
logger.log({
operationName: 'annotateDocument',
msg: 'NLP annotation succeeded',
msg: 'NLP annotation succesfully fetched',
data: { sourceId: document.documentNumber, sourceName: document.source },
});
documentService.updateDocumentNlpVersions(documentId, version);

Expand All @@ -216,6 +221,7 @@ function buildAnnotator(
logger.log({
operationName: 'annotateDocument',
msg: 'NLP treatment created in DB',
data: { sourceId: document.documentNumber, sourceName: document.source },
});

if (document.decisionMetadata.motivationOccultation === true) {
Expand All @@ -227,6 +233,10 @@ function buildAnnotator(
logger.log({
operationName: 'annotateDocument',
msg: 'Annotate motivation zone because motivationOccultation is true',
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});

createMotivationOccultationTreatment(
Expand All @@ -242,6 +252,10 @@ function buildAnnotator(
msg: `Annotate decision motivation zone impossible because motication zone was not found for document ${formatDocumentInfos(
document,
)}`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});
}
}
Expand All @@ -251,6 +265,7 @@ function buildAnnotator(
logger.log({
operationName: 'annotateDocument',
msg: 'Annotation report created in DB',
data: { sourceId: document.documentNumber, sourceName: document.source },
});

if (
Expand All @@ -260,6 +275,10 @@ function buildAnnotator(
logger.log({
operationName: 'annotateDocument',
msg: `additionalTermsParsingFailed found, updating with value ${additionalTermsParsingFailed}`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});
await documentService.updateDocumentAdditionalTermsParsingFailed(
documentId,
Expand All @@ -272,6 +291,10 @@ function buildAnnotator(
logger.log({
operationName: 'annotateDocument',
msg: `categoriesToUnAnnotate found, adding '${newCategoriesToUnAnnotate}' to categoriesToOmit if not already in`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});
newCategoriesToOmit = Array.from(
new Set(newCategoriesToOmit.concat(newCategoriesToUnAnnotate)),
Expand All @@ -282,6 +305,10 @@ function buildAnnotator(
logger.log({
operationName: 'annotateDocument',
msg: `categoriesToAnnotate found, removing '${newCategoriesToAnnotate}' from categoriesToOmit if present`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});
newCategoriesToOmit = newCategoriesToOmit.filter(
(category) => !newCategoriesToAnnotate.includes(category),
Expand All @@ -292,6 +319,10 @@ function buildAnnotator(
logger.log({
operationName: 'annotateDocument',
msg: `updating categoriesToOmit in database`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});
documentService.updateDocumentCategoriesToOmit(
documentId,
Expand All @@ -304,6 +335,10 @@ function buildAnnotator(
operationName: 'annotateDocument',
msg:
'Additionals terms to annotate or to unannotate found, adding to document...',
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});

await documentService.updateDocumentComputedAdditionalTerms(
Expand Down Expand Up @@ -352,6 +387,7 @@ function buildAnnotator(
logger.log({
operationName: 'annotateDocument',
msg: `Annotation done for document ${formatDocumentInfos(document)}`,
data: { sourceId: document.documentNumber, sourceName: document.source },
});
}

Expand Down
47 changes: 43 additions & 4 deletions packages/generic/backend/src/lib/connector/buildConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function buildConnector(connectorConfig: connectorConfigType) {
threshold: number;
}) {
logger.log({
operationName: 'annotateDocument',
msg: `importChainedDocumentsFromSder: ${threshold}`,
operationName: 'importChainedDocumentsFromSder',
msg: `Import chained documents from sder with ${threshold} threshold`,
});

const documentRepository = buildDocumentRepository();
Expand Down Expand Up @@ -813,12 +813,51 @@ async function insertDocuments(documents: documentType[]) {
const documentRepository = buildDocumentRepository();

for await (const document of documents) {
await documentRepository.insert(document);
try {
await documentRepository.insert(document);
logger.log({
operationName: 'documentInsertion',
msg: `Document ${document.source}:${document.documentNumber} has been inserted in database imported by ${document.importer}`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});
} catch {
logger.error({
operationName: 'documentInsertion',
msg: `Failed to import ${document.source}:${document.documentNumber} document`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});
}
}
}

function insertDocument(document: documentType) {
const documentRepository = buildDocumentRepository();

return documentRepository.insert(document);
try {
const insertedDocument = documentRepository.insert(document);
logger.log({
operationName: 'documentInsertion',
msg: `Document ${document.source}:${document.documentNumber} has been inserted in database imported by ${document.importer}`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});
return insertedDocument;
} catch {
logger.error({
operationName: 'documentInsertion',
msg: `Failed to import ${document.source}:${document.documentNumber} document`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});
}
}
8 changes: 8 additions & 0 deletions packages/generic/backend/src/lib/exporter/buildExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ function buildExporter(
pseudonymizationText: anonymizer.anonymizeDocument(document).text,
labelTreatments: treatmentModule.lib.concat(treatments, version),
});
logger.log({
operationName: 'exportDocument',
msg: `Document ${document.source}:${document.documentNumber} has been exported`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
},
});

await statisticService.saveStatisticsOfDocument(document, settings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { documentType, idModule } from '@label/core';
import { assignationService } from '../../../assignation';
import { buildDocumentRepository } from '../../repository';
import { resetDocument } from './resetDocument';
import { logger } from '../../../../utils';

export { updateDocumentStatus };

Expand All @@ -27,5 +28,13 @@ async function updateDocumentStatus(
} else if (status === 'loaded') {
await resetDocument(_id);
}
logger.log({
operationName: 'updateDocumentStatus',
msg: `Document ${updatedDocument.source}:${updatedDocument.documentNumber} status updated to ${status}`,
data: {
sourceId: updatedDocument.documentNumber,
sourceName: updatedDocument.source,
},
});
return updatedDocument;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { documentService } from '../../document';
import { userService } from '../../user';
import { buildProblemReportRepository } from '../repository';
import { logger } from '../../../utils';

export { problemReportService };

Expand Down Expand Up @@ -35,6 +36,18 @@ const problemReportService = {
type: problemType,
}),
);
const document = await documentService.fetchAllDocumentsByIds([documentId]);
const user = await userService.fetchUsersByIds([userId]);
logger.log({
operationName: 'createProblemReport',
msg: `Problem reported created on document ${document[0].source}:${document[0].documentNumber} by ${user[0].name}`,
data: {
sourceId: document[0].documentNumber,
sourceName: document[0].source,
userId: userId,
userName: user[0].name,
},
});
},

async deleteProblemReportById(problemReportId: problemReportType['_id']) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {
documentType,
idModule,
settingsType,
statisticsCreator,
timeOperator,
treatmentModule,
} from '@label/core';
import { assignationService } from '../../assignation';
import { treatmentService } from '../../treatment';
import { buildStatisticRepository } from '../repository';
import { logger } from '../../../utils';
import { userService } from '../../../modules/user';

export { saveStatisticsOfDocument };

Expand Down Expand Up @@ -38,5 +42,56 @@ async function saveStatisticsOfDocument(
settings,
});

if (humanTreatments && humanTreatments.length > 0) {
const userIds = humanTreatments.map((humanTreatment) =>
idModule.lib.buildId(humanTreatment.userId),
);
const users = await userService.fetchUsersByIds(userIds);

for (const humanTreatment of humanTreatments) {
const user = users[idModule.lib.convertToString(humanTreatment.userId)];

if (user) {
logger.log({
operationName: 'documentStatistics',
msg: `Human treatment for document ${document.source}:${
document.documentNumber
} : ${
user.name
} treat the document in ${timeOperator.convertDurationToReadableDuration(
humanTreatment.treatment.duration,
)} on ${timeOperator.convertTimestampToReadableDate(
humanTreatment.treatment.lastUpdateDate,
true,
)}`,
data: {
sourceId: document.documentNumber,
sourceName: document.source,
treatmentDuration: humanTreatment.treatment.duration,
lastUpdateDate: humanTreatment.treatment.lastUpdateDate,
},
});
}
}
}

logger.log({
operationName: 'documentStatistics',
msg: `Create statistics for document ${document.source}:${document.documentNumber}`,
data: {
sourceId: statistic.documentNumber,
sourceName: statistic.source,
statistics: {
route: statistic.route,
wordsCount: statistic.wordsCount,
surAnnotationsCount: statistic.surAnnotationsCount,
subAnnotationsSensitiveCount: statistic.subAnnotationsSensitiveCount,
subAnnotationsNonSensitiveCount:
statistic.subAnnotationsNonSensitiveCount,
linkedEntitiesCount: statistic.linkedEntitiesCount,
},
},
});

await statisticRepository.insert(statistic);
}
Loading