|
| 1 | +import { Component, OnInit, Input } from '@angular/core'; |
| 2 | +import { UsageReportDataService } from 'src/app/core/statistics/usage-report-data.service'; |
| 3 | +import { catchError } from 'rxjs/operators'; |
| 4 | +import { of } from 'rxjs'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Component that displays the total number of downloads for all bitstreams within a DSpace item. |
| 8 | + * |
| 9 | + * This component fetches download statistics for a given item using its UUID and aggregates |
| 10 | + * the download counts from all bitstreams associated with that item. The result is displayed |
| 11 | + * as a single total download count. |
| 12 | + */ |
| 13 | +@Component({ |
| 14 | + selector: 'ds-total-downloads', |
| 15 | + templateUrl: './total-downloads.component.html', |
| 16 | + styleUrls: ['./total-downloads.component.scss'] |
| 17 | +}) |
| 18 | +export class TotalDownloadsComponent implements OnInit { |
| 19 | + |
| 20 | + /** |
| 21 | + * The UUID of the DSpace item for which to fetch download statistics. |
| 22 | + */ |
| 23 | + @Input() itemUuid!: string; |
| 24 | + |
| 25 | + /** |
| 26 | + * The total number of downloads across all bitstreams for the item. |
| 27 | + * Defaults to 0 and will show 0 if no data is available or an error occurs. |
| 28 | + */ |
| 29 | + totalDownloads: number = 0; |
| 30 | + |
| 31 | + /** |
| 32 | + * The translation key for the downloadsLabel displayed alongside the download count. |
| 33 | + */ |
| 34 | + readonly downloadsLabel = 'item.page.files.downloads'; |
| 35 | + |
| 36 | + |
| 37 | + constructor(private usageReportDataService: UsageReportDataService) { } |
| 38 | + |
| 39 | + /** |
| 40 | + * Fetches the total download statistics for the item specified by itemUuid. |
| 41 | + * The component will: |
| 42 | + * 1. Call the UsageReportDataService with the item UUID and 'TotalDownloads' report type |
| 43 | + * 2. Aggregate all download counts (views) from all bitstreams in the response |
| 44 | + * 3. Set the totalDownloads property with the sum |
| 45 | + * 4. Handle errors gracefully by setting totalDownloads to 0 and logging the error |
| 46 | + * |
| 47 | + * @throws Will log an error to console if the API call fails, but won't throw an exception |
| 48 | + */ |
| 49 | + ngOnInit(): void { |
| 50 | + if (!this.itemUuid) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const reportType = 'TotalDownloads'; |
| 55 | + this.usageReportDataService.getStatistic(this.itemUuid, reportType) |
| 56 | + .pipe( |
| 57 | + catchError(error => { |
| 58 | + console.error('Failed to fetch total downloads statistics:', error); |
| 59 | + return of(null); |
| 60 | + }) |
| 61 | + ) |
| 62 | + .subscribe(report => { |
| 63 | + if (report) { |
| 64 | + this.totalDownloads = report.points.reduce((total, point) => { |
| 65 | + const views = point.values.views || 0; |
| 66 | + return total + views; |
| 67 | + }, 0); |
| 68 | + } else { |
| 69 | + this.totalDownloads = 0; // Show 0 instead of null when no data is available |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | +} |
0 commit comments