A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
update dashboard cache
aichy126ffec3a0b9cca
diff --git a/docs/docs.go b/docs/docs.goindex 5c31c2fb..dc079d1e 100644--- a/docs/docs.go+++ b/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}}},diff --git a/docs/swagger.json b/docs/swagger.jsonindex f7545616..8f58a20a 100644--- a/docs/swagger.json+++ b/docs/swagger.json@@ -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}}},diff --git a/docs/swagger.yaml b/docs/swagger.yamlindex 8fab9053..9576a81f 100644--- a/docs/swagger.yaml+++ b/docs/swagger.yaml@@ -989,6 +989,9 @@ definitions:type: objectschema.SiteGeneralReq:properties:+ contact_email:+ maxLength: 512+ type: stringdescription:maxLength: 2000type: string@@ -998,13 +1001,21 @@ definitions:short_description:maxLength: 255type: string+ site_url:+ maxLength: 512+ type: stringrequired:+ - contact_email- description- name- short_description+ - site_urltype: objectschema.SiteGeneralResp:properties:+ contact_email:+ maxLength: 512+ type: stringdescription:maxLength: 2000type: string@@ -1014,10 +1025,15 @@ definitions:short_description:maxLength: 255type: string+ site_url:+ maxLength: 512+ type: stringrequired:+ - contact_email- description- name- short_description+ - site_urltype: objectschema.SiteInterfaceReq:properties:diff --git a/internal/schema/dashboard_schema.go b/internal/schema/dashboard_schema.goindex d08ee2b2..1ed09010 100644--- a/internal/schema/dashboard_schema.go+++ b/internal/schema/dashboard_schema.go@@ -6,7 +6,7 @@ var AppStartTime time.Timeconst (DashBoardCachekey = "answer@dashboard"- DashBoardCacheTime = 31 * time.Minute+ DashBoardCacheTime = 60 * time.Minute)type DashboardInfo struct {diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.goindex 446b986d..9a0a4839 100644--- a/internal/schema/siteinfo_schema.go+++ b/internal/schema/siteinfo_schema.go@@ -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 requestdiff --git a/internal/service/dashboard/dashboard_service.go b/internal/service/dashboard/dashboard_service.goindex 3cef6286..af462823 100644--- a/internal/service/dashboard/dashboard_service.go+++ b/internal/service/dashboard/dashboard_service.go@@ -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.DashboardIif 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+ }+ siteUrl, err := url.Parse(siteGeneral.SiteUrl)if err != nil {return dashboardInfo, err}- size := ds.formatFileSize(dirSize)+ 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 = sizestartTime := 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))- }--}diff --git a/pkg/dir/dir.go b/pkg/dir/dir.goindex 559f0d1c..808bef6e 100644--- a/pkg/dir/dir.go+++ b/pkg/dir/dir.go@@ -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))+ }++}