idecnode/pkg/idec/echo.go

60 lines
1.1 KiB
Go

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
}
messages, err := i.GetMessagesByEcho(echoID, offset, limit)
if err != nil {
return nil, err
}
res[echoID] = model.Echo{
Name: echoID,
Description: echoCfg.Description,
Messages: messages,
Count: len(messages),
}
}
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
}