2020-11-01 18:56:36 +03:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
2023-04-03 21:30:23 +03:00
|
|
|
"github.com/duke-git/lancet/v2/slice"
|
2022-03-19 12:30:32 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2020-11-01 18:56:36 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-01 22:14:10 +03:00
|
|
|
"gorm.io/gorm"
|
2021-02-06 02:33:20 +03:00
|
|
|
"gorm.io/gorm/clause"
|
2020-11-01 18:56:36 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HeartbeatRepository struct {
|
2022-03-19 12:30:32 +03:00
|
|
|
db *gorm.DB
|
|
|
|
config *conf.Config
|
2020-11-01 18:56:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHeartbeatRepository(db *gorm.DB) *HeartbeatRepository {
|
2022-03-19 12:30:32 +03:00
|
|
|
return &HeartbeatRepository{config: conf.Get(), db: db}
|
2020-11-01 18:56:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-18 11:59:13 +03:00
|
|
|
// Use with caution!!
|
|
|
|
func (r *HeartbeatRepository) GetAll() ([]*models.Heartbeat, error) {
|
|
|
|
var heartbeats []*models.Heartbeat
|
|
|
|
if err := r.db.Find(&heartbeats).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return heartbeats, nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
func (r *HeartbeatRepository) InsertBatch(heartbeats []*models.Heartbeat) error {
|
2021-02-06 02:33:20 +03:00
|
|
|
if err := r.db.
|
|
|
|
Clauses(clause.OnConflict{
|
|
|
|
DoNothing: true,
|
|
|
|
}).
|
|
|
|
Create(&heartbeats).Error; err != nil {
|
2020-11-01 18:56:36 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-30 11:12:28 +03:00
|
|
|
func (r *HeartbeatRepository) GetLatestByUser(user *models.User) (*models.Heartbeat, error) {
|
|
|
|
var heartbeat models.Heartbeat
|
|
|
|
if err := r.db.
|
|
|
|
Model(&models.Heartbeat{}).
|
|
|
|
Where(&models.Heartbeat{UserID: user.ID}).
|
|
|
|
Order("time desc").
|
|
|
|
First(&heartbeat).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &heartbeat, nil
|
|
|
|
}
|
|
|
|
|
2021-02-06 02:31:30 +03:00
|
|
|
func (r *HeartbeatRepository) GetLatestByOriginAndUser(origin string, user *models.User) (*models.Heartbeat, error) {
|
|
|
|
var heartbeat models.Heartbeat
|
|
|
|
if err := r.db.
|
|
|
|
Model(&models.Heartbeat{}).
|
|
|
|
Where(&models.Heartbeat{
|
|
|
|
UserID: user.ID,
|
|
|
|
Origin: origin,
|
|
|
|
}).
|
|
|
|
Order("time desc").
|
|
|
|
First(&heartbeat).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &heartbeat, nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
func (r *HeartbeatRepository) GetAllWithin(from, to time.Time, user *models.User) ([]*models.Heartbeat, error) {
|
2021-05-14 10:38:31 +03:00
|
|
|
// https://stackoverflow.com/a/20765152/3112139
|
2020-11-01 18:56:36 +03:00
|
|
|
var heartbeats []*models.Heartbeat
|
|
|
|
if err := r.db.
|
|
|
|
Where(&models.Heartbeat{UserID: user.ID}).
|
2021-04-25 15:15:18 +03:00
|
|
|
Where("time >= ?", from.Local()).
|
|
|
|
Where("time < ?", to.Local()).
|
2020-11-01 18:56:36 +03:00
|
|
|
Order("time asc").
|
|
|
|
Find(&heartbeats).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return heartbeats, nil
|
|
|
|
}
|
|
|
|
|
2022-03-13 10:17:50 +03:00
|
|
|
func (r *HeartbeatRepository) GetAllWithinByFilters(from, to time.Time, user *models.User, filterMap map[string][]string) ([]*models.Heartbeat, error) {
|
|
|
|
// https://stackoverflow.com/a/20765152/3112139
|
|
|
|
var heartbeats []*models.Heartbeat
|
|
|
|
|
|
|
|
q := r.db.
|
|
|
|
Where(&models.Heartbeat{UserID: user.ID}).
|
|
|
|
Where("time >= ?", from.Local()).
|
|
|
|
Where("time < ?", to.Local()).
|
|
|
|
Order("time asc")
|
|
|
|
|
|
|
|
for col, vals := range filterMap {
|
2023-04-03 21:30:23 +03:00
|
|
|
q = q.Where(col+" in ?", slice.Map[string, string](vals, func(i int, val string) string {
|
|
|
|
// query for "unknown" projects, languages, etc.
|
|
|
|
if val == "-" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}))
|
2022-03-13 10:17:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := q.Find(&heartbeats).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return heartbeats, nil
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:01:35 +03:00
|
|
|
func (r *HeartbeatRepository) GetFirstByUsers() ([]*models.TimeByUser, error) {
|
|
|
|
var result []*models.TimeByUser
|
|
|
|
r.db.Model(&models.User{}).
|
|
|
|
Select("users.id as user, min(time) as time").
|
|
|
|
Joins("left join heartbeats on users.id = heartbeats.user_id").
|
|
|
|
Group("user").
|
|
|
|
Scan(&result)
|
|
|
|
return result, nil
|
2020-11-01 18:56:36 +03:00
|
|
|
}
|
|
|
|
|
2021-02-13 13:23:58 +03:00
|
|
|
func (r *HeartbeatRepository) GetLastByUsers() ([]*models.TimeByUser, error) {
|
|
|
|
var result []*models.TimeByUser
|
|
|
|
r.db.Model(&models.User{}).
|
|
|
|
Select("users.id as user, max(time) as time").
|
|
|
|
Joins("left join heartbeats on users.id = heartbeats.user_id").
|
|
|
|
Group("user").
|
|
|
|
Scan(&result)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-03-19 12:30:32 +03:00
|
|
|
func (r *HeartbeatRepository) Count(approximate bool) (count int64, err error) {
|
|
|
|
if r.config.Db.IsMySQL() && approximate {
|
|
|
|
err = r.db.Table("information_schema.tables").
|
|
|
|
Select("table_rows").
|
|
|
|
Where("table_schema = ?", r.config.Db.Name).
|
|
|
|
Where("table_name = 'heartbeats'").
|
|
|
|
Scan(&count).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
err = r.db.
|
|
|
|
Model(&models.Heartbeat{}).
|
|
|
|
Count(&count).Error
|
2021-02-13 13:23:58 +03:00
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HeartbeatRepository) CountByUser(user *models.User) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
if err := r.db.
|
|
|
|
Model(&models.Heartbeat{}).
|
|
|
|
Where(&models.Heartbeat{UserID: user.ID}).
|
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *HeartbeatRepository) CountByUsers(users []*models.User) ([]*models.CountByUser, error) {
|
|
|
|
var counts []*models.CountByUser
|
|
|
|
|
|
|
|
userIds := make([]string, len(users))
|
|
|
|
for i, u := range users {
|
|
|
|
userIds[i] = u.ID
|
|
|
|
}
|
|
|
|
|
2022-03-19 12:30:32 +03:00
|
|
|
if len(userIds) == 0 {
|
|
|
|
return counts, nil
|
|
|
|
}
|
|
|
|
|
2021-02-13 13:23:58 +03:00
|
|
|
if err := r.db.
|
2021-05-14 10:38:31 +03:00
|
|
|
Model(&models.Heartbeat{}).
|
|
|
|
Select("user_id as user, count(id) as count").
|
2021-02-13 13:23:58 +03:00
|
|
|
Where("user_id in ?", userIds).
|
|
|
|
Group("user").
|
|
|
|
Find(&counts).Error; err != nil {
|
|
|
|
return counts, err
|
|
|
|
}
|
2022-03-19 12:30:32 +03:00
|
|
|
|
2021-02-13 13:23:58 +03:00
|
|
|
return counts, nil
|
|
|
|
}
|
|
|
|
|
2022-12-01 22:26:03 +03:00
|
|
|
func (r *HeartbeatRepository) GetEntitySetByUser(entityType uint8, user *models.User) ([]string, error) {
|
2021-05-01 14:52:03 +03:00
|
|
|
var results []string
|
|
|
|
if err := r.db.
|
|
|
|
Model(&models.Heartbeat{}).
|
2022-03-13 10:56:35 +03:00
|
|
|
Distinct(models.GetEntityColumn(entityType)).
|
2021-05-01 14:52:03 +03:00
|
|
|
Where(&models.Heartbeat{UserID: user.ID}).
|
|
|
|
Find(&results).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
func (r *HeartbeatRepository) DeleteBefore(t time.Time) error {
|
|
|
|
if err := r.db.
|
2021-04-25 15:15:18 +03:00
|
|
|
Where("time <= ?", t.Local()).
|
2020-11-01 18:56:36 +03:00
|
|
|
Delete(models.Heartbeat{}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-17 13:55:13 +03:00
|
|
|
|
|
|
|
func (r *HeartbeatRepository) DeleteByUser(user *models.User) error {
|
|
|
|
if err := r.db.
|
|
|
|
Where("user_id = ?", user.ID).
|
|
|
|
Delete(models.Heartbeat{}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-01 22:26:03 +03:00
|
|
|
|
|
|
|
func (r *HeartbeatRepository) DeleteByUserBefore(user *models.User, t time.Time) error {
|
|
|
|
if err := r.db.
|
|
|
|
Where("user_id = ?", user.ID).
|
|
|
|
Where("time <= ?", t.Local()).
|
|
|
|
Delete(models.Heartbeat{}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|