<commit_before>package watch

import (
	"bufio"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
	"time"

	log "github.com/cihub/seelog"
	CONF "github.com/grindlemire/GoSentry/c"
	L "github.com/vrecan/life"
)

// Watch watches audit files and alerts if there are errors
type Watch struct {
	*L.Life
	Files     []string
	Ticker    *time.Ticker
	OutputDir string
	Year      int
	Month     int
	Day       int
	Regexs    []*regexp.Regexp
	BadLines  []string
}

// NewWatch creates a new watch object
func NewWatch(c CONF.Conf) (newWatch *Watch, err error) {
	regexs := []*regexp.Regexp{}
	for _, rS := range c.Regexs {
		r, err := regexp.Compile(rS)
		if err != nil {
			return nil, log.Error("Regex Does not compile: ", err)
		}
		regexs = append(regexs, r)
	}

	duration, err := parseDuration(c.ScanEvery)
	if err != nil {
		return nil, log.Error("Error parsing scanEvery duration: ", err)
	}

	newWatch = &Watch{
		Life:      L.NewLife(),
		Files:     c.Files,
		Ticker:    time.NewTicker(duration),
		Year:      c.Year,
		Month:     c.Month,
		Day:       c.Day,
		OutputDir: c.OutputDir,
		Regexs:    regexs,
		BadLines:  c.Flagged,
	}

	newWatch.SetRun(newWatch.run)
	return newWatch, err

}

// run starts watch
func (w Watch) run() {
	log.Info("Watcher running")

	for {
		select {
		case <-w.Ticker.C:
			w.ReadFiles()
		case <-w.Life.Done:
			return
		}
	}
}

// ReadFiles reads all the specified files and parses them<commit_msg>change to run immediately then at chron<commit_after>package watch

import (
	"bufio"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
	"time"

	log "github.com/cihub/seelog"
	CONF "github.com/grindlemire/GoSentry/c"
	L "github.com/vrecan/life"
)

// Watch watches audit files and alerts if there are errors
type Watch struct {
	*L.Life
	Files     []string
	Timer     *time.Timer
	OutputDir string
	Year      int
	Month     int
	Day       int
	Regexs    []*regexp.Regexp
	BadLines  []string
	Duration  time.Duration
}

// NewWatch creates a new watch object
func NewWatch(c CONF.Conf) (newWatch *Watch, err error) {
	regexs := []*regexp.Regexp{}
	for _, rS := range c.Regexs {
		r, err := regexp.Compile(rS)
		if err != nil {
			return nil, log.Error("Regex Does not compile: ", err)
		}
		regexs = append(regexs, r)
	}

	duration, err := parseDuration(c.ScanEvery)
	if err != nil {
		return nil, log.Error("Error parsing scanEvery duration: ", err)
	}

	newWatch = &Watch{
		Life:      L.NewLife(),
		Files:     c.Files,
		Timer:     time.NewTimer(0 * time.Second),
		Year:      c.Year,
		Month:     c.Month,
		Day:       c.Day,
		OutputDir: c.OutputDir,
		Regexs:    regexs,
		BadLines:  c.Flagged,
		Duration:  duration,
	}

	newWatch.SetRun(newWatch.run)
	return newWatch, err

}

// run starts watch
func (w Watch) run() {
	log.Info("Watcher running")

	for {
		select {
		case <-w.Timer.C:
			w.ReadFiles()
			w.Timer.Reset(w.Duration)
		case <-w.Life.Done:
			return
		}
	}
}

// ReadFiles reads all the specified files and parses them
{'fraction_non_alphanumeric': 0.1291401806624289, 'fraction_numerical': 0.00033456005352960856, 'mean_word_length': 2.8983050847457625, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2588478', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1133, 'n_words': 299}