-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathapplications.go
113 lines (99 loc) · 2.91 KB
/
applications.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2021 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package insightcollector
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
"github.com/pipe-cd/pipe/pkg/datastore"
"github.com/pipe-cd/pipe/pkg/insight"
"github.com/pipe-cd/pipe/pkg/model"
)
// collectApplicationCount collects application count data.
func (c *Collector) collectApplicationCount(ctx context.Context, apps []*model.Application, target time.Time) error {
var lastErr error
appmap := groupApplicationsByProjectID(apps)
for pid, apps := range appmap {
if err := c.updateApplicationCounts(ctx, pid, apps, target); err != nil {
c.logger.Error("failed to update ApplicationCounts data",
zap.String("project", pid),
zap.Error(err),
)
lastErr = err
}
}
return lastErr
}
func (c *Collector) updateApplicationCounts(ctx context.Context, projectID string, apps []*model.Application, target time.Time) error {
counts := insight.MakeApplicationCounts(apps, target)
if err := c.insightstore.PutApplicationCounts(ctx, projectID, counts); err != nil {
return fmt.Errorf("failed to put application counts: %w", err)
}
return nil
}
func (c *Collector) listApplications(ctx context.Context, to time.Time) ([]*model.Application, error) {
const limit = 100
var cursor string
var applications []*model.Application
for {
apps, next, err := c.applicationStore.ListApplications(ctx, datastore.ListOptions{
Filters: []datastore.ListFilter{
{
Field: "Deleted",
Operator: "==",
Value: false,
},
},
Orders: []datastore.Order{
{
Field: "CreatedAt",
Direction: datastore.Asc,
},
{
Field: "Id",
Direction: datastore.Asc,
},
},
Cursor: cursor,
Limit: limit,
})
if err != nil {
return nil, err
}
applications = append(applications, apps...)
if next == "" {
break
}
cursor = next
}
return applications, nil
}
// groupApplicationsByProjectID groups applications by projectID
func groupApplicationsByProjectID(applications []*model.Application) map[string][]*model.Application {
apps := make(map[string][]*model.Application)
for _, a := range applications {
apps[a.ProjectId] = append(apps[a.ProjectId], a)
}
return apps
}
func findOldestApplication(apps []*model.Application) *model.Application {
oldestApp := apps[0]
for _, a := range apps {
if a.CreatedAt < oldestApp.CreatedAt {
oldestApp = a
}
}
return oldestApp
}