diff --git a/server/xpub-model/entities/audit/data-access.js b/server/xpub-model/entities/audit/data-access.js index 80d45fc750a5b633c36badbbe1d6067791b5669f..af3dc27b5641b5b72684e4f2a4faa77dcdbcc207 100644 --- a/server/xpub-model/entities/audit/data-access.js +++ b/server/xpub-model/entities/audit/data-access.js @@ -96,18 +96,23 @@ right join publishers p on p.user_id = created.user_id // xml_errors, xml_tagging, xml_tagging_within_3_days, xml_tagging_within_3_days_perc const metrics = await knex.raw(` with audits as ( + select au.* from audit.audit_log au + inner join manuscript m on au.manuscript_id = m.id + where m.organization_id='${orgId}' + and au.created between date_trunc('month', current_date AT TIME ZONE 'Europe/London' - '${endMonth} month'::interval) + and date_trunc('month', current_date AT TIME ZONE 'Europe/London' - '${startMonth - + 1} month'::interval) + and ( + au.changes->>'status' is not null or + au.changes::text ~* '(re-|re)tag' + ) +), tag_audits as ( select au.* from audit.audit_log au inner join manuscript m on au.manuscript_id = m.id where m.organization_id='${orgId}' and au.changes->>'type' = 'PMC' - or ( - au.created between date_trunc('month', current_date AT TIME ZONE 'Europe/London' - '${endMonth} month'::interval) - and date_trunc('month', current_date AT TIME ZONE 'Europe/London' - '${startMonth - - 1} month'::interval) - and ( - au.changes->>'status' is not null or - au.changes::text ~* '(re-|re)tag' - ) + and au.user_id in ( + select user_id from team t where t.role_name='tagger' ) ), external as ( select * from audits ae where ae.user_id not in ( @@ -120,14 +125,8 @@ with audits as ( from ( select aa.manuscript_id, aa.manuscript_version, ab.sent, aa.created, generate_series(ab.sent, aa.created - '1 day'::interval, '1 day'::interval) as date_range from ( - select * from ( - select at.manuscript_id, at.manuscript_version, min(at.created) created from audits at - where at.user_id in ( - select user_id from team t where t.role_name='tagger' - ) - and at.changes->>'type' = 'PMC' - group by at.manuscript_id, at.manuscript_version - ) am where am.created between date_trunc('month', current_date AT TIME ZONE 'Europe/London' - '${endMonth} month'::interval) and date_trunc('month', current_date AT TIME ZONE 'Europe/London' - '${startMonth - + select * from tag_audits at + where at.created between date_trunc('month', current_date AT TIME ZONE 'Europe/London' - '${endMonth} month'::interval) and date_trunc('month', current_date AT TIME ZONE 'Europe/London' - '${startMonth - 1} month'::interval) union select a.manuscript_id, a.manuscript_version, a.created created from audits a