mirror of https://gitee.com/answerdev/answer.git
update dashboard cache
This commit is contained in:
parent
ff5ea64f09
commit
ffec3a0b9c
24
docs/docs.go
24
docs/docs.go
|
@ -5342,11 +5342,17 @@ const docTemplate = `{
|
|||
"schema.SiteGeneralReq": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"contact_email",
|
||||
"description",
|
||||
"name",
|
||||
"short_description"
|
||||
"short_description",
|
||||
"site_url"
|
||||
],
|
||||
"properties": {
|
||||
"contact_email": {
|
||||
"type": "string",
|
||||
"maxLength": 512
|
||||
},
|
||||
"description": {
|
||||
"type": "string",
|
||||
"maxLength": 2000
|
||||
|
@ -5358,17 +5364,27 @@ const docTemplate = `{
|
|||
"short_description": {
|
||||
"type": "string",
|
||||
"maxLength": 255
|
||||
},
|
||||
"site_url": {
|
||||
"type": "string",
|
||||
"maxLength": 512
|
||||
}
|
||||
}
|
||||
},
|
||||
"schema.SiteGeneralResp": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"contact_email",
|
||||
"description",
|
||||
"name",
|
||||
"short_description"
|
||||
"short_description",
|
||||
"site_url"
|
||||
],
|
||||
"properties": {
|
||||
"contact_email": {
|
||||
"type": "string",
|
||||
"maxLength": 512
|
||||
},
|
||||
"description": {
|
||||
"type": "string",
|
||||
"maxLength": 2000
|
||||
|
@ -5380,6 +5396,10 @@ const docTemplate = `{
|
|||
"short_description": {
|
||||
"type": "string",
|
||||
"maxLength": 255
|
||||
},
|
||||
"site_url": {
|
||||
"type": "string",
|
||||
"maxLength": 512
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -5330,11 +5330,17 @@
|
|||
"schema.SiteGeneralReq": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"contact_email",
|
||||
"description",
|
||||
"name",
|
||||
"short_description"
|
||||
"short_description",
|
||||
"site_url"
|
||||
],
|
||||
"properties": {
|
||||
"contact_email": {
|
||||
"type": "string",
|
||||
"maxLength": 512
|
||||
},
|
||||
"description": {
|
||||
"type": "string",
|
||||
"maxLength": 2000
|
||||
|
@ -5346,17 +5352,27 @@
|
|||
"short_description": {
|
||||
"type": "string",
|
||||
"maxLength": 255
|
||||
},
|
||||
"site_url": {
|
||||
"type": "string",
|
||||
"maxLength": 512
|
||||
}
|
||||
}
|
||||
},
|
||||
"schema.SiteGeneralResp": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"contact_email",
|
||||
"description",
|
||||
"name",
|
||||
"short_description"
|
||||
"short_description",
|
||||
"site_url"
|
||||
],
|
||||
"properties": {
|
||||
"contact_email": {
|
||||
"type": "string",
|
||||
"maxLength": 512
|
||||
},
|
||||
"description": {
|
||||
"type": "string",
|
||||
"maxLength": 2000
|
||||
|
@ -5368,6 +5384,10 @@
|
|||
"short_description": {
|
||||
"type": "string",
|
||||
"maxLength": 255
|
||||
},
|
||||
"site_url": {
|
||||
"type": "string",
|
||||
"maxLength": 512
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -989,6 +989,9 @@ definitions:
|
|||
type: object
|
||||
schema.SiteGeneralReq:
|
||||
properties:
|
||||
contact_email:
|
||||
maxLength: 512
|
||||
type: string
|
||||
description:
|
||||
maxLength: 2000
|
||||
type: string
|
||||
|
@ -998,13 +1001,21 @@ definitions:
|
|||
short_description:
|
||||
maxLength: 255
|
||||
type: string
|
||||
site_url:
|
||||
maxLength: 512
|
||||
type: string
|
||||
required:
|
||||
- contact_email
|
||||
- description
|
||||
- name
|
||||
- short_description
|
||||
- site_url
|
||||
type: object
|
||||
schema.SiteGeneralResp:
|
||||
properties:
|
||||
contact_email:
|
||||
maxLength: 512
|
||||
type: string
|
||||
description:
|
||||
maxLength: 2000
|
||||
type: string
|
||||
|
@ -1014,10 +1025,15 @@ definitions:
|
|||
short_description:
|
||||
maxLength: 255
|
||||
type: string
|
||||
site_url:
|
||||
maxLength: 512
|
||||
type: string
|
||||
required:
|
||||
- contact_email
|
||||
- description
|
||||
- name
|
||||
- short_description
|
||||
- site_url
|
||||
type: object
|
||||
schema.SiteInterfaceReq:
|
||||
properties:
|
||||
|
|
|
@ -6,7 +6,7 @@ var AppStartTime time.Time
|
|||
|
||||
const (
|
||||
DashBoardCachekey = "answer@dashboard"
|
||||
DashBoardCacheTime = 31 * time.Minute
|
||||
DashBoardCacheTime = 60 * time.Minute
|
||||
)
|
||||
|
||||
type DashboardInfo struct {
|
||||
|
|
|
@ -5,6 +5,8 @@ type SiteGeneralReq struct {
|
|||
Name string `validate:"required,gt=1,lte=128" form:"name" json:"name"`
|
||||
ShortDescription string `validate:"required,gt=3,lte=255" form:"short_description" json:"short_description"`
|
||||
Description string `validate:"required,gt=3,lte=2000" form:"description" json:"description"`
|
||||
SiteUrl string `validate:"required,gt=1,lte=512,url" form:"site_url" json:"site_url"`
|
||||
ContactEmail string `validate:"required,gt=1,lte=512,email" form:"contact_email" json:"contact_email"`
|
||||
}
|
||||
|
||||
// SiteInterfaceReq site interface request
|
||||
|
|
|
@ -6,8 +6,7 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/answerdev/answer/internal/base/constant"
|
||||
|
@ -24,6 +23,7 @@ import (
|
|||
"github.com/answerdev/answer/internal/service/service_config"
|
||||
"github.com/answerdev/answer/internal/service/siteinfo_common"
|
||||
usercommon "github.com/answerdev/answer/internal/service/user_common"
|
||||
"github.com/answerdev/answer/pkg/dir"
|
||||
"github.com/segmentfault/pacman/errors"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
@ -71,24 +71,29 @@ func NewDashboardService(
|
|||
}
|
||||
|
||||
func (ds *DashboardService) StatisticalByCache(ctx context.Context) (*schema.DashboardInfo, error) {
|
||||
ds.SetCache(ctx)
|
||||
dashboardInfo := &schema.DashboardInfo{}
|
||||
infoStr, err := ds.data.Cache.GetString(ctx, schema.DashBoardCachekey)
|
||||
if err != nil {
|
||||
return dashboardInfo, err
|
||||
info, statisticalErr := ds.Statistical(ctx)
|
||||
if statisticalErr != nil {
|
||||
return dashboardInfo, err
|
||||
}
|
||||
setCacheErr := ds.SetCache(ctx, info)
|
||||
if setCacheErr != nil {
|
||||
log.Error("ds.SetCache", setCacheErr)
|
||||
}
|
||||
return info, err
|
||||
}
|
||||
err = json.Unmarshal([]byte(infoStr), dashboardInfo)
|
||||
if err != nil {
|
||||
return dashboardInfo, err
|
||||
}
|
||||
startTime := time.Now().Unix() - schema.AppStartTime.Unix()
|
||||
dashboardInfo.AppStartTime = fmt.Sprintf("%d", startTime)
|
||||
return dashboardInfo, nil
|
||||
}
|
||||
|
||||
func (ds *DashboardService) SetCache(ctx context.Context) error {
|
||||
info, err := ds.Statistical(ctx)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
|
||||
}
|
||||
func (ds *DashboardService) SetCache(ctx context.Context, info *schema.DashboardInfo) error {
|
||||
infoStr, err := json.Marshal(info)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
|
||||
|
@ -167,12 +172,23 @@ func (ds *DashboardService) Statistical(ctx context.Context) (*schema.DashboardI
|
|||
if emailconfig.SMTPHost != "" {
|
||||
dashboardInfo.SMTP = true
|
||||
}
|
||||
dashboardInfo.HTTPS = true
|
||||
dirSize, err := ds.DirSize(ds.serviceConfig.UploadPath)
|
||||
siteGeneral, err := ds.siteInfoService.GetSiteGeneral(ctx)
|
||||
if err != nil {
|
||||
return dashboardInfo, err
|
||||
}
|
||||
size := ds.formatFileSize(dirSize)
|
||||
siteUrl, err := url.Parse(siteGeneral.SiteUrl)
|
||||
if err != nil {
|
||||
return dashboardInfo, err
|
||||
}
|
||||
if siteUrl.Scheme == "https" {
|
||||
dashboardInfo.HTTPS = true
|
||||
}
|
||||
|
||||
dirSize, err := dir.DirSize(ds.serviceConfig.UploadPath)
|
||||
if err != nil {
|
||||
return dashboardInfo, err
|
||||
}
|
||||
size := dir.FormatFileSize(dirSize)
|
||||
dashboardInfo.OccupyingStorageSpace = size
|
||||
startTime := time.Now().Unix() - schema.AppStartTime.Unix()
|
||||
dashboardInfo.AppStartTime = fmt.Sprintf("%d", startTime)
|
||||
|
@ -219,32 +235,3 @@ func (ds *DashboardService) GetEmailConfig() (ec *export.EmailConfig, err error)
|
|||
}
|
||||
return ec, nil
|
||||
}
|
||||
|
||||
func (ds *DashboardService) DirSize(path string) (int64, error) {
|
||||
var size int64
|
||||
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
|
||||
if !info.IsDir() {
|
||||
size += info.Size()
|
||||
}
|
||||
return err
|
||||
})
|
||||
return size, err
|
||||
}
|
||||
|
||||
func (ds *DashboardService) formatFileSize(fileSize int64) (size string) {
|
||||
if fileSize < 1024 {
|
||||
//return strconv.FormatInt(fileSize, 10) + "B"
|
||||
return fmt.Sprintf("%.2f B", float64(fileSize)/float64(1))
|
||||
} else if fileSize < (1024 * 1024) {
|
||||
return fmt.Sprintf("%.2f KB", float64(fileSize)/float64(1024))
|
||||
} else if fileSize < (1024 * 1024 * 1024) {
|
||||
return fmt.Sprintf("%.2f MB", float64(fileSize)/float64(1024*1024))
|
||||
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
|
||||
return fmt.Sprintf("%.2f GB", float64(fileSize)/float64(1024*1024*1024))
|
||||
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
|
||||
return fmt.Sprintf("%.2f TB", float64(fileSize)/float64(1024*1024*1024*1024))
|
||||
} else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
|
||||
return fmt.Sprintf("%.2f EB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package dir
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func CreateDirIfNotExist(path string) error {
|
||||
return os.MkdirAll(path, os.ModePerm)
|
||||
|
@ -15,3 +19,32 @@ func CheckFileExist(path string) bool {
|
|||
f, err := os.Stat(path)
|
||||
return err == nil && !f.IsDir()
|
||||
}
|
||||
|
||||
func DirSize(path string) (int64, error) {
|
||||
var size int64
|
||||
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
|
||||
if !info.IsDir() {
|
||||
size += info.Size()
|
||||
}
|
||||
return err
|
||||
})
|
||||
return size, err
|
||||
}
|
||||
|
||||
func FormatFileSize(fileSize int64) (size string) {
|
||||
if fileSize < 1024 {
|
||||
//return strconv.FormatInt(fileSize, 10) + "B"
|
||||
return fmt.Sprintf("%.2f B", float64(fileSize)/float64(1))
|
||||
} else if fileSize < (1024 * 1024) {
|
||||
return fmt.Sprintf("%.2f KB", float64(fileSize)/float64(1024))
|
||||
} else if fileSize < (1024 * 1024 * 1024) {
|
||||
return fmt.Sprintf("%.2f MB", float64(fileSize)/float64(1024*1024))
|
||||
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
|
||||
return fmt.Sprintf("%.2f GB", float64(fileSize)/float64(1024*1024*1024))
|
||||
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
|
||||
return fmt.Sprintf("%.2f TB", float64(fileSize)/float64(1024*1024*1024*1024))
|
||||
} else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
|
||||
return fmt.Sprintf("%.2f EB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue