1Panel/backend/app/service/logs.go

87 lines
2.4 KiB
Go
Raw Normal View History

2022-11-15 17:20:57 +08:00
package service
import (
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
2022-11-15 17:20:57 +08:00
"github.com/jinzhu/copier"
"github.com/pkg/errors"
)
type LogService struct{}
const logs = "https://resource.fit2cloud.com/installation-log.sh"
2022-11-15 17:20:57 +08:00
type ILogService interface {
CreateLoginLog(operation model.LoginLog) error
2023-02-07 18:48:32 +08:00
PageLoginLog(search dto.SearchLgLogWithPage) (int64, interface{}, error)
2022-11-15 17:20:57 +08:00
CreateOperationLog(operation model.OperationLog) error
2023-02-07 18:48:32 +08:00
PageOperationLog(search dto.SearchOpLogWithPage) (int64, interface{}, error)
2022-11-15 18:44:50 +08:00
CleanLogs(logtype string) error
2022-11-15 17:20:57 +08:00
}
func NewILogService() ILogService {
return &LogService{}
}
func (u *LogService) CreateLoginLog(operation model.LoginLog) error {
return logRepo.CreateLoginLog(&operation)
}
2023-02-07 18:48:32 +08:00
func (u *LogService) PageLoginLog(req dto.SearchLgLogWithPage) (int64, interface{}, error) {
total, ops, err := logRepo.PageLoginLog(
req.Page,
req.PageSize,
logRepo.WithByIP(req.IP),
logRepo.WithByStatus(req.Status),
commonRepo.WithOrderBy("created_at desc"),
)
2022-11-15 17:20:57 +08:00
var dtoOps []dto.LoginLog
for _, op := range ops {
var item dto.LoginLog
if err := copier.Copy(&item, &op); err != nil {
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
dtoOps = append(dtoOps, item)
}
return total, dtoOps, err
}
func (u *LogService) CreateOperationLog(operation model.OperationLog) error {
return logRepo.CreateOperationLog(&operation)
}
2023-02-07 18:48:32 +08:00
func (u *LogService) PageOperationLog(req dto.SearchOpLogWithPage) (int64, interface{}, error) {
total, ops, err := logRepo.PageOperationLog(
req.Page,
req.PageSize,
logRepo.WithByGroup(req.Source),
logRepo.WithByLikeOperation(req.Operation),
logRepo.WithByStatus(req.Status),
commonRepo.WithOrderBy("created_at desc"),
)
2022-11-15 17:20:57 +08:00
var dtoOps []dto.OperationLog
for _, op := range ops {
var item dto.OperationLog
if err := copier.Copy(&item, &op); err != nil {
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
dtoOps = append(dtoOps, item)
}
return total, dtoOps, err
}
2022-11-15 18:44:50 +08:00
func (u *LogService) CleanLogs(logtype string) error {
if logtype == "operation" {
return logRepo.CleanOperation()
}
return logRepo.CleanLogin()
}
func writeLogs(version string) {
_, _ = cmd.Execf("curl -sfL %s | sh -s 1p upgrade %s", logs, version)
}