fix: improve db call when only count is required (on views) (#2769)

* fix: improve db call when only count is required (old views)

* Update query.go
This commit is contained in:
Livio Amstutz 2021-12-06 16:28:49 +01:00 committed by GitHub
parent a8eed4a215
commit 81efd86a8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 12 deletions

View File

@ -22,6 +22,6 @@ func (repo *UserSessionRepo) GetMyUserSessions(ctx context.Context) ([]*usr_mode
}
func (repo *UserSessionRepo) ActiveUserSessionCount() int64 {
userSessions, _ := repo.View.ActiveUserSessions()
return int64(len(userSessions))
userSessions, _ := repo.View.ActiveUserSessionsCount()
return int64(userSessions)
}

View File

@ -24,7 +24,7 @@ func (v *View) UserSessionsByAgentID(agentID string) ([]*model.UserSessionView,
return view.UserSessionsByAgentID(v.Db, userSessionTable, agentID)
}
func (v *View) ActiveUserSessions() ([]*model.UserSessionView, error) {
func (v *View) ActiveUserSessionsCount() (uint64, error) {
return view.ActiveUserSessions(v.Db, userSessionTable)
}

View File

@ -1,14 +1,14 @@
package view
import (
"github.com/jinzhu/gorm"
auth_model "github.com/caos/zitadel/internal/auth_request/model"
"github.com/caos/zitadel/internal/domain"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/view/repository"
"github.com/jinzhu/gorm"
usr_model "github.com/caos/zitadel/internal/user/model"
"github.com/caos/zitadel/internal/user/repository/view/model"
"github.com/caos/zitadel/internal/view/repository"
)
func UserSessionByIDs(db *gorm.DB, table, agentID, userID string) (*model.UserSessionView, error) {
@ -59,8 +59,7 @@ func UserSessionsByAgentID(db *gorm.DB, table, agentID string) ([]*model.UserSes
return userSessions, err
}
func ActiveUserSessions(db *gorm.DB, table string) ([]*model.UserSessionView, error) {
userSessions := make([]*model.UserSessionView, 0)
func ActiveUserSessions(db *gorm.DB, table string) (uint64, error) {
activeQuery := &usr_model.UserSessionSearchQuery{
Key: usr_model.UserSessionSearchKeyState,
Method: domain.SearchMethodEquals,
@ -69,8 +68,7 @@ func ActiveUserSessions(db *gorm.DB, table string) ([]*model.UserSessionView, er
query := repository.PrepareSearchQuery(table, model.UserSessionSearchRequest{
Queries: []*usr_model.UserSessionSearchQuery{activeQuery},
})
_, err := query(db, &userSessions)
return userSessions, err
return query(db, nil)
}
func PutUserSession(db *gorm.DB, table string, session *model.UserSessionView) error {

View File

@ -2,11 +2,12 @@ package repository
import (
"fmt"
"github.com/caos/zitadel/internal/domain"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/jinzhu/gorm"
"github.com/lib/pq"
"github.com/caos/zitadel/internal/domain"
caos_errs "github.com/caos/zitadel/internal/errors"
)
type SearchRequest interface {
@ -48,6 +49,9 @@ func PrepareSearchQuery(table string, request SearchRequest) func(db *gorm.DB, r
}
query = query.Count(&count)
if res == nil {
return count, nil
}
if request.GetLimit() != 0 {
query = query.Limit(request.GetLimit())
}