-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature/attachment report #22
Open
idelcano
wants to merge
9
commits into
development
Choose a base branch
from
feature/attachment_report
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1da263d
added new attachement report
idelcano f63c0da
remove dataelement and add working link
idelcano 08657e5
added variant name into readme
idelcano 37b9006
fix typo
idelcano 84dd153
Merge branch 'development' of github.com:EyeSeeTea/d2-reports into fe…
eperedo d9e0821
Reuse filter component
eperedo 7a5ad29
remove duplicate components, methods, entities and refactor code
eperedo c7d7601
add download file option
eperedo d36dc90
update i18n
eperedo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,106 @@ | ||
import _ from "lodash"; | ||
import { DataAttachmentItem } from "../domain/nhwa-attachments/entities/DataAttachmentItem"; | ||
import { | ||
NHWADataAttachmentsRepository, | ||
NHWADataAttachmentsRepositoryGetOptions, | ||
} from "../domain/nhwa-attachments/repositories/NHWADataAttachmentsRepository"; | ||
import { D2Api, PaginatedObjects, Id } from "../types/d2-api"; | ||
import { Dhis2SqlViews } from "./Dhis2SqlViews"; | ||
import { CsvWriterDataSource } from "./CsvWriterCsvDataSource"; | ||
import { downloadFile } from "./utils/download-file"; | ||
import { CsvData } from "./CsvDataSource"; | ||
|
||
interface Variables { | ||
orgUnitIds: string; | ||
dataSetIds: string; | ||
periods: string; | ||
orderByColumn: SqlField; | ||
orderByDirection: "asc" | "desc"; | ||
} | ||
|
||
type SqlField = "datasetname" | "link" | "period" | "storedby" | "orgunit" | "lastupdated"; | ||
|
||
const fieldMapping: Record<keyof DataAttachmentItem, SqlField> = { | ||
period: "period", | ||
orgUnit: "orgunit", | ||
dataSet: "datasetname", | ||
link: "link", | ||
lastUpdated: "lastupdated", | ||
storedBy: "storedby", | ||
}; | ||
|
||
export class NHWAAttachementsDefaultRepository implements NHWADataAttachmentsRepository { | ||
constructor(private api: D2Api) {} | ||
|
||
async get(options: NHWADataAttachmentsRepositoryGetOptions): Promise<PaginatedObjects<DataAttachmentItem>> { | ||
const { config, dataSetIds, orgUnitIds, periods } = options; | ||
const { paging, sorting } = options; | ||
|
||
const allDataSetIds = _.values(config.dataSets).map(ds => ds.id); | ||
const dataSetIds2 = _.isEmpty(dataSetIds) ? allDataSetIds : dataSetIds; | ||
|
||
const sqlViews = new Dhis2SqlViews(this.api); | ||
const { pager, rows } = await sqlViews | ||
.query<Variables, SqlField>( | ||
config.dataAttachmentSqlView.id, | ||
{ | ||
orgUnitIds: sqlViewJoinIds(orgUnitIds), | ||
periods: sqlViewJoinIds(_.isEmpty(periods) ? config.years : periods), | ||
dataSetIds: sqlViewJoinIds(dataSetIds2), | ||
orderByColumn: fieldMapping[sorting.field], | ||
orderByDirection: sorting.direction, | ||
}, | ||
paging | ||
) | ||
.getData(); | ||
|
||
// A data value is not associated to a specific data set, but we can still map it | ||
// through the data element (1 data value -> 1 data element -> N data sets). | ||
|
||
const dataValues: Array<DataAttachmentItem> = rows.map( | ||
(dv): DataAttachmentItem => ({ | ||
period: dv.period.split("-")[0] ?? "", | ||
orgUnit: { name: dv.orgunit }, | ||
dataSet: { name: dv.datasetname }, | ||
link: this.api.apiPath + "/" + dv.link, | ||
lastUpdated: new Date(dv.lastupdated), | ||
storedBy: dv.storedby, | ||
}) | ||
); | ||
|
||
return { pager, objects: dataValues }; | ||
} | ||
|
||
async save(filename: string, dataValues: DataAttachmentItem[]): Promise<void> { | ||
const headers = csvFields.map(field => ({ id: field, text: field })); | ||
const rows = dataValues.map( | ||
(dataValue): DataValueRow => ({ | ||
period: dataValue.period, | ||
orgUnit: dataValue.orgUnit.name, | ||
dataSet: dataValue.dataSet.name, | ||
lastUpdated: dataValue.lastUpdated.toISOString(), | ||
storedBy: dataValue.storedBy, | ||
link: dataValue.link, | ||
}) | ||
); | ||
|
||
const csvDataSource = new CsvWriterDataSource(); | ||
const csvData: CsvData<CsvField> = { headers, rows }; | ||
const csvContents = csvDataSource.toString(csvData); | ||
|
||
await downloadFile(csvContents, filename, "text/csv"); | ||
} | ||
} | ||
|
||
const csvFields = ["dataSet", "period", "orgUnit", "link", "lastUpdated", "storedBy"] as const; | ||
|
||
type CsvField = typeof csvFields[number]; | ||
|
||
type DataValueRow = Record<CsvField, string>; | ||
|
||
/* From the docs: "The variables must contain alphanumeric, dash, underscore and | ||
whitespace characters only.". Use "-" as id separator and also "-" as empty value. | ||
*/ | ||
function sqlViewJoinIds(ids: Id[]): string { | ||
return ids.join("-") || "-"; | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/domain/nhwa-attachments/entities/DataAttachmentItem.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,14 @@ | ||
import { Id, Named } from "../../common/entities/Base"; | ||
|
||
export interface DataAttachmentItem { | ||
period: string; | ||
orgUnit: Named; | ||
dataSet: Named; | ||
link: string; | ||
lastUpdated: Date; | ||
storedBy: string; | ||
} | ||
|
||
export function getDataAttachmentsItemId(dataValue: DataAttachmentItem): Id { | ||
return [dataValue.dataSet, dataValue.period, dataValue.orgUnit.name].join("-"); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/domain/nhwa-attachments/repositories/NHWADataAttachmentsRepository.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,18 @@ | ||
import { DataAttachmentItem } from "../entities/DataAttachmentItem"; | ||
import { Id } from "../../common/entities/Base"; | ||
import { Config } from "../../common/entities/Config"; | ||
import { PaginatedObjects, Paging, Sorting } from "../../common/entities/PaginatedObjects"; | ||
|
||
export interface NHWADataAttachmentsRepository { | ||
get(options: NHWADataAttachmentsRepositoryGetOptions): Promise<PaginatedObjects<DataAttachmentItem>>; | ||
save(filename: string, dataValues: DataAttachmentItem[]): Promise<void>; | ||
} | ||
|
||
export interface NHWADataAttachmentsRepositoryGetOptions { | ||
config: Config; | ||
paging: Paging; | ||
sorting: Sorting<DataAttachmentItem>; | ||
periods: string[]; | ||
orgUnitIds: Id[]; | ||
dataSetIds: Id[]; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/domain/nhwa-attachments/usecases/GetAttachementsUseCase.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,17 @@ | ||
import { | ||
NHWADataAttachmentsRepository, | ||
NHWADataAttachmentsRepositoryGetOptions, | ||
} from "../repositories/NHWADataAttachmentsRepository"; | ||
import { DataAttachmentItem } from "../entities/DataAttachmentItem"; | ||
import { PaginatedObjects } from "../../common/entities/PaginatedObjects"; | ||
|
||
type GetAttachementsUseCaseOptions = NHWADataAttachmentsRepositoryGetOptions; | ||
|
||
export class GetAttachementsUseCase { | ||
constructor(private dataValueRepository: NHWADataAttachmentsRepository) {} | ||
|
||
execute(options: GetAttachementsUseCaseOptions): Promise<PaginatedObjects<DataAttachmentItem>> { | ||
// FUTURE: Return a Future-like instead, to allow better error handling and cancellation. | ||
return this.dataValueRepository.get(options); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/domain/nhwa-attachments/usecases/SaveAttachementsUseCase.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,10 @@ | ||
import { DataAttachmentItem } from "../entities/DataAttachmentItem"; | ||
import { NHWADataAttachmentsRepository } from "../repositories/NHWADataAttachmentsRepository"; | ||
|
||
export class SaveAttachementsUseCase { | ||
constructor(private dataValueRepository: NHWADataAttachmentsRepository) {} | ||
|
||
async execute(filename: string, dataValues: DataAttachmentItem[]): Promise<void> { | ||
this.dataValueRepository.save(filename, dataValues); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: attachments, in variables/functions and file names.