-
Notifications
You must be signed in to change notification settings - Fork 219
/
status.go
149 lines (138 loc) · 4.66 KB
/
status.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
// Copyright 2022 Google LLC
//
// 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 status
import (
"context"
"flag"
"fmt"
"io"
"sort"
"strings"
"time"
"github.com/ServiceWeaver/weaver/runtime/colors"
"github.com/ServiceWeaver/weaver/runtime/logging"
dtool "github.com/ServiceWeaver/weaver/runtime/tool"
)
// StatusCommand returns a "status" subcommand that pretty prints the status of
// all active applications registered with the provided registry. tool is the
// name of the command-line tool the returned subcommand runs as (e.g.,
// "weaver single").
func StatusCommand(tool string, registry func(context.Context) (*Registry, error)) *dtool.Command {
return &dtool.Command{
Name: "status",
Description: "Show the status of Service Weaver applications",
Help: fmt.Sprintf(`Usage:
%s status
Flags:
-h, --help Print this help message.`, tool),
Flags: flag.NewFlagSet("status", flag.ContinueOnError),
Fn: func(ctx context.Context, _ []string) error {
r, err := registry(ctx)
if err != nil {
return err
}
regs, err := r.List(ctx)
if err != nil {
return err
}
var statuses []*Status
for _, reg := range regs {
status, err := NewClient(reg.Addr).Status(ctx)
if err != nil {
return err
}
statuses = append(statuses, status)
}
fmt.Print(format(statuses))
return nil
},
}
}
// format pretty-prints the provided statuses.
func format(statuses []*Status) string {
sort.Slice(statuses, func(i, j int) bool {
// Sort by app name, breaking ties on age.
x, y := statuses[i], statuses[j]
if x.App != y.App {
return x.App < y.App
}
return x.SubmissionTime.AsTime().Before(y.SubmissionTime.AsTime())
})
var b strings.Builder
formatDeployments(&b, statuses)
formatComponents(&b, statuses)
formatListeners(&b, statuses)
return b.String()
}
// formatId returns a pretty-printed prefix and suffix of the provided id. Both
// prefix and suffix are colored, and the prefix is underlined.
func formatId(id string) (prefix, suffix colors.Atom) {
short := logging.Shorten(id)
code := colors.ColorHash(id)
prefix = colors.Atom{S: short, Color: code, Underline: true}
suffix = colors.Atom{S: strings.TrimPrefix(id, short), Color: code}
return prefix, suffix
}
// formatDeployments pretty-prints the set of deployments.
func formatDeployments(w io.Writer, statuses []*Status) {
title := []colors.Text{{{S: "DEPLOYMENTS", Bold: true}}}
t := colors.NewTabularizer(w, title, colors.PrefixDim)
defer t.Flush()
t.Row("APP", "DEPLOYMENT", "AGE")
for _, status := range statuses {
prefix, suffix := formatId(status.DeploymentId)
age := time.Since(status.SubmissionTime.AsTime()).Truncate(time.Second)
t.Row(status.App, colors.Text{prefix, suffix}, age)
}
}
// formatDeployments pretty-prints the set of components.
func formatComponents(w io.Writer, statuses []*Status) {
title := []colors.Text{{{S: "COMPONENTS", Bold: true}}}
t := colors.NewTabularizer(w, title, colors.PrefixDim)
defer t.Flush()
t.Row("APP", "DEPLOYMENT", "COMPONENT", "REPLICA PIDS")
for _, status := range statuses {
sort.Slice(status.Components, func(i, j int) bool {
return status.Components[i].Name < status.Components[j].Name
})
for _, component := range status.Components {
prefix, _ := formatId(status.DeploymentId)
c := logging.ShortenComponent(component.Name)
sort.Slice(component.Pids, func(i, j int) bool {
return component.Pids[i] < component.Pids[j]
})
pids := make([]string, len(component.Pids))
for i, pid := range component.Pids {
pids[i] = fmt.Sprint(pid)
}
t.Row(status.App, prefix, c, strings.Join(pids, ", "))
}
}
}
// formatDeployments pretty-prints the set of listeners.
func formatListeners(w io.Writer, statuses []*Status) {
title := []colors.Text{{{S: "LISTENERS", Bold: true}}}
t := colors.NewTabularizer(w, title, colors.PrefixDim)
defer t.Flush()
t.Row("APP", "DEPLOYMENT", "LISTENER", "ADDRESS")
for _, status := range statuses {
sort.Slice(status.Listeners, func(i, j int) bool {
return status.Listeners[i].Name < status.Listeners[j].Name
})
for _, lis := range status.Listeners {
prefix, _ := formatId(status.DeploymentId)
t.Row(status.App, prefix, lis.Name, lis.Addr)
}
}
}