refactor
parent
6b962ea509
commit
a3630a8fda
54
storage.go
54
storage.go
|
|
@ -25,55 +25,33 @@ func (s Storage) MessagesSince(ctx context.Context, t time.Time) ([]Message, err
|
|||
})
|
||||
}
|
||||
|
||||
func (s Storage) EventNamesSince(ctx context.Context, t time.Time) ([]string, error) {
|
||||
messages, err := s.MessagesSince(ctx, t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := map[string]struct{}{}
|
||||
for _, m := range messages {
|
||||
names[m.EventName] = struct{}{}
|
||||
}
|
||||
result := make([]string, 0, len(names))
|
||||
for k := range names {
|
||||
result = append(result, k)
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s Storage) EventsSince(ctx context.Context, t time.Time) ([]string, error) {
|
||||
messages, err := s.MessagesSince(ctx, t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
events := map[string]struct{}{}
|
||||
for _, m := range messages {
|
||||
events[m.Event] = struct{}{}
|
||||
}
|
||||
result := make([]string, 0, len(events))
|
||||
for k := range events {
|
||||
result = append(result, k)
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s Storage) Threads(ctx context.Context) ([]string, error) {
|
||||
return s.ThreadsSince(ctx, time.Unix(0, 0))
|
||||
}
|
||||
|
||||
func (s Storage) ThreadsSince(ctx context.Context, t time.Time) ([]string, error) {
|
||||
return s.fieldsSince(ctx, t, func(m Message) string { return m.Thread })
|
||||
}
|
||||
|
||||
func (s Storage) EventNamesSince(ctx context.Context, t time.Time) ([]string, error) {
|
||||
return s.fieldsSince(ctx, t, func(m Message) string { return m.EventName })
|
||||
}
|
||||
|
||||
func (s Storage) EventsSince(ctx context.Context, t time.Time) ([]string, error) {
|
||||
return s.fieldsSince(ctx, t, func(m Message) string { return m.Event })
|
||||
}
|
||||
|
||||
func (s Storage) fieldsSince(ctx context.Context, t time.Time, fielder func(Message) string) ([]string, error) {
|
||||
messages, err := s.MessagesSince(ctx, t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
threads := map[string]struct{}{}
|
||||
values := map[string]struct{}{}
|
||||
for _, m := range messages {
|
||||
threads[m.Thread] = struct{}{}
|
||||
values[fielder(m)] = struct{}{}
|
||||
}
|
||||
result := make([]string, 0, len(threads))
|
||||
for k := range threads {
|
||||
result := make([]string, 0, len(values))
|
||||
for k := range values {
|
||||
result = append(result, k)
|
||||
}
|
||||
sort.Strings(result)
|
||||
|
|
|
|||
Loading…
Reference in New Issue