summaryrefslogtreecommitdiff
path: root/pkg/idec/echo.go
blob: db3cd70ff6d27a47b5658aa715157ed09b929649 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
}