Skip to content

Commit

Permalink
fix(variants): return error for variants overview when ES is down
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Sep 25, 2023
1 parent 69de8b9 commit 3530493
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/api/mvc/data-types/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ func GetDataTypes(c echo.Context) error {

// accumulate number of variants associated with each
// sampleId fetched from the variants overview
// TODO: refactor to handle errors better
resultsMap := variantService.GetVariantsOverview(es, cfg)
resultsMap, err := variantService.GetVariantsOverview(es, cfg)

if err != nil {
// Could not talk to Elasticsearch, return an error
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
"error": err.Error(),
})
}

variantDataTypeJson["count"] = sumAllValues(resultsMap["sampleIDs"])

// Data types are basically stand-ins for schema blocks
Expand Down
10 changes: 8 additions & 2 deletions src/api/mvc/variants/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,14 @@ func GetVariantsOverview(c echo.Context) error {
es := c.(*contexts.GohanContext).Es7Client
cfg := c.(*contexts.GohanContext).Config

// TODO: refactor to handle errors better
resultsMap := variantService.GetVariantsOverview(es, cfg)
resultsMap, err := variantService.GetVariantsOverview(es, cfg)

if err != nil {
// Could not talk to Elasticsearch, return an error
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
"error": err.Error(),
})
}

return c.JSON(http.StatusOK, resultsMap)
}
Expand Down
11 changes: 9 additions & 2 deletions src/api/services/variants/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package variantsService

import (
"errors"
"fmt"
"gohan/api/models"
esRepo "gohan/api/repositories/elasticsearch"
Expand All @@ -23,7 +24,7 @@ func NewVariantService(cfg *models.Config) *VariantService {
return vs
}

func GetVariantsOverview(es *elasticsearch.Client, cfg *models.Config) map[string]interface{} {
func GetVariantsOverview(es *elasticsearch.Client, cfg *models.Config) (map[string]interface{}, error) {
resultsMap := map[string]interface{}{}
resultsMux := sync.RWMutex{}

Expand Down Expand Up @@ -70,6 +71,12 @@ func GetVariantsOverview(es *elasticsearch.Client, cfg *models.Config) map[strin
resultsMux.Unlock()
}

// First, make sure the ES cluster is running - otherwise this will hang for a long time
_, err := es.Ping()
if err != nil {
return nil, errors.New("could not contact Elasticsearch - make sure it's running")
}

// get distribution of chromosomes
wg.Add(1)
go callGetBucketsByKeyword("chromosomes", "chrom.keyword", &wg)
Expand All @@ -92,5 +99,5 @@ func GetVariantsOverview(es *elasticsearch.Client, cfg *models.Config) map[strin

wg.Wait()

return resultsMap
return resultsMap, nil
}

0 comments on commit 3530493

Please sign in to comment.