Skip to content

Commit

Permalink
Beta364 (#376)
Browse files Browse the repository at this point in the history
* beta338

* beta319

* beta340

* beta341

* actionfix

* beta342

* beta342

* beta342

* beta344

* beta345

* beta346

* beta347

* beta348

* beta349

* beta350

* beta351

* beta352

* beta353

* beta354

* beta355

* beta356

* beta357

* beta358

* beta359

* beta360

* beta361

* beta362

* beta363
  • Loading branch information
Hoshinonyaruko authored Apr 17, 2024
1 parent fdd7ded commit 1be7d5b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion botstats/botstats.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package botstats

import (
"errors"
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/hoshinonyaruko/gensokyo/mylog"
"go.etcd.io/bbolt"
)

Expand Down Expand Up @@ -47,6 +50,10 @@ func RecordMessageSent() {

// 收到增量 发出增量
func recordStats(receivedIncrement int, sentIncrement int) {
if db == nil {
mylog.Printf("recordStats db is nil")
return
}
db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
now := time.Now()
Expand Down Expand Up @@ -82,14 +89,23 @@ func updateCounter(b *bbolt.Bucket, key string, increment int) {
func GetStats() (int, int, int64, error) {
var messageReceived, messageSent int
var lastMessageTime int64
if db == nil {
return 0, 0, 0, errors.New("database is not initialized")
}
err := db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
if b == nil {
return fmt.Errorf("bucket %s not found", bucketName)
}
messageReceived = getInt(b, messageReceivedKey)
messageSent = getInt(b, messageSentKey)
lastMessageTime = getLastMessageTime(b)
return nil
})
return messageReceived, messageSent, lastMessageTime, err
if err != nil {
return 0, 0, 0, err
}
return messageReceived, messageSent, lastMessageTime, nil
}

func getInt(b *bbolt.Bucket, key string) int {
Expand Down

0 comments on commit 1be7d5b

Please sign in to comment.