// AvgRate returns the average of all the message rates in the SampleGroup
972173
func (sg *SampleGroup) AvgRate() int64 {
	sum := uint64(0)
	for _, s := range sg.Samples {
		sum += uint64(s.Rate())
	}
	return int64(sum / uint64(len(sg.Samples)))
}
934151
func (c *countDistinctAcc) Accumulate(v interface{}) (interface{}, error) {
	vs := fmt.Sprintf("%v", v)
	c.state[vs]++
	return int64(len(c.state)), nil
}
1287116
func (s *SelectStatement) GroupByInterval() (time.Duration, error) {
	// return if we've already pulled it out
	if s.groupByInterval != 0 {
		return s.groupByInterval, nil
	}

	// Ignore if there are no dimensions.
	if len(s.Dimensions) == 0 {
		return 0, nil
	}

	for _, d := range s.Dimensions {
		if call, ok := d.Expr.(*Call); ok && call.Name == "time" {
			// Make sure there is exactly one argument.
			if got := len(call.Args); got < 1 || got > 2 {
				return 0, errors.New("time dimension expected 1 or 2 arguments")
			}

			// Ensure the argument is a duration.
			lit, ok := call.Args[0].(*DurationLiteral)
			if !ok {
				return 0, errors.New("time dimension must have duration argument")
			}
			s.groupByInterval = lit.Val
			return lit.Val, nil
		}
	}
	return 0, nil
}
149737
func (data ChartFloats) Avg(s, e int) float64 {
	if e <= s {
		return 0
	}
	var sum float64
	for _, v := range data[s:e] {
		sum += v
	}
	return sum / float64(e-s)
}
635769
func (cs *ConnStats) AvgRPCBytes() (send float64, recv float64) {
	return float64(cs.BytesWritten) / float64(cs.RPCCalls), float64(cs.BytesRead) / float64(cs.RPCCalls)
}
504508
func NewAggregate(errs ...error) error {
	// filter out possible nil values
	var nonNils []error
	for _, err := range errs {
		if err != nil {
			nonNils = append(nonNils, err)
		}
	}
	if len(nonNils) == 0 {
		return nil
	}
	return wrapWithDepth(aggregate(nonNils), 2)
}
290514
func (g *Gormigrate) Migrate() error {
	if !g.hasMigrations() {
		return ErrNoMigrationDefined
	}
	var targetMigrationID string
	if len(g.migrations) > 0 {
		targetMigrationID = g.migrations[len(g.migrations)-1].ID
	}
	return g.migrate(targetMigrationID)
}
372832
func (n *NetworkServerAPI) UpdateMulticastGroup(ctx context.Context, req *ns.UpdateMulticastGroupRequest) (*empty.Empty, error) {
	if req.MulticastGroup == nil {
		return nil, grpc.Errorf(codes.InvalidArgument, "multicast_group must not be nil")
	}

	var mgID uuid.UUID
	copy(mgID[:], req.MulticastGroup.Id)

	err := storage.Transaction(func(tx sqlx.Ext) error {
		mg, err := storage.GetMulticastGroup(tx, mgID, true)
		if err != nil {
			return errToRPCError(err)
		}

		copy(mg.MCAddr[:], req.MulticastGroup.McAddr)
		copy(mg.MCNwkSKey[:], req.MulticastGroup.McNwkSKey)
		copy(mg.ServiceProfileID[:], req.MulticastGroup.ServiceProfileId)
		copy(mg.RoutingProfileID[:], req.MulticastGroup.RoutingProfileId)
		mg.FCnt = req.MulticastGroup.FCnt
		mg.DR = int(req.MulticastGroup.Dr)
		mg.Frequency = int(req.MulticastGroup.Frequency)
		mg.PingSlotPeriod = int(req.MulticastGroup.PingSlotPeriod)

		switch req.MulticastGroup.GroupType {
		case ns.MulticastGroupType_CLASS_B:
			mg.GroupType = storage.MulticastGroupB
		case ns.MulticastGroupType_CLASS_C:
			mg.GroupType = storage.MulticastGroupC
		default:
			return grpc.Errorf(codes.InvalidArgument, "invalid group_type")
		}

		if err := storage.UpdateMulticastGroup(tx, &mg); err != nil {
			return errToRPCError(err)
		}

		return nil
	})
	if err != nil {
		return nil, err
	}

	return &empty.Empty{}, nil
}
431621
func (format *CollectdToInflux08) ApplyFormatter(msg *core.Message) error {
	contentData := format.GetAppliedContent(msg)

	collectdData, err := parseCollectdPacket(contentData)
	if err != nil {
		format.Logger.Error("Collectd parser error: ", err)
		return err
	}

	// Manually convert to JSON lines
	influxData := tio.NewByteStream(len(contentData))
	name := format.createMetricName(collectdData.Plugin,
		collectdData.PluginInstance,
		collectdData.PluginType,
		collectdData.TypeInstance,
		collectdData.Host)

	for _, value := range collectdData.Values {
		fmt.Fprintf(&influxData, `{"name": "%s", "columns": ["time", "value"], "points":[[%d, %f]]},`, name, int32(collectdData.Time), value)
	}

	format.SetAppliedContent(msg, influxData.Bytes())
	return nil
}
198273
func ConvertSampleRate(srcSamples []int32, srcSampleRate, dstSampleRate int) (dstSamples []int32, err error) {
	// Do nothing
	if srcSampleRate == dstSampleRate {
		dstSamples = srcSamples
		return
	}

	// For now we don't handle data loss
	if srcSampleRate < dstSampleRate {
		err = errors.New("astiaudio: src sample rate < dst sample rate")
		return
	}

	// Get ratio
	ratio := float64(srcSampleRate) / float64(dstSampleRate)

	// Loop
	var idx float64
	for {
		// Take floor of index
		floor := int(math.Floor(idx))

		// Out of range
		if floor >= len(srcSamples) {
			break
		}

		// Append sample
		dstSamples = append(dstSamples, srcSamples[floor])

		// Increment index
		idx += ratio
	}
	return
}
985847
func (status *NagiosStatus) Aggregate(otherStatuses []*NagiosStatus) {
	for _, s := range otherStatuses {
		if status.Value < s.Value {
			status.Value = s.Value
		}

		status.Message += " - " + s.Message
	}
}
1280457
func (orig *SampleJSON) Convert() (*Sample, error) {
	ret := &Sample{}

	b := NewSampleJSONBuilder().AddAll()
	for _, info := range b._properties {
		if err := info.Decoder(orig, ret); err != nil {
			return nil, err
		}
	}

	return ret, nil
}
603066
func (data ChartUints) Avg(s, e int) uint64 {
	if e <= s {
		return 0
	}
	var sum uint64
	for _, v := range data[s:e] {
		sum += v
	}
	return sum / uint64(e-s)
}
1121830
func (s *DB) AutoMigrate(values ...interface{}) *DB {
	db := s.Unscoped()
	for _, value := range values {
		db = db.NewScope(value).autoMigrate().db
	}
	return db
}
579334
func NewSampleGroup() *SampleGroup {
	s := new(SampleGroup)
	s.Samples = make([]*Sample, 0)
	return s
}
627169
func (a *AvgRateCounter) Rate() float64 {
	hits, value := a.hits.Rate(), a.counter.Rate()

	if hits == 0 {
		return 0 // Avoid division by zero
	}

	return float64(value) / float64(hits)
}
80556
func MemProfileRate(rate int) func(*Profile) {
	return func(p *Profile) {
		p.memProfileRate = rate
		p.mode = memMode
	}
}