This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch feat/1.2.1/dashboard
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git

commit 92911e37be90c4e1585c02934fd529999910247b
Author: LinkinStars <[email protected]>
AuthorDate: Tue Nov 14 10:25:26 2023 +0800

    feat(dashboard): add more information to dashboard
---
 cmd/main.go                                     |  3 ++
 internal/base/constant/constant.go              |  5 +-
 internal/repo/user/user_repo.go                 |  7 +--
 internal/schema/dashboard_schema.go             |  6 ++-
 internal/service/dashboard/dashboard_service.go | 66 +++++++++++++++++++++++--
 5 files changed, 77 insertions(+), 10 deletions(-)

diff --git a/cmd/main.go b/cmd/main.go
index 306d841c..f57cc4b5 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -47,6 +47,8 @@ var (
        Revision = "-"
        // Time is the build time of the project
        Time = "-"
+       // GoVersion is the go version of the project
+       GoVersion = "1.19"
        // log level
        logLevel = os.Getenv("LOG_LEVEL")
        // log path
@@ -75,6 +77,7 @@ func runApp() {
        }
        constant.Version = Version
        constant.Revision = Revision
+       constant.GoVersion = GoVersion
        schema.AppStartTime = time.Now()
        fmt.Println("answer Version:", constant.Version, " Revision:", 
constant.Revision)
 
diff --git a/internal/base/constant/constant.go 
b/internal/base/constant/constant.go
index 56a45b50..f467f157 100644
--- a/internal/base/constant/constant.go
+++ b/internal/base/constant/constant.go
@@ -25,6 +25,7 @@ const (
 )
 
 var (
-       Version  string = ""
-       Revision string = ""
+       Version   = ""
+       Revision  = ""
+       GoVersion = ""
 )
diff --git a/internal/repo/user/user_repo.go b/internal/repo/user/user_repo.go
index 2b21e265..deb1427d 100644
--- a/internal/repo/user/user_repo.go
+++ b/internal/repo/user/user_repo.go
@@ -236,12 +236,13 @@ func (ur *userRepo) GetByEmail(ctx context.Context, email 
string) (userInfo *ent
 }
 
 func (ur *userRepo) GetUserCount(ctx context.Context) (count int64, err error) 
{
-       list := make([]*entity.User, 0)
-       count, err = ur.data.DB.Context(ctx).Where("mail_status =?", 
entity.EmailStatusAvailable).And("status =?", 
entity.UserStatusAvailable).FindAndCount(&list)
+       session := ur.data.DB.Context(ctx)
+       session.Where("status = ? OR status = ?", entity.UserStatusAvailable, 
entity.UserStatusSuspended)
+       count, err = session.Count(&entity.User{})
        if err != nil {
                return count, 
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
        }
-       return
+       return count, nil
 }
 
 func (ur *userRepo) SearchUserListByName(ctx context.Context, name string, 
limit int) (userList []*entity.User, err error) {
diff --git a/internal/schema/dashboard_schema.go 
b/internal/schema/dashboard_schema.go
index 1fa90f7e..2d780b8c 100644
--- a/internal/schema/dashboard_schema.go
+++ b/internal/schema/dashboard_schema.go
@@ -36,12 +36,16 @@ type DashboardInfo struct {
        UserCount             int64                `json:"user_count"`
        ReportCount           int64                `json:"report_count"`
        UploadingFiles        bool                 `json:"uploading_files"`
-       SMTP                  bool                 `json:"smtp"`
+       SMTP                  string               `json:"smtp"`
        HTTPS                 bool                 `json:"https"`
        TimeZone              string               `json:"time_zone"`
        OccupyingStorageSpace string               
`json:"occupying_storage_space"`
        AppStartTime          string               `json:"app_start_time"`
        VersionInfo           DashboardInfoVersion `json:"version_info"`
+       LoginRequired         bool                 `json:"login_required"`
+       GoVersion             string               `json:"go_version"`
+       DatabaseVersion       string               `json:"database_version"`
+       DatabaseSize          string               `json:"database_size"`
 }
 
 type DashboardInfoVersion struct {
diff --git a/internal/service/dashboard/dashboard_service.go 
b/internal/service/dashboard/dashboard_service.go
index 521776a7..5d3aa4ed 100644
--- a/internal/service/dashboard/dashboard_service.go
+++ b/internal/service/dashboard/dashboard_service.go
@@ -23,10 +23,12 @@ import (
        "context"
        "encoding/json"
        "fmt"
+       "github.com/apache/incubator-answer/pkg/converter"
        "io"
        "net/http"
        "net/url"
        "time"
+       "xorm.io/xorm/schemas"
 
        "github.com/apache/incubator-answer/internal/base/constant"
        "github.com/apache/incubator-answer/internal/base/data"
@@ -100,6 +102,8 @@ func (ds *dashboardService) Statistical(ctx 
context.Context) (*schema.DashboardI
                dashboardInfo.VoteCount = ds.voteCount(ctx)
                dashboardInfo.OccupyingStorageSpace = ds.calculateStorage()
                dashboardInfo.VersionInfo.RemoteVersion = ds.remoteVersion(ctx)
+               dashboardInfo.DatabaseVersion = ds.getDatabaseInfo()
+               dashboardInfo.DatabaseSize = ds.GetDatabaseSize()
        }
 
        dashboardInfo.SMTP = ds.smtpStatus(ctx)
@@ -109,6 +113,10 @@ func (ds *dashboardService) Statistical(ctx 
context.Context) (*schema.DashboardI
        dashboardInfo.AppStartTime = fmt.Sprintf("%d", 
time.Now().Unix()-schema.AppStartTime.Unix())
        dashboardInfo.VersionInfo.Version = constant.Version
        dashboardInfo.VersionInfo.Revision = constant.Revision
+       dashboardInfo.GoVersion = constant.GoVersion
+       if siteLogin, err := ds.siteInfoService.GetSiteLogin(ctx); err == nil {
+               dashboardInfo.LoginRequired = siteLogin.LoginRequired
+       }
 
        ds.setCache(ctx, dashboardInfo)
        return dashboardInfo, nil
@@ -226,19 +234,22 @@ func (ds *dashboardService) remoteVersion(ctx 
context.Context) string {
        return remoteVersion.Release.Version
 }
 
-func (ds *dashboardService) smtpStatus(ctx context.Context) (enabled bool) {
+func (ds *dashboardService) smtpStatus(ctx context.Context) (smtpStatus 
string) {
        emailConf, err := ds.configService.GetStringValue(ctx, "email.config")
        if err != nil {
                log.Errorf("get email config failed: %s", err)
-               return false
+               return "disabled"
        }
        ec := &export.EmailConfig{}
        err = json.Unmarshal([]byte(emailConf), ec)
        if err != nil {
                log.Errorf("parsing email config failed: %s", err)
-               return false
+               return "disabled"
        }
-       return ec.SMTPHost != ""
+       if ec.SMTPHost != "" {
+               smtpStatus = "enabled"
+       }
+       return smtpStatus
 }
 
 func (ds *dashboardService) httpsStatus(ctx context.Context) (enabled bool) {
@@ -271,3 +282,50 @@ func (ds *dashboardService) calculateStorage() string {
        }
        return dir.FormatFileSize(dirSize)
 }
+
+func (ds *dashboardService) getDatabaseInfo() (versionDesc string) {
+       dbVersion, err := ds.data.DB.DBVersion()
+       if err != nil {
+               log.Errorf("get db version failed: %s", err)
+       } else {
+               versionDesc = fmt.Sprintf("%s %s", 
ds.data.DB.Dialect().URI().DBType, dbVersion.Number)
+       }
+       return versionDesc
+}
+
+func (ds *dashboardService) GetDatabaseSize() (dbSize string) {
+       switch ds.data.DB.Dialect().URI().DBType {
+       case schemas.MYSQL:
+               sql := fmt.Sprintf("SELECT SUM(DATA_LENGTH) as db_size FROM 
information_schema.TABLES WHERE table_schema = '%s'",
+                       ds.data.DB.Dialect().URI().DBName)
+               res, err := ds.data.DB.QueryInterface(sql)
+               if err != nil {
+                       log.Warnf("get db size failed: %s", err)
+               } else {
+                       if res != nil && len(res) > 0 && res[0]["db_size"] != 
nil {
+                               dbSizeStr, _ := res[0]["db_size"].(string)
+                               dbSize = 
dir.FormatFileSize(converter.StringToInt64(dbSizeStr))
+                       }
+               }
+       case schemas.POSTGRES:
+               sql := fmt.Sprintf("SELECT pg_database_size('%s') AS db_size",
+                       ds.data.DB.Dialect().URI().DBName)
+               res, err := ds.data.DB.QueryInterface(sql)
+               if err != nil {
+                       log.Warnf("get db size failed: %s", err)
+               } else {
+                       if res != nil && len(res) > 0 && res[0]["db_size"] != 
nil {
+                               dbSizeStr, _ := res[0]["db_size"].(int32)
+                               dbSize = dir.FormatFileSize(int64(dbSizeStr))
+                       }
+               }
+       case schemas.SQLITE:
+               dirSize, err := dir.DirSize(ds.data.DB.DataSourceName())
+               if err != nil {
+                       log.Errorf("get upload dir size failed: %s", err)
+                       return ""
+               }
+               dbSize = dir.FormatFileSize(dirSize)
+       }
+       return dbSize
+}

Reply via email to