Skip to content

Commit

Permalink
Merge branch 'master' of github.com:muety/wakapi
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Feb 5, 2025
2 parents c86419f + a75f177 commit 674296c
Show file tree
Hide file tree
Showing 26 changed files with 5,870 additions and 5,477 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ pkged.go
package-lock.json
node_modules
.DS_Store
.venv
venv
11,066 changes: 5,622 additions & 5,444 deletions coverage/coverage.out

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ func main() {
// Services
mailService = mail.NewMailService()
aliasService = services.NewAliasService(aliasRepository)
userService = services.NewUserService(mailService, userRepository)
keyValueService = services.NewKeyValueService(keyValueRepository)
userService = services.NewUserService(keyValueService, mailService, userRepository)
languageMappingService = services.NewLanguageMappingService(languageMappingRepository)
projectLabelService = services.NewProjectLabelService(projectLabelRepository)
heartbeatService = services.NewHeartbeatService(heartbeatRepository, languageMappingService)
durationService = services.NewDurationService(heartbeatService)
summaryService = services.NewSummaryService(summaryRepository, heartbeatService, durationService, aliasService, projectLabelService)
aggregationService = services.NewAggregationService(userService, summaryService, heartbeatService)
keyValueService = services.NewKeyValueService(keyValueRepository)
reportService = services.NewReportService(summaryService, userService, mailService)
activityService = services.NewActivityService(summaryService)
diagnosticsService = services.NewDiagnosticsService(diagnosticsRepository)
Expand Down
1 change: 1 addition & 0 deletions mocks/alias_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

type AliasRepositoryMock struct {
BaseRepositoryMock
mock.Mock
}

Expand Down
24 changes: 24 additions & 0 deletions mocks/base_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mocks

import (
"github.com/stretchr/testify/mock"
)

type BaseRepositoryMock struct {
mock.Mock
}

func (m *BaseRepositoryMock) GetDialector() string {
args := m.Called()
return args.Get(0).(string)
}

func (m *BaseRepositoryMock) GetTableDDLMysql(s string) (string, error) {
args := m.Called(s)
return args.Get(0).(string), args.Error(1)
}

func (m *BaseRepositoryMock) GetTableDDLSqlite(s string) (string, error) {
args := m.Called(s)
return args.Get(0).(string), args.Error(1)
}
5 changes: 5 additions & 0 deletions mocks/key_value_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ func (m *KeyValueServiceMock) DeleteString(s string) error {
args := m.Called(s)
return args.Error(0)
}

func (m *KeyValueServiceMock) ReplaceKeySuffix(s1, s2 string) error {
args := m.Called(s1, s2)
return args.Error(0)
}
1 change: 1 addition & 0 deletions mocks/summary_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type SummaryRepositoryMock struct {
BaseRepositoryMock
mock.Mock
}

Expand Down
5 changes: 5 additions & 0 deletions mocks/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func (m *UserServiceMock) GenerateResetToken(user *models.User) (*models.User, e
return args.Get(0).(*models.User), args.Error(1)
}

func (m *UserServiceMock) ChangeUserId(user *models.User, s1 string) (*models.User, error) {
args := m.Called(user, s1)
return args.Get(0).(*models.User), args.Error(1)
}

func (m *UserServiceMock) FlushCache() {
m.Called()
}
Expand Down
4 changes: 2 additions & 2 deletions repositories/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

type AliasRepository struct {
db *gorm.DB
BaseRepository
}

func NewAliasRepository(db *gorm.DB) *AliasRepository {
return &AliasRepository{db: db}
return &AliasRepository{BaseRepository: NewBaseRepository(db)}
}

func (r *AliasRepository) GetAll() ([]*models.Alias, error) {
Expand Down
40 changes: 40 additions & 0 deletions repositories/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package repositories

import (
"errors"
"gorm.io/gorm"
)

type BaseRepository struct {
db *gorm.DB
}

func NewBaseRepository(db *gorm.DB) BaseRepository {
return BaseRepository{db: db}
}

func (r *BaseRepository) GetDialector() string {
return r.db.Dialector.Name()
}

func (r *BaseRepository) GetTableDDLMysql(tableName string) (result string, err error) {
if dialector := r.GetDialector(); dialector == "sqlite" || dialector == "sqlite3" {
err = r.db.Raw("show create table ?", tableName).Scan(&result).Error
} else {
err = errors.New("not a mysql database")
}
return result, err
}

func (r *BaseRepository) GetTableDDLSqlite(tableName string) (result string, err error) {
if dialector := r.GetDialector(); dialector == "sqlite" || dialector == "sqlite3" {
err = r.db.Table("sqlite_master").
Select("sql").
Where("type = ?", "table").
Where("name = ?", tableName).
Take(&result).Error
} else {
err = errors.New("not an sqlite database")
}
return result, err
}
4 changes: 2 additions & 2 deletions repositories/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

type DiagnosticsRepository struct {
db *gorm.DB
BaseRepository
}

func NewDiagnosticsRepository(db *gorm.DB) *DiagnosticsRepository {
return &DiagnosticsRepository{db: db}
return &DiagnosticsRepository{BaseRepository: NewBaseRepository(db)}
}

func (r *DiagnosticsRepository) Insert(diagnostics *models.Diagnostics) (*models.Diagnostics, error) {
Expand Down
4 changes: 2 additions & 2 deletions repositories/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
)

type HeartbeatRepository struct {
db *gorm.DB
BaseRepository
config *conf.Config
}

func NewHeartbeatRepository(db *gorm.DB) *HeartbeatRepository {
return &HeartbeatRepository{config: conf.Get(), db: db}
return &HeartbeatRepository{BaseRepository: NewBaseRepository(db), config: conf.Get()}
}

// Use with caution!!
Expand Down
29 changes: 27 additions & 2 deletions repositories/key_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repositories

import (
"errors"
"fmt"

"github.com/muety/wakapi/models"
"github.com/muety/wakapi/utils"
Expand All @@ -10,11 +11,11 @@ import (
)

type KeyValueRepository struct {
db *gorm.DB
BaseRepository
}

func NewKeyValueRepository(db *gorm.DB) *KeyValueRepository {
return &KeyValueRepository{db: db}
return &KeyValueRepository{BaseRepository: NewBaseRepository(db)}
}

func (r *KeyValueRepository) GetAll() ([]*models.KeyStringValue, error) {
Expand Down Expand Up @@ -77,3 +78,27 @@ func (r *KeyValueRepository) DeleteString(key string) error {

return nil
}

// ReplaceKeySuffix will search for key-value pairs whose key ends with suffixOld and replace it with suffixNew instead.
func (r *KeyValueRepository) ReplaceKeySuffix(suffixOld, suffixNew string) error {
if dialector := r.db.Dialector.Name(); dialector == "mysql" || dialector == "postgres" {
patternOld := fmt.Sprintf("(.+)%s$", suffixOld)
patternNew := fmt.Sprintf("$1%s", suffixNew) // mysql group replace style
if dialector == "postgres" {
patternNew = fmt.Sprintf("\\1%s", suffixNew) // postgres group replace style
}

return r.db.Model(&models.KeyStringValue{}).
Where(utils.QuoteSql(r.db, "%s like ?", "key"), "%"+suffixOld).
Update("key", gorm.Expr(
utils.QuoteSql(r.db, "regexp_replace(%s, ?, ?)", "key"),
patternOld,
patternNew,
)).Error
} else {
// a bit less safe, because not only replacing suffixes
return r.db.Model(&models.KeyStringValue{}).
Where("key like ?", "%"+suffixOld).
Update("key", gorm.Expr("replace(key, ?, ?)", suffixOld, suffixNew)).Error
}
}
4 changes: 2 additions & 2 deletions repositories/language_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

type LanguageMappingRepository struct {
BaseRepository
config *config.Config
db *gorm.DB
}

func NewLanguageMappingRepository(db *gorm.DB) *LanguageMappingRepository {
return &LanguageMappingRepository{config: config.Get(), db: db}
return &LanguageMappingRepository{BaseRepository: NewBaseRepository(db), config: config.Get()}
}

func (r *LanguageMappingRepository) GetAll() ([]*models.LanguageMapping, error) {
Expand Down
4 changes: 2 additions & 2 deletions repositories/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

type LeaderboardRepository struct {
db *gorm.DB
BaseRepository
}

func NewLeaderboardRepository(db *gorm.DB) *LeaderboardRepository {
return &LeaderboardRepository{db: db}
return &LeaderboardRepository{BaseRepository: NewBaseRepository(db)}
}

func (r *LeaderboardRepository) InsertBatch(items []*models.LeaderboardItem) error {
Expand Down
4 changes: 2 additions & 2 deletions repositories/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

type MetricsRepository struct {
BaseRepository
config *config.Config
db *gorm.DB
}

const sizeTplMysql = `
Expand All @@ -23,7 +23,7 @@ SELECT page_count * page_size as size
FROM pragma_page_count(), pragma_page_size();`

func NewMetricsRepository(db *gorm.DB) *MetricsRepository {
return &MetricsRepository{config: config.Get(), db: db}
return &MetricsRepository{BaseRepository: NewBaseRepository(db), config: config.Get()}
}

func (srv *MetricsRepository) GetDatabaseSize() (size int64, err error) {
Expand Down
4 changes: 2 additions & 2 deletions repositories/project_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

type ProjectLabelRepository struct {
BaseRepository
config *config.Config
db *gorm.DB
}

func NewProjectLabelRepository(db *gorm.DB) *ProjectLabelRepository {
return &ProjectLabelRepository{config: config.Get(), db: db}
return &ProjectLabelRepository{BaseRepository: NewBaseRepository(db), config: config.Get()}
}

func (r *ProjectLabelRepository) GetAll() ([]*models.ProjectLabel, error) {
Expand Down
16 changes: 16 additions & 0 deletions repositories/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import (
"time"
)

type IBaseRepository interface {
GetDialector() string
GetTableDDLMysql(string) (string, error)
GetTableDDLSqlite(string) (string, error)
}

type IAliasRepository interface {
IBaseRepository
Insert(*models.Alias) (*models.Alias, error)
Delete(uint) error
DeleteBatch([]uint) error
Expand All @@ -17,6 +24,7 @@ type IAliasRepository interface {
}

type IHeartbeatRepository interface {
IBaseRepository
InsertBatch([]*models.Heartbeat) error
GetAll() ([]*models.Heartbeat, error)
GetAllWithin(time.Time, time.Time, *models.User) ([]*models.Heartbeat, error)
Expand All @@ -37,18 +45,22 @@ type IHeartbeatRepository interface {
}

type IDiagnosticsRepository interface {
IBaseRepository
Insert(diagnostics *models.Diagnostics) (*models.Diagnostics, error)
}

type IKeyValueRepository interface {
IBaseRepository
GetAll() ([]*models.KeyStringValue, error)
GetString(string) (*models.KeyStringValue, error)
PutString(*models.KeyStringValue) error
DeleteString(string) error
Search(string) ([]*models.KeyStringValue, error)
ReplaceKeySuffix(string, string) error
}

type ILanguageMappingRepository interface {
IBaseRepository
GetAll() ([]*models.LanguageMapping, error)
GetById(uint) (*models.LanguageMapping, error)
GetByUser(string) ([]*models.LanguageMapping, error)
Expand All @@ -57,6 +69,7 @@ type ILanguageMappingRepository interface {
}

type IProjectLabelRepository interface {
IBaseRepository
GetAll() ([]*models.ProjectLabel, error)
GetById(uint) (*models.ProjectLabel, error)
GetByUser(string) ([]*models.ProjectLabel, error)
Expand All @@ -65,6 +78,7 @@ type IProjectLabelRepository interface {
}

type ISummaryRepository interface {
IBaseRepository
Insert(*models.Summary) error
GetAll() ([]*models.Summary, error)
GetByUserWithin(*models.User, time.Time, time.Time) ([]*models.Summary, error)
Expand All @@ -74,6 +88,7 @@ type ISummaryRepository interface {
}

type IUserRepository interface {
IBaseRepository
FindOne(user models.User) (*models.User, error)
GetByIds([]string) ([]*models.User, error)
GetAll() ([]*models.User, error)
Expand All @@ -91,6 +106,7 @@ type IUserRepository interface {
}

type ILeaderboardRepository interface {
IBaseRepository
InsertBatch([]*models.LeaderboardItem) error
CountAllByUser(string) (int64, error)
CountUsers(bool) (int64, error)
Expand Down
4 changes: 2 additions & 2 deletions repositories/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
)

type SummaryRepository struct {
db *gorm.DB
BaseRepository
}

func NewSummaryRepository(db *gorm.DB) *SummaryRepository {
return &SummaryRepository{db: db}
return &SummaryRepository{BaseRepository: NewBaseRepository(db)}
}

func (r *SummaryRepository) GetAll() ([]*models.Summary, error) {
Expand Down
4 changes: 2 additions & 2 deletions repositories/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

type UserRepository struct {
db *gorm.DB
BaseRepository
}

func NewUserRepository(db *gorm.DB) *UserRepository {
return &UserRepository{db: db}
return &UserRepository{BaseRepository: NewBaseRepository(db)}
}

func (r *UserRepository) FindOne(attributes models.User) (*models.User, error) {
Expand Down
Loading

0 comments on commit 674296c

Please sign in to comment.