Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<ds-metadata-field-wrapper [label]="downloadsLabel | translate">
<ds-metadata-field-wrapper
*ngIf="totalDownloadsEnabled | async"
[label]="downloadsLabel | translate">
<div class="total-downloads-section">
<div class="total-downloads-count">
<span>{{ totalDownloads }}</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Component, OnInit, Input } from '@angular/core';
import { UsageReportDataService } from 'src/app/core/statistics/usage-report-data.service';
import { ConfigurationDataService } from 'src/app/core/data/configuration-data.service';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { BehaviorSubject, of } from 'rxjs';

/**
* Component that displays the total number of downloads for all bitstreams within a DSpace item.
Expand All @@ -26,23 +27,34 @@ export class TotalDownloadsComponent implements OnInit {
* The total number of downloads across all bitstreams for the item.
* Defaults to 0 and will show 0 if no data is available or an error occurs.
*/
totalDownloads: number = 0;
totalDownloads = 0;
Comment thread
milanmajchrak marked this conversation as resolved.
Outdated

/**
* Flag indicating whether the total downloads feature is enabled in the configuration.
* Defaults to false to hide downloads unless explicitly enabled in configuration.
*/
totalDownloadsEnabled = new BehaviorSubject<boolean>(false);
Comment thread
milanmajchrak marked this conversation as resolved.

/**
* The translation key for the downloadsLabel displayed alongside the download count.
*/
readonly downloadsLabel = 'item.page.files.downloads';


constructor(private usageReportDataService: UsageReportDataService) { }
constructor(
private usageReportDataService: UsageReportDataService,
private configService: ConfigurationDataService
) { }

/**
* Fetches the total download statistics for the item specified by itemUuid.
* Fetches the configuration to check if total downloads should be shown,
* and if enabled, fetches the total download statistics for the item specified by itemUuid.
* The component will:
* 1. Call the UsageReportDataService with the item UUID and 'TotalDownloads' report type
* 2. Aggregate all download counts (views) from all bitstreams in the response
* 3. Set the totalDownloads property with the sum
* 4. Handle errors gracefully by setting totalDownloads to 0 and logging the error
* 1. Check the 'item.view.total.downloads.enabled' configuration property
* 2. If enabled (or config not found - defaults to true), call the UsageReportDataService
Comment thread
milanmajchrak marked this conversation as resolved.
Outdated
* 3. Aggregate all download counts (views) from all bitstreams in the response
* 4. Set the totalDownloads property with the sum
* 5. Handle errors gracefully by setting totalDownloads to 0 and logging the error
Comment thread
milanmajchrak marked this conversation as resolved.
Outdated
*
* @throws Will log an error to console if the API call fails, but won't throw an exception
*/
Expand All @@ -51,6 +63,34 @@ export class TotalDownloadsComponent implements OnInit {
return;
}

// First, check if total downloads feature is enabled in configuration
this.configService.findByPropertyName('item.view.total.downloads.enabled')
.pipe(
catchError(error => {
console.error('Failed to fetch total downloads configuration:', error);
// Default to true if configuration cannot be retrieved
return of(null);
})
)
.subscribe(configData => {
// Extract configuration value, default to 'true' if not found
const itemViewTotalDownloadsEnabled = configData?.payload?.values?.[0];
this.totalDownloadsEnabled.next(itemViewTotalDownloadsEnabled === 'true');

// Only fetch download statistics if the feature is enabled
if (this.totalDownloadsEnabled) {
Comment thread
milanmajchrak marked this conversation as resolved.
Outdated
this.fetchDownloadStatistics();
} else {
this.totalDownloads = 0; // Ensure it's 0 when disabled
Comment thread
milanmajchrak marked this conversation as resolved.
Outdated
}
});
}

/**
* Private method to fetch download statistics from the usage report service.
* This method is called only when the total downloads feature is enabled.
*/
private fetchDownloadStatistics(): void {
const reportType = 'TotalDownloads';
this.usageReportDataService.getStatistic(this.itemUuid, reportType)
.pipe(
Expand Down
Loading