func (s *StoreSuite) TestListCountersBy(c *C) { incs := []struct { key []string day int }{ {[]string{"a"}, 1}, {[]string{"a"}, 1}, {[]string{"b"}, 1}, {[]string{"a", "b"}, 1}, {[]string{"a", "c"}, 1}, {[]string{"a"}, 3}, {[]string{"a", "b"}, 3}, {[]string{"b"}, 9}, {[]string{"b"}, 9}, {[]string{"a", "c", "d"}, 9}, {[]string{"a", "c", "e"}, 9}, {[]string{"a", "c", "f"}, 9}, } day := func(i int) time.Time { return time.Date(2012, time.May, i, 0, 0, 0, 0, time.UTC) } counters := s.Session.DB("juju").C("stat.counters") for i, inc := range incs { err := s.store.IncCounter(inc.key) c.Assert(err, IsNil) // Hack time so counters are assigned to 2012-05-<day> filter := bson.M{"t": bson.M{"$gt": store.TimeToStamp(time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC))}} stamp := store.TimeToStamp(day(inc.day)) stamp += int32(i) * 60 // Make every entry unique. err = counters.Update(filter, bson.D{{"$set", bson.D{{"t", stamp}}}}) c.Check(err, IsNil) } tests := []struct { request store.CounterRequest result []store.Counter }{ { store.CounterRequest{ Key: []string{"a"}, Prefix: false, List: false, By: store.ByDay, }, []store.Counter{ {Key: []string{"a"}, Prefix: false, Count: 2, Time: day(1)}, {Key: []string{"a"}, Prefix: false, Count: 1, Time: day(3)}, }, }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: false, By: store.ByDay, }, []store.Counter{ {Key: []string{"a"}, Prefix: true, Count: 2, Time: day(1)}, {Key: []string{"a"}, Prefix: true, Count: 1, Time: day(3)}, {Key: []string{"a"}, Prefix: true, Count: 3, Time: day(9)}, }, }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: false, By: store.ByDay, Start: day(2), }, []store.Counter{ {Key: []string{"a"}, Prefix: true, Count: 1, Time: day(3)}, {Key: []string{"a"}, Prefix: true, Count: 3, Time: day(9)}, }, }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: false, By: store.ByDay, Stop: day(4), }, []store.Counter{ {Key: []string{"a"}, Prefix: true, Count: 2, Time: day(1)}, {Key: []string{"a"}, Prefix: true, Count: 1, Time: day(3)}, }, }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: false, By: store.ByDay, Start: day(3), Stop: day(8), }, []store.Counter{ {Key: []string{"a"}, Prefix: true, Count: 1, Time: day(3)}, }, }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: true, By: store.ByDay, }, []store.Counter{ {Key: []string{"a", "b"}, Prefix: false, Count: 1, Time: day(1)}, {Key: []string{"a", "c"}, Prefix: false, Count: 1, Time: day(1)}, {Key: []string{"a", "b"}, Prefix: false, Count: 1, Time: day(3)}, {Key: []string{"a", "c"}, Prefix: true, Count: 3, Time: day(9)}, }, }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: false, By: store.ByWeek, }, []store.Counter{ {Key: []string{"a"}, Prefix: true, Count: 3, Time: day(6)}, {Key: []string{"a"}, Prefix: true, Count: 3, Time: day(13)}, }, }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: true, By: store.ByWeek, }, []store.Counter{ {Key: []string{"a", "b"}, Prefix: false, Count: 2, Time: day(6)}, {Key: []string{"a", "c"}, Prefix: false, Count: 1, Time: day(6)}, {Key: []string{"a", "c"}, Prefix: true, Count: 3, Time: day(13)}, }, }, } for _, test := range tests { result, err := s.store.Counters(&test.request) c.Assert(err, IsNil) c.Assert(result, DeepEquals, test.result) } }
func (s *StoreSuite) TestStatsCounterBy(c *C) { incs := []struct { key []string day int }{ {[]string{"a"}, 1}, {[]string{"a"}, 1}, {[]string{"b"}, 1}, {[]string{"a", "b"}, 1}, {[]string{"a", "c"}, 1}, {[]string{"a"}, 3}, {[]string{"a", "b"}, 3}, {[]string{"b"}, 9}, {[]string{"b"}, 9}, {[]string{"a", "c", "d"}, 9}, {[]string{"a", "c", "e"}, 9}, {[]string{"a", "c", "f"}, 9}, } day := func(i int) time.Time { return time.Date(2012, time.May, i, 0, 0, 0, 0, time.UTC) } server, _ := s.prepareServer(c) counters := s.Session.DB("juju").C("stat.counters") for i, inc := range incs { err := s.store.IncCounter(inc.key) c.Assert(err, IsNil) // Hack time so counters are assigned to 2012-05-<day> filter := bson.M{"t": bson.M{"$gt": store.TimeToStamp(time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC))}} stamp := store.TimeToStamp(day(inc.day)) stamp += int32(i) * 60 // Make every entry unique. err = counters.Update(filter, bson.D{{"$set", bson.D{{"t", stamp}}}}) c.Check(err, IsNil) } tests := []struct { request store.CounterRequest format string result string }{ { store.CounterRequest{ Key: []string{"a"}, Prefix: false, List: false, By: store.ByDay, }, "", "2012-05-01 2\n2012-05-03 1\n", }, { store.CounterRequest{ Key: []string{"a"}, Prefix: false, List: false, By: store.ByDay, }, "csv", "2012-05-01,2\n2012-05-03,1\n", }, { store.CounterRequest{ Key: []string{"a"}, Prefix: false, List: false, By: store.ByDay, }, "json", `[["2012-05-01",2],["2012-05-03",1]]`, }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: false, By: store.ByDay, }, "", "2012-05-01 2\n2012-05-03 1\n2012-05-09 3\n", }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: false, By: store.ByDay, Start: time.Date(2012, 5, 2, 0, 0, 0, 0, time.UTC), }, "", "2012-05-03 1\n2012-05-09 3\n", }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: false, By: store.ByDay, Stop: time.Date(2012, 5, 4, 0, 0, 0, 0, time.UTC), }, "", "2012-05-01 2\n2012-05-03 1\n", }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: false, By: store.ByDay, Start: time.Date(2012, 5, 3, 0, 0, 0, 0, time.UTC), Stop: time.Date(2012, 5, 3, 0, 0, 0, 0, time.UTC), }, "", "2012-05-03 1\n", }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: true, By: store.ByDay, }, "", "a:b 2012-05-01 1\na:c 2012-05-01 1\na:b 2012-05-03 1\na:c:* 2012-05-09 3\n", }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: false, By: store.ByWeek, }, "", "2012-05-06 3\n2012-05-13 3\n", }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: true, By: store.ByWeek, }, "", "a:b 2012-05-06 2\na:c 2012-05-06 1\na:c:* 2012-05-13 3\n", }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: true, By: store.ByWeek, }, "csv", "a:b,2012-05-06,2\na:c,2012-05-06,1\na:c:*,2012-05-13,3\n", }, { store.CounterRequest{ Key: []string{"a"}, Prefix: true, List: true, By: store.ByWeek, }, "json", `[["a:b","2012-05-06",2],["a:c","2012-05-06",1],["a:c:*","2012-05-13",3]]`, }, } for _, test := range tests { path := "/stats/counter/" + strings.Join(test.request.Key, ":") if test.request.Prefix { path += ":*" } req, err := http.NewRequest("GET", path, nil) req.Form = url.Values{} c.Assert(err, IsNil) if test.request.List { req.Form.Set("list", "1") } if test.format != "" { req.Form.Set("format", test.format) } if !test.request.Start.IsZero() { req.Form.Set("start", test.request.Start.Format("2006-01-02")) } if !test.request.Stop.IsZero() { req.Form.Set("stop", test.request.Stop.Format("2006-01-02")) } switch test.request.By { case store.ByDay: req.Form.Set("by", "day") case store.ByWeek: req.Form.Set("by", "week") } rec := httptest.NewRecorder() server.ServeHTTP(rec, req) data, err := ioutil.ReadAll(rec.Body) c.Assert(string(data), Equals, test.result) c.Assert(rec.Header().Get("Content-Type"), Equals, "text/plain") c.Assert(rec.Header().Get("Content-Length"), Equals, strconv.Itoa(len(test.result))) } }