Skip to content

Commit 3032030

Browse files
authored
Merge branch 'main' into PC-14729-update-messages-and-links
2 parents 66f3880 + ea7646e commit 3032030

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

cspell.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ words:
4444
- slos
4545
- svcs
4646
- tpng
47+
- unmarshalling
4748
- vuln
4849
- vulns
4950
- wrapf

internal/apply.go

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (a ApplyCmd) runReplay(cmd *cobra.Command, objects []manifest.Object) error
105105
return nil
106106
}
107107
replayCmd := ReplayCmd{client: a.client}
108+
replayCmd.arePlaylistEnabled(cmd.Context())
108109
replays := make([]ReplayConfig, 0, len(slos))
109110
for _, slo := range slos {
110111
replays = append(replays, ReplayConfig{

internal/replay.go

+38-22
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ import (
2929
)
3030

3131
type ReplayCmd struct {
32-
client *sdk.Client
33-
from TimeValue
34-
configPaths []string
35-
sloName string
36-
project string
37-
deleteAll bool
32+
client *sdk.Client
33+
from TimeValue
34+
configPaths []string
35+
sloName string
36+
project string
37+
deleteAll bool
38+
playlistsAvailable bool
3839
}
3940

4041
//go:embed replay_example.sh
@@ -76,6 +77,7 @@ func (r *ReplayCmd) Run(cmd *cobra.Command) error {
7677
if r.client.Config.Project == "*" {
7778
return errProjectWildcardIsNotAllowed
7879
}
80+
r.arePlaylistEnabled(cmd.Context())
7981
replays, err := r.prepareConfigs()
8082
if err != nil {
8183
return err
@@ -89,9 +91,7 @@ func (r *ReplayCmd) RunReplays(cmd *cobra.Command, replays []ReplayConfig) (fail
8991
return 0, err
9092
}
9193

92-
arePlaylistEnabled := r.arePlaylistEnabled(cmd.Context())
93-
94-
if arePlaylistEnabled {
94+
if r.playlistsAvailable {
9595
cmd.Println(colorstring.Color("[yellow]- Your organization has access to Replay queues!"))
9696
cmd.Println(colorstring.Color("[yellow]- To learn more about Replay queues, follow this link: " +
9797
"https://docs.nobl9.com/replay/replay-sloctl [reset]"))
@@ -104,7 +104,7 @@ func (r *ReplayCmd) RunReplays(cmd *cobra.Command, replays []ReplayConfig) (fail
104104
i+1, len(replays), replay.SLO, replay.Project,
105105
replay.From.Format(timeLayout), time.Now().In(replay.From.Location()).Format(timeLayout))))
106106

107-
if arePlaylistEnabled {
107+
if r.playlistsAvailable {
108108
cmd.Println("Replay is added to the queue...")
109109
err = r.runReplay(cmd.Context(), replay)
110110

@@ -134,7 +134,12 @@ func (r *ReplayCmd) RunReplays(cmd *cobra.Command, replays []ReplayConfig) (fail
134134
return len(failedIndexes), nil
135135
}
136136

137-
func (r *ReplayCmd) arePlaylistEnabled(ctx context.Context) bool {
137+
type PlaylistConfiguration struct {
138+
EnabledPlaylists bool `json:"enabledPlaylists"`
139+
}
140+
141+
func (r *ReplayCmd) arePlaylistEnabled(ctx context.Context) {
142+
r.playlistsAvailable = true
138143
data, _, err := r.doRequest(
139144
ctx,
140145
http.MethodGet,
@@ -143,17 +148,13 @@ func (r *ReplayCmd) arePlaylistEnabled(ctx context.Context) bool {
143148
nil,
144149
nil)
145150
if err != nil {
146-
return true
151+
fmt.Printf("error checking playlist availability: %v\n", err)
147152
}
148153
var pc PlaylistConfiguration
149154
if err = json.Unmarshal(data, &pc); err != nil {
150-
return true
155+
fmt.Printf("error unmarshalling playlist configuration: %v\n", err)
151156
}
152-
return pc.EnabledPlaylists
153-
}
154-
155-
type PlaylistConfiguration struct {
156-
EnabledPlaylists bool `json:"enabledPlaylists"`
157+
r.playlistsAvailable = pc.EnabledPlaylists
157158
}
158159

159160
type ReplayConfig struct {
@@ -345,16 +346,28 @@ outer:
345346
}
346347

347348
// Check Replay availability.
349+
if err := r.checkReplayAvailability(ctx, replays); err != nil {
350+
return err
351+
}
352+
353+
return nil
354+
}
355+
356+
func (r *ReplayCmd) checkReplayAvailability(ctx context.Context, replays []ReplayConfig) error {
348357
notAvailable := make([]string, 0)
349358
mu := sync.Mutex{}
350359
eg, ctx := errgroup.WithContext(ctx)
351360
eg.SetLimit(10)
361+
352362
for i := range replays {
353363
eg.Go(func() error {
354364
c := replays[i]
355365
timeNow := time.Now()
356366
tt := c.ToReplay(timeNow)
357-
offset := i * int(averageReplayDuration.Minutes())
367+
offset := 0
368+
if !r.playlistsAvailable {
369+
offset = i * int(averageReplayDuration.Minutes())
370+
}
358371
expectedDuration := offset + tt.Duration.Value
359372
av, err := r.getReplayAvailability(ctx, c, tt.Duration.Unit, expectedDuration)
360373
if err != nil {
@@ -363,6 +376,7 @@ outer:
363376
}
364377
if !av.Available {
365378
mu.Lock()
379+
defer mu.Unlock()
366380
notAvailable = append(notAvailable,
367381
fmt.Sprintf("['%s' SLO in '%s' Project] %s",
368382
c.SLO, c.Project, r.replayUnavailabilityReasonExplanation(
@@ -371,18 +385,20 @@ outer:
371385
time.Duration(expectedDuration)*time.Minute,
372386
time.Duration(offset)*time.Minute,
373387
timeNow)))
374-
mu.Unlock()
375388
}
376389
return nil
377390
})
378391
}
379-
if err = eg.Wait(); err != nil {
392+
393+
if err := eg.Wait(); err != nil {
380394
return err
381395
}
396+
382397
if len(notAvailable) > 0 {
383398
return errors.Errorf("The following SLOs are not available for Replay: \n - %s",
384399
strings.Join(notAvailable, "\n - "))
385400
}
401+
386402
return nil
387403
}
388404

@@ -542,7 +558,7 @@ func (r *ReplayCmd) replayUnavailabilityReasonExplanation(
542558
" + %dm (start offset to ensure Replay covers the desired time window) %s."+
543559
" Edit the Data Source and run Replay once again.",
544560
replay.metricSource.Name, replay.metricSource.Project, expectedDuration.String(),
545-
timeNow.Format(timeLayout), r.from.Format(timeLayout), startOffsetMinutes, offsetNotice)
561+
timeNow.Format(timeLayout), replay.From.Format(timeLayout), startOffsetMinutes, offsetNotice)
546562
case sdkModels.ReplayConcurrentReplayRunsLimitExhausted:
547563
return "You've exceeded the limit of concurrent Replay runs. Wait until the current Replay(s) are done."
548564
case sdkModels.ReplayUnknownAgentVersion:

0 commit comments

Comments
 (0)