GitLabgitlab-org

cli

main
ply8 git calls285 ms99 KiB6 cachedprotocol v2
285 lines6.8 KiBRaw
1package graph
3import (
4 "context"
5 "fmt"
6 "strings"
8 "github.com/d2lang/d2/d2graph"
9 "github.com/d2lang/d2/d2lib"
10 "github.com/d2lang/d2/d2oracle"
11 "k8s.io/apimachinery/pkg/util/sets"
13 "gitlab.com/gitlab-org/cli/internal/iostreams"
16type graphBuilder struct {
17 io *iostreams.IOStreams
18 g *d2graph.Graph
19 objsPerNS map[string]int32 // namespace -> object counter
20 knownVertex sets.Set[vertex]
21 arc2Key map[Arc]string
24func newGraphBuilder(ctx context.Context, io *iostreams.IOStreams) (*graphBuilder, error) {
25 _, g, err := d2lib.Compile(ctx, "", nil, nil)
26 if err != nil {
27 return nil, err
28 }
29 b := &graphBuilder{
30 io: io,
31 g: g,
32 objsPerNS: map[string]int32{},
33 knownVertex: sets.Set[vertex]{},
34 arc2Key: map[Arc]string{},
35 }
36 err = b.setOnGraph(globals())
37 if err != nil {
38 return nil, err
39 }
40 return b, nil
43func (b *graphBuilder) applyActions(actions []jsonWatchGraphAction) error {
44 for _, action := range actions {
45 switch {
46 case action.SetVertex != nil:
47 b.io.LogInfof("Set Vertex %s\n", action.SetVertex.Vertex)
48 err := b.handleSetVertex(action.SetVertex)
49 if err != nil {
50 return err
51 }
52 case action.DeleteVertex != nil:
53 b.io.LogInfof("Delete Vertex %s\n", action.DeleteVertex.Vertex)
54 err := b.handleDeleteVertex(action.DeleteVertex)
55 if err != nil {
56 return err
57 }
58 case action.SetArc != nil:
59 b.io.LogInfof("Set Arc (%s)->(%s)\n", action.SetArc.Source, action.SetArc.Destination)
60 err := b.handleSetArc(action.SetArc)
61 if err != nil {
62 return err
63 }
64 case action.DeleteArc != nil:
65 b.io.LogInfof("Delete Arc (%s)->(%s)\n", action.DeleteArc.Source, action.DeleteArc.Destination)
66 err := b.handleDeleteArc(action.DeleteArc)
67 if err != nil {
68 return err
69 }
70 }
71 }
72 return nil
75func (b *graphBuilder) handleSetVertex(sv *jsonSetVertex) error {
76 return b.ensureVertex(sv.Vertex)
79func (b *graphBuilder) ensureVertex(v vertex) error {
80 if b.knownVertex.Has(v) {
81 return nil
82 }
83 b.knownVertex.Insert(v)
84 switch {
85 case v.IsNamespace(): // vertex itself is a Namespace object.
86 err := b.ensureNamespace(v.Name)
87 if err != nil {
88 return err
89 }
90 case v.IsNamespaced(): // vertex is a namespaced object.
91 err := b.ensureNamespace(v.Namespace)
92 if err != nil {
93 return err
94 }
95 err = b.createVertex(v)
96 if err != nil {
97 return err
98 }
99 b.objsPerNS[v.Namespace]++
100 default: // cluster-scoped object, but not a Namespace.
101 err := b.createVertex(v)
102 if err != nil {
103 return err
104 }
105 }
106 return nil
109func (b *graphBuilder) createVertex(v vertex) error {
110 var err error
111 vKey := keyForVertex(v)
112 b.g, _, err = d2oracle.Create(b.g, nil, vKey)
113 if err != nil {
114 return err
115 }
116 err = b.setOnGraph(map[string]any{
117 "label": v.Name,
118 "class": classForVertex(v),
119 "tooltip": fmt.Sprintf("%s/%s", v.Group, v.Resource),
120 }, vKey)
121 if err != nil {
122 return err
123 }
124 return nil
127func (b *graphBuilder) ensureNamespace(ns string) error {
128 if b.isKnownNamespace(ns) {
129 return nil
130 }
131 var err error
132 nsV := vertex{
133 Group: "",
134 Version: "v1",
135 Resource: "namespaces",
136 Namespace: "",
137 Name: ns,
138 }
139 nsKey := keyForVertex(nsV)
140 b.g, _, err = d2oracle.Create(b.g, nil, nsKey)
141 if err != nil {
142 return err
143 }
144 err = b.setOnGraph(map[string]any{
145 "label": ns,
146 "class": classForVertex(nsV),
147 }, nsKey)
148 if err != nil {
149 return err
150 }
151 return nil
154func (b *graphBuilder) handleDeleteVertex(dv *jsonDeleteVertex) error {
155 b.knownVertex.Delete(dv.Vertex)
156 var err error
157 b.g, err = d2oracle.Delete(b.g, nil, keyForVertex(dv.Vertex))
158 if err != nil {
159 return err
160 }
161 if !dv.Vertex.IsNamespaced() { // Just deleted a cluster-scoped object, nothing else to do here.
162 return nil
163 }
164 // Need to do some bookkeeping for namespaced objects
165 n := b.objsPerNS[dv.Vertex.Namespace] - 1
166 if n > 0 { // There are still objects in this namespace
167 b.objsPerNS[dv.Vertex.Namespace] = n
168 return nil
169 }
170 // No more objects in this namespace
171 delete(b.objsPerNS, dv.Vertex.Namespace)
172 if b.isNamespaceKnownAsObject(dv.Vertex.Namespace) {
173 // We are watching Namespace objects directly, don't want to remove the vertex since the namespace is still there.
174 return nil
175 }
176 nsV := vertex{
177 Group: "",
178 Version: "v1",
179 Resource: "namespaces",
180 Namespace: "",
181 Name: dv.Vertex.Namespace,
182 }
183 b.g, err = d2oracle.Delete(b.g, nil, keyForVertex(nsV))
184 return err
187func (b *graphBuilder) handleSetArc(sa *jsonSetArc) error {
188 if _, ok := b.arc2Key[sa.Arc]; ok { // already exists
189 return nil
190 }
191 err := b.ensureVertex(sa.Source)
192 if err != nil {
193 return err
194 }
195 err = b.ensureVertex(sa.Destination)
196 if err != nil {
197 return err
198 }
199 aKey := createKeyForArc(sa.Arc)
200 var realArcKey string
201 b.g, realArcKey, err = d2oracle.Create(b.g, nil, aKey)
202 if err != nil {
203 return err
204 }
205 err = b.setOnGraph(map[string]any{
206 "class": classForArc(sa.Arc.Type),
207 }, realArcKey)
208 if err != nil {
209 return err
210 }
211 b.arc2Key[sa.Arc] = realArcKey
212 return nil
215func (b *graphBuilder) handleDeleteArc(da *jsonDeleteArc) error {
216 realArcKey := b.arc2Key[da.Arc]
217 delete(b.arc2Key, da.Arc)
218 var err error
219 b.g, err = d2oracle.Delete(b.g, nil, realArcKey)
220 // TODO may need to delete vertices!
221 return err
224func (b *graphBuilder) isKnownNamespace(namespace string) bool {
225 // We have at least one object in the namespace, or we have the Namespace object.
226 return b.objsPerNS[namespace] > 0 || b.isNamespaceKnownAsObject(namespace)
229func (b *graphBuilder) isNamespaceKnownAsObject(namespace string) bool {
230 return b.knownVertex.Has(vertex{
231 Group: "",
232 Version: "v1",
233 Resource: "namespaces",
234 Namespace: "",
235 Name: namespace,
236 })
239func (b *graphBuilder) setOnGraph(toSet map[string]any, keys ...string) error {
240 for k, v := range toSet {
241 var err error
242 switch val := v.(type) {
243 case string:
244 b.g, err = d2oracle.Set(b.g, nil, strings.Join(append(keys, k), "."), nil, new(val))
245 case map[string]any:
246 err = b.setOnGraph(val, append(keys, k)...)
247 default:
248 return fmt.Errorf("invalid value type: %T", v)
249 }
250 if err != nil {
251 return err
252 }
253 }
254 return nil
257func classForVertex(vertex vertex) string {
258 return classForGR(vertex.Group, vertex.Resource)
261func classForGR(group, resource string) string {
262 return fmt.Sprintf("%s_%s", group, resource)
265func classForArc(at arcType) string {
266 return "arc_" + string(at)
269// Some group names contain dots, so this function quotes the produced keys to avoid confusing D2:
270// otherwise D2 thinks those are nested objects.
271func keyForVertex(v vertex) string {
272 k := bareKey(v.Group, v.Resource, v.Name)
273 if v.Namespace == "" {
274 return `"` + k + `"`
275 }
276 return fmt.Sprintf(`%q.%q`, bareKey("", "namespaces", v.Namespace), k)
279func bareKey(group, resource, name string) string {
280 return fmt.Sprintf("%s/%s/%s", group, resource, name)
283func createKeyForArc(a Arc) string {
284 return fmt.Sprintf("%s -> %s", keyForVertex(a.Source), keyForVertex(a.Destination))
Diagnostics8 git calls · 285 ms
CallStatusSizeTime0285 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200200 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=200
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=9988d47506dcbe3004b8b72cbdb9d66d6d75cda4
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=200
git.pkt.payload.max=173
git.sideband.band1.bytes=173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=173
git.pack.trailer=948ee807a1ee817af67f2df94421a202e82325f9
Open pack map
fetchtree:0200289 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=289
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=86a13f848f75f5d885c75aa02d7eff6ecdc7349d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=289
git.pkt.payload.max=262
git.sideband.band1.bytes=262
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=262
git.pack.trailer=7f5e9a5963305286f535c0b6b2cedf1990871d65
Open pack map
fetch2002.0 KiB271 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=271
git.response.bytes=2053
git.response.cache=MISS
git.response.duration_ms=271
git.fetch.wants=4f07b637316bce0e657c243f90d8c47605177830
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2053
git.pkt.payload.max=2026
git.sideband.band1.bytes=2026
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e2914468a124d3
Request
8b193686…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format