package rules

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

// Stat is a Rule store statistic
type Stat struct {
	RuleCount            uint64                   `json:"rule_count"`
	AverageIterationTime stat.AvgTime             `json:"average_time"`
	AverageRuleTime      map[string]*stat.AvgTime `json:"average_rule_time"`
	IncidentCount        uint64                   `json:"incident_count"`
	EventsCount          uint64                   `json:"events_count"`
	Errors               map[string]string        `json:"errors"`
}

// NewStat crete new Stat structure
func NewStat() *Stat {
	return &Stat{
		AverageRuleTime: make(map[string]*stat.AvgTime),
		IncidentCount:   0,
		EventsCount:     0,
		Errors:          make(map[string]string),
	}
}

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)
}
