<commit_before>package expr

import (
	"fmt"
	"strings"

	"github.com/grafana/metrictank/api/models"
	"github.com/grafana/metrictank/schema"
)

type FuncCountSeries struct {
	in []GraphiteFunc
}

func NewCountSeries() GraphiteFunc {
	return &FuncCountSeries{}
}

func (s *FuncCountSeries) Signature() ([]Arg, []Arg) {
	return []Arg{
		ArgSeriesLists{val: &s.in},
	}, []Arg{ArgSeries{}}
}

func (s *FuncCountSeries) Context(context Context) Context {
	return context
}

func (s *FuncCountSeries) Exec(cache map[Req][]models.Series) ([]models.Series, error) {
	series, queryPatts, err := consumeFuncs(cache, s.in)
	if err != nil {
		return nil, err
	}

	if len(series) == 0 {
		return series, nil
	}

	cons, queryCons := summarizeCons(series)
	name := fmt.Sprintf("countSeries(%s)", strings.Join(queryPatts, ","))
	out := pointSlicePool.Get().([]schema.Point)

	for _, p := range series[0].Datapoints {
		p.Val = float64(len(series))
		out = append(out, p)
	}

	var meta models.SeriesMeta
	for _, s := range series {
		meta = meta.Merge(s.Meta)
	}

	output := series[0]
	output.Target = name
	output.QueryPatt = name
	output.Tags = map[string]string{"name": name}
	output.Datapoints = out
	output.Consolidator = cons
	output.QueryCons = queryCons
	output.Meta = meta
	cache[Req{}] = append(cache[Req{}], output)

	return []models.Series{output}, nil
}
<commit_msg>slight countSeries perf tweak<commit_after>package expr

import (
	"fmt"
	"strings"

	"github.com/grafana/metrictank/api/models"
	"github.com/grafana/metrictank/schema"
)

type FuncCountSeries struct {
	in []GraphiteFunc
}

func NewCountSeries() GraphiteFunc {
	return &FuncCountSeries{}
}

func (s *FuncCountSeries) Signature() ([]Arg, []Arg) {
	return []Arg{
		ArgSeriesLists{val: &s.in},
	}, []Arg{ArgSeries{}}
}

func (s *FuncCountSeries) Context(context Context) Context {
	return context
}

func (s *FuncCountSeries) Exec(cache map[Req][]models.Series) ([]models.Series, error) {
	series, queryPatts, err := consumeFuncs(cache, s.in)
	if err != nil {
		return nil, err
	}

	if len(series) == 0 {
		return series, nil
	}

	cons, queryCons := summarizeCons(series)
	name := fmt.Sprintf("countSeries(%s)", strings.Join(queryPatts, ","))
	out := pointSlicePool.Get().([]schema.Point)

	l := float64(len(series))
	for _, p := range series[0].Datapoints {
		p.Val = l
		out = append(out, p)
	}

	var meta models.SeriesMeta
	for _, s := range series {
		meta = meta.Merge(s.Meta)
	}

	output := series[0]
	output.Target = name
	output.QueryPatt = name
	output.Tags = map[string]string{"name": name}
	output.Datapoints = out
	output.Consolidator = cons
	output.QueryCons = queryCons
	output.Meta = meta
	cache[Req{}] = append(cache[Req{}], output)

	return []models.Series{output}, nil
}

{'fraction_non_alphanumeric': 0.1572052401746725, 'fraction_numerical': 0.00363901018922853, 'mean_word_length': 4.610204081632653, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4298771', 'n_tokens_mistral': 1032, 'n_tokens_neox': 992, 'n_words': 223}