Skip to content

Commit

Permalink
finishing pagination for medical history page.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed Oct 4, 2023
1 parent 345b2d5 commit 1fda510
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 23 deletions.
2 changes: 1 addition & 1 deletion backend/pkg/database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type DatabaseRepository interface {
AddResourceAssociation(ctx context.Context, source *models.SourceCredential, resourceType string, resourceId string, relatedSource *models.SourceCredential, relatedResourceType string, relatedResourceId string) error
RemoveResourceAssociation(ctx context.Context, source *models.SourceCredential, resourceType string, resourceId string, relatedSource *models.SourceCredential, relatedResourceType string, relatedResourceId string) error
FindResourceAssociationsByTypeAndId(ctx context.Context, source *models.SourceCredential, resourceType string, resourceId string) ([]models.RelatedResource, error)
GetFlattenedResourceGraph(ctx context.Context, graphType pkg.ResourceGraphType, options models.ResourceGraphOptions) (map[string][]*models.ResourceBase, error)
GetFlattenedResourceGraph(ctx context.Context, graphType pkg.ResourceGraphType, options models.ResourceGraphOptions) (map[string][]*models.ResourceBase, *models.ResourceGraphMetadata, error)
AddResourceComposition(ctx context.Context, compositionTitle string, resources []*models.ResourceBase) error
//UpsertProfile(context.Context, *models.Profile) error
//UpsertOrganziation(context.Context, *models.Organization) error
Expand Down
36 changes: 24 additions & 12 deletions backend/pkg/database/sqlite_repository_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ func (rp *VertexResourcePlaceholder) ID() string {
// Retrieve a list of all fhir resources (vertex), and a list of all associations (edge)
// Generate a graph
// return list of root nodes, and their flattened related resources.
func (sr *SqliteRepository) GetFlattenedResourceGraph(ctx context.Context, graphType pkg.ResourceGraphType, options models.ResourceGraphOptions) (map[string][]*models.ResourceBase, error) {
func (sr *SqliteRepository) GetFlattenedResourceGraph(ctx context.Context, graphType pkg.ResourceGraphType, options models.ResourceGraphOptions) (map[string][]*models.ResourceBase, *models.ResourceGraphMetadata, error) {
currentUser, currentUserErr := sr.GetCurrentUser(ctx)
if currentUserErr != nil {
return nil, currentUserErr
return nil, nil, currentUserErr
}

//initialize the graph results metadata
resourceGraphMetadata := models.ResourceGraphMetadata{
TotalElements: 0,
PageSize: 20, //TODO: replace this with pkg.DefaultPageSize
Page: options.Page,
}

// Get list of all (non-reciprocal) relationships
Expand All @@ -44,7 +51,7 @@ func (sr *SqliteRepository) GetFlattenedResourceGraph(ctx context.Context, graph
}).
Find(&relatedResourceRelationships)
if result.Error != nil {
return nil, result.Error
return nil, nil, result.Error
}

//Generate Graph
Expand Down Expand Up @@ -97,7 +104,7 @@ func (sr *SqliteRepository) GetFlattenedResourceGraph(ctx context.Context, graph
&resourcePlaceholder,
)
if err != nil {
return nil, fmt.Errorf("an error occurred while adding vertex: %v", err)
return nil, nil, fmt.Errorf("an error occurred while adding vertex: %v", err)
}
}

Expand Down Expand Up @@ -134,7 +141,7 @@ func (sr *SqliteRepository) GetFlattenedResourceGraph(ctx context.Context, graph
// }
adjacencyMap, err := g.AdjacencyMap()
if err != nil {
return nil, fmt.Errorf("error while generating AdjacencyMap: %v", err)
return nil, nil, fmt.Errorf("error while generating AdjacencyMap: %v", err)
}

// For a directed graph, PredecessorMap is the complement of AdjacencyMap. This is because in a directed graph, only
Expand All @@ -143,7 +150,7 @@ func (sr *SqliteRepository) GetFlattenedResourceGraph(ctx context.Context, graph
// ie. "empty" verticies in this map are "root" nodes.
predecessorMap, err := g.PredecessorMap()
if err != nil {
return nil, fmt.Errorf("error while generating PredecessorMap: %v", err)
return nil, nil, fmt.Errorf("error while generating PredecessorMap: %v", err)
}

// Doing this in one massive function, because passing graph by reference is difficult due to generics.
Expand Down Expand Up @@ -193,10 +200,11 @@ func (sr *SqliteRepository) GetFlattenedResourceGraph(ctx context.Context, graph
// Step 2: now that we've created a relationship graph using placeholders, we need to determine which page of resources to return
// and look up the actual resources from the database.

resourceListDictionary, err := sr.InflateResourceGraphAtPage(resourcePlaceholderListDictionary, options.Page)
resourceListDictionary, totalElements, err := sr.InflateResourceGraphAtPage(resourcePlaceholderListDictionary, options.Page)
if err != nil {
return nil, fmt.Errorf("error while paginating & inflating resource graph: %v", err)
return nil, nil, fmt.Errorf("error while paginating & inflating resource graph: %v", err)
}
resourceGraphMetadata.TotalElements = totalElements

// Step 3: define a function. When given a resource, should find all related resources, flatten the heirarchy and set the RelatedResourceFhir list
flattenRelatedResourcesFn := func(resource *models.ResourceBase) {
Expand Down Expand Up @@ -279,15 +287,16 @@ func (sr *SqliteRepository) GetFlattenedResourceGraph(ctx context.Context, graph

// Step 5: return the populated resource list dictionary

return resourceListDictionary, nil
return resourceListDictionary, &resourceGraphMetadata, nil
}

// LoadResourceGraphAtPage - this function will take a dictionary of placeholder "sources" graph and load the actual resources from the database, for a specific page
// - first, it will load all the "source" resources (eg. Encounter, Condition, etc)
// - sort the root resources by date, desc
// - use the page number + page size to determine which root resources to return
// - return a dictionary of "source" resource lists
func (sr *SqliteRepository) InflateResourceGraphAtPage(resourcePlaceholderListDictionary map[string][]*VertexResourcePlaceholder, page int) (map[string][]*models.ResourceBase, error) {
func (sr *SqliteRepository) InflateResourceGraphAtPage(resourcePlaceholderListDictionary map[string][]*VertexResourcePlaceholder, page int) (map[string][]*models.ResourceBase, int, error) {
totalElements := 0
// Step 3a: since we cant calulate the sort order until the resources are loaded, we need to load all the root resources first.

//TODO: maybe its more performant to query each resource by type/id/source, since they are indexed already?
Expand All @@ -307,7 +316,7 @@ func (sr *SqliteRepository) InflateResourceGraphAtPage(resourcePlaceholderListDi

tableName, err := databaseModel.GetTableNameByResourceType(resourceType)
if err != nil {
return nil, err
return nil, totalElements, err
}
var tableWrappedResourceModels []models.ResourceBase
sr.GormClient.
Expand All @@ -322,6 +331,9 @@ func (sr *SqliteRepository) InflateResourceGraphAtPage(resourcePlaceholderListDi
//sort
rootWrappedResourceModels = utils.SortResourceListByDate(rootWrappedResourceModels)

//calculate total elements
totalElements = len(rootWrappedResourceModels)

//paginate (by calculating window for the slice)
rootWrappedResourceModels = utils.PaginateResourceList(rootWrappedResourceModels, page, 20) //todo: replace size with pkg.ResourceListPageSize

Expand All @@ -336,7 +348,7 @@ func (sr *SqliteRepository) InflateResourceGraphAtPage(resourcePlaceholderListDi
}

// Step 4: return the populated resource list dictionary
return resourceListDictionary, nil
return resourceListDictionary, totalElements, nil
}

//We need to support the following types of graphs:
Expand Down
7 changes: 7 additions & 0 deletions backend/pkg/models/resource_graph_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package models

type ResourceGraphMetadata struct {
TotalElements int `json:"total_elements"`
PageSize int `json:"page_size"`
Page int `json:"page"`
}
7 changes: 5 additions & 2 deletions backend/pkg/web/handler/resource_fhir.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,15 @@ func GetResourceFhirGraph(c *gin.Context) {
graphOptions.Page = pageNumb
}

resourceListDictionary, err := databaseRepo.GetFlattenedResourceGraph(c, pkg.ResourceGraphType(graphType), graphOptions)
resourceListDictionary, resourceListMetadata, err := databaseRepo.GetFlattenedResourceGraph(c, pkg.ResourceGraphType(graphType), graphOptions)
if err != nil {
logger.Errorln("An error occurred while retrieving list of resources", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
return
}

c.JSON(http.StatusOK, gin.H{"success": true, "data": resourceListDictionary})
c.JSON(http.StatusOK, gin.H{"success": true, "data": map[string]interface{}{
"results": resourceListDictionary,
"metadata": resourceListMetadata,
}})
}
12 changes: 12 additions & 0 deletions frontend/src/app/models/fasten/resource-graph-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {ResourceFhir} from './resource_fhir';

export class ResourceGraphResponse {
results: {[resource_type: string]: ResourceFhir[]}
metadata: ResourceGraphMetadata
}

export class ResourceGraphMetadata {
total_elements: number
page_size: number
page: number
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="row mt-5 mb-3">
<div class="col-12">

<div class="alert alert-warning" role="alert">
<div [hidden]="true" class="alert alert-warning" role="alert">
<strong>Warning!</strong> Fasten has detected medical Encounters that are not associated with a Condition.
They are grouped under the "Unassigned" section below.
<br/>
Expand All @@ -34,6 +34,13 @@ <h1 class="az-dashboard-title">Condition</h1>
<!-- Condition List -->
<app-report-medical-history-condition *ngFor="let condition of conditions; let i = index" [conditionGroup]="condition"></app-report-medical-history-condition>
<app-report-medical-history-explanation-of-benefit *ngFor="let eob of explanationOfBenefits; let i = index" [explanationOfBenefitGroup]="eob"></app-report-medical-history-explanation-of-benefit>

<ngb-pagination
*ngIf="resourceGraphMetadata.total_elements > resourceGraphMetadata.page_size"
[(page)]="resourceGraphMetadata.page"
(pageChange)="pageChange($event)"
[pageSize]="resourceGraphMetadata.page_size"
[collectionSize]="resourceGraphMetadata.total_elements"></ngb-pagination>
</ng-template>

<ng-template #emptyReport>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {ResourceFhir} from '../../models/fasten/resource_fhir';
import { ModalDismissReasons, NgbModal } from '@ng-bootstrap/ng-bootstrap';
import {ReportMedicalHistoryEditorComponent} from '../../components/report-medical-history-editor/report-medical-history-editor.component';
import {forkJoin} from 'rxjs';
import {ResourceGraphMetadata, ResourceGraphResponse} from '../../models/fasten/resource-graph-response';
// import {ReportEditorRelatedComponent} from '../../components/report-editor-related/report-editor-related.component';

@Component({
Expand All @@ -21,19 +22,34 @@ export class MedicalHistoryComponent implements OnInit {
unassigned_encounters: ResourceFhir[] = []
resourceLookup: {[name: string]: ResourceFhir} = {}

resourceGraphMetadata: ResourceGraphMetadata = {
total_elements: 0,
page_size: 0,
page: 1
}

constructor(
private fastenApi: FastenApiService,
private modalService: NgbModal
) { }


ngOnInit(): void {
//load the first page
this.pageChange(1)
}

pageChange(page: number){
this.loading = true
this.fastenApi.getResourceGraph().subscribe(results => {
this.fastenApi.getResourceGraph(null, page).subscribe((response: ResourceGraphResponse) => {
this.loading = false
this.conditions = [].concat(results["Condition"] || [], results["Composition"] || [])
this.unassigned_encounters = results["Encounter"] || []
this.explanationOfBenefits = results["ExplanationOfBenefit"] || []
this.resourceGraphMetadata = response.metadata
//page counter is 1 indexed but the backend is 0 indexed
this.resourceGraphMetadata.page = this.resourceGraphMetadata.page + 1

this.conditions = [].concat(response.results["Condition"] || [], response.results["Composition"] || [])
this.unassigned_encounters = response.results["Encounter"] || []
this.explanationOfBenefits = response.results["ExplanationOfBenefit"] || []

//populate a lookup table with all resources
for(let condition of this.conditions){
Expand Down Expand Up @@ -66,6 +82,7 @@ export class MedicalHistoryComponent implements OnInit {

}


openEditorRelated(): void {
const modalRef = this.modalService.open(ReportMedicalHistoryEditorComponent, {
size: 'xl',
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/app/services/fasten-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as fhirpath from 'fhirpath';
import _ from 'lodash';
import {DashboardConfig} from '../models/widget/dashboard-config';
import {DashboardWidgetQuery} from '../models/widget/dashboard-widget-query';
import {ResourceGraphResponse} from '../models/fasten/resource-graph-response';
import { fetchEventSource } from '@microsoft/fetch-event-source';

@Injectable({
Expand Down Expand Up @@ -178,16 +179,21 @@ export class FastenApiService {
return this._httpClient.post<any>(`${GetEndpointAbsolutePath(globalThis.location, environment.fasten_api_endpoint_base)}/secure/query`, query)
}

getResourceGraph(graphType?: string): Observable<{[resourceType: string]: ResourceFhir[]}> {
getResourceGraph(graphType?: string, page?:number): Observable<ResourceGraphResponse> {
if(!graphType){
graphType = "MedicalHistory"
}
let queryParams = {}
if(page){
//the backend is 0 indexed, but the frontend is 1 indexed
queryParams["page"] = page - 1
}

return this._httpClient.get<any>(`${GetEndpointAbsolutePath(globalThis.location, environment.fasten_api_endpoint_base)}/secure/resource/graph/${graphType}`)
return this._httpClient.get<any>(`${GetEndpointAbsolutePath(globalThis.location, environment.fasten_api_endpoint_base)}/secure/resource/graph/${graphType}`, {params: queryParams})
.pipe(
map((response: ResponseWrapper) => {
console.log("RESPONSE", response)
return response.data as {[name: string]: ResourceFhir[]}
return response.data as ResourceGraphResponse
})
);
}
Expand Down

0 comments on commit 1fda510

Please sign in to comment.