// Save metric and index into db. func (d *Detector) save(m *models.Metric, idx *models.Index) error { // Save index. if err := d.db.Index.Put(idx); err != nil { return err } // Save metric. m.LinkTo(idx) // Important if err := d.db.Metric.Put(m); err != nil { return err } return nil }
// analyze given metric with 3sigma, returns the new index. // Steps: // 1. Get index. // 2. Get history values. // 3. Do 3sigma calculation. // 4. Move the index next. func (d *Detector) analyze(idx *models.Index, m *models.Metric, rules []*models.Rule) *models.Index { fz := idx != nil && d.shouldFill0(m, rules) if idx != nil { m.LinkTo(idx) } vals, err := d.values(m, fz) if err != nil { return nil } d.div3Sigma(m, vals) return d.nextIdx(idx, m, d.pickTrendingFactor(rules)) }