-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathheartbeat.go
209 lines (181 loc) · 5.35 KB
/
heartbeat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package heartbeat
import (
"fmt"
"os"
"regexp"
"runtime"
"strings"
"github.com/wakatime/wakatime-cli/pkg/log"
"github.com/wakatime/wakatime-cli/pkg/system"
"github.com/wakatime/wakatime-cli/pkg/version"
"github.com/matishsiao/goInfo"
)
// remoteAddressRegex is a pattern for (ssh|sftp)://user:pass@host:port.
var remoteAddressRegex = regexp.MustCompile(`(?i)^((ssh|sftp)://)+(?P<credentials>[^:@]+(:([^:@])+)?@)?[^:]+(:\d+)?`)
// Heartbeat is a structure representing activity for a user on a some entity.
type Heartbeat struct {
ApiKey string `json:"-"`
Branch *string `json:"branch"`
BranchAlternate string `json:"-"`
Category Category `json:"category"`
CursorPosition *int `json:"cursorpos"`
Dependencies []string `json:"dependencies"`
Entity string `json:"entity"`
EntityType EntityType `json:"type"`
IsUnsavedEntity bool `json:"-"`
IsWrite *bool `json:"is_write"`
Language *string `json:"language"`
LanguageAlternate string `json:"-"`
LineNumber *int `json:"lineno"`
Lines *int `json:"lines"`
LocalFile string `json:"-"`
LocalFileNeedsCleanup bool `json:"-"`
Project *string `json:"project"`
ProjectAlternate string `json:"-"`
ProjectOverride string `json:"-"`
ProjectPath string `json:"-"`
ProjectPathOverride string `json:"-"`
Time float64 `json:"time"`
UserAgent string `json:"user_agent"`
}
// New creates a new instance of Heartbeat with formatted entity
// and local file paths for file type heartbeats.
func New(
branchAlternate string,
category Category,
cursorPosition *int,
entity string,
entityType EntityType,
isUnsavedEntity bool,
isWrite *bool,
language *string,
languageAlternate string,
lineNumber *int,
lines *int,
localFile string,
projectAlternate string,
projectOverride string,
projectPathOverride string,
time float64,
userAgent string,
) Heartbeat {
return Heartbeat{
BranchAlternate: branchAlternate,
Category: category,
CursorPosition: cursorPosition,
Entity: entity,
EntityType: entityType,
IsUnsavedEntity: isUnsavedEntity,
IsWrite: isWrite,
Language: language,
LanguageAlternate: languageAlternate,
LineNumber: lineNumber,
Lines: lines,
LocalFile: localFile,
ProjectAlternate: projectAlternate,
ProjectOverride: projectOverride,
ProjectPathOverride: projectPathOverride,
Time: time,
UserAgent: userAgent,
}
}
// ID returns an ID generated from the heartbeat data.
func (h Heartbeat) ID() string {
var branch string
if h.Branch != nil {
branch = *h.Branch
}
var project string
if h.Project != nil {
project = *h.Project
}
var isWrite bool
if h.IsWrite != nil {
isWrite = *h.IsWrite
}
return fmt.Sprintf("%f-%s-%s-%s-%s-%s-%t",
h.Time,
h.EntityType,
h.Category,
project,
branch,
h.Entity,
isWrite,
)
}
// IsRemote returns true when entity is a remote file.
func (h Heartbeat) IsRemote() bool {
if h.EntityType != FileType {
return false
}
if h.IsUnsavedEntity {
return false
}
return remoteAddressRegex.MatchString(h.Entity)
}
// Result represents a response from the wakatime api.
type Result struct {
Errors []string
Status int
Heartbeat Heartbeat
}
// Sender sends heartbeats to the wakatime api.
type Sender interface {
SendHeartbeats(hh []Heartbeat) ([]Result, error)
}
// Noop is a noop api client, used to always skip sending to the API.
type Noop struct{}
// SendHeartbeats always returns nil.
func (Noop) SendHeartbeats(_ []Heartbeat) ([]Result, error) {
return nil, nil
}
// Handle does processing of heartbeats.
type Handle func(hh []Heartbeat) ([]Result, error)
// HandleOption is a function, which allows chaining multiple Handles.
type HandleOption func(next Handle) Handle
// NewHandle creates a new Handle, which acts like a processing pipeline,
// with a sender eventually sending the heartbeats.
func NewHandle(sender Sender, opts ...HandleOption) Handle {
return func(heartbeats []Heartbeat) ([]Result, error) {
var handle Handle = sender.SendHeartbeats
for i := len(opts) - 1; i >= 0; i-- {
handle = opts[i](handle)
}
return handle(heartbeats)
}
}
// UserAgent generates a user agent from various system infos, including a
// a passed in value for plugin.
func UserAgent(plugin string) string {
info, err := goInfo.GetInfo()
if err != nil {
log.Debugf("goInfo.GetInfo error: %s", err)
}
if plugin == "" {
plugin = "Unknown/0"
}
return fmt.Sprintf(
"wakatime/%s (%s-%s-%s) %s %s",
version.Version,
system.OSName(),
info.Core,
info.Platform,
runtime.Version(),
plugin,
)
}
// PluginFromUserAgent parses the plugin name from a wakatime user agent.
func PluginFromUserAgent(userAgent string) string {
splitted := strings.Split(userAgent, " ")
splitted = strings.Split(splitted[len(splitted)-1], "/")
splitted = strings.Split(splitted[0], "-")
return splitted[0]
}
// PointerTo returns a pointer to the value passed in.
func PointerTo[t bool | int | string](v t) *t {
return &v
}
func isDir(filepath string) bool {
info, _ := os.Stat(filepath)
return info.IsDir()
}