Skip to content

Commit

Permalink
added getStatusLimit (#92)
Browse files Browse the repository at this point in the history
* added getStatusLimit

* gofmt
  • Loading branch information
kcajmagic authored Nov 25, 2019
1 parent f3a3059 commit 0252421
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
2 changes: 1 addition & 1 deletion deviceStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
20 changes: 10 additions & 10 deletions deviceStatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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(
Expand Down
29 changes: 23 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ var (

type Config struct {
Db cassandra.Config
GetLimit int
GetEventsLimit int
GetStatusLimit int
GetRetries int
RetryInterval time.Duration
Health HealthConfig
Expand Down Expand Up @@ -131,6 +132,7 @@ func gungnir(arguments []string) {

v.Unmarshal(config)
dbConfig := config.Db
validateConfig(config)

//vaultClient, err := xvault.Initialize(v)
//if err != nil {
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
}
11 changes: 6 additions & 5 deletions primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions primaryHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@

- added getStatusLimit

0 comments on commit 0252421

Please sign in to comment.