@@ -6,6 +6,8 @@ package utils
6
6
7
7
import (
8
8
"context"
9
+ "fmt"
10
+ "time"
9
11
10
12
gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
11
13
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
@@ -14,41 +16,100 @@ import (
14
16
log "github.com/sirupsen/logrus"
15
17
)
16
18
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
+
17
30
type TrackCommandUsageParams struct {
18
31
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"`
20
35
WorkspaceId string `json:"workspaceId,omitempty"`
21
36
WorkspaceInstanceId string `json:"workspaceInstanceId,omitempty"`
22
37
Timestamp int64 `json:"timestamp,omitempty"`
23
38
}
24
39
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
+
26
53
wsInfo , err := supervisorClient .Info .WorkspaceInfo (ctx , & api.WorkspaceInfoRequest {})
27
54
if err != nil {
28
55
LogError (ctx , err , "Could not fetch the workspace info" , supervisorClient )
29
- return
56
+ return nil
30
57
}
31
58
32
- client , err := gitpod .ConnectToServer (ctx , wsInfo , []string {"function:trackEvent" })
59
+ serverClient , err := gitpod .ConnectToServer (ctx , wsInfo , []string {"function:trackEvent" })
33
60
if err != nil {
34
61
log .WithError (err ).Fatal ("error connecting to server" )
62
+ return nil
35
63
}
36
- defer client .Close ()
64
+ defer serverClient .Close ()
37
65
38
- params := & TrackCommandUsageParams {
66
+ tracker .serverClient = serverClient
67
+ tracker .data = & TrackCommandUsageParams {
39
68
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 )
44
96
}
97
+ return t
98
+ }
99
+
100
+ func (t * EventTracker ) Send (ctx context.Context ) {
101
+ t .Set ("DurationMs" , time .Since (t .startTime ).Milliseconds ())
102
+
45
103
event := & serverapi.RemoteTrackMessage {
46
104
Event : "gp_command" ,
47
- Properties : * params ,
105
+ Properties : t . data ,
48
106
}
49
107
50
- err = client .TrackEvent (ctx , event )
108
+ err := t . serverClient .TrackEvent (ctx , event )
51
109
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
53
112
}
113
+
114
+ fmt .Println ("EVENT SENT TO SERVER" )
54
115
}
0 commit comments