From 76a20ca64ff934b1f7310a24418eedb16f4e2e3c Mon Sep 17 00:00:00 2001 From: imcaizheng Date: Wed, 3 Feb 2021 03:09:24 +0800 Subject: [PATCH] RCRM Import script: add total records created or being already exist to ummary --- scripts/recruit-crm-job-import/index.js | 26 ++++++++++++------------ scripts/recruit-crm-job-import/report.js | 17 +++++++++++++--- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/scripts/recruit-crm-job-import/index.js b/scripts/recruit-crm-job-import/index.js index e807e7f9..b0d891ae 100644 --- a/scripts/recruit-crm-job-import/index.js +++ b/scripts/recruit-crm-job-import/index.js @@ -84,8 +84,8 @@ function getPathname () { * Process single job data. The processing consists of: * - Validate the data. * - Skip processing if externalId is missing. - * - Create a job if it is not already exists. - * - Create a resource booking if it is not already exists. + * - Create a job if it does not already exist. + * - Create a resource booking if it does not already exist. * - Update the resourceBooking based on startDate and endDate. * * @param {Object} job the job data @@ -96,47 +96,47 @@ async function processJob (job, info = []) { // validate the data const { value: data, error } = validateJob(job) if (error) { - info.push(error.details[0].message) + info.push({ text: error.details[0].message, tag: 'validation_error' }) return { status: constants.ProcessingStatus.Failed, info } } if (!data.externalId) { - info.push('externalId is missing') + info.push({ text: 'externalId is missing', tag: 'external_id_missing' }) return { status: constants.ProcessingStatus.Skipped, info } } data.projectId = (await helper.getProjectByDirectProjectId(data.directProjectId)).id - // create a job if it is not already exists + // create a job if it does not already exist try { const result = await helper.getJobByExternalId(data.externalId) - info.push(`id: ${result.id} externalId: ${data.externalId} job already exists`) + info.push({ text: `id: ${result.id} externalId: ${data.externalId} job already exists`, tag: 'job_already_exists' }) data.jobId = result.id } catch (err) { if (!(err.message && err.message.includes('job not found'))) { throw err } const result = await helper.createJob(_.pick(data, ['projectId', 'externalId', 'title', 'numPositions', 'skills'])) - info.push(`id: ${result.id} job created`) + info.push({ text: `id: ${result.id} job created`, tag: 'job_created' }) data.jobId = result.id } data.userId = (await helper.getUserByHandle(data.userHandle)).id logger.debug(`userHandle: ${data.userHandle} userId: ${data.userId}`) - // create a resource booking if it is not already exists + // create a resource booking if it does not already exist try { const result = await helper.getResourceBookingByJobIdAndUserId(data.jobId, data.userId) - info.push(`id: ${result.id} resource booking already exists`) + info.push({ text: `id: ${result.id} resource booking already exists`, tag: 'resource_booking_already_exists' }) return { status: constants.ProcessingStatus.Successful, info } } catch (err) { if (!(err.message && err.message.includes('resource booking not found'))) { throw err } const result = await helper.createResourceBooking(_.pick(data, ['projectId', 'jobId', 'userId', 'startDate', 'endDate', 'memberRate', 'customerRate', 'rateType'])) - info.push(`id: ${result.id} resource booking created`) + info.push({ text: `id: ${result.id} resource booking created`, tag: 'resource_booking_created' }) data.resourceBookingId = result.id } // update the resourceBooking based on startDate and endDate const resourceBookingStatus = dateFNS.compareAsc(new Date(data.startDate), new Date(data.endDate)) === 1 ? 'closed' : 'assigned' logger.debug(`resourceBookingId: ${data.resourceBookingId} status: ${resourceBookingStatus}`) await helper.updateResourceBookingStatus(data.resourceBookingId, resourceBookingStatus) - info.push(`id: ${data.resourceBookingId} status: ${resourceBookingStatus} resource booking updated`) + info.push({ text: `id: ${data.resourceBookingId} status: ${resourceBookingStatus} resource booking updated`, tag: 'resource_booking_status_updated' }) return { status: constants.ProcessingStatus.Successful, info } } @@ -156,9 +156,9 @@ async function main () { report.add({ lnum: job._lnum, ...result }) } catch (err) { if (err.response) { - report.add({ lnum: job._lnum, status: constants.ProcessingStatus.Failed, info: [err.response.error.toString().split('\n')[0]] }) + report.add({ lnum: job._lnum, status: constants.ProcessingStatus.Failed, info: [{ text: err.response.error.toString().split('\n')[0], tag: 'request_error' }] }) } else { - report.add({ lnum: job._lnum, status: constants.ProcessingStatus.Failed, info: [err.message] }) + report.add({ lnum: job._lnum, status: constants.ProcessingStatus.Failed, info: [{ text: err.message, tag: 'internal_error' }] }) } } report.print() diff --git a/scripts/recruit-crm-job-import/report.js b/scripts/recruit-crm-job-import/report.js index 574dd8f3..ef31a4c8 100644 --- a/scripts/recruit-crm-job-import/report.js +++ b/scripts/recruit-crm-job-import/report.js @@ -19,7 +19,7 @@ class Report { // print the last message to the console print () { const lastMessage = this.messages[this.messages.length - 1] - const output = `#${lastMessage.lnum} - ${lastMessage.info.join('; ')}` + const output = `#${lastMessage.lnum} - ${_.map(lastMessage.info, 'text').join('; ')}` if (lastMessage.status === constants.ProcessingStatus.Skipped) { logger.warn(output) } @@ -33,15 +33,26 @@ class Report { // print a summary to the console printSummary () { + // summarize total success, failure, skips const groups = _.groupBy(this.messages, 'status') - const sucesss = groups[constants.ProcessingStatus.Successful] || [] + const success = groups[constants.ProcessingStatus.Successful] || [] const failure = groups[constants.ProcessingStatus.Failed] || [] const skips = groups[constants.ProcessingStatus.Skipped] || [] + // summarize records created or already existing + const groupsByTag = _.groupBy(_.flatten(_.map(this.messages, message => message.info)), 'tag') + const jobsCreated = groupsByTag.job_created || [] + const resourceBookingsCreated = groupsByTag.resource_booking_created || [] + const jobsAlreadyExist = groupsByTag.job_already_exists || [] + const resourceBookingsAlreadyExist = groupsByTag.resource_booking_already_exists || [] logger.info('=== summary ===') logger.info(`total: ${this.messages.length}`) - logger.info(`success: ${sucesss.length}`) + logger.info(`success: ${success.length}`) logger.info(`failure: ${failure.length}`) logger.info(`skips: ${skips.length}`) + logger.info(`jobs created: ${jobsCreated.length}`) + logger.info(`resource bookings created: ${resourceBookingsCreated.length}`) + logger.info(`jobs already exist: ${jobsAlreadyExist.length}`) + logger.info(`resource bookings already exist: ${resourceBookingsAlreadyExist.length}`) logger.info('=== summary ===') } }