diff --git a/deviceStatus.go b/deviceStatus.go index bea46dc..30a60a8 100644 --- a/deviceStatus.go +++ b/deviceStatus.go @@ -151,7 +151,7 @@ func (app *App) getStatusInfo(deviceID string) (Status, error) { s Status ) - stateInfo, hErr := app.eventGetter.GetRecordsOfType(deviceID, app.getLimit, db.State) + stateInfo, hErr := app.eventGetter.GetRecordsOfType(deviceID, app.getStatusLimit, db.State) if hErr != nil { return s, serverErr{emperror.WrapWith(hErr, "Failed to get state records", "device id", deviceID), http.StatusInternalServerError} diff --git a/deviceStatus_test.go b/deviceStatus_test.go index c5ddcb5..0100896 100644 --- a/deviceStatus_test.go +++ b/deviceStatus_test.go @@ -198,11 +198,11 @@ func TestGetStatusInfo(t *testing.T) { } app := App{ - eventGetter: mockGetter, - getLimit: 5, - logger: logging.DefaultLogger(), - decrypters: ciphers, - measures: m, + eventGetter: mockGetter, + getStatusLimit: 5, + logger: logging.DefaultLogger(), + decrypters: ciphers, + measures: m, } status, err := app.getStatusInfo("test") @@ -301,11 +301,11 @@ func TestHandleGetStatus(t *testing.T) { } app := App{ - eventGetter: mockGetter, - getLimit: 5, - logger: logging.DefaultLogger(), - decrypters: ciphers, - measures: m, + eventGetter: mockGetter, + getStatusLimit: 5, + logger: logging.DefaultLogger(), + decrypters: ciphers, + measures: m, } rr := httptest.NewRecorder() request := mux.SetURLVars( diff --git a/main.go b/main.go index 9810b9c..a2b6efd 100644 --- a/main.go +++ b/main.go @@ -69,7 +69,8 @@ var ( type Config struct { Db cassandra.Config - GetLimit int + GetEventsLimit int + GetStatusLimit int GetRetries int RetryInterval time.Duration Health HealthConfig @@ -131,6 +132,7 @@ func gungnir(arguments []string) { v.Unmarshal(config) dbConfig := config.Db + validateConfig(config) //vaultClient, err := xvault.Initialize(v) //if err != nil { @@ -165,11 +167,12 @@ func gungnir(arguments []string) { measures := NewMeasures(metricsRegistry) // MARK: Actual server logic app := &App{ - eventGetter: retryService, - logger: logger, - getLimit: config.GetLimit, - decrypters: decrypters, - measures: measures, + eventGetter: retryService, + logger: logger, + getEventLimit: config.GetEventsLimit, + getStatusLimit: config.GetStatusLimit, + decrypters: decrypters, + measures: measures, } logging.GetLogger(context.Background()) @@ -256,6 +259,20 @@ func exitIfError(logger log.Logger, err error) { } } +const ( + defaultGetEventsLimit = 50 + defaultGetStatusLimit = 10 +) + +func validateConfig(config *Config) { + if config.GetEventsLimit < 1 { + config.GetEventsLimit = defaultGetEventsLimit + } + if config.GetStatusLimit < 1 { + config.GetStatusLimit = defaultGetStatusLimit + } +} + func main() { gungnir(os.Args) } diff --git a/primaryHandler.go b/primaryHandler.go index 496fa10..0c77557 100644 --- a/primaryHandler.go +++ b/primaryHandler.go @@ -46,10 +46,11 @@ import ( //go:generate swagger generate spec -m -o swagger.spec type App struct { - eventGetter db.RecordGetter - logger log.Logger - getLimit int - decrypters voynicrypto.Ciphers + eventGetter db.RecordGetter + logger log.Logger + getEventLimit int + getStatusLimit int + decrypters voynicrypto.Ciphers measures *Measures } @@ -97,7 +98,7 @@ type ErrResponse struct { func (app *App) getDeviceInfo(deviceID string) ([]Event, error) { - records, hErr := app.eventGetter.GetRecords(deviceID, app.getLimit) + records, hErr := app.eventGetter.GetRecords(deviceID, app.getEventLimit) events := []Event{} // if both have errors or are empty, return an error diff --git a/primaryHandler_test.go b/primaryHandler_test.go index e6b11d7..2143697 100644 --- a/primaryHandler_test.go +++ b/primaryHandler_test.go @@ -191,11 +191,11 @@ func TestGetDeviceInfo(t *testing.T) { p := xmetricstest.NewProvider(nil, Metrics) m := NewMeasures(p) app := App{ - eventGetter: mockGetter, - logger: logging.DefaultLogger(), - decrypters: ciphers, - measures: m, - getLimit: 5, + eventGetter: mockGetter, + logger: logging.DefaultLogger(), + decrypters: ciphers, + measures: m, + getEventLimit: 5, } p.Assert(t, UnmarshalFailureCounter)(xmetricstest.Value(0.0)) events, err := app.getDeviceInfo("test") @@ -274,11 +274,11 @@ func TestHandleGetEvents(t *testing.T) { m := NewMeasures(p) app := App{ - eventGetter: mockGetter, - getLimit: 5, - logger: logging.DefaultLogger(), - decrypters: ciphers, - measures: m, + eventGetter: mockGetter, + getEventLimit: 5, + logger: logging.DefaultLogger(), + decrypters: ciphers, + measures: m, } rr := httptest.NewRecorder() request := mux.SetURLVars( diff --git a/release_notes.md b/release_notes.md index 8b13789..8fe565d 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1 +1 @@ - +- added getStatusLimit