<commit_before>// -*- mode: go; tab-width: 2; indent-tabs-mode: 1; st-rulers: [70] -*-
// vim: ts=4 sw=4 ft=lua noet
//--------------------------------------------------------------------
// @author <NAME> <<EMAIL>>
// Copyright (C) Pagoda Box, Inc - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly
// prohibited. Proprietary and confidential
//
// @doc
//
// @end
// Created :   31 August 2015 by <NAME> <<EMAIL>>
//--------------------------------------------------------------------
package collector

import (
	"time"
)

type (
	Stat      func() int
	Collector interface {
		Stop()
		Start()
		Values() map[string]int
		Flush()
		SetInterval(time.Duration)
		OverrideInterval(time.Duration, time.Duration)
	}

	gauge struct {
		stat     Stat
		current  int
		done     chan interface{}
		next     <-chan time.Time
		revert   chan bool
		interval time.Duration
		override time.Duration
	}
)

func NewCollector(stat Stat) Collector {
	gauge := &gauge{
		stat:     stat,
		current:  stat(),
		override: 0,
	}

	return gauge
}

func (gauge *gauge) Stop() {
	if gauge.done != nil {
		close(gauge.done)
		gauge.done = nil
	}
}

func (gauge *gauge) Start() {
	if gauge.done == nil {
		gauge.reset()
		gauge.done = make(chan interface{})
		go func() {
			for {
				select {
				case <-gauge.done:
					return
				case <-gauge.next:
					gauge.reset()
					gauge.current = gauge.stat()
				}
			}
		}()
	}
}

func (gauge *gauge) reset() {
	switch {
	case gauge.override != 0:
		gauge.next = time.After(gauge.override)
	default:
		gauge.next = time.After(gauge.interval)
	}

}

func (gauge *gauge) Values() map[string]int {
	return map[string]int{"": gauge.current}
}

func (gauge *gauge) Flush() {
	gauge.current = 0
}

func (gauge *gauge) SetInterval(interval time.Duration) {
	gauge.interval = interval
}

func (gauge *gauge) OverrideInterval(newInterval time.Duration, howLong time.Duration) {
	if gauge.override != 0 {
		close(gauge.revert)
	}
	gauge.override = newInterval
	gauge.revert = make(chan bool)
	go func() {
		select {
		case <-time.After(howLong):
			gauge.override = 0<commit_msg>adjusting the collection interval now happens instantly<commit_after>// -*- mode: go; tab-width: 2; indent-tabs-mode: 1; st-rulers: [70] -*-
// vim: ts=4 sw=4 ft=lua noet
//--------------------------------------------------------------------
// @author <NAME> <<EMAIL>>
// Copyright (C) Pagoda Box, Inc - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly
// prohibited. Proprietary and confidential
//
// @doc
//
// @end
// Created :   31 August 2015 by <NAME> <<EMAIL>>
//--------------------------------------------------------------------
package collector

import (
	"time"
)

type (
	Stat      func() int
	Collector interface {
		Stop()
		Start()
		Values() map[string]int
		Flush()
		SetInterval(time.Duration)
		OverrideInterval(time.Duration, time.Duration)
	}

	gauge struct {
		stat     Stat
		current  int
		done     chan interface{}
		next     <-chan time.Time
		revert   chan bool
		interval time.Duration
		override time.Duration
	}
)

func NewCollector(stat Stat) Collector {
	gauge := &gauge{
		stat:     stat,
		current:  stat(),
		override: 0,
	}

	return gauge
}

func (gauge *gauge) Stop() {
	if gauge.done != nil {
		close(gauge.done)
		gauge.done = nil
	}
}

func (gauge *gauge) Start() {
	if gauge.done == nil {
		gauge.reset()
		gauge.done = make(chan interface{})
		go func() {
			for {
				select {
				case <-gauge.done:
					return
				case <-gauge.next:
					gauge.reset()
					gauge.current = gauge.stat()
				}
			}
		}()
	}
}

func (gauge *gauge) reset() {
	switch {
	case gauge.override != 0:
		gauge.next = time.After(gauge.override)
	default:
		gauge.next = time.After(gauge.interval)
	}

}

func (gauge *gauge) Values() map[string]int {
	return map[string]int{"": gauge.current}
}

func (gauge *gauge) Flush() {
	gauge.current = 0
}

func (gauge *gauge) SetInterval(interval time.Duration) {
	gauge.interval = interval
	gauge.reset()
}

func (gauge *gauge) OverrideInterval(newInterval time.Duration, howLong time.Duration) {
	if gauge.override != 0 {
		close(gauge.revert)
	}
	gauge.override = newInterval
	gauge.revert = make(chan bool)
	gauge.reset()
	go func() {
		select {
		case <-time.After(howLong):
			gauge.override = 0
			gauge.reset()
{'fraction_non_alphanumeric': 0.19769850402761796, 'fraction_numerical': 0.007825086306098964, 'mean_word_length': 2.400625978090767, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 15, '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': '2093511', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1415, 'n_words': 386}