Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CreateCsvDownloaderService #2367

Merged
merged 15 commits into from
Sep 15, 2023
3 changes: 3 additions & 0 deletions projects/observability/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,6 @@ export * from './shared/components/bar-gauge/bar-gauge.module';

// Time Range utils
export * from './shared/utils/time-range';

// CSV Downloader Service
export * from './shared/services/csv-downloader/csv-downloader.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';
import { Dictionary } from '@hypertrace/common';
import { CsvDownloadFileConfig, FileDownloadService } from '@hypertrace/components';
import { Observable, of } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class CSVDownloaderService {
palbizu marked this conversation as resolved.
Show resolved Hide resolved
private readonly componentHandlers: Map<string, Observable<Dictionary<unknown>[]>> = new Map();
palbizu marked this conversation as resolved.
Show resolved Hide resolved

public constructor(private readonly fileDownloaderService: FileDownloadService) {}

public downloadCsv(key: string, fileDownloadConfig?: CsvDownloadFileConfig): void {
palbizu marked this conversation as resolved.
Show resolved Hide resolved
this.fileDownloaderService.downloadAsCsv({
...fileDownloadConfig,
palbizu marked this conversation as resolved.
Show resolved Hide resolved
fileName: fileDownloadConfig?.fileName ?? 'file-name.csv',
palbizu marked this conversation as resolved.
Show resolved Hide resolved
dataSource: this.componentHandlers.get(key) ?? of([])
});
}

public registerHandler(key: string, source: Observable<Dictionary<unknown>[]>): void {
if (!this.componentHandlers.has(key)) {
this.componentHandlers.set(key, source);
}
}

public hasRegisteredHandler(key: string): boolean {
return this.componentHandlers.has(key);
}

public deleteRegisteredHandler(key: string): void {
if (this.componentHandlers.has(key)) {
this.componentHandlers.delete(key);
}
}

public clearRegisteredHandler(): void {
this.componentHandlers.clear();
}
}