Skip to content

Instantly share code, notes, and snippets.

@alanhamlett
Last active November 4, 2024 10:43
Show Gist options
  • Save alanhamlett/0d0c496fe40d977dbdbf0abf0b34ec57 to your computer and use it in GitHub Desktop.
Save alanhamlett/0d0c496fe40d977dbdbf0abf0b34ec57 to your computer and use it in GitHub Desktop.
Turn an array of heartbeats into durations by combining heartbeats that are within Keystroke Timeout minutes from each other.
/*
BSD 3-Clause License
Copyright (c) 2023 Alan Hamlett.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided
with the distribution.
* Neither the names of WakaTime, nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Returns an array of duration objects, same structure as the example response https://wakatime.com/developers/#durations
function processHeartbeatsIntoDurations(heartbeats, start, end, timeout, meetingsOverCoding, yesterdaysLastHeartbeat, tomorrowsFirstHeartbeat, externalDurations) {
var start = moment(start).unix();
var end = moment(end).unix();
if (heartbeats.length > 0) {
if (yesterdaysLastHeartbeat && heartbeats[0].time - yesterdaysLastHeartbeat.time < timeout * 60) {
yesterdaysLastHeartbeat.time = round(start, 6);
heartbeats.unshift(yesterdaysLastHeartbeat);
}
if (tomorrowsFirstHeartbeat && tomorrowsFirstHeartbeat.time - _.last(heartbeats).time < timeout * 60) {
tomorrowsFirstHeartbeat.time = round(end, 6);
heartbeats.push(tomorrowsFirstHeartbeat);
}
}
var mini_durations = heartbeatsToMiniDurations(heartbeats, timeout);
var mini_durations_with_ext = insertExternalDurations(mini_durations, start, end, meetingsOverCoding, externalDurations);
var durations = combineMiniDurations(mini_durations_with_ext, timeout);
return durations;
}
function heartbeatsToMiniDurations(heartbeats, timeout) {
return _.each(heartbeats, function (heartbeat, index) {
var duration = 0;
var next_heartbeat = index < heartbeats.length - 1 ? heartbeats[index + 1] : undefined;
heartbeat.project = heartbeat.project || 'Unknown Project';
heartbeat.language = heartbeat.language || 'Other';
if (next_heartbeat && next_heartbeat.time - heartbeat.time < timeout * 60) {
duration = next_heartbeat.time - heartbeat.time;
if (duration < 0) duration = 0;
}
heartbeat.duration = duration;
});
}
function insertExternalDurations(durations, start, end, meetingsOverCoding, externalDurations) {
var index = 0;
var maxindex = durations.length;
return _.each(externalDurations, function (ext) {
// skip external durations over 23 hrs long
if (ext.end_time - ext.start_time > 82800) return;
while (index < maxindex && durations[index].time + durations[index].duration <= ext.start_time) index++;
// a normal duration overlaps this external duration
if (index < maxindex && durations[index].time < ext.end_time) {
if (meetingsOverCoding && lower(ext.category) == 'meeting') {
while (index < maxindex && durations[index].time < ext.end_time) {
durations.splice(index, 1);
maxindex--;
}
} else {
return;
}
}
ext.time = ext.start_time;
if (ext.start_time < start) ext.time = start;
var end_time = ext.end_time;
if (end_time > end) end_time = end;
ext.duration = end_time - ext.time;
ext.project = ext.project || 'Unknown Project';
durations.splice(index, 0, ext);
index++;
maxindex++;
});
}
function combineMiniDurations(mini_durations, timeout) {
var last = null;
var durations = [];
for (var i = 0; i < mini_durations.length; i++) {
var duration = mini_durations[i];
// combine durations
var current = durations.length > 0 ? _.last(durations) : null;
if (current && shouldJoinDuration(duration, last, timeout)) {
var endtime = round(duration.time, 2) + round(duration.duration, 2);
current.duration = Math.round(endtime - round(current.time, 2));
if (current.duration < 0) current.duration = 0;
} else {
current = _.clone(duration);
durations.push(current);
}
last = duration;
}
return durations;
}
function shouldJoinDuration(duration, last, timeout) {
if (!last) return false;
if (lower(last.project) != lower(duration.project)) return false;
var last_time = last.time + last.duration;
var time_span = duration.time - last_time;
if (time_span < 0) time_span = 0;
if (time_span > timeout * 60) return false;
return true;
}
// Math.round with support for decimal precision
function round(number, precision) {
var factor = Math.pow(10, precision);
var tmp = number * factor;
var roundedTmp = Math.round(tmp);
return roundedTmp / factor;
}
function lower(str) {
if (str) return str.toLowerCase();
return str;
}
var heartbeats = [
{
id: '4864f7d3-f333-4b89-8085-854b8fc02ab9',
entity: '/Users/user/git/wakatime-cli/pkg/remote/remote.go',
type: 'file',
time: 1694761859.472183,
project: 'wakatime-cli',
project_root_count: 5,
branch: 'develop',
language: 'Go',
dependencies: [
'io',
'github.com/wakatime/wakatime-cli/pkg/heartbeat',
'github.com/wakatime/wakatime-cli/pkg/log',
'golang.org/x/crypto/ssh',
'golang.org/x/crypto/ssh/agent',
'github.com/pkg/sftp',
'bufio',
'time',
'strings',
'strconv',
'net/url',
'path/filepath',
'net',
'os/exec',
'github.com/mitchellh/go-homedir',
'github.com/kevinburke/ssh_config',
'context',
'os',
],
lines: 570,
lineno: null,
cursorpos: null,
is_write: false,
category: 'coding',
created_at: '2023-09-15T07:12:49Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: 'b0b05433-d6f4-49bf-a883-15394a873faa',
machine_name_id: '78a00491-9188-429f-9b84-ec8dde88a550',
},
{
id: 'f47929d7-0958-4be6-a756-6e264f0e95c0',
entity: '/Users/user/git/wakatime-cli/pkg/remote/remote.go',
type: 'file',
time: 1694761981.01841,
project: 'wakatime-cli',
project_root_count: 5,
branch: 'develop',
language: 'Go',
dependencies: [
'path/filepath',
'github.com/kevinburke/ssh_config',
'strings',
'time',
'bufio',
'net/url',
'strconv',
'github.com/pkg/sftp',
'context',
'os/exec',
'os',
'golang.org/x/crypto/ssh',
'net',
'io',
'github.com/mitchellh/go-homedir',
'github.com/wakatime/wakatime-cli/pkg/heartbeat',
'github.com/wakatime/wakatime-cli/pkg/log',
'golang.org/x/crypto/ssh/agent',
],
lines: 570,
lineno: null,
cursorpos: null,
is_write: false,
category: 'coding',
created_at: '2023-09-15T07:13:21Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: 'b0b05433-d6f4-49bf-a883-15394a873faa',
machine_name_id: '41437d2c-27a7-4ccb-91bc-519744a9a04a',
},
{
id: 'dbd4f798-e079-4cf2-9bd6-fc591110cbb1',
entity: '/Users/user/git/wakatime-cli/pkg/remote/remote.go',
type: 'file',
time: 1694762011.044992,
project: 'wakatime-cli',
project_root_count: 5,
branch: 'develop',
language: 'Go',
dependencies: [
'path/filepath',
'github.com/kevinburke/ssh_config',
'strings',
'time',
'bufio',
'net/url',
'strconv',
'github.com/pkg/sftp',
'context',
'os/exec',
'os',
'golang.org/x/crypto/ssh',
'net',
'io',
'github.com/mitchellh/go-homedir',
'github.com/wakatime/wakatime-cli/pkg/heartbeat',
'github.com/wakatime/wakatime-cli/pkg/log',
'golang.org/x/crypto/ssh/agent',
],
lines: 570,
lineno: null,
cursorpos: null,
is_write: true,
category: 'coding',
created_at: '2023-09-15T07:14:17Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: 'b0b05433-d6f4-49bf-a883-15394a873faa',
machine_name_id: '78a00491-9188-429f-9b84-ec8dde88a550',
},
{
id: '557da688-f569-4d16-b7e5-099938858694',
entity: '/Users/user/git/wakatime-cli/pkg/api/api.go',
type: 'file',
time: 1694762362.454982,
project: 'wakatime-cli',
project_root_count: 5,
branch: 'develop',
language: 'Go',
dependencies: ['net', 'net/http', 'github.com/wakatime/wakatime-cli/pkg/log', 'strings', 'errors'],
lines: 116,
lineno: null,
cursorpos: null,
is_write: false,
category: 'coding',
created_at: '2023-09-15T07:50:30Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: 'b0b05433-d6f4-49bf-a883-15394a873faa',
machine_name_id: '41437d2c-27a7-4ccb-91bc-519744a9a04a',
},
{
id: '25fe74a4-344c-4352-b5dd-20d51b6c96d7',
entity: '/Users/user/git/wakatime-cli/main_test.go',
type: 'file',
time: 1694763018.592351,
project: 'wakatime-cli',
project_root_count: 5,
branch: 'develop',
language: 'Go',
dependencies: [
'regexp',
'net/http/httptest',
'github.com/wakatime/wakatime-cli/pkg/exitcode',
'github.com/wakatime/wakatime-cli/pkg/windows',
'sync',
'os',
'github.com/wakatime/wakatime-cli/pkg/offline',
'io',
'net/http',
'runtime',
'path/filepath',
'time',
'github.com/stretchr/testify/require',
'github.com/wakatime/wakatime-cli/pkg/project',
'os/exec',
'strings',
'github.com/wakatime/wakatime-cli/pkg/version',
'testing',
'github.com/stretchr/testify/assert',
'github.com/gandarez/go-realpath',
'github.com/wakatime/wakatime-cli/pkg/heartbeat',
'bytes',
],
lines: 942,
lineno: 1,
cursorpos: 1,
is_write: false,
category: 'coding',
created_at: '2023-09-15T07:30:19Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: 'fc2bb1b8-3222-42be-93b8-2132d68c1b2a',
machine_name_id: '78a00491-9188-429f-9b84-ec8dde88a550',
},
{
id: 'a9266913-aef3-46a5-9b15-bbb2347fbd42',
entity: '/var/folders/tl/0nj81xs92z95bcnpypxh84xr0000gn/T/TestRunCmd_BackoffLoggedWithVerbose4188755494/001/4227910206',
type: 'file',
time: 1694763108.576038,
project: 'wakatime-cli',
project_root_count: 8,
branch: null,
language: null,
dependencies: [],
lines: 0,
lineno: null,
cursorpos: null,
is_write: false,
category: 'coding',
created_at: '2023-09-15T07:36:07Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: '4aefd47b-04d2-48a2-b35e-72cfe5c9e6da',
machine_name_id: 'f7a333da-ab29-4a48-8fb4-4f167f1a9720',
},
{
id: '1ae18ac8-4764-49fb-a425-165f9098dcad',
entity: '/Users/user/git/wakatime-cli/main_test.go',
type: 'file',
time: 1694763227.183379,
project: 'wakatime-cli',
project_root_count: 5,
branch: 'develop',
language: 'Go',
dependencies: [
'regexp',
'runtime',
'strings',
'github.com/wakatime/wakatime-cli/pkg/windows',
'path/filepath',
'net/http',
'github.com/wakatime/wakatime-cli/pkg/version',
'os',
'github.com/wakatime/wakatime-cli/pkg/heartbeat',
'github.com/wakatime/wakatime-cli/pkg/exitcode',
'github.com/gandarez/go-realpath',
'net/http/httptest',
'testing',
'io',
'github.com/wakatime/wakatime-cli/pkg/project',
'github.com/stretchr/testify/require',
'bytes',
'github.com/wakatime/wakatime-cli/pkg/offline',
'time',
'github.com/stretchr/testify/assert',
'os/exec',
'sync',
],
lines: 942,
lineno: 10,
cursorpos: 2,
is_write: false,
category: 'coding',
created_at: '2023-09-15T07:36:07Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: 'fc2bb1b8-3222-42be-93b8-2132d68c1b2a',
machine_name_id: 'f7a333da-ab29-4a48-8fb4-4f167f1a9720',
},
{
id: '66719b8d-c1ce-4918-9d26-8c026ef45342',
entity: '/Users/user/git/wakatime-cli/main_test.go',
type: 'file',
time: 1694763365.306059,
project: 'wakatime-cli',
project_root_count: 5,
branch: 'develop',
language: 'Go',
dependencies: [
'regexp',
'net/http/httptest',
'github.com/wakatime/wakatime-cli/pkg/exitcode',
'github.com/wakatime/wakatime-cli/pkg/windows',
'sync',
'os',
'github.com/wakatime/wakatime-cli/pkg/offline',
'io',
'net/http',
'runtime',
'path/filepath',
'time',
'github.com/stretchr/testify/require',
'github.com/wakatime/wakatime-cli/pkg/project',
'os/exec',
'strings',
'github.com/wakatime/wakatime-cli/pkg/version',
'testing',
'github.com/stretchr/testify/assert',
'github.com/gandarez/go-realpath',
'github.com/wakatime/wakatime-cli/pkg/heartbeat',
'bytes',
],
lines: 942,
lineno: 278,
cursorpos: 19,
is_write: false,
category: 'coding',
created_at: '2023-09-15T07:36:06Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: 'fc2bb1b8-3222-42be-93b8-2132d68c1b2a',
machine_name_id: 'f7a333da-ab29-4a48-8fb4-4f167f1a9720',
},
{
id: 'cd2750af-c361-4987-a119-4013e9db1c2c',
entity: '/Users/user/git/wakatime-cli/main_test.go',
type: 'file',
time: 1694763409.637431,
project: 'wakatime-cli',
project_root_count: 5,
branch: 'develop',
language: 'Go',
dependencies: [
'regexp',
'net/http/httptest',
'github.com/wakatime/wakatime-cli/pkg/exitcode',
'github.com/wakatime/wakatime-cli/pkg/windows',
'sync',
'os',
'github.com/wakatime/wakatime-cli/pkg/offline',
'io',
'net/http',
'runtime',
'path/filepath',
'time',
'github.com/stretchr/testify/require',
'github.com/wakatime/wakatime-cli/pkg/project',
'os/exec',
'strings',
'github.com/wakatime/wakatime-cli/pkg/version',
'testing',
'github.com/stretchr/testify/assert',
'github.com/gandarez/go-realpath',
'github.com/wakatime/wakatime-cli/pkg/heartbeat',
'bytes',
],
lines: 940,
lineno: 1,
cursorpos: 1,
is_write: true,
category: 'coding',
created_at: '2023-09-15T07:36:50Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: 'fc2bb1b8-3222-42be-93b8-2132d68c1b2a',
machine_name_id: 'f7a333da-ab29-4a48-8fb4-4f167f1a9720',
},
{
id: 'bdd2b3fd-4ad3-4bcd-a59d-a265f16d8f39',
entity: '/Users/user/git/wakatime-cli/main_test.go',
type: 'file',
time: 1694763555.399909,
project: 'wakatime-cli',
project_root_count: 5,
branch: 'develop',
language: 'Go',
dependencies: [
'regexp',
'runtime',
'strings',
'github.com/wakatime/wakatime-cli/pkg/windows',
'path/filepath',
'net/http',
'github.com/wakatime/wakatime-cli/pkg/version',
'os',
'github.com/wakatime/wakatime-cli/pkg/heartbeat',
'github.com/wakatime/wakatime-cli/pkg/exitcode',
'github.com/gandarez/go-realpath',
'net/http/httptest',
'testing',
'io',
'github.com/wakatime/wakatime-cli/pkg/project',
'github.com/stretchr/testify/require',
'bytes',
'github.com/wakatime/wakatime-cli/pkg/offline',
'time',
'github.com/stretchr/testify/assert',
'os/exec',
'sync',
],
lines: 940,
lineno: 887,
cursorpos: 19,
is_write: false,
category: 'coding',
created_at: '2023-09-15T07:39:16Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: 'fc2bb1b8-3222-42be-93b8-2132d68c1b2a',
machine_name_id: 'f7a333da-ab29-4a48-8fb4-4f167f1a9720',
},
];
var externalDurations = [
{
id: '021961ac-8cff-466e-b666-05bbd569e59f',
entity: 'Meeting',
project: null,
language: null,
branch: null,
meta: null,
start_time: 1694800800.0,
end_time: 1694802600.0,
external_id: 'a@wakatime.com/ccsm2o9',
created_at: '2023-09-13T00:24:06Z',
is_external: true,
editor: 'Google Calendar',
type: 'event',
category: 'meeting',
provider: 'google',
},
];
var start = '2023-09-14T22:00:00Z';
var end = '2023-09-15T21:59:59Z';
var yesterdaysLastHeartbeat = {
id: '38096b49-4f17-41e7-b167-379c7e308291',
entity: '/Users/user/git/wakatime/__init__.py',
type: 'file',
time: 1694721586.453452,
project: 'wakatime',
project_root_count: 5,
branch: 'master',
language: 'Python',
dependencies: [],
lines: 61,
lineno: null,
cursorpos: null,
is_write: true,
category: 'coding',
created_at: '2023-09-15T09:35:38Z',
user_id: '66b6796d-eb84-4bb9-b9d2-8dc882f4c6ac',
user_agent_id: 'b0b05433-d6f4-49bf-a883-15394a873faa',
machine_name_id: 'f7a333da-ab29-4a48-8fb4-4f167f1a9720',
};
var tomorrowsFirstHeartbeat = null;
var keystrokeTimeout = 15; // minutes
var meetingsOverCoding = false; // user preference in settings
console.log(processHeartbeatsIntoDurations(heartbeats, start, end, keystrokeTimeout, meetingsOverCoding, yesterdaysLastHeartbeat, tomorrowsFirstHeartbeat));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment