telegram-ollama-reply-bot/stats/stats.go

95 lines
1.6 KiB
Go
Raw Normal View History

2024-03-11 20:15:27 +00:00
package stats
import (
"encoding/json"
"sync"
"time"
)
type Stats struct {
mu sync.Mutex
RunningSince time.Time
GroupRequests uint64
PrivateRequests uint64
InlineQueries uint64
2024-03-11 20:15:27 +00:00
Mentions uint64
2024-03-11 20:15:27 +00:00
SummarizeRequests uint64
}
func NewStats() *Stats {
return &Stats{
RunningSince: time.Now(),
GroupRequests: 0,
PrivateRequests: 0,
InlineQueries: 0,
2024-03-11 20:15:27 +00:00
Mentions: 0,
2024-03-11 20:15:27 +00:00
SummarizeRequests: 0,
}
}
func (s *Stats) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Uptime string `json:"uptime"`
GroupRequests uint64 `json:"group_requests"`
PrivateRequests uint64 `json:"private_requests"`
InlineQueries uint64 `json:"inline_queries"`
2024-03-11 20:15:27 +00:00
Mentions uint64 `json:"mentions"`
2024-03-11 20:15:27 +00:00
SummarizeRequests uint64 `json:"summarize_requests"`
}{
Uptime: time.Now().Sub(s.RunningSince).String(),
GroupRequests: s.GroupRequests,
PrivateRequests: s.PrivateRequests,
InlineQueries: s.InlineQueries,
2024-03-11 20:15:27 +00:00
Mentions: s.Mentions,
2024-03-11 20:15:27 +00:00
SummarizeRequests: s.SummarizeRequests,
})
}
func (s *Stats) String() string {
data, err := json.MarshalIndent(s, "", " ")
if err != nil {
return "{\"error\": \"cannot serialize stats\"}"
}
return string(data)
}
func (s *Stats) InlineQuery() {
s.mu.Lock()
defer s.mu.Unlock()
s.InlineQueries++
}
2024-03-11 20:15:27 +00:00
func (s *Stats) GroupRequest() {
s.mu.Lock()
defer s.mu.Unlock()
s.GroupRequests++
}
func (s *Stats) PrivateRequest() {
s.mu.Lock()
defer s.mu.Unlock()
s.PrivateRequests++
}
func (s *Stats) Mention() {
2024-03-11 20:15:27 +00:00
s.mu.Lock()
defer s.mu.Unlock()
s.Mentions++
2024-03-11 20:15:27 +00:00
}
func (s *Stats) SummarizeRequest() {
s.mu.Lock()
defer s.mu.Unlock()
s.SummarizeRequests++
}