<commit_before>package main

import (
	"errors"
	"fmt"
	"github.com/grafana/metrictank/stacktest/graphite"
	"github.com/grafana/metrictank/stats"
	log "github.com/sirupsen/logrus"
	"math"
	"net/http"
	"regexp"
	"strconv"
	"time"
)

type seriesStats struct {
	lastTs uint32
	//the partition currently being checked
	partition int32
	//the number of nans present in the time series
	nans int32
	//the sum of abs(value - ts) across the time series
	deltaSum float64
	//the number of timestamps where value != ts
	numNonMatching int32

	//tracks the last seen non-NaN time stamp (useful for lag
	lastSeen uint32
}

func monitor() {
	for tick := range time.NewTicker(queryInterval).C {

		query := graphite.ExecuteRenderQuery(buildRequest(tick))
		if query.HTTPErr != nil {
			stats.NewCounter32("parrot.monitoring.error;error=http").Inc()
		}
		if query.DecodeErr != nil {
			stats.NewCounter32("parrot.monitoring.error;error=decode").Inc()
		}

		for _, s := range query.Decoded {
			log.Infof("%d - %d", s.Datapoints[0].Ts, s.Datapoints[len(s.Datapoints)-1].Ts)

			partition, err := extractPartition(s.Target)
			if err != nil {
				log.Debug("unable to extract partition", err)
				stats.NewCounter32("parrot.monitoring.error;error=parsePartition").Inc()
				continue
			}
			serStats := seriesStats{}
			serStats.lastTs = s.Datapoints[len(s.Datapoints)-1].Ts

			for _, dp := range s.Datapoints {

				if math.IsNaN(dp.Val) {
					serStats.nans += 1
					continue
				}
				if diff := dp.Val - float64(dp.Ts); diff != 0 {
					log.Debugf("partition=%d dp.Val=%f dp.Ts=%d diff=%f", partition, dp.Val, dp.Ts, diff)
					serStats.lastSeen = dp.Ts
					serStats.deltaSum += diff
					serStats.numNonMatching += 1
				}
			}<commit_msg>declare counters once<commit_after>package main

import (
	"errors"
	"fmt"
	"github.com/grafana/metrictank/stacktest/graphite"
	"github.com/grafana/metrictank/stats"
	log "github.com/sirupsen/logrus"
	"math"
	"net/http"
	"regexp"
	"strconv"
	"time"
)

var (
	httpError   = stats.NewCounter32("parrot.monitoring.error;error=http")
	decodeError = stats.NewCounter32("parrot.monitoring.error;error=decode")
)

type seriesStats struct {
	lastTs uint32
	//the partition currently being checked
	partition int32
	//the number of nans present in the time series
	nans int32
	//the sum of abs(value - ts) across the time series
	deltaSum float64
	//the number of timestamps where value != ts
	numNonMatching int32

	//tracks the last seen non-NaN time stamp (useful for lag
	lastSeen uint32
}

func monitor() {
	for tick := range time.NewTicker(queryInterval).C {

		query := graphite.ExecuteRenderQuery(buildRequest(tick))
		if query.HTTPErr != nil {
			httpError.Inc()
		}
		if query.DecodeErr != nil {
			decodeError.Inc()
		}

		for _, s := range query.Decoded {
			log.Infof("%d - %d", s.Datapoints[0].Ts, s.Datapoints[len(s.Datapoints)-1].Ts)

			partition, err := extractPartition(s.Target)
			if err != nil {
				log.Debug("unable to extract partition", err)
				stats.NewCounter32("parrot.monitoring.error;error=parsePartition").Inc()
				continue
			}
			serStats := seriesStats{}
			serStats.lastTs = s.Datapoints[len(s.Datapoints)-1].Ts

			for _, dp := range s.Datapoints {

				if math.IsNaN(dp.Val) {
					serStats.nans += 1
					continue
				}
				if diff := dp.Val - float64(dp.Ts); diff != 0 {
					log.Debugf("partition=%d dp.Val=%f dp.Ts=%d diff=%f", partition, dp.Val, dp.Ts, diff)
					serStats.lastSeen = dp.Ts
					serStats.deltaSum += diff
					serStats.numNonMatching += 1
				}
			}
{'fraction_non_alphanumeric': 0.13359750919898103, 'fraction_numerical': 0.01471836965751486, 'mean_word_length': 3.5019108280254776, 'pattern_counts': {'":': 0, '<': 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': '2325335', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1268, 'n_words': 285}