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

Compare commits

..

7 Commits
2.0.0 ... 2.0.1

13 changed files with 793 additions and 555 deletions

26
Caddyfile Normal file
View File

@ -0,0 +1,26 @@
wakapi.yourdomain.tld {
encode zstd gzip
header {
Strict-Transport-Security "max-age=2592000; includeSubDomains"
}
log {
output file /var/log/caddy/wakapi.dev.access.log
format single_field common_log
}
reverse_proxy http://[::1]:3000
@api path_regexp "^/api.*"
@notapi not path_regexp "^/api.*"
push @notapi /assets/vendor/source-sans-3.css
push @notapi /assets/css/app.dist.css
push @notapi /assets/vendor/petite-vue.min.js
push @notapi /assets/vendor/chart.min.js
push @notapi /assets/vendor/iconify.basic.min.js
push @notapi /assets/js/icons.dist.js
push @notapi /assets/js/base.js
push @notapi /assets/images/logo.svg
}

File diff suppressed because it is too large Load Diff

View File

@ -29,6 +29,11 @@ func (m *AliasServiceMock) GetByUser(s string) ([]*models.Alias, error) {
return args.Get(0).([]*models.Alias), args.Error(1)
}
func (m *AliasServiceMock) GetByUserAndType(s string, u uint8) ([]*models.Alias, error) {
args := m.Called(s, u)
return args.Get(0).([]*models.Alias), args.Error(1)
}
func (m *AliasServiceMock) GetByUserAndKeyAndType(s string, s2 string, u uint8) ([]*models.Alias, error) {
args := m.Called(s, s2, u)
return args.Get(0).([]*models.Alias), args.Error(1)

View File

@ -17,7 +17,7 @@ import (
const (
intervalPattern = `interval:([a-z0-9_]+)`
entityFilterPattern = `(project|os|editor|language|machine|label):([_a-zA-Z0-9-\s]+)`
entityFilterPattern = `(project|os|editor|language|machine|label):([_a-zA-Z0-9-\s\.]+)`
)
type BadgeHandler struct {

View File

@ -0,0 +1,43 @@
package v1
import (
"github.com/stretchr/testify/assert"
"regexp"
"testing"
)
func TestBadgeHandler_EntityPattern(t *testing.T) {
type test struct {
test string
key string
val string
}
pathPrefix := "/compat/shields/v1/current/today/"
tests := []test{
{test: pathPrefix + "project:wakapi", key: "project", val: "wakapi"},
{test: pathPrefix + "os:Linux", key: "os", val: "Linux"},
{test: pathPrefix + "editor:VSCode", key: "editor", val: "VSCode"},
{test: pathPrefix + "language:Java", key: "language", val: "Java"},
{test: pathPrefix + "machine:devmachine", key: "machine", val: "devmachine"},
{test: pathPrefix + "label:work", key: "label", val: "work"},
{test: pathPrefix + "foo:bar", key: "", val: ""}, // invalid entity
{test: pathPrefix + "project:01234", key: "project", val: "01234"}, // digits only
{test: pathPrefix + "project:anchr-web-ext", key: "project", val: "anchr-web-ext"}, // with dashes
{test: pathPrefix + "project:wakapi v2", key: "project", val: "wakapi v2"}, // with blank space
{test: pathPrefix + "project:project", key: "project", val: "project"},
{test: pathPrefix + "project:Anchr-Android_v2.0", key: "project", val: "Anchr-Android_v2.0"}, // all the way
}
sut := regexp.MustCompile(entityFilterPattern)
for _, tc := range tests {
var key, val string
if groups := sut.FindStringSubmatch(tc.test); len(groups) > 2 {
key, val = groups[1], groups[2]
}
assert.Equal(t, tc.key, key)
assert.Equal(t, tc.val, val)
}
}

View File

@ -10,6 +10,7 @@ import (
"github.com/muety/wakapi/middlewares"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/models/view"
routeutils "github.com/muety/wakapi/routes/utils"
"github.com/muety/wakapi/services"
"github.com/muety/wakapi/services/imports"
"github.com/muety/wakapi/utils"
@ -669,12 +670,11 @@ func (h *SettingsHandler) buildViewModel(r *http.Request) *view.SettingsViewMode
})
// projects
projects, err := h.heartbeatSrvc.GetEntitySetByUser(models.SummaryProject, user)
projects, err := routeutils.GetEffectiveProjectsList(user, h.heartbeatSrvc, h.aliasSrvc)
if err != nil {
conf.Log().Request(r).Error("error while fetching projects - %v", err)
return &view.SettingsViewModel{Error: criticalError}
}
sort.Strings(projects)
return &view.SettingsViewModel{
User: user,

View File

@ -0,0 +1,53 @@
package utils
import (
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/services"
"sort"
)
// GetEffectiveProjectsList returns the user's projects, including all alias targets and excluding all remapped project names (alias sources)
// Example: "A" mapped to "AB" using an alias
// -> "A" itself should not appear as a project anymore
// -> Instead, the "virtual" project "AB" shall appear
// See https://github.com/muety/wakapi/issues/231
func GetEffectiveProjectsList(user *models.User, heartbeatSrvc services.IHeartbeatService, aliasSrvc services.IAliasService) ([]string, error) {
projectsMap := make(map[string]bool) // proper sets as part of stdlib would be nice...
// extract actual projects from heartbeats
realProjects, err := heartbeatSrvc.GetEntitySetByUser(models.SummaryProject, user)
if err != nil {
return []string{}, err
}
// create a "set" / lookup table
for _, p := range realProjects {
projectsMap[p] = true
}
// fetch aliases
projectAliases, err := aliasSrvc.GetByUserAndType(user.ID, models.SummaryProject)
if err != nil {
return []string{}, err
}
// remove alias values (source of a mapping)
// add alias key (target of a mapping) instead
for _, a := range projectAliases {
if projectsMap[a.Value] {
projectsMap[a.Value] = false
}
projectsMap[a.Key] = true
}
projects := make([]string, 0, len(projectsMap))
for key, val := range projectsMap {
if !val {
continue
}
projects = append(projects, key)
}
sort.Strings(projects)
return projects, nil
}

View File

@ -56,21 +56,18 @@ func (srv *AliasService) GetByUser(userId string) ([]*models.Alias, error) {
}
}
func (srv *AliasService) GetByUserAndType(userId string, summaryType uint8) ([]*models.Alias, error) {
check := func(a *models.Alias) bool {
return a.Type == summaryType
}
return srv.getFiltered(userId, check)
}
func (srv *AliasService) GetByUserAndKeyAndType(userId, key string, summaryType uint8) ([]*models.Alias, error) {
if !srv.IsInitialized(userId) {
srv.MayInitializeUser(userId)
}
if aliases, ok := userAliases.Load(userId); ok {
filteredAliases := make([]*models.Alias, 0, len(aliases.([]*models.Alias)))
for _, a := range aliases.([]*models.Alias) {
if a.Key == key && a.Type == summaryType {
filteredAliases = append(filteredAliases, a)
}
}
return filteredAliases, nil
} else {
return nil, errors.New(fmt.Sprintf("no user aliases loaded for user %s", userId))
check := func(a *models.Alias) bool {
return a.Key == key && a.Type == summaryType
}
return srv.getFiltered(userId, check)
}
func (srv *AliasService) GetAliasOrDefault(userId string, summaryType uint8, value string) (string, error) {
@ -94,7 +91,11 @@ func (srv *AliasService) Create(alias *models.Alias) (*models.Alias, error) {
if err != nil {
return nil, err
}
// manually update cache
srv.updateCache(alias, false)
// reload entire cache (async, though)
go srv.MayInitializeUser(alias.UserID)
return result, nil
}
@ -103,7 +104,14 @@ func (srv *AliasService) Delete(alias *models.Alias) error {
return errors.New("no user id specified")
}
err := srv.repository.Delete(alias.ID)
// manually update cache
if err == nil {
srv.updateCache(alias, false)
}
// reload entire cache (async, though)
go srv.MayInitializeUser(alias.UserID)
return err
}
@ -120,9 +128,53 @@ func (srv *AliasService) DeleteMulti(aliases []*models.Alias) error {
err := srv.repository.DeleteBatch(ids)
// manually update cache
if err == nil {
for _, a := range aliases {
srv.updateCache(a, true)
}
}
// reload entire cache (async, though)
for k := range affectedUsers {
go srv.MayInitializeUser(k)
}
return err
}
func (srv *AliasService) updateCache(reason *models.Alias, removal bool) {
if !removal {
if aliases, ok := userAliases.Load(reason.UserID); ok {
updatedAliases := aliases.([]*models.Alias)
updatedAliases = append(updatedAliases, reason)
userAliases.Store(reason.UserID, updatedAliases)
}
} else {
if aliases, ok := userAliases.Load(reason.UserID); ok {
updatedAliases := make([]*models.Alias, 0, len(aliases.([]*models.Alias))) // if we only had generics...
for _, a := range aliases.([]*models.Alias) {
if a.ID != reason.ID {
updatedAliases = append(updatedAliases, a)
}
}
userAliases.Store(reason.UserID, updatedAliases)
}
}
}
func (srv *AliasService) getFiltered(userId string, check func(alias *models.Alias) bool) ([]*models.Alias, error) {
if !srv.IsInitialized(userId) {
srv.MayInitializeUser(userId)
}
if aliases, ok := userAliases.Load(userId); ok {
filteredAliases := make([]*models.Alias, 0, len(aliases.([]*models.Alias)))
for _, a := range aliases.([]*models.Alias) {
if check(a) {
filteredAliases = append(filteredAliases, a)
}
}
return filteredAliases, nil
} else {
return nil, errors.New(fmt.Sprintf("no user aliases loaded for user %s", userId))
}
}

View File

@ -21,6 +21,7 @@ type IAliasService interface {
IsInitialized(string) bool
InitializeUser(string) error
GetByUser(string) ([]*models.Alias, error)
GetByUserAndType(string, uint8) ([]*models.Alias, error)
GetByUserAndKeyAndType(string, string, uint8) ([]*models.Alias, error)
GetAliasOrDefault(string, uint8, string) (string, error)
}

View File

@ -1,4 +1,3 @@
const LEGEND_MAX_ENTRIES = 9
// dirty hack to vertically align legends across multiple charts
// however, without monospace font, it's still not perfectly aligned
// waiting for https://github.com/chartjs/Chart.js/discussions/9890
@ -31,8 +30,8 @@ let topNPickers = [...document.getElementsByClassName('top-picker')]
topNPickers.sort(((a, b) => parseInt(a.attributes['data-entity'].value) - parseInt(b.attributes['data-entity'].value)))
topNPickers.forEach(e => {
const idx = parseInt(e.attributes['data-entity'].value)
e.max = Math.min(data[idx].length, 10)
e.value = e.max
e.max = data[idx].length
e.value = Math.min(e.max, 9)
})
let charts = []
@ -77,7 +76,7 @@ function draw(subselection) {
function filterLegendItem(item) {
item.text = item.text.length > LEGEND_CHARACTERS ? item.text.slice(0, LEGEND_CHARACTERS - 3).padEnd(LEGEND_CHARACTERS, '.') : item.text
item.text = item.text.padEnd(LEGEND_CHARACTERS + 3)
return item.index < LEGEND_MAX_ENTRIES
return true
}
function shouldUpdate(index) {

View File

@ -46,7 +46,7 @@ func Add(i, j int) int {
}
func ParseUserAgent(ua string) (string, string, error) {
re := regexp.MustCompile(`(?iU)^wakatime\/v?[\d+.]+\s\((\w+)-.*\)\s.+\s([^\/\s]+)-wakatime\/.+$`)
re := regexp.MustCompile(`(?iU)^wakatime\/(?:v?[\d+.]+|unset)\s\((\w+)-.*\)\s.+\s([^\/\s]+)-wakatime\/.+$`)
groups := re.FindAllStringSubmatch(ua, -1)
if len(groups) == 0 || len(groups[0]) != 3 {
return "", "", errors.New("failed to parse user agent string")

View File

@ -43,6 +43,12 @@ func TestCommon_ParseUserAgent(t *testing.T) {
"emacs",
nil,
},
{
"wakatime/unset (linux-5.11.0-44-generic-x86_64) go1.16.13 emacs-wakatime/1.0.2",
"linux",
"emacs",
nil,
},
}
for _, test := range tests {

View File

@ -1 +1 @@
2.0.0
2.0.1