2021-02-05 20:47:28 +03:00
|
|
|
package imports
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2021-04-10 11:18:09 +03:00
|
|
|
"errors"
|
2021-02-05 20:47:28 +03:00
|
|
|
"fmt"
|
2022-01-28 14:28:47 +03:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
"github.com/emvi/logbuch"
|
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
wakatime "github.com/muety/wakapi/models/compat/wakatime/v1"
|
|
|
|
"github.com/muety/wakapi/utils"
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
"golang.org/x/sync/semaphore"
|
|
|
|
)
|
|
|
|
|
2021-02-06 02:31:30 +03:00
|
|
|
const OriginWakatime = "wakatime"
|
2021-04-10 11:18:09 +03:00
|
|
|
const (
|
|
|
|
// wakatime api permits a max. rate of 10 req / sec
|
|
|
|
// https://github.com/wakatime/wakatime/issues/261
|
|
|
|
// with 5 workers, each sleeping slightly over 1/2 sec after every req., we should stay well below that limit
|
|
|
|
maxWorkers = 5
|
|
|
|
throttleDelay = 550 * time.Millisecond
|
|
|
|
)
|
2021-02-05 20:47:28 +03:00
|
|
|
|
|
|
|
type WakatimeHeartbeatImporter struct {
|
|
|
|
ApiKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWakatimeHeartbeatImporter(apiKey string) *WakatimeHeartbeatImporter {
|
|
|
|
return &WakatimeHeartbeatImporter{
|
|
|
|
ApiKey: apiKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 02:31:30 +03:00
|
|
|
func (w *WakatimeHeartbeatImporter) Import(user *models.User, minFrom time.Time, maxTo time.Time) <-chan *models.Heartbeat {
|
2021-02-05 20:47:28 +03:00
|
|
|
out := make(chan *models.Heartbeat)
|
|
|
|
|
|
|
|
go func(user *models.User, out chan *models.Heartbeat) {
|
2022-01-21 14:35:05 +03:00
|
|
|
baseUrl := user.WakaTimeURL(config.WakatimeApiUrl)
|
|
|
|
|
|
|
|
startDate, endDate, err := w.fetchRange(baseUrl)
|
2021-02-05 20:47:28 +03:00
|
|
|
if err != nil {
|
2022-02-17 14:20:22 +03:00
|
|
|
config.Log().Error("failed to fetch date range while importing wakatime heartbeats for user '%s' - %v", user.ID, err)
|
2021-02-05 20:47:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-06 02:31:30 +03:00
|
|
|
if startDate.Before(minFrom) {
|
|
|
|
startDate = minFrom
|
|
|
|
}
|
|
|
|
if endDate.After(maxTo) {
|
|
|
|
endDate = maxTo
|
|
|
|
}
|
|
|
|
|
2022-01-21 14:35:05 +03:00
|
|
|
userAgents, err := w.fetchUserAgents(baseUrl)
|
2021-02-05 20:47:28 +03:00
|
|
|
if err != nil {
|
2022-02-17 14:20:22 +03:00
|
|
|
config.Log().Error("failed to fetch user agents while importing wakatime heartbeats for user '%s' - %v", user.ID, err)
|
2021-02-05 20:47:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-21 14:35:05 +03:00
|
|
|
machinesNames, err := w.fetchMachineNames(baseUrl)
|
2021-02-11 00:08:00 +03:00
|
|
|
if err != nil {
|
2022-02-17 14:20:22 +03:00
|
|
|
config.Log().Error("failed to fetch machine names while importing wakatime heartbeats for user '%s' - %v", user.ID, err)
|
2021-02-11 00:08:00 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
days := generateDays(startDate, endDate)
|
|
|
|
|
|
|
|
c := atomic.NewUint32(uint32(len(days)))
|
|
|
|
ctx := context.TODO()
|
|
|
|
sem := semaphore.NewWeighted(maxWorkers)
|
|
|
|
|
|
|
|
for _, d := range days {
|
|
|
|
if err := sem.Acquire(ctx, 1); err != nil {
|
2022-02-17 14:20:22 +03:00
|
|
|
logbuch.Error("failed to acquire semaphore - %v", err)
|
2021-02-05 20:47:28 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(day time.Time) {
|
|
|
|
defer sem.Release(1)
|
2021-04-10 11:18:09 +03:00
|
|
|
defer time.Sleep(throttleDelay)
|
2021-02-05 20:47:28 +03:00
|
|
|
|
2021-02-12 21:25:59 +03:00
|
|
|
d := day.Format(config.SimpleDateFormat)
|
2022-01-21 14:35:05 +03:00
|
|
|
heartbeats, err := w.fetchHeartbeats(d, baseUrl)
|
2021-02-05 20:47:28 +03:00
|
|
|
if err != nil {
|
2022-02-17 14:20:22 +03:00
|
|
|
config.Log().Error("failed to fetch heartbeats for day '%s' and user '%s' - &v", d, user.ID, err)
|
2021-02-05 20:47:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range heartbeats {
|
2021-02-11 00:08:00 +03:00
|
|
|
out <- mapHeartbeat(h, userAgents, machinesNames, user)
|
2021-02-05 20:47:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Dec() == 0 {
|
|
|
|
close(out)
|
|
|
|
}
|
|
|
|
}(d)
|
|
|
|
}
|
|
|
|
}(user, out)
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-02-06 02:31:30 +03:00
|
|
|
func (w *WakatimeHeartbeatImporter) ImportAll(user *models.User) <-chan *models.Heartbeat {
|
|
|
|
return w.Import(user, time.Time{}, time.Now())
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
// https://wakatime.com/api/v1/users/current/heartbeats?date=2021-02-05
|
2021-08-29 11:54:00 +03:00
|
|
|
// https://pastr.de/p/b5p4od5s8w0pfntmwoi117jy
|
2022-01-21 14:35:05 +03:00
|
|
|
func (w *WakatimeHeartbeatImporter) fetchHeartbeats(day string, baseUrl string) ([]*wakatime.HeartbeatEntry, error) {
|
2021-02-05 20:47:28 +03:00
|
|
|
httpClient := &http.Client{Timeout: 10 * time.Second}
|
|
|
|
|
2022-01-21 14:35:05 +03:00
|
|
|
req, err := http.NewRequest(http.MethodGet, baseUrl+config.WakatimeApiHeartbeatsUrl, nil)
|
2021-02-05 20:47:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Add("date", day)
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
res, err := httpClient.Do(w.withHeaders(req))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-04-10 11:18:09 +03:00
|
|
|
} else if res.StatusCode >= 400 {
|
|
|
|
return nil, errors.New(fmt.Sprintf("got status %d from wakatime api", res.StatusCode))
|
2021-02-05 20:47:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var heartbeatsData wakatime.HeartbeatsViewModel
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&heartbeatsData); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return heartbeatsData.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://wakatime.com/api/v1/users/current/all_time_since_today
|
2021-08-29 11:54:00 +03:00
|
|
|
// https://pastr.de/p/w8xb4biv575pu32pox7jj2gr
|
2022-01-21 14:35:05 +03:00
|
|
|
func (w *WakatimeHeartbeatImporter) fetchRange(baseUrl string) (time.Time, time.Time, error) {
|
2021-02-05 20:47:28 +03:00
|
|
|
httpClient := &http.Client{Timeout: 10 * time.Second}
|
|
|
|
|
|
|
|
notime := time.Time{}
|
|
|
|
|
2022-01-21 14:35:05 +03:00
|
|
|
req, err := http.NewRequest(http.MethodGet, baseUrl+config.WakatimeApiAllTimeUrl, nil)
|
2021-02-05 20:47:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return notime, notime, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := httpClient.Do(w.withHeaders(req))
|
|
|
|
if err != nil {
|
|
|
|
return notime, notime, err
|
|
|
|
}
|
|
|
|
|
2021-02-11 00:08:00 +03:00
|
|
|
var allTimeData wakatime.AllTimeViewModel
|
2021-02-05 20:47:28 +03:00
|
|
|
if err := json.NewDecoder(res.Body).Decode(&allTimeData); err != nil {
|
|
|
|
return notime, notime, err
|
|
|
|
}
|
|
|
|
|
2021-02-12 21:25:59 +03:00
|
|
|
startDate, err := time.Parse(config.SimpleDateFormat, allTimeData.Data.Range.StartDate)
|
2021-02-05 20:47:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return notime, notime, err
|
|
|
|
}
|
|
|
|
|
2021-02-12 21:25:59 +03:00
|
|
|
endDate, err := time.Parse(config.SimpleDateFormat, allTimeData.Data.Range.EndDate)
|
2021-02-05 20:47:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return notime, notime, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return startDate, endDate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://wakatime.com/api/v1/users/current/user_agents
|
2021-08-29 11:54:00 +03:00
|
|
|
// https://pastr.de/p/05k5do8q108k94lic4lfl3pc
|
2022-01-21 14:35:05 +03:00
|
|
|
func (w *WakatimeHeartbeatImporter) fetchUserAgents(baseUrl string) (map[string]*wakatime.UserAgentEntry, error) {
|
2021-02-05 20:47:28 +03:00
|
|
|
httpClient := &http.Client{Timeout: 10 * time.Second}
|
|
|
|
|
2022-01-01 22:33:58 +03:00
|
|
|
userAgents := make(map[string]*wakatime.UserAgentEntry)
|
2021-02-05 20:47:28 +03:00
|
|
|
|
2022-01-02 13:06:00 +03:00
|
|
|
for page := 1; ; page++ {
|
2022-01-21 14:35:05 +03:00
|
|
|
url := fmt.Sprintf("%s%s?page=%d", baseUrl, config.WakatimeApiUserAgentsUrl, page)
|
2022-01-02 13:06:00 +03:00
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
2022-01-01 22:33:58 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-05 20:47:28 +03:00
|
|
|
|
2022-01-01 22:33:58 +03:00
|
|
|
res, err := httpClient.Do(w.withHeaders(req))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var userAgentsData wakatime.UserAgentsViewModel
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&userAgentsData); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ua := range userAgentsData.Data {
|
|
|
|
userAgents[ua.Id] = ua
|
|
|
|
}
|
2022-01-02 13:06:00 +03:00
|
|
|
|
|
|
|
if page == userAgentsData.TotalPages {
|
|
|
|
break
|
|
|
|
}
|
2021-02-05 20:47:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return userAgents, nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 00:08:00 +03:00
|
|
|
// https://wakatime.com/api/v1/users/current/machine_names
|
2021-08-29 11:54:00 +03:00
|
|
|
// https://pastr.de/p/v58cv0xrupp3zvyyv8o6973j
|
2022-01-21 14:35:05 +03:00
|
|
|
func (w *WakatimeHeartbeatImporter) fetchMachineNames(baseUrl string) (map[string]*wakatime.MachineEntry, error) {
|
2021-02-11 00:08:00 +03:00
|
|
|
httpClient := &http.Client{Timeout: 10 * time.Second}
|
|
|
|
|
2022-01-01 22:33:58 +03:00
|
|
|
machines := make(map[string]*wakatime.MachineEntry)
|
2021-02-11 00:08:00 +03:00
|
|
|
|
2022-01-02 13:06:00 +03:00
|
|
|
for page := 1; ; page++ {
|
2022-01-21 14:35:05 +03:00
|
|
|
url := fmt.Sprintf("%s%s?page=%d", baseUrl, config.WakatimeApiMachineNamesUrl, page)
|
2022-01-02 13:06:00 +03:00
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
2022-01-01 22:33:58 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-11 00:08:00 +03:00
|
|
|
|
2022-01-01 22:33:58 +03:00
|
|
|
res, err := httpClient.Do(w.withHeaders(req))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var machineData wakatime.MachineViewModel
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&machineData); err != nil {
|
2022-01-02 13:06:00 +03:00
|
|
|
return nil, err
|
2022-01-01 22:33:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ma := range machineData.Data {
|
|
|
|
machines[ma.Id] = ma
|
|
|
|
}
|
2022-01-02 13:06:00 +03:00
|
|
|
|
|
|
|
if page == machineData.TotalPages {
|
|
|
|
break
|
|
|
|
}
|
2021-02-11 00:08:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return machines, nil
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
func (w *WakatimeHeartbeatImporter) withHeaders(req *http.Request) *http.Request {
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(w.ApiKey))))
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapHeartbeat(
|
|
|
|
entry *wakatime.HeartbeatEntry,
|
|
|
|
userAgents map[string]*wakatime.UserAgentEntry,
|
2021-02-11 00:08:00 +03:00
|
|
|
machineNames map[string]*wakatime.MachineEntry,
|
2021-02-05 20:47:28 +03:00
|
|
|
user *models.User,
|
|
|
|
) *models.Heartbeat {
|
|
|
|
ua := userAgents[entry.UserAgentId]
|
|
|
|
if ua == nil {
|
|
|
|
ua = &wakatime.UserAgentEntry{
|
|
|
|
Editor: "unknown",
|
|
|
|
Os: "unknown",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 00:08:00 +03:00
|
|
|
ma := machineNames[entry.MachineNameId]
|
|
|
|
if ma == nil {
|
|
|
|
ma = &wakatime.MachineEntry{
|
|
|
|
Id: entry.MachineNameId,
|
|
|
|
Value: entry.MachineNameId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:47:28 +03:00
|
|
|
return (&models.Heartbeat{
|
|
|
|
User: user,
|
|
|
|
UserID: user.ID,
|
|
|
|
Entity: entry.Entity,
|
|
|
|
Type: entry.Type,
|
|
|
|
Category: entry.Category,
|
|
|
|
Project: entry.Project,
|
|
|
|
Branch: entry.Branch,
|
|
|
|
Language: entry.Language,
|
|
|
|
IsWrite: entry.IsWrite,
|
|
|
|
Editor: ua.Editor,
|
|
|
|
OperatingSystem: ua.Os,
|
2021-02-11 00:08:00 +03:00
|
|
|
Machine: ma.Value,
|
2021-08-29 11:54:00 +03:00
|
|
|
UserAgent: ua.Value,
|
2022-01-28 14:28:47 +03:00
|
|
|
Time: models.CustomTime(time.Unix(0, int64(entry.Time*1e9))),
|
2021-02-06 02:31:30 +03:00
|
|
|
Origin: OriginWakatime,
|
|
|
|
OriginId: entry.Id,
|
2022-02-13 12:59:14 +03:00
|
|
|
CreatedAt: models.CustomTime(entry.CreatedAt),
|
2021-02-05 20:47:28 +03:00
|
|
|
}).Hashed()
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateDays(from, to time.Time) []time.Time {
|
|
|
|
days := make([]time.Time, 0)
|
|
|
|
|
|
|
|
from = utils.StartOfDay(from)
|
2022-01-02 15:17:30 +03:00
|
|
|
to = utils.StartOfDay(to.AddDate(0, 0, 1))
|
2021-02-05 20:47:28 +03:00
|
|
|
|
2022-01-02 15:17:30 +03:00
|
|
|
for d := from; d.Before(to); d = d.AddDate(0, 0, 1) {
|
2021-02-05 20:47:28 +03:00
|
|
|
days = append(days, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
return days
|
|
|
|
}
|