package correlator

import (
	"time"

	log "github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"iwarma.ru/console/correlator/config"
	"iwarma.ru/console/correlator/rules"
)

func Correlator(store *rules.RuleStore, stop chan interface{}) {
	cl := log.WithField("part", "Correlator")
	tick := time.Tick(time.Second)
	errs := make(chan error, viper.GetInt(config.Threads)+5)

	cl.Info("Starting Correlator")

	for {
		select {
		case <-tick:
			cl.Debug("Iteration")
			errs <- store.RunRulesSync()
		case err := <-errs:
			if err != nil {
				cl.Errorf("Got error: %v", err)
			}
		case <-stop:
			cl.Info("Stopping Correlator")
			return
		}
	}
}
