• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / iot-manager / 1401702106

05 Aug 2024 08:32PM UTC coverage: 87.577%. Remained the same
1401702106

push

gitlab-ci

web-flow
Merge pull request #295 from mendersoftware/dependabot/docker/docker-dependencies-03b04ac819

chore: bump golang from 1.22.4-alpine3.19 to 1.22.5-alpine3.19 in the docker-dependencies group

3264 of 3727 relevant lines covered (87.58%)

11.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.05
/store/mongo/datastore_mongo_events.go
1
// Copyright 2022 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package mongo
16

17
import (
18
        "context"
19
        "time"
20

21
        "github.com/google/uuid"
22
        "github.com/pkg/errors"
23
        "go.mongodb.org/mongo-driver/bson"
24
        mopts "go.mongodb.org/mongo-driver/mongo/options"
25

26
        mstore "github.com/mendersoftware/go-lib-micro/store/v2"
27

28
        "github.com/mendersoftware/iot-manager/model"
29
)
30

31
const (
32
        CollNameLog = "log"
33

34
        KeyEventTs       = "event_ts"
35
        KeyEventExpireTs = "expire_ts"
36
)
37

38
var (
39
        eventExpiration int64
40

41
        ErrFailedToGetEvents = errors.New("failed to get events")
42
)
43

44
func SetEventExpiration(exp int64) {
3✔
45
        eventExpiration = exp
3✔
46
}
3✔
47

48
func (db *DataStoreMongo) GetEvents(
49
        ctx context.Context,
50
        fltr model.EventsFilter,
51
) ([]model.Event, error) {
6✔
52
        var (
6✔
53
                err     error
6✔
54
                results = []model.Event{}
6✔
55
        )
6✔
56

6✔
57
        collEvents := db.Collection(CollNameLog)
6✔
58
        findOpts := mopts.Find().
6✔
59
                SetSort(bson.D{{Key: KeyEventTs, Value: -1}}).
6✔
60
                SetSkip(fltr.Skip)
6✔
61
        if fltr.Limit > 0 {
10✔
62
                findOpts.SetLimit(fltr.Limit)
4✔
63
        }
4✔
64

65
        cur, err := collEvents.Find(ctx,
6✔
66
                mstore.WithTenantID(ctx, bson.D{}),
6✔
67
                findOpts,
6✔
68
        )
6✔
69
        if err != nil {
6✔
70
                return nil, errors.Wrap(err, "error executing log collection request")
×
71
        }
×
72
        if err = cur.All(ctx, &results); err != nil {
6✔
73
                return nil, errors.Wrap(err, "error retrieving log collection results")
×
74
        }
×
75

76
        return results, nil
6✔
77
}
78

79
func (db *DataStoreMongo) SaveEvent(
80
        ctx context.Context,
81
        event model.Event,
82
) error {
9✔
83
        now := time.Now()
9✔
84
        event.EventTS = now
9✔
85
        event.ExpireTS = now.Add(time.Second * time.Duration(eventExpiration))
9✔
86

9✔
87
        if event.ID == uuid.Nil {
11✔
88
                event.ID = uuid.New()
2✔
89
        }
2✔
90

91
        collEvents := db.Collection(CollNameLog)
9✔
92

9✔
93
        _, err := collEvents.
9✔
94
                InsertOne(ctx, mstore.WithTenantID(ctx, event))
9✔
95
        if err != nil {
9✔
96
                return errors.Wrapf(err, "failed to store an event %v", event)
×
97
        }
×
98

99
        return nil
9✔
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc