From d0432b5dc35075de1de13be6a5ca9391f603ca7c Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Wed, 3 Jul 2024 17:21:09 -0300 Subject: [PATCH] fix(notifications): count messages and events with precision --- .../src/graphql-api/resolvers/email_stats.ts | 110 ++++++++++-------- .../src/graphql-api/schema/schema.graphql | 13 ++- 2 files changed, 69 insertions(+), 54 deletions(-) diff --git a/packages/notifications/src/graphql-api/resolvers/email_stats.ts b/packages/notifications/src/graphql-api/resolvers/email_stats.ts index 8ad69c9..7902e59 100644 --- a/packages/notifications/src/graphql-api/resolvers/email_stats.ts +++ b/packages/notifications/src/graphql-api/resolvers/email_stats.ts @@ -1,26 +1,19 @@ import logger, { apmAgent } from "../../logger"; import { client } from "../../core/elasticsearchdb"; -interface EmailEvent { - email: string; - event: string; - timestamp: number; - sg_event_id: string; - sg_message_id: string; - useragent: string; - ip: string; - url: string; -} interface EmailStatsResponse { - events: EmailEvent[]; + total: number; stats: { - open: number; - delivered: number; - bounced: number; - processed: number; - click: number; - total: number; + processed?: number; + delivered?: number; + open?: number; + unsubscribe?: number; + bounce?: number; + deferred?: number; + dropped?: number; + spamreport?: number; + click?: number; }; } @@ -29,6 +22,11 @@ interface EmailStatsArgs { category?: string; } +interface Bucket { + key: 'processed' | 'delivered' | 'open' | 'unsubscribe' | 'bounce' | 'deferred' | 'dropped' | 'spamreport' | 'click'; + doc_count: number; +} + export default async (_: void, args: EmailStatsArgs): Promise => { const { widget_id, category } = args; @@ -46,51 +44,65 @@ export default async (_: void, args: EmailStatsArgs): Promise ({ - email: hit._source.email, - event: hit._source.event, - timestamp: hit._source.timestamp, - sg_event_id: hit._source.sg_event_id, - sg_message_id: hit._source.sg_message_id, - useragent: hit._source.useragent, - ip: hit._source.ip, - url: hit._source.url, - })); + const stats = {}; + body.aggregations.events_count.buckets.forEach((item: Bucket) => { + stats[item.key] = item.doc_count + }) - const stats = { - open: events.filter(event => event.event === "open").length, - delivered: events.filter(event => event.event === "delivered").length, - bounced: events.filter(event => event.event === "bounced").length, - processed: events.filter(event => event.event === "processed").length, - click: events.filter(event => event.event === "click").length, - total: events.length + return { + total: body.aggregations.messages_count.value, + stats }; - - return { events, stats }; } catch (error) { - logger.error(`Failed to fetch email stats`); + console.log("error", error); + logger.error(`Failed to fetch email stats`);78152 throw new Error(`Failed to fetch email stats`); } }; \ No newline at end of file diff --git a/packages/notifications/src/graphql-api/schema/schema.graphql b/packages/notifications/src/graphql-api/schema/schema.graphql index d01ba65..117b087 100644 --- a/packages/notifications/src/graphql-api/schema/schema.graphql +++ b/packages/notifications/src/graphql-api/schema/schema.graphql @@ -73,16 +73,19 @@ type EmailEvent { } type EmailStats { - open: Int - delivered: Int - bounced: Int processed: Int + delivered: Int + open: Int + unsubscribe: Int + bounce: Int + deferred: Int + dropped: Int + spamreport: Int click: Int - total: Int } type EmailStatsResponse { - events: [EmailEvent] + total: Int! stats: EmailStats }