-
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.
[NOREF] cedar system dataloader (#2577)
* feat: WIP cedar systems data loaders, need to update data loaders signature * add stubbed functions to dataloader instantiation in tests * fix loader fn * fix tests to use testconfig context with dataloaders * update system intake systems to also use dataloaders --------- Co-authored-by: Steven Wade <steven.wade@oddball.io>
- Loading branch information
1 parent
d0a3702
commit 917f763
Showing
14 changed files
with
170 additions
and
156 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package dataloaders | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/graph-gophers/dataloader" | ||
"go.uber.org/zap" | ||
|
||
"github.com/cmsgov/easi-app/pkg/appcontext" | ||
"github.com/cmsgov/easi-app/pkg/models" | ||
) | ||
|
||
const cedar_system_loader_key = "id" | ||
|
||
func (loaders *DataLoaders) getCedarSystemBatchFunction(ctx context.Context, keys dataloader.Keys) []*dataloader.Result { | ||
logger := appcontext.ZLogger(ctx) | ||
cedarSystems, err := loaders.GetCedarSystems(ctx) | ||
if err != nil { | ||
logger.Error("error getting cedar systems in data loader", zap.Error(err)) | ||
loaderErrors := make([]*dataloader.Result, len(keys)) | ||
for index := range keys { | ||
loaderErrors[index] = &dataloader.Result{Data: nil, Error: err} | ||
} | ||
return loaderErrors | ||
} | ||
|
||
systemSummaryMap := make(map[string]*models.CedarSystem) | ||
for _, sys := range cedarSystems { | ||
if sys != nil { | ||
systemSummaryMap[sys.ID.String] = sys | ||
} | ||
} | ||
output := make([]*dataloader.Result, len(keys)) | ||
|
||
for index, key := range keys { | ||
ck, ok := key.Raw().(KeyArgs) | ||
if ok { | ||
resKey := fmt.Sprint(ck.Args[cedar_system_loader_key]) | ||
cedarSystem, ok := systemSummaryMap[resKey] | ||
if ok { | ||
output[index] = &dataloader.Result{Data: cedarSystem, Error: nil} | ||
} else { | ||
err := fmt.Errorf("cedar system not found for id %s", resKey) | ||
output[index] = &dataloader.Result{Data: nil, Error: err} | ||
} | ||
} else { | ||
err := fmt.Errorf("could not retrive key from %s", key.String()) | ||
output[index] = &dataloader.Result{Data: nil, Error: err} | ||
} | ||
} | ||
|
||
return output | ||
} | ||
|
||
func GetCedarSystemByID(ctx context.Context, id string) (*models.CedarSystem, error) { | ||
allLoaders := Loaders(ctx) | ||
cedarSystemLoader := allLoaders.cedarSystemByIDLoader | ||
|
||
key := NewKeyArgs() | ||
key.Args[cedar_system_loader_key] = id | ||
|
||
thunk := cedarSystemLoader.Loader.Load(ctx, key) | ||
result, err := thunk() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
system, isCedarSystem := result.(*models.CedarSystem) | ||
if !isCedarSystem { | ||
return nil, fmt.Errorf("result is not a cedar system it is Type %T", system) | ||
} | ||
|
||
return system, nil | ||
} |
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
Oops, something went wrong.