<commit_before>package operator

var (
	window []SensorTuple
	count  int
)

func AppendTuple(st SensorTuple) (float64, bool) {
	var tmp SensorTuple
	for idx, val := range window {
		if idx == 0 {
			window[idx] = st
		} else {
			window[idx] = tmp
		}
		tmp = val
	}

	count++
	if count == 2 {
		count = 0
		return (window[0].Data + window[1].Data) / 2.0, true
	}
	return 0.0, false
}

func init() {
	window = make([]SensorTuple, 4)
}<commit_msg>Added configuration of window capacity and trigger values from environment variables<commit_after>package operator

import (
	"os"
	"strconv"

	"github.com/bahadley/esp/log"
)

const (
	defaultCapacity   = 4
	defaultTriggerQty = 2

	envWinCap  = "ESP_WINDOW_CAPACITY"
	envWinTrig = "ESP_WINDOW_TRIGGER"
)

var (
	window    []SensorTuple
	count     int
	trigger   int
	avgFactor float64
)

func AppendTuple(st SensorTuple) (float64, bool) {
	var tmp SensorTuple
	for idx, val := range window {
		if idx == 0 {
			window[idx] = st
		} else {
			window[idx] = tmp
		}
		tmp = val
	}

	count++
	if count == trigger {
		count = 0
		return (window[0].Data + window[1].Data) / avgFactor, true
	}
	return 0.0, false
}

func init() {
	var err error
	var cap int

	envVal := os.Getenv(envWinCap)
	if len(envVal) == 0 {
		cap = defaultCapacity
	} else {
		cap, err = strconv.Atoi(envVal)
		if err != nil {
			log.Error.Fatalf("Invalid environment variable: %s", envWinCap)
		}
	}

	envVal = os.Getenv(envWinTrig)
	if len(envVal) == 0 {
		trigger = defaultTriggerQty
	} else {
		trigger, err = strconv.Atoi(envVal)
		if err != nil {
			log.Error.Fatalf("Invalid environment variable: %s", envWinTrig)
		}
	}

	window = make([]SensorTuple, cap)
	avgFactor = float64(trigger)
}
{'fraction_non_alphanumeric': 0.12463428905792862, 'fraction_numerical': 0.016383850204798128, 'mean_word_length': 3.3181818181818183, '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': '1955768', 'n_tokens_mistral': 703, 'n_tokens_neox': 651, 'n_words': 171}