feat(reports): implement reports page and data visualization components
Implement a comprehensive reports dashboard including user question summaries, AI performance metrics, and usage trends. This includes the creation of several reusable chart components and supporting services. - Add `ReportsPage` with tabbed navigation for questions and performance - Implement `ReportsService` with mock data support for rapid development - Create reusable UI components: `BarChartComponent`, `DoughnutChartComponent`, and `LineChartComponent` - Add `format.util.ts` for Persian digit and number formatting - Integrate 'Lahze' font family and add font assets - Add unit tests for `ReportsPage` and `ReportsService`
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import {
|
||||
AiChannelUsage,
|
||||
AiPerformance,
|
||||
CategoryShare,
|
||||
QuestionSummary,
|
||||
TopQuestion,
|
||||
UsageTrendPoint,
|
||||
} from './reports.types';
|
||||
|
||||
const TREND_DAYS = 30;
|
||||
|
||||
/** Deterministic pseudo-random source so mock data stays stable across reloads and tests. */
|
||||
function seededNoise(seed: number): number {
|
||||
const value = Math.sin(seed * 12.9898) * 43758.5453;
|
||||
return Math.abs(value - Math.floor(value));
|
||||
}
|
||||
|
||||
function buildTrend(): UsageTrendPoint[] {
|
||||
const points: UsageTrendPoint[] = [];
|
||||
const today = new Date();
|
||||
|
||||
for (let offset = TREND_DAYS - 1; offset >= 0; offset--) {
|
||||
const date = new Date(today);
|
||||
date.setDate(today.getDate() - offset);
|
||||
|
||||
const weekday = date.getDay();
|
||||
const isWeekend = weekday === 4 || weekday === 5;
|
||||
const noise = seededNoise(offset + 1);
|
||||
|
||||
// Recent campaign window lifts the last two weeks.
|
||||
const campaign = offset <= 14 ? 9 : 0;
|
||||
const base = isWeekend ? 34 : 58;
|
||||
|
||||
const count = Math.round(base + noise * 22 - (isWeekend ? 12 : 0) + campaign);
|
||||
|
||||
points.push({
|
||||
label: date.toLocaleDateString('fa-IR', { day: 'numeric', month: 'long' }),
|
||||
count,
|
||||
});
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
export const MOCK_TOP_QUESTIONS: TopQuestion[] = [
|
||||
{ id: 'q1', text: 'چگونه رمز عبور حساب کاربری خود را بازیابی کنم؟', count: 142, category: 'حساب کاربری' },
|
||||
{ id: 'q2', text: 'ساعات کاری پشتیبانی در چه ساعتی است؟', count: 128, category: 'پشتیبانی' },
|
||||
{ id: 'q3', text: 'برای دریافت فاکتور رسمی چگونه اقدام کنم؟', count: 97, category: 'پرداخت و فاکتور' },
|
||||
{ id: 'q4', text: 'آیا ارسال به شهرستانها امکانپذیر است؟', count: 88, category: 'سفارش و ارسال' },
|
||||
{ id: 'q5', text: 'چگونه سفارش خود را پیگیری کنم؟', count: 76, category: 'سفارش و ارسال' },
|
||||
{ id: 'q6', text: 'هزینه ارسال چگونه محاسبه میشود؟', count: 61, category: 'سفارش و ارسال' },
|
||||
{ id: 'q7', text: 'شرایط مرجوع کردن کالا چیست؟', count: 54, category: 'پشتیبانی' },
|
||||
{ id: 'q8', text: 'آیا امکان پرداخت اقساطی وجود دارد؟', count: 47, category: 'پرداخت و فاکتور' },
|
||||
];
|
||||
|
||||
export const MOCK_QUESTION_SUMMARY: QuestionSummary = {
|
||||
totalQuestions: 4802,
|
||||
uniqueUsers: 1736,
|
||||
aiAnswered: 4318,
|
||||
escalatedToAgent: 484,
|
||||
periodLabel: '۳۰ روز گذشته',
|
||||
};
|
||||
|
||||
export const MOCK_CATEGORY_SHARES: CategoryShare[] = [
|
||||
{ category: 'حساب کاربری', count: 1265 },
|
||||
{ category: 'سفارش و ارسال', count: 1148 },
|
||||
{ category: 'پرداخت و فاکتور', count: 862 },
|
||||
{ category: 'پشتیبانی', count: 668 },
|
||||
{ category: 'محصولات', count: 479 },
|
||||
{ category: 'سایر', count: 380 },
|
||||
];
|
||||
|
||||
export const MOCK_USAGE_TREND: UsageTrendPoint[] = buildTrend();
|
||||
|
||||
export const MOCK_AI_CHANNELS: AiChannelUsage[] = [
|
||||
{ label: 'گفتگوی متنی', count: 3622 },
|
||||
{ label: 'گفتگوی صوتی', count: 1180 },
|
||||
];
|
||||
|
||||
export const MOCK_AI_PERFORMANCE: AiPerformance = {
|
||||
totalResponses: 4802,
|
||||
faqBasedResponses: 3553,
|
||||
avgResponseSeconds: 1.8,
|
||||
successRate: 91,
|
||||
satisfactionRate: 87,
|
||||
escalationRate: 10,
|
||||
channels: MOCK_AI_CHANNELS,
|
||||
};
|
||||
@@ -0,0 +1,83 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { provideZonelessChangeDetection } from '@angular/core';
|
||||
import { provideAnimations } from '@angular/platform-browser/animations';
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
import { provideRouter } from '@angular/router';
|
||||
import { TOAST_CONFIG, DefaultGlobalConfig } from 'ngx-toastr';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import { ReportsService } from './reports.service';
|
||||
|
||||
describe('ReportsService', () => {
|
||||
let service: ReportsService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
provideZonelessChangeDetection(),
|
||||
provideHttpClient(),
|
||||
provideRouter([]),
|
||||
provideAnimations(),
|
||||
{ provide: TOAST_CONFIG, useValue: { default: DefaultGlobalConfig, config: {} } },
|
||||
],
|
||||
});
|
||||
service = TestBed.inject(ReportsService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
|
||||
it('returns top questions ranked by frequency', async () => {
|
||||
const questions = await firstValueFrom(service.getTopQuestions());
|
||||
|
||||
expect(questions.length).toBeGreaterThan(0);
|
||||
const counts = questions.map((question) => question.count);
|
||||
expect([...counts].sort((a, b) => b - a)).toEqual(counts);
|
||||
});
|
||||
|
||||
it('returns a question summary whose parts stay within the total', async () => {
|
||||
const summary = await firstValueFrom(service.getQuestionSummary());
|
||||
|
||||
expect(summary.totalQuestions).toBeGreaterThan(0);
|
||||
expect(summary.aiAnswered + summary.escalatedToAgent).toBeLessThanOrEqual(
|
||||
summary.totalQuestions,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns category shares summing up to the total question count', async () => {
|
||||
const categories = await firstValueFrom(service.getCategoryDistribution());
|
||||
const summary = await firstValueFrom(service.getQuestionSummary());
|
||||
const total = categories.reduce((sum, category) => sum + category.count, 0);
|
||||
|
||||
expect(total).toBe(summary.totalQuestions);
|
||||
});
|
||||
|
||||
it('returns a 30 point usage trend with valid labels', async () => {
|
||||
const trend = await firstValueFrom(service.getUsageTrend());
|
||||
|
||||
expect(trend).toHaveLength(30);
|
||||
trend.forEach((point) => {
|
||||
expect(point.label).toBeTruthy();
|
||||
expect(point.count).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns AI performance stats with bounded rates', async () => {
|
||||
const performance = await firstValueFrom(service.getAiPerformance());
|
||||
|
||||
expect(performance.totalResponses).toBeGreaterThan(0);
|
||||
expect(performance.channels.length).toBeGreaterThan(0);
|
||||
expect(performance.successRate).toBeLessThanOrEqual(100);
|
||||
expect(performance.satisfactionRate).toBeLessThanOrEqual(100);
|
||||
});
|
||||
|
||||
it('returns a complete overview combining every report', async () => {
|
||||
const overview = await firstValueFrom(service.getOverview());
|
||||
|
||||
expect(overview.topQuestions.length).toBeGreaterThan(0);
|
||||
expect(overview.categories.length).toBeGreaterThan(0);
|
||||
expect(overview.trend).toHaveLength(30);
|
||||
expect(overview.summary.totalQuestions).toBeGreaterThan(0);
|
||||
expect(overview.aiPerformance.totalResponses).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { map, Observable, of } from 'rxjs';
|
||||
import { HttpService } from '../http-request/http.service';
|
||||
import {
|
||||
AiPerformance,
|
||||
CategoryShare,
|
||||
QuestionSummary,
|
||||
ReportsView,
|
||||
TopQuestion,
|
||||
UsageTrendPoint,
|
||||
} from './reports.types';
|
||||
import {
|
||||
MOCK_AI_PERFORMANCE,
|
||||
MOCK_CATEGORY_SHARES,
|
||||
MOCK_QUESTION_SUMMARY,
|
||||
MOCK_TOP_QUESTIONS,
|
||||
MOCK_USAGE_TREND,
|
||||
} from './reports.mock';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class ReportsService {
|
||||
private readonly http = inject(HttpService);
|
||||
|
||||
/** Flip to false once the backend report endpoints are deployed. */
|
||||
private readonly useMockData = true;
|
||||
|
||||
getTopQuestions(limit = 8): Observable<TopQuestion[]> {
|
||||
return this.request<TopQuestion[]>('reports/top-questions', { limit }, MOCK_TOP_QUESTIONS);
|
||||
}
|
||||
|
||||
getQuestionSummary(): Observable<QuestionSummary> {
|
||||
return this.request<QuestionSummary>('reports/questions-summary', {}, MOCK_QUESTION_SUMMARY);
|
||||
}
|
||||
|
||||
getCategoryDistribution(): Observable<CategoryShare[]> {
|
||||
return this.request<CategoryShare[]>(
|
||||
'reports/questions-distribution',
|
||||
{},
|
||||
MOCK_CATEGORY_SHARES,
|
||||
);
|
||||
}
|
||||
|
||||
getUsageTrend(days = 30): Observable<UsageTrendPoint[]> {
|
||||
return this.request<UsageTrendPoint[]>('reports/usage-trend', { days }, MOCK_USAGE_TREND);
|
||||
}
|
||||
|
||||
getAiPerformance(): Observable<AiPerformance> {
|
||||
return this.request<AiPerformance>('reports/ai-performance', {}, MOCK_AI_PERFORMANCE);
|
||||
}
|
||||
|
||||
getOverview(): Observable<ReportsView> {
|
||||
return this.request<ReportsView>('reports/overview', {}, {
|
||||
topQuestions: MOCK_TOP_QUESTIONS,
|
||||
summary: MOCK_QUESTION_SUMMARY,
|
||||
categories: MOCK_CATEGORY_SHARES,
|
||||
trend: MOCK_USAGE_TREND,
|
||||
aiPerformance: MOCK_AI_PERFORMANCE,
|
||||
});
|
||||
}
|
||||
|
||||
private request<T>(endpoint: string, params: Record<string, unknown>, fallback: T): Observable<T> {
|
||||
if (this.useMockData) {
|
||||
return of(fallback);
|
||||
}
|
||||
|
||||
return this.http
|
||||
.get<T>(endpoint, { params, skipToast: true, skipHasErrorCheck: true })
|
||||
.pipe(map((response) => response.result));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
export interface TopQuestion {
|
||||
id: string;
|
||||
text: string;
|
||||
count: number;
|
||||
category: string;
|
||||
}
|
||||
|
||||
export interface QuestionSummary {
|
||||
totalQuestions: number;
|
||||
uniqueUsers: number;
|
||||
aiAnswered: number;
|
||||
escalatedToAgent: number;
|
||||
periodLabel: string;
|
||||
}
|
||||
|
||||
export interface CategoryShare {
|
||||
category: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
export interface UsageTrendPoint {
|
||||
label: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
export interface AiChannelUsage {
|
||||
label: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
export interface AiPerformance {
|
||||
totalResponses: number;
|
||||
faqBasedResponses: number;
|
||||
avgResponseSeconds: number;
|
||||
successRate: number;
|
||||
satisfactionRate: number;
|
||||
escalationRate: number;
|
||||
channels: AiChannelUsage[];
|
||||
}
|
||||
|
||||
export interface ReportsView {
|
||||
topQuestions: TopQuestion[];
|
||||
summary: QuestionSummary;
|
||||
categories: CategoryShare[];
|
||||
trend: UsageTrendPoint[];
|
||||
aiPerformance: AiPerformance;
|
||||
}
|
||||
Reference in New Issue
Block a user