提交 v1.3.0 beta
This commit is contained in:
@ -2,15 +2,15 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"next-terminal/server/common/nt"
|
||||
"time"
|
||||
|
||||
"next-terminal/server/config"
|
||||
"next-terminal/server/constant"
|
||||
"next-terminal/server/dto"
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var SessionRepository = new(sessionRepository)
|
||||
|
||||
type sessionRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
@ -22,7 +22,10 @@ func (r sessionRepository) Find(c context.Context, pageIndex, pageSize int, stat
|
||||
|
||||
params = append(params, status)
|
||||
|
||||
itemSql := "SELECT s.id,s.mode, s.protocol,s.recording, s.connection_id, s.asset_id, s.creator, s.client_ip, s.width, s.height, s.ip, s.port, s.username, s.status, s.connected_time, s.disconnected_time,s.code,s.reviewed, s.message, a.name AS asset_name, u.nickname AS creator_name FROM sessions s LEFT JOIN assets a ON s.asset_id = a.id LEFT JOIN users u ON s.creator = u.id WHERE s.STATUS = ? "
|
||||
itemSql := "SELECT s.id,s.mode, s.protocol,s.recording, s.connection_id, s.asset_id, s.creator, s.client_ip, s.width, s.height, s.ip, s.port, s.username, s.status, s.connected_time, s.disconnected_time,s.code,s.reviewed, s.message,s.command_count, a.name AS asset_name, u.nickname AS creator_name FROM sessions s " +
|
||||
"LEFT JOIN assets a ON s.asset_id = a.id " +
|
||||
"LEFT JOIN users u ON s.creator = u.id " +
|
||||
"WHERE s.STATUS = ? "
|
||||
countSql := "select count(*) from sessions as s where s.status = ? "
|
||||
|
||||
if len(userId) > 0 {
|
||||
@ -81,7 +84,7 @@ func (r sessionRepository) FindByStatusIn(c context.Context, statuses []string)
|
||||
|
||||
func (r sessionRepository) FindOutTimeSessions(c context.Context, dayLimit int) (o []model.Session, err error) {
|
||||
limitTime := time.Now().Add(time.Duration(-dayLimit*24) * time.Hour)
|
||||
err = r.GetDB(c).Where("status = ? and connected_time < ?", constant.Disconnected, limitTime).Find(&o).Error
|
||||
err = r.GetDB(c).Where("status = ? and connected_time < ?", nt.Disconnected, limitTime).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
@ -117,25 +120,17 @@ func (r sessionRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.Session{}).Error
|
||||
}
|
||||
|
||||
func (r sessionRepository) DeleteByIds(c context.Context, sessionIds []string) error {
|
||||
recordingPath := config.GlobalCfg.Guacd.Recording
|
||||
for i := range sessionIds {
|
||||
if err := os.RemoveAll(path.Join(recordingPath, sessionIds[i])); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.DeleteById(c, sessionIds[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r sessionRepository) DeleteByStatus(c context.Context, status string) error {
|
||||
return r.GetDB(c).Where("status = ?", status).Delete(&model.Session{}).Error
|
||||
}
|
||||
|
||||
func (r sessionRepository) CountOnlineSession(c context.Context) (total int64, err error) {
|
||||
err = r.GetDB(c).Where("status = ?", constant.Connected).Find(&model.Session{}).Count(&total).Error
|
||||
err = r.GetDB(c).Where("status = ?", nt.Connected).Find(&model.Session{}).Count(&total).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r sessionRepository) CountOfflineSession(c context.Context) (total int64, err error) {
|
||||
err = r.GetDB(c).Where("status = ?", nt.Disconnected).Find(&model.Session{}).Count(&total).Error
|
||||
return
|
||||
}
|
||||
|
||||
@ -149,16 +144,6 @@ func (r sessionRepository) CountByStatus(c context.Context, status string) (tota
|
||||
return
|
||||
}
|
||||
|
||||
func (r sessionRepository) OverviewAccess(c context.Context) (o []model.SessionForAccess, err error) {
|
||||
db := r.GetDB(c)
|
||||
sql := "SELECT s.asset_id, s.ip, s.port, s.protocol, s.username, count(s.asset_id) AS access_count FROM sessions AS s GROUP BY s.asset_id, s.ip, s.port, s.protocol, s.username ORDER BY access_count DESC limit 10"
|
||||
err = db.Raw(sql).Scan(&o).Error
|
||||
if o == nil {
|
||||
o = make([]model.SessionForAccess, 0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r sessionRepository) UpdateReadByIds(c context.Context, reviewed bool, ids []string) error {
|
||||
sql := "update sessions set reviewed = ? where id in ?"
|
||||
return r.GetDB(c).Exec(sql, reviewed, ids).Error
|
||||
@ -173,3 +158,8 @@ func (r sessionRepository) UpdateMode(c context.Context) error {
|
||||
sql := "update sessions set mode = 'native' where mode = 'naive'"
|
||||
return r.GetDB(c).Exec(sql).Error
|
||||
}
|
||||
|
||||
func (r sessionRepository) CountWithGroupByLoginTime(c context.Context, t time.Time) (counter []dto.DateCounter, err error) {
|
||||
err = r.GetDB(c).Table("sessions").Select("date(connected_time) as date, count(id) as value").Where("connected_time > ?", t).Group("date(connected_time)").Scan(&counter).Error
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user