コード例 #1
0
ファイル: static.go プロジェクト: cswank/quimby
func getOnTime(name string, period time.Duration, g *quimby.Gadget) string {
	e := time.Now()
	s := e.Add(period)
	data, err := g.GetDataPoints(name, s, e, 0, true)
	if err != nil {
		return "0hr"
	}

	var d time.Duration
	var prev quimby.DataPoint
	end := len(data) - 1
	for i, p := range data {
		if p.Value > 0.5 && prev.Value < 0.5 {
			prev = p
		} else if p.Value < 0.5 && prev.Value > 0.5 {
			d += p.Time.Sub(prev.Time)
			prev = p
		} else if i == end && p.Value > 0.5 && prev.Value > 0.5 {
			d += p.Time.Sub(prev.Time)
		}
	}
	return parseDuration(d)
}
コード例 #2
0
ファイル: gadget_test.go プロジェクト: cswank/quimby
	It("registers with a gogadgets instance", func() {
		h, err := g.Register(ts.URL, "fakecookie")
		Expect(err).To(BeNil())
		Expect(h).To(Equal(ts.URL))
		Expect(len(clients)).To(Equal(1))
		c := clients[0]
		Expect(c["address"]).To(Equal(ts.URL))
	})

	It("gets datapoints", func() {
		ts := time.Now()
		g.SaveDataPoint("kitchen temperature", quimby.DataPoint{Time: ts, Value: 23.2})
		time.Sleep(10 * time.Millisecond)
		ts2 := time.Now()
		g.SaveDataPoint("kitchen temperature", quimby.DataPoint{Time: ts2, Value: 23.4})
		points, err := g.GetDataPoints("kitchen temperature", ts, ts2, 0, false)
		Expect(err).To(BeNil())
		Expect(len(points)).To(Equal(2))

		p1 := points[0]
		Expect(p1.Time.Format(time.RFC3339)).To(Equal(ts.Format(time.RFC3339)))
		Expect(p1.Value).To(Equal(23.2))

		p2 := points[1]
		Expect(p2.Time.Format(time.RFC3339)).To(Equal(ts2.Format(time.RFC3339)))
		Expect(p2.Value).To(Equal(23.4))
	})

	It("gets binary datapoints", func() {
		ts := time.Now()
		g.SaveDataPoint("kitchen temperature", quimby.DataPoint{Time: ts, Value: 0.0})