idecnode/pkg/idec/echo.go

61 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-10-20 03:39:07 +03:00
package idec
import (
"gitrepo.ru/neonxp/idecnode/pkg/model"
"go.etcd.io/bbolt"
)
func (i *IDEC) GetEchosByIDs(echoIDs []string, offset, limit int) (map[string]model.Echo, error) {
res := make(map[string]model.Echo, len(echoIDs))
for _, echoID := range echoIDs {
echoCfg, ok := i.config.Echos[echoID]
if !ok {
// unknown echo
res[echoID] = model.Echo{
Name: echoID,
}
continue
}
2024-10-29 01:21:05 +03:00
messages, count, err := i.GetMessageIDsByEcho(echoID, offset, limit)
2024-10-20 03:39:07 +03:00
if err != nil {
return nil, err
}
res[echoID] = model.Echo{
Name: echoID,
Description: echoCfg.Description,
Messages: messages,
2024-10-29 01:21:05 +03:00
Count: count,
2024-10-20 03:39:07 +03:00
}
}
return res, nil
}
func (i *IDEC) GetEchos() ([]model.Echo, error) {
result := make([]model.Echo, 0, len(i.config.Echos))
for name, e := range i.config.Echos {
e := model.Echo{
Name: name,
Description: e.Description,
}
err := i.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(name))
if b == nil {
return nil
}
e.Count = b.Stats().KeyN
return nil
})
if err != nil {
return nil, err
}
result = append(result, e)
}
return result, nil
}