Retrieve 0 stats/samples when user asked for zero stats/samples
This commit is contained in:
parent
6213f8b0c3
commit
55b65f4eb9
@ -71,6 +71,11 @@ func HandleRequest(m manager.Manager, w http.ResponseWriter, r *http.Request) er
|
||||
log.Printf("Api - Container(%s)", containerName)
|
||||
|
||||
var query info.ContainerInfoRequest
|
||||
|
||||
// If a user does not specify number of stats/samples he wants,
|
||||
// it's 64 by default
|
||||
query.NumStats = 64
|
||||
query.NumSamples = 64
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&query)
|
||||
if err != nil && err != io.EOF {
|
||||
|
@ -77,10 +77,10 @@ func (self *ContainerInfoRequest) FillDefaults() *ContainerInfoRequest {
|
||||
if ret == nil {
|
||||
ret = new(ContainerInfoRequest)
|
||||
}
|
||||
if ret.NumStats <= 0 {
|
||||
if ret.NumStats < 0 {
|
||||
ret.NumStats = 1024
|
||||
}
|
||||
if ret.NumSamples <= 0 {
|
||||
if ret.NumSamples < 0 {
|
||||
ret.NumSamples = 1024
|
||||
}
|
||||
if len(ret.CpuUsagePercentiles) == 0 {
|
||||
|
@ -320,6 +320,9 @@ func (self *influxdbStorage) AddStats(ref info.ContainerReference, stats *info.C
|
||||
}
|
||||
|
||||
func (self *influxdbStorage) RecentStats(containerName string, numStats int) ([]*info.ContainerStats, error) {
|
||||
if numStats == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
// TODO(dengnan): select only columns that we need
|
||||
// TODO(dengnan): escape names
|
||||
query := fmt.Sprintf("select * from %v where %v='%v' and %v='%v'", self.tableName, colContainerName, containerName, colMachineName, self.machineName)
|
||||
@ -352,6 +355,9 @@ func (self *influxdbStorage) RecentStats(containerName string, numStats int) ([]
|
||||
}
|
||||
|
||||
func (self *influxdbStorage) Samples(containerName string, numSamples int) ([]*info.ContainerStatsSample, error) {
|
||||
if numSamples == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
// TODO(dengnan): select only columns that we need
|
||||
// TODO(dengnan): escape names
|
||||
query := fmt.Sprintf("select * from %v where %v='%v' and %v='%v'", self.tableName, colContainerName, containerName, colMachineName, self.machineName)
|
||||
|
@ -146,3 +146,13 @@ func TestPercentilesWithoutStats(t *testing.T) {
|
||||
t.SkipNow()
|
||||
runStorageTest(test.StorageDriverTestPercentilesWithoutStats, t)
|
||||
}
|
||||
|
||||
func TestRetrieveZeroStats(t *testing.T) {
|
||||
t.SkipNow()
|
||||
runStorageTest(test.StorageDriverTestRetrieveZeroRecentStats, t)
|
||||
}
|
||||
|
||||
func TestRetrieveZeroSamples(t *testing.T) {
|
||||
t.SkipNow()
|
||||
runStorageTest(test.StorageDriverTestRetrieveZeroSamples, t)
|
||||
}
|
||||
|
@ -73,3 +73,11 @@ func TestNoSamples(t *testing.T) {
|
||||
func TestPercentilesWithoutStats(t *testing.T) {
|
||||
runStorageTest(test.StorageDriverTestPercentilesWithoutStats, t)
|
||||
}
|
||||
|
||||
func TestRetrieveZeroStats(t *testing.T) {
|
||||
runStorageTest(test.StorageDriverTestRetrieveZeroRecentStats, t)
|
||||
}
|
||||
|
||||
func TestRetrieveZeroSamples(t *testing.T) {
|
||||
runStorageTest(test.StorageDriverTestRetrieveZeroSamples, t)
|
||||
}
|
||||
|
@ -482,3 +482,61 @@ func StorageDriverTestPercentilesWithoutStats(driver storage.StorageDriver, t *t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func StorageDriverTestRetrieveZeroRecentStats(driver storage.StorageDriver, t *testing.T) {
|
||||
defer driver.Close()
|
||||
N := 100
|
||||
memTrace := make([]uint64, N)
|
||||
cpuTrace := make([]uint64, N)
|
||||
for i := 0; i < N; i++ {
|
||||
memTrace[i] = uint64(i + 1)
|
||||
cpuTrace[i] = uint64(1)
|
||||
}
|
||||
|
||||
ref := info.ContainerReference{
|
||||
Name: "container",
|
||||
}
|
||||
|
||||
trace := buildTrace(cpuTrace, memTrace, 1*time.Second)
|
||||
|
||||
for _, stats := range trace {
|
||||
driver.AddStats(ref, stats)
|
||||
}
|
||||
|
||||
recentStats, err := driver.RecentStats(ref.Name, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(recentStats) > 0 {
|
||||
t.Errorf("RecentStats() returns %v stats when requests for 0 stats", len(recentStats))
|
||||
}
|
||||
}
|
||||
|
||||
func StorageDriverTestRetrieveZeroSamples(driver storage.StorageDriver, t *testing.T) {
|
||||
defer driver.Close()
|
||||
N := 100
|
||||
memTrace := make([]uint64, N)
|
||||
cpuTrace := make([]uint64, N)
|
||||
for i := 0; i < N; i++ {
|
||||
memTrace[i] = uint64(i + 1)
|
||||
cpuTrace[i] = uint64(1)
|
||||
}
|
||||
|
||||
ref := info.ContainerReference{
|
||||
Name: "container",
|
||||
}
|
||||
|
||||
trace := buildTrace(cpuTrace, memTrace, 1*time.Second)
|
||||
|
||||
for _, stats := range trace {
|
||||
driver.AddStats(ref, stats)
|
||||
}
|
||||
|
||||
samples, err := driver.Samples(ref.Name, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(samples) > 0 {
|
||||
t.Errorf("RecentStats() returns %v stats when requests for 0 stats", len(samples))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user