From f5d478757074ae172835141309c0ded53e6f8732 Mon Sep 17 00:00:00 2001 From: DevOps-zhuang Date: Wed, 3 Jul 2024 00:13:28 +0800 Subject: [PATCH] feat: Add CopilotUsageChecker class for data analysis The code changes include adding a new file `CopilotUsageChecker.ts` which contains the implementation of the `CopilotUsageChecker` class. This class is responsible for performing various checks on the data, such as checking for missing dates, empty breakdowns, and zero activity days. The class also provides a method `runChecks()` to run all the checks and return the results. --- src/api/CopilotUsageChecker.ts | 83 ++++++++++++++++++++++++++++++++++ src/components/ApiResponse.vue | 63 ++++++++++++++++++++++++-- 2 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 src/api/CopilotUsageChecker.ts diff --git a/src/api/CopilotUsageChecker.ts b/src/api/CopilotUsageChecker.ts new file mode 100644 index 00000000..49f516bf --- /dev/null +++ b/src/api/CopilotUsageChecker.ts @@ -0,0 +1,83 @@ +// 首先,导入Metrics类 +import { Metrics } from '../model/Metrics'; + +export class CopilotUsageChecker { + private data: Metrics[]; + private missingDates: string[]; + private emptyBreakdowns: string[]; + private zeroActivityDays: string[]; + + constructor(jsonText: string) { + // Map to Metrics instances + this.data = JSON.parse(jsonText).map((item: any) => new Metrics(item)); + this.missingDates = []; + this.emptyBreakdowns = []; + this.zeroActivityDays = []; + } + + private parseISO(isoString: string): Date { + return new Date(isoString); + } + + private addDays(date: Date, days: number): Date { + const result = new Date(date); + result.setDate(result.getDate() + days); + return result; + } + + private differenceInCalendarDays(date1: Date, date2: Date): number { + const diffTime = Math.abs(date2.getTime() - date1.getTime()); + return Math.ceil(diffTime / (1000 * 60 * 60 * 24)); + } + + private formatDate(date: Date): string { + return date.toISOString().split('T')[0]; + } + + private checkDatesContinuity(): void { + const dates = this.data.map(item => this.parseISO(item.day)).sort((a, b) => a.getTime() - b.getTime()); + for (let i = 1; i < dates.length; i++) { + const diff = this.differenceInCalendarDays(dates[i], dates[i - 1]); + if (diff > 1) { + let missingDate = this.addDays(dates[i - 1], 1); + while (this.differenceInCalendarDays(dates[i], missingDate) > 0) { + this.missingDates.push(this.formatDate(missingDate)); + missingDate = this.addDays(missingDate, 1); + } + } + } + } + + private checkEmptyBreakdowns(): void { + this.data.forEach(item => { + if (!item.breakdown || item.breakdown.length === 0) { + this.emptyBreakdowns.push(item.day); + } + }); + } + + private checkZeroActivityDays(): void { + this.data.forEach(item => { + //&& item.total_chat_turns === 0 && item.total_lines_suggested === 0, and just ignore the total_chat_turns now. will add it later. + if (item.total_lines_suggested === 0 ) { + this.zeroActivityDays.push(item.day); + } + }); + } + + public runChecks(): { missingDates: string[], emptyBreakdowns: string[], zeroActivityDays: string[], hasDataIssues: boolean } { + this.checkDatesContinuity(); + this.checkEmptyBreakdowns(); + this.checkZeroActivityDays(); + const hasDataIssues = this.missingDates.length > 0 || this.emptyBreakdowns.length > 0 || this.zeroActivityDays.length > 0; + return { missingDates: this.missingDates, emptyBreakdowns: this.emptyBreakdowns, zeroActivityDays: this.zeroActivityDays, hasDataIssues }; + } +} + +// Usage example: +// const jsonText = '...'; // JSON text from enterprise_response_sample.json +// const checker = new CopilotUsageChecker(jsonText); +// const { missingDates, emptyBreakdowns, zeroActivityDays,hasDataIssues } = checker.runChecks(); +// console.log("Missing dates:", missingDates); +// console.log("Days with empty breakdowns:", emptyBreakdowns); +// console.log("Days with zero activity:", zeroActivityDays); \ No newline at end of file diff --git a/src/components/ApiResponse.vue b/src/components/ApiResponse.vue index e79658a7..98e6f97e 100644 --- a/src/components/ApiResponse.vue +++ b/src/components/ApiResponse.vue @@ -2,7 +2,7 @@ -
{{ JSON.stringify(metrics, null, 2) }}
+
{{ JSON.stringify(metrics, null, 2) }}

@@ -11,11 +11,18 @@
{{ message }}
+
+
+ Check Data Quality + +
{{ message }}
+
+


-
{{ JSON.stringify(seats, null, 2) }}
+
{{ JSON.stringify(seats, null, 2) }}

@@ -29,6 +36,10 @@