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

fix: correctly parse os and editor for chrome plugin

fix: handle last project special keyword
This commit is contained in:
Ferdinand Mütsch 2023-07-09 10:28:23 +02:00
parent 37e6acd058
commit 38286c7f3a
7 changed files with 743 additions and 720 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@ package models
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/strutil"
"github.com/emvi/logbuch" "github.com/emvi/logbuch"
"github.com/mitchellh/hashstructure/v2" "github.com/mitchellh/hashstructure/v2"
"strings" "strings"
@ -39,6 +40,22 @@ func (h *Heartbeat) Timely(maxAge time.Duration) bool {
return now.Sub(h.Time.T()) <= maxAge && h.Time.T().Sub(now) < 1*time.Hour return now.Sub(h.Time.T()) <= maxAge && h.Time.T().Sub(now) < 1*time.Hour
} }
func (h *Heartbeat) Sanitize() *Heartbeat {
// wakatime has a special keyword that indicates to use the most recent project for a given heartbeat
// in chrome, the browser extension sends this keyword for (most?) heartbeats
// presumably the idea behind this is that if you're coding, your browsing activity will likely also relate to that coding project
// but i don't really like this, i'd rather have all browsing activity under the "unknown" project (as the case with firefox, for whatever reason)
// see https://github.com/wakatime/browser-wakatime/pull/206
if (h.Type == "url" || h.Type == "domain") && h.Project == "<<LAST_PROJECT>>" {
h.Project = ""
}
h.OperatingSystem = strutil.Capitalize(h.OperatingSystem)
h.Editor = strutil.Capitalize(h.Editor)
return h
}
func (h *Heartbeat) Augment(languageMappings map[string]string) { func (h *Heartbeat) Augment(languageMappings map[string]string) {
maxPrec := -1 // precision / mapping complexity -> more concrete ones shall take precedence maxPrec := -1 // precision / mapping complexity -> more concrete ones shall take precedence
for ending, value := range languageMappings { for ending, value := range languageMappings {

View File

@ -1,6 +1,7 @@
package routes package routes
import ( import (
"github.com/duke-git/lancet/v2/strutil"
"github.com/muety/wakapi/helpers" "github.com/muety/wakapi/helpers"
"html/template" "html/template"
"net/http" "net/http"
@ -34,7 +35,7 @@ func DefaultTemplateFuncs() template.FuncMap {
"title": strings.Title, "title": strings.Title,
"join": strings.Join, "join": strings.Join,
"add": add, "add": add,
"capitalize": utils.Capitalize, "capitalize": strutil.Capitalize,
"lower": strings.ToLower, "lower": strings.ToLower,
"toRunes": utils.ToRunes, "toRunes": utils.ToRunes,
"localTZOffset": utils.LocalTZOffset, "localTZOffset": utils.LocalTZOffset,

View File

@ -64,6 +64,7 @@ func (srv *HeartbeatService) InsertBatch(heartbeats []*models.Heartbeat) error {
filteredHeartbeats := make([]*models.Heartbeat, 0, len(heartbeats)) filteredHeartbeats := make([]*models.Heartbeat, 0, len(heartbeats))
for _, hb := range heartbeats { for _, hb := range heartbeats {
if !hashes.Contain(hb.Hash) { if !hashes.Contain(hb.Hash) {
hb = hb.Sanitize()
filteredHeartbeats = append(filteredHeartbeats, hb) filteredHeartbeats = append(filteredHeartbeats, hb)
hashes.Add(hb.Hash) hashes.Add(hb.Hash)
} }

View File

@ -55,6 +55,12 @@ func TestCommon_ParseUserAgent(t *testing.T) {
"chrome", "chrome",
nil, nil,
}, },
{
"Chrome/114.0.0.0 linux_x86-64 chrome-wakatime/3.0.17",
"linux",
"chrome",
nil,
},
} }
for _, test := range tests { for _, test := range tests {

View File

@ -78,7 +78,7 @@ func ParsePageParamsWithDefault(r *http.Request, page, size int) *PageParams {
} }
func ParseUserAgent(ua string) (string, string, error) { func ParseUserAgent(ua string) (string, string, error) {
re := regexp.MustCompile(`(?iU)^(?:(?:wakatime|chrome|firefox)\/(?:v?[\d+.]+|unset)\s)?(?:\((\w+)-.*\)\s.+\s)?([^\/\s]+)-wakatime\/.+$`) re := regexp.MustCompile(`(?iU)^(?:(?:wakatime|chrome|firefox)\/(?:v?[\d+.]+|unset)\s)?(?:\(?(\w+)[-_].*\)?.+\s)?([^\/\s]+)-wakatime\/.+$`)
groups := re.FindAllStringSubmatch(ua, -1) groups := re.FindAllStringSubmatch(ua, -1)
if len(groups) == 0 || len(groups[0]) != 3 { if len(groups) == 0 || len(groups[0]) != 3 {
return "", "", errors.New("failed to parse user agent string") return "", "", errors.New("failed to parse user agent string")

View File

@ -1,14 +1,9 @@
package utils package utils
import ( import (
"fmt"
"strings" "strings"
) )
func Capitalize(s string) string {
return fmt.Sprintf("%s%s", strings.ToUpper(s[:1]), s[1:])
}
func SplitMulti(s string, delimiters ...string) []string { func SplitMulti(s string, delimiters ...string) []string {
return strings.FieldsFunc(s, func(r rune) bool { return strings.FieldsFunc(s, func(r rune) bool {
for _, d := range delimiters { for _, d := range delimiters {