-
Notifications
You must be signed in to change notification settings - Fork 111
/
search.go
226 lines (196 loc) · 5.8 KB
/
search.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package project
import (
"context"
"fmt"
"strings"
"github.com/rilldata/rill/admin/client"
"github.com/rilldata/rill/cli/pkg/cmdutil"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
runtimeclient "github.com/rilldata/rill/runtime/client"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc/status"
)
func SearchCmd(ch *cmdutil.Helper) *cobra.Command {
var pageSize uint32
var pageToken string
var annotations map[string]string
var statusFlag bool
searchCmd := &cobra.Command{
Use: "search [<pattern>]",
Args: cobra.MaximumNArgs(1),
Short: "Search projects by pattern",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
client, err := ch.Client()
if err != nil {
return err
}
pattern := "%"
// If args is not empty, use the first element as the pattern
if len(args) > 0 {
pattern = args[0]
}
res, err := client.SearchProjectNames(ctx, &adminv1.SearchProjectNamesRequest{
NamePattern: pattern,
Annotations: annotations,
PageSize: pageSize,
PageToken: pageToken,
})
if err != nil {
return err
}
if len(res.Names) == 0 {
ch.PrintfWarn("No projects found\n")
return nil
}
if !statusFlag {
ch.PrintData(res.Names)
} else {
// We need to fetch the status of each project by connecting to their individual runtime instances.
// Using an errgroup to parallelize the requests.
table := make([]*projectStatusTableRow, len(res.Names))
grp, ctx := errgroup.WithContext(ctx)
for idx, name := range res.Names {
org := strings.Split(name, "/")[0]
project := strings.Split(name, "/")[1]
idx := idx
grp.Go(func() error {
row, err := newProjectStatusTableRow(ctx, client, org, project)
if err != nil {
return err
}
row.DeploymentStatus = truncMessage(row.DeploymentStatus, 35)
table[idx] = row
return nil
})
}
err := grp.Wait()
if err != nil {
return err
}
ch.PrintData(table)
}
if res.NextPageToken != "" {
cmd.Println()
cmd.Printf("Next page token: %s\n", res.NextPageToken)
}
return nil
},
}
searchCmd.Flags().BoolVar(&statusFlag, "status", false, "Include project status")
searchCmd.Flags().StringToStringVar(&annotations, "annotation", nil, "Annotations to filter projects by (supports wildcard values)")
searchCmd.Flags().Uint32Var(&pageSize, "page-size", 50, "Number of projects to return per page")
searchCmd.Flags().StringVar(&pageToken, "page-token", "", "Pagination token")
return searchCmd
}
type projectStatusTableRow struct {
Org string `header:"org"`
Project string `header:"project"`
DeploymentStatus string `header:"deployment"`
IdleCount int `header:"idle"`
PendingCount int `header:"pending"`
RunningCount int `header:"running"`
ReconcileErrorsCount int `header:"reconcile errors"`
ParseErrorsCount int `header:"parse errors"`
}
func newProjectStatusTableRow(ctx context.Context, c *client.Client, org, project string) (*projectStatusTableRow, error) {
proj, err := c.GetProject(ctx, &adminv1.GetProjectRequest{
OrganizationName: org,
Name: project,
})
if err != nil {
return nil, err
}
depl := proj.ProdDeployment
if depl == nil {
return &projectStatusTableRow{
Org: org,
Project: project,
DeploymentStatus: "Hibernated",
}, nil
}
if depl.Status != adminv1.DeploymentStatus_DEPLOYMENT_STATUS_OK {
var deplStatus string
switch depl.Status {
case adminv1.DeploymentStatus_DEPLOYMENT_STATUS_PENDING:
deplStatus = "Pending"
case adminv1.DeploymentStatus_DEPLOYMENT_STATUS_ERROR:
deplStatus = "Error"
default:
deplStatus = depl.Status.String()
}
return &projectStatusTableRow{
Org: org,
Project: project,
DeploymentStatus: deplStatus,
}, nil
}
rt, err := runtimeclient.New(depl.RuntimeHost, proj.Jwt)
if err != nil {
return &projectStatusTableRow{
Org: org,
Project: project,
DeploymentStatus: fmt.Sprintf("Connection error: %v", err),
}, nil
}
res, err := rt.ListResources(ctx, &runtimev1.ListResourcesRequest{InstanceId: depl.RuntimeInstanceId})
if err != nil {
msg := err.Error()
if s, ok := status.FromError(err); ok {
msg = s.Message()
}
return &projectStatusTableRow{
Org: org,
Project: project,
DeploymentStatus: fmt.Sprintf("Runtime error: %v", msg),
}, nil
}
var parser *runtimev1.ProjectParser
var parseErrorsCount int
var idleCount int
var reconcileErrorsCount int
var pendingCount int
var runningCount int
for _, r := range res.Resources {
if r.Meta.Name.Kind == runtime.ResourceKindProjectParser {
parser = r.GetProjectParser()
}
if r.Meta.Hidden {
continue
}
switch r.Meta.ReconcileStatus {
case runtimev1.ReconcileStatus_RECONCILE_STATUS_IDLE:
idleCount++
if r.Meta.GetReconcileError() != "" {
reconcileErrorsCount++
}
case runtimev1.ReconcileStatus_RECONCILE_STATUS_PENDING:
pendingCount++
case runtimev1.ReconcileStatus_RECONCILE_STATUS_RUNNING:
runningCount++
}
}
// check if there are any parser errors
if parser.State != nil && len(parser.State.ParseErrors) != 0 {
parseErrorsCount++
}
return &projectStatusTableRow{
Org: org,
Project: project,
DeploymentStatus: "OK",
IdleCount: idleCount,
PendingCount: pendingCount,
RunningCount: runningCount,
ReconcileErrorsCount: reconcileErrorsCount,
ParseErrorsCount: parseErrorsCount,
}, nil
}
func truncMessage(s string, n int) string {
if len(s) <= n {
return s
}
return s[:(n-3)] + "..."
}