From 814c65059a2f213f1953c0a840a0279a50e575b4 Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Wed, 25 Mar 2015 13:15:10 -0700 Subject: [PATCH 1/2] Document and specify defaults for ContainerRequestInfo. --- api/handler.go | 6 +----- info/v1/container.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/api/handler.go b/api/handler.go index 703d3c57..18c848ae 100644 --- a/api/handler.go +++ b/api/handler.go @@ -164,11 +164,7 @@ func streamResults(results chan *events.Event, w http.ResponseWriter, r *http.Re } func getContainerInfoRequest(body io.ReadCloser) (*info.ContainerInfoRequest, error) { - var query info.ContainerInfoRequest - - // Default stats and samples is 64. - query.NumStats = 64 - + query := info.DefaultContainerInfoRequest() decoder := json.NewDecoder(body) err := decoder.Decode(&query) if err != nil && err != io.EOF { diff --git a/info/v1/container.go b/info/v1/container.go index 14ce7d95..a6e70fb1 100644 --- a/info/v1/container.go +++ b/info/v1/container.go @@ -81,7 +81,9 @@ func (self ContainerReferenceSlice) Less(i, j int) bool { return self[i].Name < // ContainerInfoQuery is used when users check a container info from the REST api. // It specifies how much data users want to get about a container type ContainerInfoRequest struct { - // Max number of stats to return. + // Max number of stats to return. Specify -1 for all stats currently available. + // If start and end time are specified this limit is ignored. + // Default: 60 NumStats int `json:"num_stats,omitempty"` // Start time for which to query information. @@ -93,6 +95,13 @@ type ContainerInfoRequest struct { End time.Time `json:"end,omitempty"` } +// Returns a ContainerInfoRequest with all default values specified. +func DefaultContainerInfoRequest() ContainerInfoRequest { + return ContainerInfoRequest{ + NumStats: 60, + } +} + func (self *ContainerInfoRequest) Equals(other ContainerInfoRequest) bool { return self.NumStats == other.NumStats && self.Start.Equal(other.Start) && From bacf817e6ab6a0ac6c2d1f023c74a3bc073ca5c5 Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Wed, 25 Mar 2015 13:18:54 -0700 Subject: [PATCH 2/2] If start and end specified, ignore maxResults. --- storage/memory/stats_buffer.go | 8 +++++++- storage/memory/stats_buffer_test.go | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/storage/memory/stats_buffer.go b/storage/memory/stats_buffer.go index a1ed7458..f5438c65 100644 --- a/storage/memory/stats_buffer.go +++ b/storage/memory/stats_buffer.go @@ -47,13 +47,19 @@ func (self *StatsBuffer) Add(item *info.ContainerStats) { } // Returns up to maxResult elements in the specified time period (inclusive). -// Results are from first to last. maxResults of -1 means no limit. +// Results are from first to last. maxResults of -1 means no limit. When first +// and last are specified, maxResults is ignored. func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*info.ContainerStats { // No stats, return empty. if self.size == 0 { return []*info.ContainerStats{} } + // Return all results in a time range if specified. + if !start.IsZero() && !end.IsZero() { + maxResults = -1 + } + // NOTE: Since we store the elments in descending timestamp order "start" will // be a higher index than "end". diff --git a/storage/memory/stats_buffer_test.go b/storage/memory/stats_buffer_test.go index 4ac28e63..f142e931 100644 --- a/storage/memory/stats_buffer_test.go +++ b/storage/memory/stats_buffer_test.go @@ -161,6 +161,9 @@ func TestInTimeRange(t *testing.T) { expectElements(t, sb.InTimeRange(createTime(3), createTime(5), 10), []int32{3, 4}) assert.Empty(sb.InTimeRange(createTime(5), createTime(5), 10)) + // Start and end time ignores maxResults. + expectElements(t, sb.InTimeRange(createTime(1), createTime(5), 1), []int32{1, 2, 3, 4}) + // No start time. expectElements(t, sb.InTimeRange(empty, createTime(5), 10), []int32{1, 2, 3, 4}) expectElements(t, sb.InTimeRange(empty, createTime(4), 10), []int32{1, 2, 3, 4})