Skip to content

Commit ffede8e

Browse files
authored
Merge pull request #1213 from wakatime/feature/line-additions-deletions
Add AI and Human line additions and deletions
2 parents 7f01a85 + cec711e commit ffede8e

File tree

11 files changed

+442
-335
lines changed

11 files changed

+442
-335
lines changed

cmd/heartbeat/heartbeat.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,19 @@ func buildHeartbeats(ctx context.Context, params params.Params) []heartbeat.Hear
211211
var heartbeats = make([]heartbeat.Heartbeat, 0, 1+len(params.Heartbeat.ExtraHeartbeats))
212212

213213
heartbeats = append(heartbeats, heartbeat.New(
214+
params.Heartbeat.AIAdditions,
215+
params.Heartbeat.AIDeletions,
214216
params.Heartbeat.Project.BranchAlternate,
215217
params.Heartbeat.Category.String(),
216218
params.Heartbeat.CursorPosition,
217219
params.Heartbeat.Entity,
218220
params.Heartbeat.EntityType,
221+
params.Heartbeat.HumanAdditions,
222+
params.Heartbeat.HumanDeletions,
219223
params.Heartbeat.IsUnsavedEntity,
220224
params.Heartbeat.IsWrite,
221225
params.Heartbeat.Language,
222226
params.Heartbeat.LanguageAlternate,
223-
params.Heartbeat.LineAdditions,
224-
params.Heartbeat.LineDeletions,
225227
params.Heartbeat.LineNumber,
226228
params.Heartbeat.LinesInFile,
227229
params.Heartbeat.LocalFile,

cmd/offline/offline.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,19 @@ func buildHeartbeats(ctx context.Context, params params.Params) []heartbeat.Hear
8181
userAgent := heartbeat.UserAgent(ctx, params.API.Plugin)
8282

8383
heartbeats = append(heartbeats, heartbeat.New(
84+
params.Heartbeat.AIAdditions,
85+
params.Heartbeat.AIDeletions,
8486
params.Heartbeat.Project.BranchAlternate,
8587
params.Heartbeat.Category.String(),
8688
params.Heartbeat.CursorPosition,
8789
params.Heartbeat.Entity,
8890
params.Heartbeat.EntityType,
91+
params.Heartbeat.HumanAdditions,
92+
params.Heartbeat.HumanDeletions,
8993
params.Heartbeat.IsUnsavedEntity,
9094
params.Heartbeat.IsWrite,
9195
params.Heartbeat.Language,
9296
params.Heartbeat.LanguageAlternate,
93-
params.Heartbeat.LineAdditions,
94-
params.Heartbeat.LineDeletions,
9597
params.Heartbeat.LineNumber,
9698
params.Heartbeat.LinesInFile,
9799
params.Heartbeat.LocalFile,

cmd/root.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func NewRootCMD() *cobra.Command {
5252

5353
func setFlags(cmd *cobra.Command, v *viper.Viper) {
5454
flags := cmd.Flags()
55+
flags.Int("ai-additions", 0, "Optional number of lines added by AI since last heartbeat in the current file.")
56+
flags.Int("ai-deletions", 0, "Optional number of lines deleted by AI since last heartbeat in the current file.")
5557
flags.String("alternate-branch", "", "Optional alternate branch name. Auto-detected branch takes priority.")
5658
flags.String("alternate-language", "", "Optional alternate language name. Auto-detected language takes priority.")
5759
flags.String("alternate-project", "", "Optional alternate project name. Auto-detected project takes priority.")
@@ -149,6 +151,8 @@ func setFlags(cmd *cobra.Command, v *viper.Viper) {
149151
" created with a random project name.",
150152
)
151153
flags.String("hostname", "", "Optional name of local machine. Defaults to local machine name read from system.")
154+
flags.Int("human-additions", 0, "Optional number of lines added by humans since last heartbeat in the current file.")
155+
flags.Int("human-deletions", 0, "Optional number of lines deleted by humans since last heartbeat in the current file.")
152156
flags.StringSlice(
153157
"include",
154158
nil,
@@ -175,8 +179,6 @@ func setFlags(cmd *cobra.Command, v *viper.Viper) {
175179
0,
176180
"Optional lines in the file. Normally, this is detected automatically but"+
177181
" can be provided manually for performance, accuracy, or when using --local-file.")
178-
flags.Int("line-additions", 0, "Optional number of lines added since last heartbeat in the current file.")
179-
flags.Int("line-deletions", 0, "Optional number of lines deleted since last heartbeat in the current file.")
180182
flags.String(
181183
"local-file",
182184
"",

main_test.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ func testSendHeartbeats(t *testing.T, projectFolder, entity, prj string) {
138138
"--cursorpos", "12",
139139
"--offline-queue-file", offlineQueueFile.Name(),
140140
"--offline-queue-file-legacy", offlineQueueFileLegacy.Name(),
141-
"--line-additions", "123",
142-
"--line-deletions", "456",
141+
"--ai-additions", "123",
142+
"--ai-deletions", "456",
143+
"--human-additions", "8999",
144+
"--human-deletions", "145",
143145
"--lineno", "42",
144146
"--lines-in-file", "100",
145147
"--time", "1585598059",
@@ -233,8 +235,10 @@ func TestSendHeartbeats_SecondaryApiKey(t *testing.T) {
233235
"--cursorpos", "12",
234236
"--offline-queue-file", offlineQueueFile.Name(),
235237
"--offline-queue-file-legacy", offlineQueueFileLegacy.Name(),
236-
"--line-additions", "123",
237-
"--line-deletions", "456",
238+
"--ai-additions", "123",
239+
"--ai-deletions", "456",
240+
"--human-additions", "8999",
241+
"--human-deletions", "145",
238242
"--lineno", "42",
239243
"--lines-in-file", "100",
240244
"--time", "1585598059",
@@ -314,8 +318,10 @@ func TestSendHeartbeats_Timeout(t *testing.T) {
314318
"--cursorpos", "12",
315319
"--offline-queue-file", offlineQueueFile.Name(),
316320
"--offline-queue-file-legacy", offlineQueueFileLegacy.Name(),
317-
"--line-additions", "123",
318-
"--line-deletions", "456",
321+
"--ai-additions", "123",
322+
"--ai-deletions", "456",
323+
"--human-additions", "8999",
324+
"--human-deletions", "145",
319325
"--lineno", "42",
320326
"--lines-in-file", "100",
321327
"--time", "1585598059",
@@ -926,8 +932,10 @@ func TestSendHeartbeats_Err(t *testing.T) {
926932
"--cursorpos", "12",
927933
"--offline-queue-file", offlineQueueFile.Name(),
928934
"--offline-queue-file-legacy", offlineQueueFileLegacy.Name(),
929-
"--line-additions", "123",
930-
"--line-deletions", "456",
935+
"--ai-additions", "123",
936+
"--ai-deletions", "456",
937+
"--human-additions", "8999",
938+
"--human-deletions", "145",
931939
"--lineno", "42",
932940
"--lines-in-file", "100",
933941
"--time", "1585598059",

pkg/api/heartbeat.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func parseHeartbeatResponse(ctx context.Context, data []json.RawMessage) (heartb
163163
}
164164

165165
var responseBodyData responseBody
166+
166167
err = json.Unmarshal(data[0], &responseBodyData)
167168
if err != nil {
168169
return heartbeat.Result{}, fmt.Errorf("failed to parse json heartbeat: %s", err)

pkg/heartbeat/heartbeat.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var remoteAddressRegex = regexp.MustCompile(`(?i)^((ssh|sftp)://)+(?P<credential
2020

2121
// Heartbeat is a structure representing activity for a user on a some entity.
2222
type Heartbeat struct {
23+
AIAdditions *int `json:"ai_additions,omitempty"`
24+
AIDeletions *int `json:"ai_deletions,omitempty"`
2325
APIKey string `json:"-"`
2426
Branch *string `json:"branch,omitempty"`
2527
BranchAlternate string `json:"-"`
@@ -28,12 +30,12 @@ type Heartbeat struct {
2830
Dependencies []string `json:"dependencies,omitempty"`
2931
Entity string `json:"entity"`
3032
EntityType EntityType `json:"type"`
33+
HumanAdditions *int `json:"human_additions,omitempty"`
34+
HumanDeletions *int `json:"human_deletions,omitempty"`
3135
IsUnsavedEntity bool `json:"-"`
3236
IsWrite *bool `json:"is_write,omitempty"`
3337
Language *string `json:"language,omitempty"`
3438
LanguageAlternate string `json:"-"`
35-
LineAdditions *int `json:"line_additions,omitempty"`
36-
LineDeletions *int `json:"line_deletions,omitempty"`
3739
LineNumber *int `json:"lineno,omitempty"`
3840
Lines *int `json:"lines,omitempty"`
3941
LocalFile string `json:"-"`
@@ -52,17 +54,19 @@ type Heartbeat struct {
5254
// New creates a new instance of Heartbeat with formatted entity
5355
// and local file paths for file type heartbeats.
5456
func New(
57+
adAdditions *int,
58+
aiDeletions *int,
5559
branchAlternate string,
5660
category string,
5761
cursorPosition *int,
5862
entity string,
5963
entityType EntityType,
64+
humanAdditions *int,
65+
humanDeletions *int,
6066
isUnsavedEntity bool,
6167
isWrite *bool,
6268
language *string,
6369
languageAlternate string,
64-
lineAdditions *int,
65-
lineDeletions *int,
6670
lineNumber *int,
6771
lines *int,
6872
localFile string,
@@ -74,17 +78,19 @@ func New(
7478
userAgent string,
7579
) Heartbeat {
7680
return Heartbeat{
81+
AIAdditions: adAdditions,
82+
AIDeletions: aiDeletions,
7783
BranchAlternate: branchAlternate,
7884
Category: category,
7985
CursorPosition: cursorPosition,
8086
Entity: entity,
8187
EntityType: entityType,
88+
HumanAdditions: humanAdditions,
89+
HumanDeletions: humanDeletions,
8290
IsUnsavedEntity: isUnsavedEntity,
8391
IsWrite: isWrite,
8492
Language: language,
8593
LanguageAlternate: languageAlternate,
86-
LineAdditions: lineAdditions,
87-
LineDeletions: lineDeletions,
8894
LineNumber: lineNumber,
8995
Lines: lines,
9096
LocalFile: localFile,

pkg/heartbeat/heartbeat_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@ import (
2020

2121
func TestNew(t *testing.T) {
2222
h := heartbeat.New(
23+
heartbeat.PointerTo(5),
24+
heartbeat.PointerTo(3),
2325
"feature/branch",
2426
heartbeat.CodingCategory.String(),
2527
heartbeat.PointerTo(12),
2628
"testdata/main.go",
2729
heartbeat.FileType,
30+
heartbeat.PointerTo(2),
31+
heartbeat.PointerTo(3),
2832
true,
2933
heartbeat.PointerTo(true),
3034
heartbeat.PointerTo("Go"),
3135
"Golang",
32-
heartbeat.PointerTo(2),
33-
heartbeat.PointerTo(3),
3436
heartbeat.PointerTo(42),
35-
nil,
37+
heartbeat.PointerTo(100),
3638
"/path/to/file",
3739
"billing",
3840
false,
@@ -45,17 +47,20 @@ func TestNew(t *testing.T) {
4547
assert.True(t, strings.HasSuffix(h.Entity, "testdata/main.go"))
4648

4749
assert.Equal(t, heartbeat.Heartbeat{
50+
AIAdditions: heartbeat.PointerTo(5),
51+
AIDeletions: heartbeat.PointerTo(3),
4852
BranchAlternate: "feature/branch",
4953
Category: heartbeat.CodingCategory.String(),
5054
CursorPosition: heartbeat.PointerTo(12),
5155
EntityType: heartbeat.FileType,
56+
HumanAdditions: heartbeat.PointerTo(2),
57+
HumanDeletions: heartbeat.PointerTo(3),
5258
IsUnsavedEntity: true,
5359
IsWrite: heartbeat.PointerTo(true),
5460
Language: heartbeat.PointerTo("Go"),
5561
LanguageAlternate: "Golang",
56-
LineAdditions: heartbeat.PointerTo(2),
57-
LineDeletions: heartbeat.PointerTo(3),
5862
LineNumber: heartbeat.PointerTo(42),
63+
Lines: heartbeat.PointerTo(100),
5964
LocalFile: "/path/to/file",
6065
ProjectAlternate: "billing",
6166
ProjectOverride: "pci",
@@ -90,16 +95,18 @@ func TestHeartbeat_ID_NilFields(t *testing.T) {
9095

9196
func TestHeartbeat_JSON(t *testing.T) {
9297
h := heartbeat.Heartbeat{
98+
AIAdditions: heartbeat.PointerTo(5),
99+
AIDeletions: heartbeat.PointerTo(3),
93100
Branch: heartbeat.PointerTo("heartbeat"),
94101
Category: heartbeat.DebuggingCategory.String(),
95102
CursorPosition: heartbeat.PointerTo(12),
96103
Dependencies: []string{"dep1", "dep2"},
97104
Entity: "/tmp/main.go",
98105
EntityType: heartbeat.FileType,
106+
HumanAdditions: heartbeat.PointerTo(2),
107+
HumanDeletions: heartbeat.PointerTo(3),
99108
IsWrite: heartbeat.PointerTo(true),
100109
Language: heartbeat.PointerTo("Go"),
101-
LineAdditions: heartbeat.PointerTo(123),
102-
LineDeletions: heartbeat.PointerTo(456),
103110
LineNumber: heartbeat.PointerTo(42),
104111
Lines: heartbeat.PointerTo(100),
105112
Project: heartbeat.PointerTo("wakatime"),

pkg/heartbeat/testdata/heartbeat.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
2+
"ai_additions": 5,
3+
"ai_deletions": 3,
24
"branch": "heartbeat",
35
"category": "debugging",
46
"cursorpos": 12,
57
"dependencies": ["dep1", "dep2"],
68
"entity": "/tmp/main.go",
9+
"human_additions": 2,
10+
"human_deletions": 3,
711
"is_write": true,
812
"language": "Go",
913
"lineno": 42,
1014
"lines": 100,
11-
"line_additions": 123,
12-
"line_deletions": 456,
1315
"project": "wakatime",
1416
"type": "file",
1517
"time": 1585598060.1,

0 commit comments

Comments
 (0)