@@ -6,6 +6,8 @@ package utils
66
77import (
88 "context"
9+ "fmt"
10+ "time"
911
1012 gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
1113 "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
@@ -14,41 +16,100 @@ import (
1416 log "github.com/sirupsen/logrus"
1517)
1618
19+ const (
20+ // Rebuild
21+ RebuildErrorCode_MalformedGitpodYaml = "rebuild_malformed_gitpod_yaml"
22+ RebuildErrorCode_MissingGitpodYaml = "rebuild_missing_gitpod_yaml"
23+ RebuildErrorCode_DockerfileNotFound = "rebuild_dockerfile_not_found"
24+ RebuildErrorCode_DockerfileEmpty = "rebuild_dockerfile_empty"
25+ RebuildErrorCode_DockerNotFound = "rebuild_docker_not_found"
26+ RebuildErrorCode_DockerBuildFailed = "rebuild_docker_build_failed"
27+ RebuildErrorCode_DockerErr = "rebuild_docker_err"
28+ )
29+
1730type TrackCommandUsageParams struct {
1831 Command string `json:"command,omitempty"`
19- DurationMs int64 `json:"duration,omitempty"`
32+ DurationMs int64 `json:"durationMs,omitempty"`
33+ Success bool `json:"success,omitempty"`
34+ ErrorCode string `json:"errorCode,omitempty"`
2035 WorkspaceId string `json:"workspaceId,omitempty"`
2136 WorkspaceInstanceId string `json:"workspaceInstanceId,omitempty"`
2237 Timestamp int64 `json:"timestamp,omitempty"`
2338}
2439
25- func TrackUsage (ctx context.Context , supervisorClient * supervisor.SupervisorClient , cmdParams * TrackCommandUsageParams ) {
40+ type EventTracker struct {
41+ data * TrackCommandUsageParams
42+ startTime time.Time
43+ serverClient * serverapi.APIoverJSONRPC
44+ supervisorClient * supervisor.SupervisorClient
45+ }
46+
47+ func TrackEvent (ctx context.Context , supervisorClient * supervisor.SupervisorClient , cmdParams * TrackCommandUsageParams ) * EventTracker {
48+ tracker := & EventTracker {
49+ startTime : time .Now (),
50+ supervisorClient : supervisorClient ,
51+ }
52+
2653 wsInfo , err := supervisorClient .Info .WorkspaceInfo (ctx , & api.WorkspaceInfoRequest {})
2754 if err != nil {
2855 LogError (ctx , err , "Could not fetch the workspace info" , supervisorClient )
29- return
56+ return nil
3057 }
3158
32- client , err := gitpod .ConnectToServer (ctx , wsInfo , []string {"function:trackEvent" })
59+ serverClient , err := gitpod .ConnectToServer (ctx , wsInfo , []string {"function:trackEvent" })
3360 if err != nil {
3461 log .WithError (err ).Fatal ("error connecting to server" )
62+ return nil
3563 }
36- defer client .Close ()
64+ defer serverClient .Close ()
3765
38- params := & TrackCommandUsageParams {
66+ tracker .serverClient = serverClient
67+ tracker .data = & TrackCommandUsageParams {
3968 Command : cmdParams .Command ,
40- DurationMs : cmdParams .DurationMs ,
41- WorkspaceId : "" ,
42- WorkspaceInstanceId : "" ,
43- Timestamp : 0 ,
69+ DurationMs : 0 ,
70+ WorkspaceId : wsInfo .WorkspaceId ,
71+ WorkspaceInstanceId : wsInfo .InstanceId ,
72+ ErrorCode : "" ,
73+ Timestamp : time .Now ().UnixMilli (),
74+ }
75+
76+ return tracker
77+ }
78+
79+ func (t * EventTracker ) Set (key string , value interface {}) * EventTracker {
80+ switch key {
81+ case "Command" :
82+ t .data .Command = value .(string )
83+ case "Success" :
84+ t .data .Success = value .(bool )
85+ case "ErrorCode" :
86+ t .data .ErrorCode = value .(string )
87+ t .data .Success = false
88+ case "DurationMs" :
89+ t .data .DurationMs = value .(int64 )
90+ case "WorkspaceId" :
91+ t .data .WorkspaceId = value .(string )
92+ case "WorkspaceInstanceId" :
93+ t .data .WorkspaceInstanceId = value .(string )
94+ default :
95+ log .Warnf ("Field not allowed: '%s'" , key )
4496 }
97+ return t
98+ }
99+
100+ func (t * EventTracker ) Send (ctx context.Context ) {
101+ t .Set ("DurationMs" , time .Since (t .startTime ).Milliseconds ())
102+
45103 event := & serverapi.RemoteTrackMessage {
46104 Event : "gp_command" ,
47- Properties : * params ,
105+ Properties : t . data ,
48106 }
49107
50- err = client .TrackEvent (ctx , event )
108+ err := t . serverClient .TrackEvent (ctx , event )
51109 if err != nil {
52- LogError (ctx , err , "Could not track gp command event" , supervisorClient )
110+ LogError (ctx , err , "Could not track gp command event" , t .supervisorClient )
111+ return
53112 }
113+
114+ fmt .Println ("EVENT SENT TO SERVER" )
54115}
0 commit comments