<commit_before>package metrics

import (
	"koding/kites/kloud/metrics"
	"time"

	"github.com/koding/logging"
)

type kloudKite interface {
	Call(method string, req, resp interface{}) error
}

// StartCron starts klient metrics publisher.
func StartCron(kloud kloudKite, log logging.Logger) {
	for range time.NewTicker(time.Second * 3).C {
		process(kloud, log)
	}
}

// StartCronWithMetrics starts klient metrics publisher with given db.
func StartCronWithMetrics(kloud kloudKite, log logging.Logger, m *Metrics) {
	for range time.NewTicker(time.Second * 3).C {
		processWithMetrics(kloud, log, m)
	}
}

func process(kloud kloudKite, log logging.Logger) {
	m, err := New("kd")
	if err != nil {
		log.Error(err.Error())
		return
	}

	processWithMetrics(kloud, log, m)

	if err := m.Close(); err != nil {
		log.Error(err.Error())
	}
}

func processWithMetrics(kloud kloudKite, log logging.Logger, m *Metrics) {
	if err := m.Process(func(res [][]byte) error {
		if len(res) == 0 {
			log.Debug("skipping publishing, 0 elements")
			return nil
		}

		log.Debug("publishing %d elements", len(res))

		req := &metrics.PublishRequest{
			Data: metrics.GzippedPayload(res),
		}

		var resp interface{}
		if err := kloud.Call("metrics.publish", req, resp); err != nil {
			log.Error(err.Error())
			return err
		}

		return nil
	}); err != nil {
		log.Error(err.Error())
	}
}
<commit_msg>koding/kites: fix ticker usage<commit_after>package metrics

import (
	"koding/kites/kloud/metrics"
	"time"

	"github.com/koding/logging"
)

type kloudKite interface {
	Call(method string, req, resp interface{}) error
}

// StartCron starts klient metrics publisher.
func StartCron(kloud kloudKite, log logging.Logger) {
	for range time.Tick(time.Second * 10) {
		process(kloud, log)
	}
}

// StartCronWithMetrics starts klient metrics publisher with given db.
func StartCronWithMetrics(kloud kloudKite, log logging.Logger, m *Metrics) {
	for range time.Tick(time.Second * 10) {
		processWithMetrics(kloud, log, m)
	}
}

func process(kloud kloudKite, log logging.Logger) {
	m, err := New("kd")
	if err != nil {
		log.Error(err.Error())
		return
	}

	processWithMetrics(kloud, log, m)

	if err := m.Close(); err != nil {
		log.Error(err.Error())
	}
}

func processWithMetrics(kloud kloudKite, log logging.Logger, m *Metrics) {
	if err := m.Process(func(res [][]byte) error {
		if len(res) == 0 {
			log.Debug("skipping publishing, 0 elements")
			return nil
		}

		log.Debug("publishing %d elements", len(res))

		req := &metrics.PublishRequest{
			Data: metrics.GzippedPayload(res),
		}

		var resp interface{}
		if err := kloud.Call("metrics.publish", req, resp); err != nil {
			log.Error(err.Error())
			return err
		}

		return nil
	}); err != nil {
		log.Error(err.Error())
	}
}

{'fraction_non_alphanumeric': 0.14332247557003258, 'fraction_numerical': 0.003619254433586681, 'mean_word_length': 3.9445438282647585, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5881885', 'n_tokens_mistral': 1062, 'n_tokens_neox': 987, 'n_words': 247}