1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: consider all Machine/UserAgent entries

This commit is contained in:
jabra98 2022-01-01 20:33:58 +01:00
parent aaa907a7b2
commit c7e12ba3b5
3 changed files with 59 additions and 36 deletions

View File

@ -3,7 +3,8 @@ package v1
// https://wakatime.com/api/v1/users/current/machine_names
type MachineViewModel struct {
Data []*MachineEntry `json:"data"`
Data []*MachineEntry `json:"data"`
TotalPages int `json:"total_pages"`
}
type MachineEntry struct {

View File

@ -1,7 +1,8 @@
package v1
type UserAgentsViewModel struct {
Data []*UserAgentEntry `json:"data"`
Data []*UserAgentEntry `json:"data"`
TotalPages int `json:"total_pages"`
}
type UserAgentEntry struct {

View File

@ -15,6 +15,7 @@ import (
"golang.org/x/sync/semaphore"
"net/http"
"time"
"strconv"
)
const OriginWakatime = "wakatime"
@ -174,24 +175,34 @@ func (w *WakatimeHeartbeatImporter) fetchRange() (time.Time, time.Time, error) {
func (w *WakatimeHeartbeatImporter) fetchUserAgents() (map[string]*wakatime.UserAgentEntry, error) {
httpClient := &http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest(http.MethodGet, config.WakatimeApiUrl+config.WakatimeApiUserAgentsUrl, nil)
if err != nil {
return nil, err
}
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
}
userAgents := make(map[string]*wakatime.UserAgentEntry)
for _, ua := range userAgentsData.Data {
userAgents[ua.Id] = ua
done := false
for i:=1; !done; i++ {
queryStr := "?page="+strconv.Itoa(i)
req, err := http.NewRequest(http.MethodGet, config.WakatimeApiUrl+
config.WakatimeApiUserAgentsUrl+queryStr, nil)
if err != nil {
return nil, err
}
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
}
if i == userAgentsData.TotalPages {
done = true
}
for _, ua := range userAgentsData.Data {
userAgents[ua.Id] = ua
}
}
return userAgents, nil
@ -202,24 +213,34 @@ func (w *WakatimeHeartbeatImporter) fetchUserAgents() (map[string]*wakatime.User
func (w *WakatimeHeartbeatImporter) fetchMachineNames() (map[string]*wakatime.MachineEntry, error) {
httpClient := &http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest(http.MethodGet, config.WakatimeApiUrl+config.WakatimeApiMachineNamesUrl, nil)
if err != nil {
return nil, err
}
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 {
return nil, err
}
machines := make(map[string]*wakatime.MachineEntry)
for _, ma := range machineData.Data {
machines[ma.Id] = ma
done := false
for i:=1; !done; i++ {
queryStr := "?page="+strconv.Itoa(i)
req, err := http.NewRequest(http.MethodGet, config.WakatimeApiUrl+
config.WakatimeApiMachineNamesUrl+queryStr, nil)
if err != nil {
return nil, err
}
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 {
return nil, err
}
if i == machineData.TotalPages {
done = true
}
for _, ma := range machineData.Data {
machines[ma.Id] = ma
}
}
return machines, nil