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
|
2024-03-12 22:18:01 +00:00
|
|
|
InlineQueries uint64
|
2024-03-11 20:15:27 +00:00
|
|
|
|
2024-10-27 21:35:35 +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,
|
2024-03-12 22:18:01 +00:00
|
|
|
InlineQueries: 0,
|
2024-03-11 20:15:27 +00:00
|
|
|
|
2024-10-27 21:35:35 +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"`
|
2024-03-12 22:18:01 +00:00
|
|
|
InlineQueries uint64 `json:"inline_queries"`
|
2024-03-11 20:15:27 +00:00
|
|
|
|
2024-10-27 21:35:35 +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,
|
2024-03-12 22:18:01 +00:00
|
|
|
InlineQueries: s.InlineQueries,
|
2024-03-11 20:15:27 +00:00
|
|
|
|
2024-10-27 21:35:35 +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)
|
|
|
|
}
|
|
|
|
|
2024-03-12 22:18:01 +00:00
|
|
|
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++
|
|
|
|
}
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
func (s *Stats) Mention() {
|
2024-03-11 20:15:27 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2024-10-27 21:35:35 +00:00
|
|
|
s.Mentions++
|
2024-03-11 20:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stats) SummarizeRequest() {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.SummarizeRequests++
|
|
|
|
}
|