Certificate displaying and monitoring

This commit is contained in:
yoan
2024-11-22 10:59:57 +08:00
parent 09e4b24445
commit 86761bd3a0
19 changed files with 498 additions and 381 deletions

View File

@@ -0,0 +1,11 @@
package domain
type Statistics struct {
CertificateTotal int `json:"certificateTotal"`
CertificateExpireSoon int `json:"certificateExpireSoon"`
CertificateExpired int `json:"certificateExpired"`
WorkflowTotal int `json:"workflowTotal"`
WorkflowEnabled int `json:"workflowEnabled"`
WorkflowDisabled int `json:"workflowDisabled"`
}

View File

@@ -19,8 +19,8 @@ const (
func PushExpireMsg() {
// 查询即将过期的证书
records, err := app.GetApp().Dao().FindRecordsByFilter("domains", "expiredAt<{:time}&&certUrl!=''", "-created", 500, 0,
dbx.Params{"time": xtime.GetTimeAfter(24 * time.Hour * 15)})
records, err := app.GetApp().Dao().FindRecordsByFilter("certificate", "expireAt<{:time}&&certUrl!=''", "-created", 500, 0,
dbx.Params{"time": xtime.GetTimeAfter(24 * time.Hour * 20)})
if err != nil {
app.GetApp().Logger().Error("find expired domains by filter", "error", err)
return
@@ -76,7 +76,7 @@ func buildMsg(records []*models.Record) *notifyMessage {
domains := make([]string, count)
for i, record := range records {
domains[i] = record.GetString("domain")
domains[i] = record.GetString("san")
}
countStr := strconv.Itoa(count)

View File

@@ -0,0 +1,63 @@
package repository
import (
"context"
"github.com/usual2970/certimate/internal/domain"
"github.com/usual2970/certimate/internal/utils/app"
)
type StatisticsRepository struct{}
func NewStatisticsRepository() *StatisticsRepository {
return &StatisticsRepository{}
}
type totalResp struct {
Total int `json:"total" db:"total"`
}
func (r *StatisticsRepository) Get(ctx context.Context) (*domain.Statistics, error) {
rs := &domain.Statistics{}
// 所有证书
certTotal := totalResp{}
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from certificate").One(&certTotal); err != nil {
return nil, err
}
rs.CertificateTotal = certTotal.Total
// 即将过期证书
certExpireSoonTotal := totalResp{}
if err := app.GetApp().Dao().DB().
NewQuery("select count(*) as total from certificate where expireAt > datetime('now') and expireAt < datetime('now', '+20 days')").
One(&certExpireSoonTotal); err != nil {
return nil, err
}
rs.CertificateExpireSoon = certExpireSoonTotal.Total
// 已过期证书
certExpiredTotal := totalResp{}
if err := app.GetApp().Dao().DB().
NewQuery("select count(*) as total from certificate where expireAt < datetime('now')").
One(&certExpiredTotal); err != nil {
return nil, err
}
rs.CertificateExpired = certExpiredTotal.Total
// 所有工作流
workflowTotal := totalResp{}
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from workflow").One(&workflowTotal); err != nil {
return nil, err
}
rs.WorkflowTotal = workflowTotal.Total
// 已启用工作流
workflowEnabledTotal := totalResp{}
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from workflow where enabled is TRUE").One(&workflowEnabledTotal); err != nil {
return nil, err
}
rs.WorkflowEnabled = workflowEnabledTotal.Total
rs.WorkflowDisabled = workflowTotal.Total - workflowEnabledTotal.Total
return rs, nil
}

View File

@@ -0,0 +1,35 @@
package rest
import (
"context"
"github.com/labstack/echo/v5"
"github.com/usual2970/certimate/internal/domain"
"github.com/usual2970/certimate/internal/utils/resp"
)
type StatisticsService interface {
Get(ctx context.Context) (*domain.Statistics, error)
}
type statisticsHandler struct {
service StatisticsService
}
func NewStatisticsHandler(route *echo.Group, service StatisticsService) {
handler := &statisticsHandler{
service: service,
}
group := route.Group("/statistics")
group.GET("/get", handler.get)
}
func (handler *statisticsHandler) get(c echo.Context) error {
if statistics, err := handler.service.Get(c.Request().Context()); err != nil {
return resp.Err(c, err)
} else {
return resp.Succ(c, statistics)
}
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/usual2970/certimate/internal/notify"
"github.com/usual2970/certimate/internal/repository"
"github.com/usual2970/certimate/internal/rest"
"github.com/usual2970/certimate/internal/statistics"
"github.com/usual2970/certimate/internal/workflow"
"github.com/labstack/echo/v5"
@@ -19,7 +20,12 @@ func Register(e *echo.Echo) {
workflowRepo := repository.NewWorkflowRepository()
workflowSvc := workflow.NewWorkflowService(workflowRepo)
statisticsRepo := repository.NewStatisticsRepository()
statisticsSvc := statistics.NewStatisticsService(statisticsRepo)
rest.NewWorkflowHandler(group, workflowSvc)
rest.NewNotifyHandler(group, notifySvc)
rest.NewStatisticsHandler(group, statisticsSvc)
}

View File

@@ -0,0 +1,25 @@
package statistics
import (
"context"
"github.com/usual2970/certimate/internal/domain"
)
type StatisticsRepository interface {
Get(ctx context.Context) (*domain.Statistics, error)
}
type StatisticsService struct {
repo StatisticsRepository
}
func NewStatisticsService(repo StatisticsRepository) *StatisticsService {
return &StatisticsService{
repo: repo,
}
}
func (s *StatisticsService) Get(ctx context.Context) (*domain.Statistics, error) {
return s.repo.Get(ctx)
}