summaryrefslogblamecommitdiff
path: root/pkg/idec/echo.go
blob: 04ed48c94548062b7ecf657d0667d8c374d8e879 (plain) (tree)



















                                                                                                  
                                                                                    







                                                         
                                           





























                                                            
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, count, err := i.GetMessageIDsByEcho(echoID, offset, limit)
		if err != nil {
			return nil, err
		}

		res[echoID] = model.Echo{
			Name:        echoID,
			Description: echoCfg.Description,
			Messages:    messages,
			Count:       count,
		}
	}

	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
}