-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CreateCsvDownloaderService (#2367)
* feat: CreateCsvDownloaderService * feat: fixing comments * feat: fixing comments * feat: updating the service * feat: fix prettier * feat: service refactored * feat: fix test * feat: rename fields * feat: removing unreleated code * feat: add UT * feat: fixing comments --------- Co-authored-by: Patricio Albizu <[email protected]> Co-authored-by: Patricio Albizu <[email protected]>
- Loading branch information
1 parent
f4ae953
commit 17f4dca
Showing
7 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...observability/src/shared/services/global-csv-download/global-csv-download.service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { createServiceFactory, mockProvider } from '@ngneat/spectator/jest'; | ||
import { | ||
GlobalCsvDownloadDataType, | ||
GlobalCsvDownloadService, | ||
GlobalCsvDownloadTableDataSource | ||
} from './global-csv-download.service'; | ||
import { of } from 'rxjs'; | ||
import { FileDownloadService, TableColumnConfig } from '@hypertrace/components'; | ||
|
||
describe('Global Csv Download Service', () => { | ||
const mockColumnConfigs: TableColumnConfig[] = [ | ||
{ | ||
id: 'test_1', | ||
title: 'Test_1', | ||
display: 'Test 1' | ||
}, | ||
{ | ||
id: 'test_2', | ||
title: 'Test_2', | ||
display: 'Test 2' | ||
} | ||
]; | ||
|
||
const mockModel = { | ||
getData: jest.fn(() => | ||
of({ | ||
data: [], | ||
totalCount: 0 | ||
}) | ||
) | ||
}; | ||
|
||
const serviceFactory = createServiceFactory({ | ||
service: GlobalCsvDownloadService, | ||
providers: [ | ||
mockProvider(FileDownloadService, { | ||
downloadAsCsv: jest.fn().mockReturnValue(of(undefined)) | ||
}) | ||
] | ||
}); | ||
|
||
test('Register, get, check, delete and clean data source should work as expected', () => { | ||
const spectator = serviceFactory(); | ||
spectator.service.registerDataSource('test', { | ||
type: GlobalCsvDownloadDataType.Table, | ||
columns: mockColumnConfigs, | ||
data: of(mockModel) | ||
}); | ||
expect((spectator.service.getRegisteredDataSource('test') as GlobalCsvDownloadTableDataSource).columns).toEqual( | ||
mockColumnConfigs | ||
); | ||
|
||
expect(spectator.service.hasRegisteredDataSource('test')).toBe(true); | ||
|
||
spectator.service.deleteRegisteredDataSource('test'); | ||
expect(spectator.service.hasRegisteredDataSource('test')).toBe(false); | ||
|
||
spectator.service.registerDataSource('test', { | ||
type: GlobalCsvDownloadDataType.Table, | ||
columns: mockColumnConfigs, | ||
data: of(mockModel) | ||
}); | ||
spectator.service.clearAllDataSource(); | ||
expect(spectator.service.hasRegisteredDataSource('test')).toBe(false); | ||
}); | ||
|
||
test('Download csv should work as expected', () => { | ||
const spectator = serviceFactory(); | ||
spectator.service.downloadCsv(); | ||
expect(spectator.inject(FileDownloadService).downloadAsCsv).not.toBeCalled(); | ||
spectator.service.downloadCsv({ dataSource: of([]), fileName: 'traces.csv' }); | ||
expect(spectator.inject(FileDownloadService).downloadAsCsv).toBeCalled(); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
...ects/observability/src/shared/services/global-csv-download/global-csv-download.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Dictionary } from '@hypertrace/common'; | ||
import { | ||
CsvDownloadFileConfig, | ||
FileDownloadService, | ||
TableColumnConfig, | ||
TableDataSource, | ||
TableRow | ||
} from '@hypertrace/components'; | ||
import { isNil } from 'lodash-es'; | ||
import { Observable, throwError } from 'rxjs'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class GlobalCsvDownloadService { | ||
// Note: This service should be use to connect and donwload data from two unrelated components. The component's data to be downloaded should be register and can be executed from a different component. | ||
|
||
private readonly csvDataSourceMap: Map<string, GlobalCsvDownloadData> = new Map(); | ||
|
||
public constructor(private readonly fileDownloadService: FileDownloadService) {} | ||
|
||
public downloadCsv(csvDownloadFileConfig?: CsvDownloadFileConfig): Observable<unknown> { | ||
return !isNil(csvDownloadFileConfig) | ||
? this.fileDownloadService.downloadAsCsv(csvDownloadFileConfig) | ||
: throwError('No data available.'); | ||
} | ||
|
||
public registerDataSource(key: string, source: GlobalCsvDownloadData): void { | ||
this.csvDataSourceMap.set(key, source); | ||
} | ||
|
||
public getRegisteredDataSource(key: string): GlobalCsvDownloadData | undefined { | ||
return this.csvDataSourceMap.get(key); | ||
} | ||
|
||
public hasRegisteredDataSource(key: string): boolean { | ||
return this.csvDataSourceMap.has(key); | ||
} | ||
|
||
public deleteRegisteredDataSource(key: string): void { | ||
if (this.csvDataSourceMap.has(key)) { | ||
this.csvDataSourceMap.delete(key); | ||
} | ||
} | ||
|
||
public clearAllDataSource(): void { | ||
this.csvDataSourceMap.clear(); | ||
} | ||
} | ||
|
||
export type GlobalCsvDownloadData = GlobalCsvDownloadTableDataSource | GlobalCsvDownloadDataSource; | ||
|
||
export const enum GlobalCsvDownloadDataType { | ||
Table = 'table', | ||
DataSource = 'datasource' | ||
} | ||
export interface GlobalCsvDownloadTableDataSource { | ||
type: GlobalCsvDownloadDataType.Table; | ||
columns: TableColumnConfig[]; | ||
data: Observable<TableDataSource<TableRow>>; | ||
} | ||
|
||
export interface GlobalCsvDownloadDataSource { | ||
type: GlobalCsvDownloadDataType.DataSource; | ||
data: Observable<Dictionary<unknown>[]>; | ||
} |