Skip to content

Commit

Permalink
snapshot: include creation timestamp (#5802)
Browse files Browse the repository at this point in the history
Historically, we've relied on the Tilt Cloud server timestamp for
snapshots. With the new offline flow, this isn't available, so a
new field is added with the create time.
  • Loading branch information
milas authored May 11, 2022
1 parent 4e6195e commit d7f59cb
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 69 deletions.
4 changes: 3 additions & 1 deletion internal/cli/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/pkg/browser"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/tilt-dev/tilt/internal/analytics"
engineanalytics "github.com/tilt-dev/tilt/internal/engine/analytics"
Expand Down Expand Up @@ -197,7 +198,8 @@ func createSnapshot(cmd *cobra.Command, args []string) {
body := apiGet("view")

snapshot := proto_webview.Snapshot{
View: &proto_webview.View{},
View: &proto_webview.View{},
CreatedAt: timestamppb.Now(),
}

jsEncoder := &runtime.JSONPb{}
Expand Down
8 changes: 7 additions & 1 deletion internal/cloud/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"

"google.golang.org/protobuf/types/known/timestamppb"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
Expand Down Expand Up @@ -43,7 +44,12 @@ func (s *Snapshotter) WriteSnapshot(ctx context.Context, path string) {
_ = f.Close()
}()

err = WriteSnapshotTo(ctx, &proto_webview.Snapshot{View: view}, f)
snapshot := &proto_webview.Snapshot{
View: view,
CreatedAt: timestamppb.Now(),
}

err = WriteSnapshotTo(ctx, snapshot, f)
if err != nil {
logger.Get(ctx).Errorf("Writing snapshot to file: %v", err)
return
Expand Down
7 changes: 5 additions & 2 deletions internal/cloud/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/golang/protobuf/ptypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/tilt-dev/tilt/internal/hud/webview"
Expand All @@ -30,17 +31,18 @@ func TestWriteSnapshotTo(t *testing.T) {
Spec: v1alpha1.TiltfileSpec{Path: "Tiltfile"},
},
})
now := time.Unix(1551202573, 0)
snapshot := &proto_webview.Snapshot{
View: &proto_webview.View{
UiSession: webview.ToUISession(*state),
},
CreatedAt: timestamppb.New(now),
}

resources, err := webview.ToUIResourceList(*state, make(map[string][]v1alpha1.DisableSource))
require.NoError(t, err)
snapshot.View.UiResources = resources

now := time.Unix(1551202573, 0)
for _, r := range resources {
for i, cond := range r.Status.Conditions {
// Clear the transition timestamps so that the test is hermetic.
Expand Down Expand Up @@ -94,7 +96,8 @@ func TestWriteSnapshotTo(t *testing.T) {
}
}
]
}
},
"createdAt": "2019-02-26T17:36:13Z"
}
`, buf.String())
}
15 changes: 12 additions & 3 deletions internal/hud/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
_ "net/http/pprof"

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"

"github.com/golang/protobuf/jsonpb"
Expand Down Expand Up @@ -162,11 +164,14 @@ func (s *HeadsUpServer) SnapshotJSON(w http.ResponseWriter, req *http.Request) {
return
}

snapshot := &proto_webview.Snapshot{
View: view,
CreatedAt: timestamppb.Now(),
}

w.Header().Set("Content-Type", "application/json")
var m jsonpb.Marshaler
err = m.Marshal(w, &proto_webview.Snapshot{
View: view,
})
err = m.Marshal(w, snapshot)
if err != nil {
http.Error(w, fmt.Sprintf("Error rendering view payload: %v", err), http.StatusInternalServerError)
}
Expand Down Expand Up @@ -347,6 +352,10 @@ func (s *HeadsUpServer) HandleNewSnapshot(w http.ResponseWriter, req *http.Reque
return
}

if snapshot != nil && snapshot.GetCreatedAt().AsTime().IsZero() {
snapshot.CreatedAt = timestamppb.Now()
}

id, err := s.uploader.Upload(token, teamID, snapshot)
if err != nil {
msg := fmt.Sprintf("Error creating snapshot: %v", err)
Expand Down
136 changes: 74 additions & 62 deletions pkg/webview/view.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/webview/view.proto
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ message Snapshot {
string path = 3;
SnapshotHighlight snapshot_highlight = 4;
string snapshot_link = 5;
google.protobuf.Timestamp created_at = 6;
}

message UploadSnapshotResponse {
Expand Down
4 changes: 4 additions & 0 deletions pkg/webview/view.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,10 @@
},
"snapshot_link": {
"type": "string"
},
"created_at": {
"type": "string",
"format": "date-time"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions web/src/HUD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export default class HUD extends Component<HudProps, HudState> {
view: view,
path: this.props.history.location.pathname,
snapshotHighlight: state.snapshotHighlight,
createdAt: new Date().toISOString(),
}
}

Expand Down
1 change: 1 addition & 0 deletions web/src/view.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ declare namespace Proto {
path?: string;
snapshotHighlight?: webviewSnapshotHighlight;
snapshotLink?: string;
createdAt?: string;
}
export interface webviewResource {
name?: string;
Expand Down

0 comments on commit d7f59cb

Please sign in to comment.