Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into martin/BCDA-3710_pgx
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Trang committed Dec 16, 2020
2 parents 3f1c431 + 8d36fc6 commit 53c0e9c
Show file tree
Hide file tree
Showing 137 changed files with 10,824 additions and 7,223 deletions.
48 changes: 34 additions & 14 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/bgentry/que-go"
version = "1.0.1"
Expand Down Expand Up @@ -76,3 +75,11 @@
[[constraint]]
name = "github.com/newrelic/go-agent"
version = "3.9.0"

[[constraint]]
name = "gorm.io/gorm"
version = "1.20.8"

[[constraint]]
name = "gorm.io/driver/postgres"
version = "0.2.4"
42 changes: 24 additions & 18 deletions bcda/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ func NewHandler(resources []string, basePath string) *Handler {
h := &Handler{}

db := database.GetGORMDbConnection()
db.DB().SetMaxOpenConns(utils.GetEnvInt("BCDA_DB_MAX_OPEN_CONNS", 25))
db.DB().SetMaxIdleConns(utils.GetEnvInt("BCDA_DB_MAX_IDLE_CONNS", 25))
db.DB().SetConnMaxLifetime(time.Duration(utils.GetEnvInt("BCDA_DB_CONN_MAX_LIFETIME_MIN", 5)) * time.Minute)
dbc, err := db.DB()
if err != nil {
log.Fatalf("Failed to retrieve database connection. Err: %v", err)
}
dbc.SetMaxOpenConns(utils.GetEnvInt("BCDA_DB_MAX_OPEN_CONNS", 25))
dbc.SetMaxIdleConns(utils.GetEnvInt("BCDA_DB_MAX_IDLE_CONNS", 25))
dbc.SetConnMaxLifetime(time.Duration(utils.GetEnvInt("BCDA_DB_CONN_MAX_LIFETIME_MIN", 5)) * time.Minute)

queueDatabaseURL := os.Getenv("QUEUE_DATABASE_URL")
pgxcfg, err := pgx.ParseURI(queueDatabaseURL)
Expand Down Expand Up @@ -93,7 +97,7 @@ func NewHandler(resources []string, basePath string) *Handler {
}
pgxpool.Release(c)

c1, err := db.DB().Conn(ctx)
c1, err := dbc.Conn(ctx)
if err != nil {
log.Warnf("Failed to acquire connection %s", err.Error())
continue
Expand All @@ -117,7 +121,7 @@ func NewHandler(resources []string, basePath string) *Handler {
runoutClaimThru := utils.FromEnv("RUNOUT_CLAIM_THRU_DATE", "2020-12-31")
runoutClaimThruDate, err := time.Parse(claimThruDateFmt, runoutClaimThru)
if err != nil {
log.Fatalf("Failed to parse RUNOUT_CLAIM_THRU_DATE '%s'. Err: %s", runoutClaimThru, err.Error())
log.Fatalf("Failed to parse RUNOUT_CLAIM_THRU_DATE '%s'. Err: %v", runoutClaimThru, err)
}

repository := postgres.NewRepository(db)
Expand Down Expand Up @@ -218,21 +222,23 @@ func (h *Handler) bulkRequest(resourceTypes []string, w http.ResponseWriter, r *
// their job finishes or time expires (+24 hours default) for any remaining jobs left in a pending or in-progress state.
// Overall, this will prevent a queue of concurrent calls from slowing up our system.
// NOTE: this logic is relevant to PROD only; simultaneous requests in our lower environments is acceptable (i.e., shared opensbx creds)
if (os.Getenv("DEPLOYMENT_TARGET") == "prod") &&
(!db.Find(&pendingAndInProgressJobs, "aco_id = ? AND status IN (?, ?)", acoID, "In Progress", "Pending").RecordNotFound()) {
if types, err := check429(pendingAndInProgressJobs, resourceTypes, version); err != nil {
if _, ok := err.(duplicateTypeError); ok {
w.Header().Set("Retry-After", strconv.Itoa(utils.GetEnvInt("CLIENT_RETRY_AFTER_IN_SECONDS", 0)))
w.WriteHeader(http.StatusTooManyRequests)
if os.Getenv("DEPLOYMENT_TARGET") == "prod" {
db.Where("aco_id = ? AND status IN (?, ?)", acoID, "In Progress", "Pending").Find(&pendingAndInProgressJobs)
if len(pendingAndInProgressJobs) > 0 {
if types, err := check429(pendingAndInProgressJobs, resourceTypes, version); err != nil {
if _, ok := err.(duplicateTypeError); ok {
w.Header().Set("Retry-After", strconv.Itoa(utils.GetEnvInt("CLIENT_RETRY_AFTER_IN_SECONDS", 0)))
w.WriteHeader(http.StatusTooManyRequests)
} else {
log.Error(err)
oo := responseutils.CreateOpOutcome(responseutils.Error, responseutils.Exception, responseutils.Processing, "")
responseutils.WriteError(oo, w, http.StatusInternalServerError)
}

return
} else {
log.Error(err)
oo := responseutils.CreateOpOutcome(responseutils.Error, responseutils.Exception, responseutils.Processing, "")
responseutils.WriteError(oo, w, http.StatusInternalServerError)
resourceTypes = types
}

return
} else {
resourceTypes = types
}
}

Expand Down
4 changes: 2 additions & 2 deletions bcda/api/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"github.com/pborman/uuid"

"github.com/go-chi/chi"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"
)

type RequestsTestSuite struct {
Expand Down Expand Up @@ -61,7 +61,7 @@ func (s *RequestsTestSuite) SetupTest() {
func (s *RequestsTestSuite) TearDownSuite() {
s.NoError(s.db.Unscoped().Delete(&models.Job{}, "aco_id = ?", s.acoID).Error)
s.NoError(s.db.Unscoped().Delete(&models.ACO{}, "uuid = ?", s.acoID).Error)
s.db.Close()
database.Close(s.db)
}

func (s *RequestsTestSuite) TearDownTest() {
Expand Down
4 changes: 2 additions & 2 deletions bcda/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func BulkPatientRequest(w http.ResponseWriter, r *http.Request) {
Start data export (for the specified group identifier) for all supported resource types
Initiates a job to collect data from the Blue Button API for your ACO. The supported Group identifiers are `all` and `runout`.
The `all` identifier returns data for the group of all patients attributed to the requesting ACO. If used when specifying `_since`: all claims data which has been updated since the specified date will be returned for beneficiaries which have been attributed to the ACO since before the specified date; and all historical claims data will be returned for beneficiaries which have been newly attributed to the ACO since the specified date.
The `runout` identifier returns claims runouts data.
Expand Down Expand Up @@ -113,7 +113,7 @@ func JobStatus(w http.ResponseWriter, r *http.Request) {
defer database.Close(db)

var job models.Job
err := db.Find(&job, "id = ?", jobID).Error
err := db.First(&job, jobID).Error
if err != nil {
log.Print(err)
oo := responseutils.CreateOpOutcome(responseutils.Error, responseutils.Exception, responseutils.DbErr, "")
Expand Down
8 changes: 4 additions & 4 deletions bcda/api/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
fhirmodels "github.com/eug48/fhir/models"
"github.com/go-chi/chi"
"github.com/jackc/pgx"
"github.com/jinzhu/gorm"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"

api "github.com/CMSgov/bcda-app/bcda/api"
"github.com/CMSgov/bcda-app/bcda/auth"
Expand Down Expand Up @@ -1050,14 +1050,14 @@ func (s *APITestSuite) TestAuthInfoOkta() {
auth.SetProvider(originalProvider)
}

func (s *APITestSuite) verifyJobCount(acoID string, expectedJobCount int) {
func (s *APITestSuite) verifyJobCount(acoID string, expectedJobCount int64) {
count, err := s.getJobCount(acoID)
assert.NoError(s.T(), err)
assert.Equal(s.T(), expectedJobCount, count)
}

func (s *APITestSuite) getJobCount(acoID string) (int, error) {
var count int
func (s *APITestSuite) getJobCount(acoID string) (int64, error) {
var count int64
err := s.db.Model(&models.Job{}).Where("aco_id = ?", acoID).Count(&count).Error
return count, err
}
Expand Down
4 changes: 2 additions & 2 deletions bcda/api/v2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
"github.com/CMSgov/bcda-app/bcda/database"
"github.com/CMSgov/bcda-app/bcda/models"
"github.com/go-chi/chi"
"github.com/jinzhu/gorm"
"github.com/pborman/uuid"
"github.com/samply/golang-fhir-models/fhir-models/fhir"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"
)

const (
Expand Down Expand Up @@ -57,7 +57,7 @@ func (s *APITestSuite) SetupSuite() {

func (s *APITestSuite) TearDownSuite() {
s.cleanup()
s.db.Close()
database.Close(s.db)
}

func TestAPITestSuite(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions bcda/auth/alpha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/CMSgov/bcda-app/bcda/models"
"github.com/CMSgov/bcda-app/bcda/testUtils"
"github.com/dgrijalva/jwt-go"
"github.com/jinzhu/gorm"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"
)

var connections = make(map[string]*gorm.DB)
Expand Down Expand Up @@ -52,7 +52,11 @@ func (s *AlphaAuthPluginTestSuite) AfterTest(suiteName, testName string) {
if !ok {
s.FailNow("WTF? no db connection for %s", testName)
}
if err := c.Close(); err != nil {
gc, err := c.DB()
if err != nil {
s.FailNow("error retrieving db connection: %s", err)
}
if err := gc.Close(); err != nil {
s.FailNow("error closing db connection for %s because %s", testName, err)
}
}
Expand Down
5 changes: 3 additions & 2 deletions bcda/auth/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package auth_test
import (
"encoding/json"
"fmt"
"github.com/go-chi/chi"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/go-chi/chi"

"github.com/CMSgov/bcda-app/bcda/constants"

"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"

"github.com/CMSgov/bcda-app/bcda/auth"
"github.com/CMSgov/bcda-app/bcda/database"
Expand Down
2 changes: 1 addition & 1 deletion bcda/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func RequireTokenJobMatch(next http.Handler) http.Handler {
defer database.Close(db)

var job models.Job
err = db.Find(&job, "id = ? and aco_id = ?", i, ad.ACOID).Error
err = db.First(&job, "id = ? and aco_id = ?", i, ad.ACOID).Error
if err != nil {
log.Error(err)
oo := responseutils.CreateOpOutcome(responseutils.Error, responseutils.Exception, responseutils.Not_found, "")
Expand Down
2 changes: 1 addition & 1 deletion bcda/auth/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/CMSgov/bcda-app/bcda/auth"
"github.com/CMSgov/bcda-app/bcda/database"
"github.com/CMSgov/bcda-app/bcda/testUtils"
"github.com/jinzhu/gorm"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"
)

type ModelsTestSuite struct {
Expand Down
10 changes: 6 additions & 4 deletions bcda/auth/okta_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"errors"
"fmt"
"regexp"
"testing"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"
)

const KnownFixtureACO = "DBBD1CE1-AE24-435C-807D-ED45953077D3"
Expand All @@ -25,14 +27,14 @@ type OktaAuthPluginTestSuite struct {

func (s *OktaAuthPluginTestSuite) SetupSuite() {
db := database.GetGORMDbConnection()

defer func() {
if err := db.Close(); err != nil {
assert.Failf(s.T(), err.Error(), "okta plugin test")
}
database.Close(db)
}()

var aco models.ACO
if db.Find(&aco, "UUID = ?", uuid.Parse(KnownFixtureACO)).RecordNotFound() {
err := db.First(&aco, "UUID = ?", uuid.Parse(KnownFixtureACO)).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
assert.NotNil(s.T(), fmt.Errorf("Unable to find ACO %s", KnownFixtureACO))
return
}
Expand Down
4 changes: 2 additions & 2 deletions bcda/auth/ssas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *SSASPluginTestSuite) SetupSuite() {

func (s *SSASPluginTestSuite) SetupTest() {
db := database.GetGORMDbConnection()
defer db.Close()
defer database.Close(db)

db.Create(&models.ACO{
UUID: uuid.Parse(testACOUUID),
Expand All @@ -75,7 +75,7 @@ func (s *SSASPluginTestSuite) TearDownTest() {
os.Setenv("BCDA_SSAS_SECRET", origSSASSecret)

db := database.GetGORMDbConnection()
defer db.Close()
defer database.Close(db)

db.Unscoped().Delete(models.ACO{}, "uuid = ?", testACOUUID)
}
Expand Down
Loading

0 comments on commit 53c0e9c

Please sign in to comment.