Skip to content

Commit f30021b

Browse files
Publish analysis utils
PiperOrigin-RevId: 883189301
1 parent 05db8d8 commit f30021b

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

analysis/util/alertwindow.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Package analysisutil provides utility functions.
2+
package analysisutil
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"os"
8+
"time"
9+
10+
cdenv "github.com/GoogleCloudPlatform/cloud-deploy-samples/packages/cdenv"
11+
cdapi "google.golang.org/api/clouddeploy/v1"
12+
)
13+
14+
// AlertTimeWindow holds the rollout start time and the end time to look for alerts (which is now).
15+
type AlertTimeWindow struct {
16+
// StartTime is the start time of the alert window in RFC3339 format.
17+
StartTime time.Time
18+
// EndTime is the end time of the alert window in RFC3339 format.
19+
EndTime time.Time
20+
}
21+
22+
// TimeWindow returns the start time of the rollout as a string representing the number of
23+
// milliseconds since the Unix epoch (this is the time format expected by Datadog).
24+
func TimeWindow(ctx context.Context) (*AlertTimeWindow, error) {
25+
// Construct the rollout resource name.
26+
projectID := os.Getenv(cdenv.ProjectIDEnvKey)
27+
location := os.Getenv(cdenv.LocationEnvKey)
28+
pipelineID := os.Getenv(cdenv.PipelineEnvKey)
29+
releaseID := os.Getenv(cdenv.ReleaseEnvKey)
30+
rolloutID := os.Getenv(cdenv.RolloutEnvKey)
31+
32+
rolloutName := fmt.Sprintf("projects/%s/locations/%s/deliveryPipelines/%s/releases/%s/rollouts/%s",
33+
projectID, location, pipelineID, releaseID, rolloutID)
34+
35+
cdService, err := cdapi.NewService(ctx)
36+
if err != nil {
37+
return nil, fmt.Errorf("unable to create Cloud Deploy API service: %v", err)
38+
}
39+
40+
rollout, err := cdService.Projects.Locations.DeliveryPipelines.Releases.Rollouts.Get(rolloutName).Do()
41+
if err != nil {
42+
return nil, fmt.Errorf("unable to get rollout from Cloud Deploy API: %v", err)
43+
}
44+
45+
parsedStartTime, err := time.Parse(time.RFC3339, rollout.DeployStartTime)
46+
if err != nil {
47+
return nil, fmt.Errorf("unable to convert start time to RFC3339 format in order to validate time")
48+
}
49+
50+
endTime := time.Now().Format(time.RFC3339)
51+
parsedEndTime, err := time.Parse(time.RFC3339, endTime)
52+
if err != nil {
53+
return nil, fmt.Errorf("unable to convert start time to RFC3339 format in order to validate time")
54+
}
55+
if parsedStartTime.After(parsedEndTime) {
56+
return nil, fmt.Errorf("start time is after end time")
57+
}
58+
59+
return &AlertTimeWindow{StartTime: parsedStartTime, EndTime: parsedEndTime}, nil
60+
}

analysis/util/gcs.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Package analysisutil provides utilities for uploading analysis results to GCS.
2+
package analysisutil
3+
4+
import (
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"os"
9+
10+
"cloud.google.com/go/storage"
11+
cdenv "github.com/GoogleCloudPlatform/cloud-deploy-samples/packages/cdenv"
12+
"github.com/GoogleCloudPlatform/cloud-deploy-samples/packages/gcs"
13+
)
14+
15+
// AnalysisMetadata represents the Analysis result metadata that will be uploaded to GCS.
16+
type AnalysisMetadata struct {
17+
// Metadata contains metadata associated with the analysis result.
18+
Metadata map[string]string `json:"metadata,omitempty"`
19+
}
20+
21+
// UploadResult uploads the result to GCS.
22+
func UploadResult(ctx context.Context, analysisMetadata *AnalysisMetadata, client *storage.Client) error {
23+
data, err := json.Marshal(analysisMetadata)
24+
if err != nil {
25+
return fmt.Errorf("failed to marshal analysis metadata: %v, error: %w", analysisMetadata, err)
26+
}
27+
// Get the GCS URI where the results file should be uploaded. The full path is in the format of
28+
// {outputPath}/{gcs.ResultObjectSuffix}.
29+
outputPath := os.Getenv(cdenv.OutputGCSEnvKey)
30+
uri := fmt.Sprintf("%s/%s", outputPath, gcs.ResultObjectSuffix)
31+
return gcs.Upload(ctx, client, uri, &gcs.UploadContent{Data: data})
32+
}

0 commit comments

Comments
 (0)