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:
Peyman Honarjoo
2026-10-09 17:48:18 +03:30
parent 72e7d0a007
commit 0b5a882ecb
30 changed files with 1931 additions and 5 deletions
+406
View File
@@ -0,0 +1,406 @@
.reports-page {
min-height: 100%;
padding: 2.25rem 3rem 3.5rem;
display: flex;
flex-direction: column;
gap: 1.5rem;
animation: fadeIn 0.4s ease-out;
}
.reports-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 1rem;
flex-wrap: wrap;
}
.reports-title-group {
display: flex;
align-items: center;
gap: 0.875rem;
}
.reports-title-icon {
display: flex;
align-items: center;
justify-content: center;
width: 44px;
height: 44px;
border-radius: 0.875rem;
background: var(--color-primary) / 12;
color: var(--color-primary);
flex-shrink: 0;
}
.reports-title {
margin: 0;
font-size: 1.5rem;
font-weight: 700;
color: var(--text-primary);
line-height: 1.3;
}
.reports-subtitle {
margin: 0.15rem 0 0;
font-size: 0.8125rem;
color: var(--text-muted);
}
.period-chip {
padding: 0.45rem 0.9rem;
border: 1px solid var(--card-border);
border-radius: 9999px;
background: var(--card-bg);
font-size: 0.75rem;
color: var(--text-secondary);
white-space: nowrap;
}
.reports-tabs {
display: inline-flex;
align-self: flex-start;
gap: 0.25rem;
padding: 0.3rem;
border: 1px solid var(--card-border);
border-radius: 1rem;
background: var(--sidebar-hover);
}
.tab-btn {
position: relative;
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.55rem 1.15rem;
border: none;
border-radius: 0.75rem;
background: transparent;
color: var(--text-muted);
font-size: 0.8125rem;
font-weight: 500;
cursor: pointer;
transition: color 0.2s ease, background 0.2s ease;
white-space: nowrap;
}
.tab-btn:hover {
color: var(--text-primary);
}
.tab-btn.tab-active {
background: var(--card-bg);
color: var(--color-primary);
box-shadow: 0 4px 14px rgba(15, 23, 42, 0.08);
}
.tab-indicator {
position: absolute;
bottom: 0.3rem;
left: 50%;
transform: translateX(-50%);
width: 1.5rem;
height: 0.125rem;
border-radius: 9999px;
background: var(--color-primary);
animation: slideIn 0.3s ease-out;
}
.tab-panel {
display: flex;
flex-direction: column;
gap: 1.25rem;
animation: fadeIn 0.3s ease-out;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 1.25rem;
}
.charts-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 1.25rem;
}
.ai-grid {
display: grid;
grid-template-columns: minmax(0, 5fr) minmax(0, 7fr);
gap: 1.25rem;
}
.report-card {
padding: 1.25rem 1.4rem;
border: 1px solid var(--card-border);
border-radius: 1.25rem;
background: var(--card-bg);
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.06), 0 2px 10px rgba(0, 0, 0, 0.03);
animation: fadeInUp 0.5s ease-out both;
}
.report-card:nth-child(1) { animation-delay: 0.03s; }
.report-card:nth-child(2) { animation-delay: 0.06s; }
.report-card:nth-child(3) { animation-delay: 0.09s; }
.report-card:nth-child(4) { animation-delay: 0.12s; }
.stat-card {
display: flex;
flex-direction: column;
gap: 0.35rem;
}
.stat-label {
font-size: 0.8125rem;
color: var(--text-muted);
}
.stat-value {
font-size: 1.75rem;
font-weight: 700;
color: var(--text-primary);
line-height: 1.2;
}
.stat-hint {
font-size: 0.6875rem;
color: var(--text-muted);
}
.stat-progress {
height: 6px;
border-radius: 9999px;
background: var(--stat-progress-bg, rgba(100, 116, 139, 0.16));
overflow: hidden;
}
.stat-progress-fill {
display: block;
height: 100%;
border-radius: 9999px;
background: var(--color-primary);
transition: width 0.8s cubic-bezier(0.22, 1, 0.36, 1);
}
.card-header {
margin-bottom: 1.1rem;
}
.card-title {
margin: 0;
font-size: 1.0625rem;
font-weight: 600;
color: var(--text-primary);
}
.card-description {
margin: 0.2rem 0 0;
font-size: 0.75rem;
color: var(--text-muted);
line-height: 1.7;
}
.trend-peak {
display: inline-block;
margin-inline-start: 0.35rem;
color: var(--text-secondary);
}
.ai-stats {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 0.9rem 1.25rem;
}
.ai-stat {
display: flex;
flex-direction: column;
gap: 0.3rem;
}
.ai-stat-label {
font-size: 0.75rem;
color: var(--text-muted);
}
.ai-stat-value {
font-size: 1.125rem;
font-weight: 700;
color: var(--text-primary);
}
.ai-channels {
margin-top: 1.4rem;
padding-top: 1.1rem;
border-top: 1px solid var(--card-border);
}
.ai-channels-title {
margin: 0 0 0.9rem;
font-size: 0.875rem;
font-weight: 600;
color: var(--text-secondary);
}
.error-card {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.5rem;
padding: 3rem 1.5rem;
text-align: center;
color: var(--text-muted);
}
.error-card h2 {
margin: 0.5rem 0 0;
font-size: 1.0625rem;
font-weight: 600;
color: var(--text-primary);
}
.error-card p {
margin: 0;
font-size: 0.8125rem;
}
.retry-btn {
margin-top: 0.75rem;
padding: 0.55rem 1.5rem;
border: none;
border-radius: 0.75rem;
background: var(--color-primary);
color: #ffffff;
font-size: 0.8125rem;
font-weight: 500;
cursor: pointer;
transition: opacity 0.2s ease, transform 0.2s ease;
}
.retry-btn:hover {
opacity: 0.9;
transform: translateY(-1px);
}
.skeleton-card {
min-height: 132px;
}
.skeleton {
border-radius: 0.5rem;
background: linear-gradient(
90deg,
var(--stat-progress-bg, rgba(100, 116, 139, 0.12)) 25%,
rgba(100, 116, 139, 0.05) 37%,
var(--stat-progress-bg, rgba(100, 116, 139, 0.12)) 63%
);
background-size: 400% 100%;
animation: skeletonShimmer 1.4s ease infinite;
}
.skeleton-title {
width: 45%;
height: 0.875rem;
}
.skeleton-value {
width: 30%;
height: 2rem;
margin-top: 0.75rem;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes slideIn {
from {
width: 0;
opacity: 0;
}
to {
width: 1.5rem;
opacity: 1;
}
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(16px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes skeletonShimmer {
from {
background-position: 100% 50%;
}
to {
background-position: 0 50%;
}
}
@media (prefers-reduced-motion: reduce) {
.report-card,
.skeleton {
animation: none;
}
}
@media (max-width: 1024px) {
.reports-page {
padding: 1.5rem 1.75rem 2.75rem;
}
.stats-grid {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.charts-grid,
.ai-grid {
grid-template-columns: minmax(0, 1fr);
}
}
@media (max-width: 640px) {
.reports-page {
padding: 1.25rem 1.25rem 2.25rem;
}
.stats-grid {
grid-template-columns: minmax(0, 1fr);
}
.reports-tabs {
width: 100%;
}
.tab-btn {
flex: 1;
justify-content: center;
padding: 0.55rem 0.5rem;
}
}
:host-context([data-bs-theme='dark']) .report-card {
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3), 0 2px 10px rgba(0, 0, 0, 0.2);
}
:host-context([data-bs-theme='dark']) .stat-progress {
background: rgba(148, 163, 184, 0.2);
}
:host-context([data-bs-theme='dark']) .skeleton {
background: linear-gradient(90deg, rgba(148, 163, 184, 0.14) 25%, rgba(148, 163, 184, 0.05) 37%, rgba(148, 163, 184, 0.14) 63%);
background-size: 400% 100%;
}
+215
View File
@@ -0,0 +1,215 @@
<div class="reports-page">
<header class="reports-header">
<div class="reports-title-group">
<span class="reports-title-icon">
<svg
width="22"
height="22"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<path d="M4 4v16h16" />
<path d="M8 20V11" />
<path d="M12.5 20V6" />
<path d="M17 20V9" />
</svg>
</span>
<div>
<h1 class="reports-title">گزارشات</h1>
<p class="reports-subtitle">مروری بر استفاده از دستیار هوشمند و نتایج پاسخ‌های هوش مصنوعی</p>
</div>
</div>
@if (summary(); as summary) {
<span class="period-chip">بازه گزارش: {{ summary.periodLabel }}</span>
}
</header>
@if (loading()) {
<div class="stats-grid">
@for (card of [1, 2, 3, 4]; track card) {
<div class="report-card skeleton-card">
<div class="skeleton skeleton-title"></div>
<div class="skeleton skeleton-value"></div>
</div>
}
</div>
} @else if (loadError()) {
<div class="report-card error-card" role="alert">
<svg
width="40"
height="40"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
>
<circle cx="12" cy="12" r="9" />
<path d="M12 8v4M12 16h.01" />
</svg>
<h2>دریافت گزارشات ممکن نشد</h2>
<p>ارتباط با سرور برقرار نشد. لطفاً دوباره تلاش کنید.</p>
<button type="button" class="retry-btn" (click)="retry()">تلاش مجدد</button>
</div>
} @else if (data(); as view) {
<nav class="reports-tabs" role="tablist" aria-label="بخش‌های گزارشات">
@for (tab of tabs; track tab.id) {
<button
type="button"
class="tab-btn"
role="tab"
[class.tab-active]="activeTab() === tab.id"
[attr.aria-selected]="activeTab() === tab.id"
(click)="selectTab(tab.id)"
>
<svg
width="15"
height="15"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<path [attr.d]="tab.icon" />
</svg>
<span>{{ tab.label }}</span>
@if (activeTab() === tab.id) {
<span class="tab-indicator"></span>
}
</button>
}
</nav>
@if (activeTab() === 'questions') {
<div class="tab-panel" role="tabpanel">
<div class="stats-grid">
<section class="report-card stat-card">
<span class="stat-label">تعداد کل سوالات</span>
<strong class="stat-value">{{ formatCount(view.summary.totalQuestions) }}</strong>
<span class="stat-hint">در {{ view.summary.periodLabel }}</span>
</section>
<section class="report-card stat-card">
<span class="stat-label">کاربران یکتا</span>
<strong class="stat-value">{{ formatCount(view.summary.uniqueUsers) }}</strong>
<span class="stat-hint">کاربرانی که با دستیار گفتگو کرده‌اند</span>
</section>
<section class="report-card stat-card">
<span class="stat-label">پاسخ‌های هوش مصنوعی</span>
<strong class="stat-value">{{ formatCount(view.summary.aiAnswered) }}</strong>
<div class="stat-progress" aria-hidden="true">
<span class="stat-progress-fill" [style.width.%]="aiAnswerRate()"></span>
</div>
<span class="stat-hint">{{ formatPercent(aiAnswerRate()) }} از کل سوالات</span>
</section>
<section class="report-card stat-card">
<span class="stat-label">ارجاع به کارشناس</span>
<strong class="stat-value">{{ formatCount(view.summary.escalatedToAgent) }}</strong>
<span class="stat-hint">سوالات نیازمند بررسی انسانی</span>
</section>
</div>
<div class="charts-grid">
<section class="report-card chart-card">
<div class="card-header">
<h2 class="card-title">پرتکرارترین سوالات</h2>
<p class="card-description">پرپرسش‌ترین موضوعات کاربران، به ترتیب فراوانی</p>
</div>
<app-bar-chart
[items]="topQuestions()"
ariaLabel="نمودار میله‌ای پرتکرارترین سوالات کاربران"
/>
</section>
<section class="report-card chart-card">
<div class="card-header">
<h2 class="card-title">توزیع سوالات</h2>
<p class="card-description">سهم هر دسته از کل سوالات مطرح‌شده</p>
</div>
<app-doughnut-chart
[segments]="categories()"
centerLabel="کل سوالات"
ariaLabel="نمودار دایره‌ای توزیع سوالات بر اساس دسته"
/>
</section>
</div>
</div>
} @else {
<div class="tab-panel" role="tabpanel">
<div class="ai-grid">
<section class="report-card chart-card">
<div class="card-header">
<h2 class="card-title">عملکرد هوش مصنوعی</h2>
<p class="card-description">آمار پاسخ‌دهی و کیفیت عملکرد دستیار</p>
</div>
<div class="ai-stats">
<div class="ai-stat">
<span class="ai-stat-label">میانگین زمان پاسخ</span>
<strong class="ai-stat-value">{{ formatDecimal(view.aiPerformance.avgResponseSeconds) }} ثانیه</strong>
</div>
<div class="ai-stat">
<span class="ai-stat-label">نرخ موفقیت پاسخ‌ها</span>
<strong class="ai-stat-value">{{ formatPercent(view.aiPerformance.successRate) }}</strong>
<div class="stat-progress" aria-hidden="true">
<span class="stat-progress-fill" [style.width.%]="view.aiPerformance.successRate"></span>
</div>
</div>
<div class="ai-stat">
<span class="ai-stat-label">رضایت‌مندی کاربران</span>
<strong class="ai-stat-value">{{ formatPercent(view.aiPerformance.satisfactionRate) }}</strong>
<div class="stat-progress" aria-hidden="true">
<span class="stat-progress-fill" [style.width.%]="view.aiPerformance.satisfactionRate"></span>
</div>
</div>
<div class="ai-stat">
<span class="ai-stat-label">پاسخ بر پایه سوالات متداول</span>
<strong class="ai-stat-value">{{ formatPercent(faqBasedRate()) }}</strong>
<div class="stat-progress" aria-hidden="true">
<span class="stat-progress-fill" [style.width.%]="faqBasedRate()"></span>
</div>
</div>
</div>
<div class="ai-channels">
<h3 class="ai-channels-title">پاسخ‌ها بر اساس گونه گفتگو</h3>
<app-bar-chart
[items]="channels()"
valueSuffix=" پاسخ"
ariaLabel="نمودار میله‌ای پاسخ‌ها بر اساس گونه گفتگو"
/>
</div>
</section>
<section class="report-card chart-card">
<div class="card-header">
<h2 class="card-title">روند استفاده</h2>
<p class="card-description">
تغییرات حجم سوالات در {{ view.summary.periodLabel }} — مجموع
{{ formatCount(trendTotal()) }} سوال
@if (trendPeak(); as peak) {
<span class="trend-peak">بیشترین رشد در {{ peak.label }} با {{ formatCount(peak.value) }} سوال</span>
}
</p>
</div>
<app-line-chart
[points]="trend()"
seriesLabel="تعداد سوالات"
gradientId="reportsUsageTrend"
ariaLabel="نمودار خطی روند حجم سوالات در گذر زمان"
/>
</section>
</div>
</div>
}
}
</div>
@@ -0,0 +1,65 @@
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 { ReportsPage } from './reports.page';
describe('ReportsPage', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ReportsPage],
providers: [
provideZonelessChangeDetection(),
provideHttpClient(),
provideRouter([]),
provideAnimations(),
{ provide: TOAST_CONFIG, useValue: { default: DefaultGlobalConfig, config: {} } },
],
}).compileComponents();
});
function titles(fixture: { nativeElement: HTMLElement }): (string | undefined)[] {
return Array.from(fixture.nativeElement.querySelectorAll('.card-title')).map((node) =>
node.textContent?.trim(),
);
}
it('should create the page', () => {
const fixture = TestBed.createComponent(ReportsPage);
expect(fixture.componentInstance).toBeTruthy();
});
it('renders the questions tab by default with its charts', () => {
const fixture = TestBed.createComponent(ReportsPage);
fixture.detectChanges();
const element: HTMLElement = fixture.nativeElement;
const tabLabels = Array.from(element.querySelectorAll('.tab-btn')).map((node) =>
node.textContent?.trim(),
);
expect(element.querySelector('.reports-title')?.textContent?.trim()).toBe('گزارشات');
expect(tabLabels).toEqual(['پرسش‌های کاربران', 'عملکرد و روند']);
expect(titles(fixture)).toEqual(['پرتکرارترین سوالات', 'توزیع سوالات']);
expect(element.querySelectorAll('.stat-card').length).toBe(4);
expect(element.querySelector('app-bar-chart')).toBeTruthy();
expect(element.querySelector('app-doughnut-chart')).toBeTruthy();
expect(element.textContent).toContain('۴,۸۰۲');
});
it('switches to the performance tab with AI and usage reports', () => {
const fixture = TestBed.createComponent(ReportsPage);
fixture.detectChanges();
const element = fixture.nativeElement as HTMLElement;
const tabButtons = element.querySelectorAll('.tab-btn');
(tabButtons[1] as HTMLButtonElement).click();
fixture.detectChanges();
expect(fixture.componentInstance.activeTab()).toBe('performance');
expect(titles(fixture)).toEqual(['عملکرد هوش مصنوعی', 'روند استفاده']);
expect(fixture.nativeElement.querySelector('app-line-chart')).toBeTruthy();
});
});