fix some issue

This commit is contained in:
Chen Junda
2024-01-12 23:41:18 +08:00
parent e296886d92
commit 056633c5ec
4 changed files with 16 additions and 12 deletions

View File

@@ -1,8 +1,6 @@
package migrations
import (
"fmt"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/utils"
@@ -19,7 +17,7 @@ func init() {
return nil
}
condition := fmt.Sprintf("%s = ?", utils.QuoteDbIdentifier(db, "key"))
condition := utils.QuoteSql(db, "%s = ?", "key")
imprintKv := &models.KeyStringValue{Key: "imprint", Value: "no content here"}
if err := db.

View File

@@ -1,8 +1,6 @@
package migrations
import (
"fmt"
"github.com/emvi/logbuch"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/utils"
@@ -10,7 +8,7 @@ import (
)
func hasRun(name string, db *gorm.DB) bool {
condition := fmt.Sprintf("%s = ?", utils.QuoteDbIdentifier(db, "key"))
condition := utils.QuoteSql(db, "%s = ?", "key")
lookupResult := db.Where(condition, name).First(&models.KeyStringValue{})
if lookupResult.Error == nil && lookupResult.RowsAffected > 0 {

View File

@@ -234,10 +234,17 @@ func (r *HeartbeatRepository) GetUserProjectStats(user *models.User, from, to ti
// multi-line string with backticks yields an error with the github.com/glebarez/sqlite driver
limitClause := "limit ?"
args := []interface{}{
user.ID, from.Format(time.RFC3339), to.Format(time.RFC3339),
}
limitOffsetClause := "limit ? offset ?"
if r.config.Db.IsMssql() {
limitClause = "ROWS fetch next ? rows only"
limitOffsetClause = "offset ? ROWS fetch next ? rows only"
args = append(args, offset, limit)
} else {
args = append(args, limit, offset)
}
if err := r.db.
@@ -249,13 +256,13 @@ func (r *HeartbeatRepository) GetUserProjectStats(user *models.User, from, to ti
"and language is not null and language != '' and project != '' "+
"group by project, user_id "+
"order by last desc "+
"offset ? "+limitClause+
limitOffsetClause+
") "+
"select distinct project, min(first) as first, min(last) as last, min(cnt) as count, first_value(language) over (partition by project order by count(*) desc) as top_language "+
"from heartbeats "+
"inner join projects on heartbeats.project = projects.p and heartbeats.user_id = projects.user_id "+
"group by project, language "+
"order by last desc", user.ID, from.Format(time.RFC3339), to.Format(time.RFC3339), offset, limit).
"order by last desc", args...).
Scan(&projectStats).Error; err != nil {
return nil, err
}

View File

@@ -5,6 +5,7 @@ import (
"time"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/utils"
"gorm.io/gorm"
)
@@ -86,12 +87,12 @@ func (r *UserRepository) GetByLoggedInAfter(t time.Time) ([]*models.User, error)
// NOTE: Only ID field will be populated
func (r *UserRepository) GetByLastActiveAfter(t time.Time) ([]*models.User, error) {
subQuery1 := r.db.Model(&models.Heartbeat{}).
Select("user_id , max(time) as time").
Select(utils.QuoteSql(r.db, "user_id as %s, max(time) as time", "user")).
Group("user_id")
var userIds []string
if err := r.db.
Select("user").
Select(utils.QuoteSql(r.db, "user as %s", "id")).
Table("(?) as q", subQuery1).
Where("time >= ?", t.Local()).
Scan(&userIds).Error; err != nil {