Files
grimmory/booklore-ui/src/app/features/stats/service/top-series-chart.service.ts
T
Balázs Szücs ab96eee5bc refactor(types): replace most usages of 'any' with 'unknown' and improve type safety across services and components (#2050)
* refactor(types): replace most usages of 'any' with 'unknown' and improve type safety across services and components

- Update method signatures, variables, and interfaces to use 'unknown' instead of 'any'
- Refine event and observable typings for better type inference
- Add or adjust explicit types for function parameters and return values
- Update Chart.js tooltip and context typings for chart services
- Improve type safety in Book, BookState, and related models
- Adjust template bindings and method calls to match stricter typings
- Add SecureSrcDirective for secure image loading with fallback and error handling

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* chore(angular): remove proxy configuration from development build settings

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* refactor(book-browser): streamline entity and book state management with reactive streams

- Refactor entity type and entity fetching logic to use observables and combine route params
- Simplify book state handling by consolidating filtering and fetching into a single reactive pipeline
- Remove redundant applySortOption logic and related subscriptions
- Fix book card index binding in template for improved rendering performance
- Ensure proper cleanup of epub rendition in epub-reader component

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* fix(book-browser, book-uploader): improve permission checks and enhance file upload handling

- Use optional chaining for user permission checks in book-browser.component.html to prevent runtime errors when user data is undefined
- Refactor book-uploader.component.ts to pass destination and IDs explicitly to uploadBatch, reconstruct files for strict type handling, and ensure proper upload flow and UI feedback

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* refactor(charts, services): improve type safety for book state validation and update form field checks

- Refactor isValidBookState methods across chart components and services to use type predicates and stricter type checks for BookState
- Update metadata-picker.component to simplify array field checks for authors, categories, moods, and tags
- Adjust magic-shelf-utils to remove unnecessary fallback in object spread
- Update notification-event.service to use correct timeout type for highlightTimeout

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* fix(theme): ensure safe merging of theme properties and improve type definition for themesMap

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* refactor(epub-reader): streamline book and rendition initialization logic

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

---------

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
2026-01-02 18:22:54 -07:00

278 lines
7.6 KiB
TypeScript

import {inject, Injectable, OnDestroy} from '@angular/core';
import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operators';
import {ChartConfiguration, ChartData, TooltipItem} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface SeriesStats {
seriesName: string;
bookCount: number;
}
const CHART_COLORS = [
'#4e79a7', '#f28e2c', '#e15759', '#76b7b2',
'#59a14f', '#edc949', '#af7aa1', '#ff9da7',
'#9c755f', '#bab0ab', '#5778a4', '#e69138',
'#d62728', '#6aa7b8', '#54a24b', '#fdd247',
'#b07aa1', '#ff9d9a', '#9e6762', '#c9b2d6'
] as const;
const CHART_DEFAULTS = {
borderWidth: 1,
hoverBorderWidth: 2,
hoverBorderColor: '#ffffff'
} as const;
type SeriesChartData = ChartData<'bar', number[], string>;
@Injectable({
providedIn: 'root'
})
export class TopSeriesChartService implements OnDestroy {
private readonly bookService = inject(BookService);
private readonly libraryFilterService = inject(LibraryFilterService);
private readonly destroy$ = new Subject<void>();
public readonly seriesChartType = 'bar' as const;
public readonly seriesChartOptions: ChartConfiguration<'bar'>['options'] = {
responsive: true,
maintainAspectRatio: false,
indexAxis: 'y',
scales: {
x: {
beginAtZero: true,
ticks: {
color: '#ffffff',
font: {
family: "'Inter', sans-serif",
size: 11.5
},
precision: 0
},
grid: {
color: 'rgba(255, 255, 255, 0.1)'
},
title: {
display: true,
text: 'Number of Books',
color: '#ffffff',
font: {
family: "'Inter', sans-serif",
size: 12
}
}
},
y: {
ticks: {
color: '#ffffff',
font: {
family: "'Inter', sans-serif",
size: 11
},
maxTicksLimit: 20
},
grid: {
color: 'rgba(255, 255, 255, 0.05)'
}
}
},
plugins: {
legend: {
display: false
},
tooltip: {
backgroundColor: 'rgba(0, 0, 0, 0.9)',
titleColor: '#ffffff',
bodyColor: '#ffffff',
borderColor: '#ffffff',
borderWidth: 1,
cornerRadius: 6,
displayColors: true,
padding: 12,
titleFont: {size: 14, weight: 'bold'},
bodyFont: {size: 12},
callbacks: {
title: (context) => {
const dataIndex = context[0].dataIndex;
const stats = this.getLastCalculatedStats();
return stats[dataIndex]?.seriesName || 'Unknown Series';
},
label: this.formatTooltipLabel.bind(this)
}
},
datalabels: {
display: true,
color: '#ffffff',
font: {
size: 10,
family: "'Inter', sans-serif",
weight: 'bold'
},
align: 'center',
offset: 8,
formatter: (value: number) => value.toString()
}
},
interaction: {
intersect: false,
mode: 'point'
}
};
private readonly seriesChartDataSubject = new BehaviorSubject<SeriesChartData>({
labels: [],
datasets: []
});
public readonly seriesChartData$: Observable<SeriesChartData> = this.seriesChartDataSubject.asObservable();
constructor() {
this.bookService.bookState$
.pipe(
filter(state => state.loaded),
first(),
switchMap(() =>
this.libraryFilterService.selectedLibrary$.pipe(
takeUntil(this.destroy$)
)
),
catchError((error) => {
console.error('Error processing top series stats:', error);
return EMPTY;
})
)
.subscribe(() => {
const stats = this.calculateTopSeriesStats();
this.updateChartData(stats);
});
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
private updateChartData(stats: SeriesStats[]): void {
try {
this.lastCalculatedStats = stats;
const labels = stats.map(s => this.truncateTitle(s.seriesName, 30));
const dataValues = stats.map(s => s.bookCount);
const colors = this.getColorsForData(stats.length);
this.seriesChartDataSubject.next({
labels,
datasets: [{
label: 'Books',
data: dataValues,
backgroundColor: colors,
borderColor: colors,
...CHART_DEFAULTS
}]
});
} catch (error) {
console.error('Error updating series chart data:', error);
}
}
private calculateTopSeriesStats(): SeriesStats[] {
const currentState = this.bookService.getCurrentBookState();
const selectedLibraryId = this.libraryFilterService.getCurrentSelectedLibrary();
if (!this.isValidBookState(currentState)) {
return [];
}
const filteredBooks = this.filterBooksByLibrary(currentState.books!, String(selectedLibraryId));
return this.processTopSeriesStats(filteredBooks);
}
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | null): Book[] {
return selectedLibraryId && selectedLibraryId !== 'null'
? books.filter(book => String(book.libraryId) === selectedLibraryId)
: books;
}
private processTopSeriesStats(books: Book[]): SeriesStats[] {
if (books.length === 0) {
return [];
}
const seriesMap = this.buildSeriesMap(books);
return this.convertMapToStats(seriesMap);
}
private buildSeriesMap(books: Book[]): Map<string, number> {
const seriesMap = new Map<string, number>();
for (const book of books) {
const seriesName = book.metadata?.seriesName;
if (seriesName && seriesName.trim()) {
const normalizedName = seriesName.trim();
seriesMap.set(normalizedName, (seriesMap.get(normalizedName) || 0) + 1);
}
}
return seriesMap;
}
private convertMapToStats(seriesMap: Map<string, number>): SeriesStats[] {
return Array.from(seriesMap.entries())
.map(([seriesName, bookCount]) => ({
seriesName,
bookCount
}))
.sort((a, b) => b.bookCount - a.bookCount)
.slice(0, 20);
}
private getColorsForData(dataLength: number): string[] {
const colors = [...CHART_COLORS];
while (colors.length < dataLength) {
colors.push(...CHART_COLORS);
}
return colors.slice(0, dataLength);
}
private formatTooltipLabel(context: TooltipItem<'bar'>): string {
const dataIndex = context.dataIndex;
const stats = this.getLastCalculatedStats();
if (!stats || dataIndex >= stats.length) {
return `${context.parsed.x} books`;
}
const series = stats[dataIndex];
const bookCount = series.bookCount;
const bookText = bookCount === 1 ? 'book' : 'books';
return `${bookCount} ${bookText}`;
}
private lastCalculatedStats: SeriesStats[] = [];
private getLastCalculatedStats(): SeriesStats[] {
return this.lastCalculatedStats;
}
private truncateTitle(title: string, maxLength: number): string {
return title.length > maxLength ? title.substring(0, maxLength) + '...' : title;
}
}