package events

import (
	"encoding/json"
	"fmt"
	"iwarma.ru/console/correlator/stat"
)

// Stat Aggregator statistic
type Stat struct {
	EventsProcessed      uint64       `json:"events_processed"`
	EventsAggregated     uint64       `json:"events_aggregated"`
	AverageIterationTime stat.AvgTime `json:"average_iteration"`
}

func (stat *Stat) Update(newStat Stat) {
	stat.EventsProcessed += newStat.EventsProcessed
	stat.EventsAggregated += newStat.EventsAggregated

	stat.AverageIterationTime.AddStat(&newStat.AverageIterationTime)
}

func (stat Stat) String() string {
	bytes, err := json.Marshal(stat)
	if err != nil {
		return fmt.Sprintf("Can't marshall stat: %v", err)
	}

	return string(bytes)
}
