<commit_before>package revok

import (
	"context"
	"cred-alert/db"
	"cred-alert/gitclient"
	"cred-alert/scanners"
	"cred-alert/scanners/filescanner"
	"cred-alert/sniff"
	"fmt"
	"io"
	"os"
	"time"

	"code.cloudfoundry.org/clock"
	"code.cloudfoundry.org/lager"
	"github.com/tedsuo/ifrit"
)

type headCredentialCounter struct {
	logger               lager.Logger
	repositoryRepository db.RepositoryRepository
	clock                clock.Clock
	interval             time.Duration
	gitClient            gitclient.Client
	sniffer              sniff.Sniffer
}

func NewHeadCredentialCounter(
	logger lager.Logger,
	repositoryRepository db.RepositoryRepository,
	clock clock.Clock,
	interval time.Duration,
	gitClient gitclient.Client,
	sniffer sniff.Sniffer,
) ifrit.Runner {
	return &headCredentialCounter{
		logger:               logger,
		repositoryRepository: repositoryRepository,
		clock:                clock,
		interval:             interval,
		gitClient:            gitClient,
		sniffer:              sniffer,
	}
}

func (c *headCredentialCounter) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
	logger := c.logger.Session("head-credential-counter")
	logger.Info("starting")

	close(ready)

	timer := c.clock.NewTicker(c.interval)

	ctx, cancel := context.WithCancel(context.Background())

	c.work(logger, cancel, signals)

	for {
		select {
		case <-timer.C():
			c.work(logger, cancel, signals)
		case <-signals:
			cancel()
			return nil
		case <-ctx.Done():
			return nil
		}
	}
}

func (c *headCredentialCounter) work(
	logger lager.Logger,
	cancel context.CancelFunc,
	signals <-chan os.Signal,
) {
	repositories, err := c.repositoryRepository.All()
	if err != nil {
		logger.Error("failed-getting-all-repositories", err)
	}

	for i := range repositories {
		select {
		case <-signals:
			cancel()
			return
		default:
			repository := repositories[i]
			repoLogger := logger.WithData(lager.Data{
				"ref":  repository.DefaultBranch,
				"path": repository.Path,
			})

			r, w := io.Pipe()

			errCh := make(chan error)
			go func() {
				errCh <- c.gitClient.AllBlobsForRef(repository.Path, fmt.Sprintf("refs/remotes/origin/%s", repository.DefaultBranch), w)
			}()

			var credCount uint
			_ = c.sniffer.Sniff(
				logger,
				filescanner.New(r, ""), // no filename necessary
				func(lager.Logger, scanners.Violation) error {
					credCount++
					return nil
				})

			if err := <-errCh; err != nil {
				repoLogger.Error("failed-to-get-blobs", err)
				continue
			}

			err := c.repositoryRepository.UpdateCredentialCount(&repository, credCount)
			if err != nil {
				repoLogger.Error("failed-to-update-credential-count", err)
			}
		}
	}
}<commit_msg>Use quietLogger in head credential counter<commit_after>package revok

import (
	"context"
	"cred-alert/db"
	"cred-alert/gitclient"
	"cred-alert/kolsch"
	"cred-alert/scanners"
	"cred-alert/scanners/filescanner"
	"cred-alert/sniff"
	"fmt"
	"io"
	"os"
	"time"

	"code.cloudfoundry.org/clock"
	"code.cloudfoundry.org/lager"
	"github.com/tedsuo/ifrit"
)

type headCredentialCounter struct {
	logger               lager.Logger
	repositoryRepository db.RepositoryRepository
	clock                clock.Clock
	interval             time.Duration
	gitClient            gitclient.Client
	sniffer              sniff.Sniffer
}

func NewHeadCredentialCounter(
	logger lager.Logger,
	repositoryRepository db.RepositoryRepository,
	clock clock.Clock,
	interval time.Duration,
	gitClient gitclient.Client,
	sniffer sniff.Sniffer,
) ifrit.Runner {
	return &headCredentialCounter{
		logger:               logger,
		repositoryRepository: repositoryRepository,
		clock:                clock,
		interval:             interval,
		gitClient:            gitClient,
		sniffer:              sniffer,
	}
}

func (c *headCredentialCounter) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
	logger := c.logger.Session("head-credential-counter")
	logger.Info("starting")

	close(ready)

	timer := c.clock.NewTicker(c.interval)

	ctx, cancel := context.WithCancel(context.Background())

	c.work(logger, cancel, signals)

	for {
		select {
		case <-timer.C():
			c.work(logger, cancel, signals)
		case <-signals:
			cancel()
			return nil
		case <-ctx.Done():
			return nil
		}
	}
}

func (c *headCredentialCounter) work(
	logger lager.Logger,
	cancel context.CancelFunc,
	signals <-chan os.Signal,
) {
	repositories, err := c.repositoryRepository.All()
	if err != nil {
		logger.Error("failed-getting-all-repositories", err)
	}

	quietLogger := kolsch.NewLogger()

	for i := range repositories {
		select {
		case <-signals:
			cancel()
			return
		default:
			repository := repositories[i]
			repoLogger := logger.WithData(lager.Data{
				"ref":  repository.DefaultBranch,
				"path": repository.Path,
			})

			r, w := io.Pipe()

			errCh := make(chan error)
			go func() {
				errCh <- c.gitClient.AllBlobsForRef(repository.Path, fmt.Sprintf("refs/remotes/origin/%s", repository.DefaultBranch), w)
			}()

			var credCount uint
			_ = c.sniffer.Sniff(
				quietLogger,
				filescanner.New(r, ""), // no filename necessary
				func(lager.Logger, scanners.Violation) error {
					credCount++
					return nil
				})

			if err := <-errCh; err != nil {
				repoLogger.Error("failed-to-get-blobs", err)
				continue
			}

			err := c.repositoryRepository.UpdateCredentialCount(&repository, credCount)
			if err != nil {
				repoLogger.Error("failed-to-update-credential-count", err)
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.12584722476644075, 'fraction_numerical': 0.0, 'mean_word_length': 3.0929535232383807, 'pattern_counts': {'":': 4, '<': 21, '<?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': '1554390', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1792, 'n_words': 366}