<commit_before>package radar

import (
	"os"
	"sync"
	"time"

	"github.com/concourse/atc"
	"github.com/concourse/atc/db"
	"github.com/tedsuo/ifrit"

	"github.com/pivotal-golang/lager"
)

type VersionDB interface {
	SaveVersionedResource(db.VersionedResource) error
	GetLatestVersionedResource(string) (db.VersionedResource, error)
}

type ResourceChecker interface {
	CheckResource(atc.ResourceConfig, db.Version) ([]db.Version, error)
}

type ConfigDB interface {
	GetConfig() (atc.Config, error)
}

type Radar struct {
	logger lager.Logger

	tracker  VersionDB
	interval time.Duration

	locker Locker

	configDB ConfigDB

	failing  map[string]bool
	checking map[string]bool
	statusL  *sync.Mutex
}

func NewRadar(
	logger lager.Logger,
	tracker VersionDB,
	interval time.Duration,
	locker Locker,
	configDB ConfigDB,
) *Radar {
	return &Radar{
		logger: logger,

		tracker:  tracker,
		interval: interval,

		locker: locker,

		configDB: configDB,

		failing:  make(map[string]bool),
		checking: make(map[string]bool),
		statusL:  new(sync.Mutex),
	}
}

func (radar *Radar) Scan(checker ResourceChecker, resourceName string) ifrit.Runner {
	return ifrit.RunFunc(func(signals <-chan os.Signal, ready chan<- struct{}) error {
		ticker := time.NewTicker(radar.interval)

		close(ready)

		for {
			var resourceCheckingLock db.Lock
			var err error
			select {
			case <-signals:
				return nil

			case <-ticker.C:
				resourceCheckingLock, err = radar.locker.AcquireWriteLockImmediately([]db.NamedLock{db.ResourceCheckingLock(resourceName)})
				if err != nil {
					break
				}

				config, err := radar.configDB.GetConfig()
				if err != nil {
					continue
				}

				resource, found := config.Resources.Lookup(resourceName)
				if !found {
					return nil
				}

				radar.setChecking(resource.Name)

				var from db.Version
				log := radar.logger.Session("radar", lager.Data{
					"type":     resource.Type,
					"resource": resource.Name,
					"from":     from,
				})

				lock, err := radar.locker.AcquireReadLock([]db.NamedLock{db.ResourceLock(resource.Name)})
				if err != nil {
					log.Error("failed-to-acquire-inputs-lock", err, lager.Data{
						"resource_name": resource.Name,
					})
					break
				}
				if vr, err := radar.tracker.GetLatestVersionedResource(resource.Name); err == nil {
					from = vr.Version
				}
				lock.Release()

				log.Debug("check")

				newVersions, err := checker.CheckResource(resource, from)

				radar.setFailing(resource.Name, err != nil)

				if err != nil {
					log.Error("failed-to-check", err)
					break
				}

				if len(newVersions) == 0 {
					break
				}

				log.Info("versions-found", lager.Data{
					"versions": newVersions,
					"total":    len(newVersions),
				})

				lock, err = radar.locker.AcquireWriteLock([]db.NamedLock{db.ResourceLock(resource.Name)})
				if err != nil {
					log.Error("failed-to-acquire-inputs-lock", err, lager.Data{
						"resource_name": resource.Name,
					})
					break
				}

				for _, version := range newVersions {
					err = radar.tracker.SaveVersionedResource(db.VersionedResource{<commit_msg>fix radar logging<commit_after>package radar

import (
	"os"
	"sync"
	"time"

	"github.com/concourse/atc"
	"github.com/concourse/atc/db"
	"github.com/tedsuo/ifrit"

	"github.com/pivotal-golang/lager"
)

type VersionDB interface {
	SaveVersionedResource(db.VersionedResource) error
	GetLatestVersionedResource(string) (db.VersionedResource, error)
}

type ResourceChecker interface {
	CheckResource(atc.ResourceConfig, db.Version) ([]db.Version, error)
}

type ConfigDB interface {
	GetConfig() (atc.Config, error)
}

type Radar struct {
	logger lager.Logger

	tracker  VersionDB
	interval time.Duration

	locker Locker

	configDB ConfigDB

	failing  map[string]bool
	checking map[string]bool
	statusL  *sync.Mutex
}

func NewRadar(
	logger lager.Logger,
	tracker VersionDB,
	interval time.Duration,
	locker Locker,
	configDB ConfigDB,
) *Radar {
	return &Radar{
		logger: logger,

		tracker:  tracker,
		interval: interval,

		locker: locker,

		configDB: configDB,

		failing:  make(map[string]bool),
		checking: make(map[string]bool),
		statusL:  new(sync.Mutex),
	}
}

func (radar *Radar) Scan(checker ResourceChecker, resourceName string) ifrit.Runner {
	return ifrit.RunFunc(func(signals <-chan os.Signal, ready chan<- struct{}) error {
		ticker := time.NewTicker(radar.interval)

		close(ready)

		for {
			var resourceCheckingLock db.Lock
			var err error
			select {
			case <-signals:
				return nil

			case <-ticker.C:
				resourceCheckingLock, err = radar.locker.AcquireWriteLockImmediately([]db.NamedLock{db.ResourceCheckingLock(resourceName)})
				if err != nil {
					break
				}

				config, err := radar.configDB.GetConfig()
				if err != nil {
					continue
				}

				resource, found := config.Resources.Lookup(resourceName)
				if !found {
					return nil
				}

				radar.setChecking(resource.Name)

				var from db.Version
				log := radar.logger.Session("radar", lager.Data{
					"resource": resource.Name,
					"type":     resource.Type,
				})

				lock, err := radar.locker.AcquireReadLock([]db.NamedLock{db.ResourceLock(resource.Name)})
				if err != nil {
					log.Error("failed-to-acquire-inputs-lock", err, lager.Data{
						"resource_name": resource.Name,
					})
					break
				}

				if vr, err := radar.tracker.GetLatestVersionedResource(resource.Name); err == nil {
					from = vr.Version
				}

				lock.Release()

				log.Debug("check", lager.Data{
					"from": from,
				})

				newVersions, err := checker.CheckResource(resource, from)

				radar.setFailing(resource.Name, err != nil)

				if err != nil {
					log.Error("failed-to-check", err)
					break
				}

				if len(newVersions) == 0 {
					break
				}

				log.Info("versions-found", lager.Data{
					"versions": newVersions,
					"total":    len(newVersions),
				})

				lock, err = radar.locker.AcquireWriteLock([]db.NamedLock{db.ResourceLock(resource.Name)})
				if err != nil {
					log.Error("failed-to-acquire-inputs-lock", err, lager.Data{
						"resource_name": resource.Name,
					})
					break
				}

				for _, version := range newVersions {
					err = radar.tracker.SaveVersionedResource(db.VersionedResource{
{'fraction_non_alphanumeric': 0.12542209358417752, 'fraction_numerical': 0.00032159511175430133, 'mean_word_length': 3.3074792243767313, 'pattern_counts': {'":': 14, '<': 11, '<?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': '2795304', 'n_tokens_mistral': 2593, 'n_tokens_neox': 2103, 'n_words': 424}