// Series returns the series for the given reference.
969002
func (h *headIndexReader) Series(ref uint64, lbls *labels.Labels, chks *[]chunks.Meta) error {
	s := h.head.series.getByID(ref)

	if s == nil {
		h.head.metrics.seriesNotFound.Inc()
		return ErrNotFound
	}
	*lbls = append((*lbls)[:0], s.lset...)

	s.Lock()
	defer s.Unlock()

	*chks = (*chks)[:0]

	for i, c := range s.chunks {
		// Do not expose chunks that are outside of the specified range.
		if !c.OverlapsClosedInterval(h.mint, h.maxt) {
			continue
		}
		*chks = append(*chks, chunks.Meta{
			MinTime: c.minTime,
			MaxTime: c.maxTime,
			Ref:     packChunkID(s.ref, uint64(s.chunkID(i))),
		})
	}

	return nil
}
409508
func CloneSeriesSegments(a []*SeriesSegment) []*SeriesSegment {
	other := make([]*SeriesSegment, len(a))
	for i := range a {
		other[i] = a[i].Clone()
	}
	return other
}
1350789
func (s *SeriesCollection) Copy(dst, src int) {
	if dst == src {
		return
	}
	udst, usrc := uint(dst), uint(src)
	if n := uint(len(s.Points)); udst < n && usrc < n {
		s.Points[udst] = s.Points[usrc]
	}
	if n := uint(len(s.Keys)); udst < n && usrc < n {
		s.Keys[udst] = s.Keys[usrc]
	}
	if n := uint(len(s.SeriesKeys)); udst < n && usrc < n {
		s.SeriesKeys[udst] = s.SeriesKeys[usrc]
	}
	if n := uint(len(s.Names)); udst < n && usrc < n {
		s.Names[udst] = s.Names[usrc]
	}
	if n := uint(len(s.Tags)); udst < n && usrc < n {
		s.Tags[udst] = s.Tags[usrc]
	}
	if n := uint(len(s.Types)); udst < n && usrc < n {
		s.Types[udst] = s.Types[usrc]
	}
	if n := uint(len(s.SeriesIDs)); udst < n && usrc < n {
		s.SeriesIDs[udst] = s.SeriesIDs[usrc]
	}
}
1201302
func sumSeries(series []models.Series, cache map[Req][]models.Series) models.Series {
	if len(series) == 1 {
		return series[0]
	}
	out := pointSlicePool.Get().([]schema.Point)
	crossSeriesSum(series, &out)
	var queryPatts []string

Loop:
	for _, v := range series {
		// avoid duplicates
		for _, qp := range queryPatts {
			if qp == v.QueryPatt {
				continue Loop
			}
		}
		queryPatts = append(queryPatts, v.QueryPatt)
	}
	name := fmt.Sprintf("sumSeries(%s)", strings.Join(queryPatts, ","))
	cons, queryCons := summarizeCons(series)
	sum := models.Series{
		Target:       name,
		QueryPatt:    name,
		Datapoints:   out,
		Interval:     series[0].Interval,
		Consolidator: cons,
		QueryCons:    queryCons,
		Tags:         map[string]string{"name": name},
	}
	cache[Req{}] = append(cache[Req{}], sum)
	return sum
}
566466
func NewSeriesCollection(points []models.Point) *SeriesCollection {
	out := &SeriesCollection{
		Points: append([]models.Point(nil), points...),
		Keys:   make([][]byte, 0, len(points)),
		Names:  make([][]byte, 0, len(points)),
		Tags:   make([]models.Tags, 0, len(points)),
		Types:  make([]models.FieldType, 0, len(points)),
	}

	for _, pt := range points {
		out.Keys = append(out.Keys, pt.Key())
		out.Names = append(out.Names, pt.Name())
		out.Tags = append(out.Tags, pt.Tags())

		fi := pt.FieldIterator()
		fi.Next()
		out.Types = append(out.Types, fi.Type())
	}

	return out
}
735357
func (itr *rawSeriesIDIterator) Next() (tsdb.SeriesIDElem, error) {
	if len(itr.data) == 0 {
		return tsdb.SeriesIDElem{}, nil
	}

	delta, n, err := uvarint(itr.data)
	if err != nil {
		return tsdb.SeriesIDElem{}, err
	}
	itr.data = itr.data[n:]

	seriesID := itr.prev + uint64(delta)
	itr.prev = seriesID
	return tsdb.SeriesIDElem{SeriesID: tsdb.NewSeriesID(seriesID)}, nil
}
14425
func (c *Config) DefaultSeries() (string, bool) {
	s, ok := c.defined["default-series"]
	if !ok {
		return "", false
	}
	switch s := s.(type) {
	case string:
		return s, s != ""
	default:
		logger.Errorf("invalid default-series: %q", s)
		return "", false
	}
}
1007628
func (c *Interface) Ref() error {
	var err C.virError
	ret := C.virInterfaceRefWrapper(c.ptr, &err)
	if ret == -1 {
		return makeError(&err)
	}
	return nil
}
297766
func (s *seriesIteratorAdapter) Next() (tsdb.SeriesElem, error) {
	if s.itr == nil {
		return nil, nil
	}

	row, err := s.itr.Next()
	if err != nil {
		return nil, err
	}

	if row == nil {
		return nil, nil
	}

	s.ea.name = row.Name
	s.ea.tags = row.Tags
	return s.elem, nil
}
317065
func (w *SegmentWAL) LogSeries(series []RefSeries) error {
	buf := w.getBuffer()

	flag := w.encodeSeries(buf, series)

	w.mtx.Lock()
	defer w.mtx.Unlock()

	err := w.write(WALEntrySeries, flag, buf.Get())

	w.putBuffer(buf)

	if err != nil {
		return errors.Wrap(err, "log series")
	}

	tf := w.head()

	for _, s := range series {
		if tf.minSeries > s.Ref {
			tf.minSeries = s.Ref
		}
	}
	return nil
}
1150728
func (t *partitionTracker) SetSeries(n uint64) {
	if !t.enabled {
		return
	}

	labels := t.Labels()
	t.metrics.Series.With(labels).Set(float64(n))
}
894882
func LogarithmicRegression(s Series) (regressions Series, err error) {

	if len(s) == 0 {
		return nil, EmptyInputErr
	}

	var sum [4]float64

	i := 0
	for ; i < len(s); i++ {
		sum[0] += math.Log(s[i].X)
		sum[1] += s[i].Y * math.Log(s[i].X)
		sum[2] += s[i].Y
		sum[3] += math.Pow(math.Log(s[i].X), 2)
	}

	f := float64(i)
	a := (f*sum[1] - sum[2]*sum[0]) / (f*sum[3] - sum[0]*sum[0])
	b := (sum[2] - a*sum[0]) / f

	for j := 0; j < len(s); j++ {
		regressions = append(regressions, Coordinate{
			X: s[j].X,
			Y: b + a*math.Log(s[j].X),
		})
	}

	return regressions, nil
}
491307
func Ref(ref interface{}) interface{} {
	switch v := ref.(type) {
	case NamedResource:
		ref = v.Name
	}
	return map[string]interface{}{"Ref": ref}
}
1286872
func (s Seq) Each(mapfn func(int, float64)) {
	for i := 0; i < s.Len(); i++ {
		mapfn(i, s.GetValue(i))
	}
}
158385
func (cur *seriesCursor) Next() (*SeriesCursorRow, error) {
	for {
		// Read series keys for next measurement if no more keys remaining.
		// Exit if there are no measurements remaining.
		if cur.ofs == len(cur.keys) {
			m, err := cur.mitr.Next()
			if err != nil {
				return nil, err
			} else if m == nil {
				return nil, nil
			}

			if err := cur.readSeriesKeys(m); err != nil {
				return nil, err
			}
			continue
		}

		cur.row.Name, cur.row.Tags = tsdb.ParseSeriesKey(cur.keys[cur.ofs])
		cur.ofs++
		return &cur.row, nil
	}
}
108207
func ExponentialRegression(s Series) (regressions Series, err error) {

	if len(s) == 0 {
		return nil, EmptyInputErr
	}

	var sum [6]float64

	for i := 0; i < len(s); i++ {
		if s[i].Y < 0 {
			return nil, YCoordErr
		}
		sum[0] += s[i].X
		sum[1] += s[i].Y
		sum[2] += s[i].X * s[i].X * s[i].Y
		sum[3] += s[i].Y * math.Log(s[i].Y)
		sum[4] += s[i].X * s[i].Y * math.Log(s[i].Y)
		sum[5] += s[i].X * s[i].Y
	}

	denominator := (sum[1]*sum[2] - sum[5]*sum[5])
	a := math.Pow(math.E, (sum[2]*sum[3]-sum[5]*sum[4])/denominator)
	b := (sum[1]*sum[4] - sum[5]*sum[3]) / denominator

	for j := 0; j < len(s); j++ {
		regressions = append(regressions, Coordinate{
			X: s[j].X,
			Y: a * math.Exp(b*s[j].X),
		})
	}

	return regressions, nil
}
46329
func (c *Client) Sequences() (map[string]int, error) {
	if c.BestAPIVersion() < 2 {
		return nil, errors.NotSupportedf("Sequences on v1 facade")
	}
	var result params.ModelSequencesResult
	err := c.facade.FacadeCall("Sequences", nil, &result)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return result.Sequences, nil
}