<commit_before>// Package handler is the handler for the `micro debug stats` service
package handler

import (
	"context"
	"errors"
	"sync"
	"time"

	"github.com/micro/go-micro/client"
	"github.com/micro/go-micro/config/cmd"
	debug "github.com/micro/go-micro/debug/service/proto"
	"github.com/micro/go-micro/registry"
	"github.com/micro/go-micro/registry/cache"
	"github.com/micro/go-micro/util/log"
	stats "github.com/micro/micro/debug/stats/proto"
)

// New initialises and returns a new Stats service handler
func New(done <-chan bool) (*Stats, error) {
	s := &Stats{
		registry: cache.New(*cmd.DefaultOptions().Registry),
		client:   *cmd.DefaultOptions().Client,
	}

	if err := s.scan(); err != nil {
		return nil, err
	}
	s.Start(done)
	return s, nil
}

// Stats is the Debug.Stats handler
type Stats struct {
	registry  registry.Registry
	client    client.Client
	snapshots []*stats.Snapshot
	next      []*stats.Snapshot
	cached    []*registry.Service
	sync.RWMutex
}

// Read returns gets a snapshot of all current stats
func (s *Stats) Read(ctx context.Context, req *stats.ReadRequest, rsp *stats.ReadResponse) error {
	s.RLock()
	rsp.Stats = s.snapshots
	s.RUnlock()
	return nil
}

// Stream starts streaming stats
func (s *Stats) Stream(ctx context.Context, req *stats.StreamRequest, rsp stats.Stats_StreamStream) error {
	return errors.New("Not Implemented")
}

// Start Starts scraping other services
// close the returned channel to stop scraping
func (s *Stats) Start(done <-chan bool) {
	go func(s *Stats) {
		for {
			select {
			case <-done:
				return
			default:
				s.scrape()
				time.Sleep(time.Second)
			}
		}
	}(s)
	go func(s *Stats) {
		rescan := time.NewTicker(10 * time.Second)
		for {
			select {
			case <-done:
				rescan.Stop()
				return
			case <-rescan.C:
				s.scan()
			}<commit_msg>Fix handler comment<commit_after>// Package handler is the handler for the `micro debug stats` service
package handler

import (
	"context"
	"errors"
	"sync"
	"time"

	"github.com/micro/go-micro/client"
	"github.com/micro/go-micro/config/cmd"
	debug "github.com/micro/go-micro/debug/service/proto"
	"github.com/micro/go-micro/registry"
	"github.com/micro/go-micro/registry/cache"
	"github.com/micro/go-micro/util/log"
	stats "github.com/micro/micro/debug/stats/proto"
)

// New initialises and returns a new Stats service handler
func New(done <-chan bool) (*Stats, error) {
	s := &Stats{
		registry: cache.New(*cmd.DefaultOptions().Registry),
		client:   *cmd.DefaultOptions().Client,
	}

	if err := s.scan(); err != nil {
		return nil, err
	}
	s.Start(done)
	return s, nil
}

// Stats is the Debug.Stats handler
type Stats struct {
	registry  registry.Registry
	client    client.Client
	snapshots []*stats.Snapshot
	next      []*stats.Snapshot
	cached    []*registry.Service
	sync.RWMutex
}

// Read returns gets a snapshot of all current stats
func (s *Stats) Read(ctx context.Context, req *stats.ReadRequest, rsp *stats.ReadResponse) error {
	s.RLock()
	rsp.Stats = s.snapshots
	s.RUnlock()
	return nil
}

// Stream starts streaming stats
func (s *Stats) Stream(ctx context.Context, req *stats.StreamRequest, rsp stats.Stats_StreamStream) error {
	return errors.New("Not Implemented")
}

// Start Starts scraping other services until the provided channel is closed
func (s *Stats) Start(done <-chan bool) {
	go func(s *Stats) {
		for {
			select {
			case <-done:
				return
			default:
				s.scrape()
				time.Sleep(time.Second)
			}
		}
	}(s)
	go func(s *Stats) {
		rescan := time.NewTicker(10 * time.Second)
		for {
			select {
			case <-done:
				rescan.Stop()
				return
			case <-rescan.C:
				s.scan()
			}
{'fraction_non_alphanumeric': 0.13523914238592633, 'fraction_numerical': 0.0010995052226498076, 'mean_word_length': 3.4541003671970625, 'pattern_counts': {'":': 0, '<': 13, '<?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': '1660015', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1252, 'n_words': 335}