Skip to content

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.

add dashboard service

aichy126872689b93a87
diff --git a/cmd/answer/wire_gen.go b/cmd/answer/wire_gen.go
index a965ed8b..1a1720ff 100644
--- a/cmd/answer/wire_gen.go
+++ b/cmd/answer/wire_gen.go
@@ -44,6 +44,7 @@ import (
auth2 "github.com/answerdev/answer/internal/service/auth"
"github.com/answerdev/answer/internal/service/collection_common"
comment2 "github.com/answerdev/answer/internal/service/comment"
+ "github.com/answerdev/answer/internal/service/dashboard"
export2 "github.com/answerdev/answer/internal/service/export"
"github.com/answerdev/answer/internal/service/follow"
meta2 "github.com/answerdev/answer/internal/service/meta"
@@ -148,7 +149,8 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
questionService := service.NewQuestionService(questionRepo, tagCommonService, questionCommon, userCommon, revisionService, metaService, collectionCommon, answerActivityService)
questionController := controller.NewQuestionController(questionService, rankService)
answerService := service.NewAnswerService(answerRepo, questionRepo, questionCommon, userCommon, collectionCommon, userRepo, revisionService, answerActivityService, answerCommon, voteRepo)
- answerController := controller.NewAnswerController(answerService, rankService)
+ dashboardService := dashboard.NewDashboardService(questionRepo, answerRepo, commentCommonRepo, voteRepo, userRepo, reportRepo, configRepo)
+ answerController := controller.NewAnswerController(answerService, rankService, dashboardService)
searchRepo := search_common.NewSearchRepo(dataData, uniqueIDRepo, userCommon)
searchService := service.NewSearchService(searchRepo, tagRepo, userCommon, followRepo)
searchController := controller.NewSearchController(searchService)
diff --git a/internal/controller/answer_controller.go b/internal/controller/answer_controller.go
index 43ea51a5..842b107f 100644
--- a/internal/controller/answer_controller.go
+++ b/internal/controller/answer_controller.go
@@ -9,20 +9,30 @@ import (
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service"
+ "github.com/answerdev/answer/internal/service/dashboard"
"github.com/answerdev/answer/internal/service/rank"
+ "github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/errors"
)
// AnswerController answer controller
type AnswerController struct {
- answerService *service.AnswerService
- rankService *rank.RankService
+ answerService *service.AnswerService
+ rankService *rank.RankService
+ dashboardService *dashboard.DashboardService
}
// NewAnswerController new controller
-func NewAnswerController(answerService *service.AnswerService, rankService *rank.RankService) *AnswerController {
- return &AnswerController{answerService: answerService, rankService: rankService}
+func NewAnswerController(answerService *service.AnswerService,
+ rankService *rank.RankService,
+ dashboardService *dashboard.DashboardService,
+) *AnswerController {
+ return &AnswerController{
+ answerService: answerService,
+ rankService: rankService,
+ dashboardService: dashboardService,
+ }
}
// RemoveAnswer delete answer
@@ -236,3 +246,12 @@ func (ac *AnswerController) AdminSetAnswerStatus(ctx *gin.Context) {
err := ac.answerService.AdminSetAnswerStatus(ctx, req.AnswerID, req.StatusStr)
handler.HandleResponse(ctx, err, gin.H{})
}
+
+// dashboardService
+func (ac *AnswerController) Dashboard(ctx *gin.Context) {
+ err := ac.dashboardService.Statistical(ctx)
+ spew.Dump(err)
+ handler.HandleResponse(ctx, err, gin.H{
+ "ping": "pong",
+ })
+}
diff --git a/internal/repo/activity_common/vote.go b/internal/repo/activity_common/vote.go
index abbea28f..16f73fc4 100644
--- a/internal/repo/activity_common/vote.go
+++ b/internal/repo/activity_common/vote.go
@@ -4,8 +4,10 @@ import (
"context"
"github.com/answerdev/answer/internal/base/data"
+ "github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/activity_common"
+ "github.com/segmentfault/pacman/errors"
)
// VoteRepo activity repository
@@ -39,3 +41,12 @@ func (vr *VoteRepo) GetVoteStatus(ctx context.Context, objectID, userID string)
}
return ""
}
+
+func (vr *VoteRepo) GetVoteCount(ctx context.Context, activityTypes []int) (count int64, err error) {
+ list := make([]*entity.Activity, 0)
+ count, err = vr.data.DB.Where("cancelled =0").In("activity_type", activityTypes).FindAndCount(&list)
+ if err != nil {
+ return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return
+}
diff --git a/internal/repo/answer/answer_repo.go b/internal/repo/answer/answer_repo.go
index 2020fc2f..ac6c974d 100644
--- a/internal/repo/answer/answer_repo.go
+++ b/internal/repo/answer/answer_repo.go
@@ -5,6 +5,7 @@ import (
"strings"
"time"
"unicode"
+
"xorm.io/builder"
"github.com/answerdev/answer/internal/base/constant"
@@ -102,6 +103,16 @@ func (ar *answerRepo) GetAnswer(ctx context.Context, id string) (
return
}
+// GetQuestionCount
+func (ar *answerRepo) GetAnswerCount(ctx context.Context) (count int64, err error) {
+ list := make([]*entity.Answer, 0)
+ count, err = ar.data.DB.Where("status = ?", entity.AnswerStatusAvailable).FindAndCount(&list)
+ if err != nil {
+ return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return
+}
+
// GetAnswerList get answer list all
func (ar *answerRepo) GetAnswerList(ctx context.Context, answer *entity.Answer) (answerList []*entity.Answer, err error) {
answerList = make([]*entity.Answer, 0)
diff --git a/internal/repo/comment/comment_repo.go b/internal/repo/comment/comment_repo.go
index a1a7ab90..992e6a2f 100644
--- a/internal/repo/comment/comment_repo.go
+++ b/internal/repo/comment/comment_repo.go
@@ -79,6 +79,15 @@ func (cr *commentRepo) GetComment(ctx context.Context, commentID string) (
return
}
+func (cr *commentRepo) GetCommentCount(ctx context.Context) (count int64, err error) {
+ list := make([]*entity.Comment, 0)
+ count, err = cr.data.DB.Where("status = ?", entity.CommentStatusAvailable).FindAndCount(&list)
+ if err != nil {
+ return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return
+}
+
// GetCommentPage get comment page
func (cr *commentRepo) GetCommentPage(ctx context.Context, commentQuery *comment.CommentQuery) (
commentList []*entity.Comment, total int64, err error,
diff --git a/internal/repo/question/question_repo.go b/internal/repo/question/question_repo.go
index 2e26006f..4d90a09e 100644
--- a/internal/repo/question/question_repo.go
+++ b/internal/repo/question/question_repo.go
@@ -5,6 +5,7 @@ import (
"strings"
"time"
"unicode"
+
"xorm.io/builder"
"github.com/answerdev/answer/internal/base/constant"
@@ -162,6 +163,16 @@ func (qr *questionRepo) GetQuestionList(ctx context.Context, question *entity.Qu
return
}
+func (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, err error) {
+ questionList := make([]*entity.Question, 0)
+
+ count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusclosed}).FindAndCount(&questionList)
+ if err != nil {
+ return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return
+}
+
// GetQuestionPage get question page
func (qr *questionRepo) GetQuestionPage(ctx context.Context, page, pageSize int, question *entity.Question) (questionList []*entity.Question, total int64, err error) {
questionList = make([]*entity.Question, 0)
diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go
index 56a972a8..68192ecb 100644
--- a/internal/router/answer_api_router.go
+++ b/internal/router/answer_api_router.go
@@ -86,6 +86,9 @@ func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
r.GET("/personal/comment/page", a.commentController.GetCommentPersonalWithPage)
r.GET("/comment", a.commentController.GetComment)
+ //test
+ r.GET("/test", a.answerController.Dashboard)
+
// user
r.GET("/user/info", a.userController.GetUserInfoByUserID)
r.GET("/user/status", a.userController.GetUserStatus)
diff --git a/internal/service/activity_common/vote.go b/internal/service/activity_common/vote.go
index fb97885b..16de6967 100644
--- a/internal/service/activity_common/vote.go
+++ b/internal/service/activity_common/vote.go
@@ -7,4 +7,5 @@ import (
// VoteRepo activity repository
type VoteRepo interface {
GetVoteStatus(ctx context.Context, objectId, userId string) (status string)
+ GetVoteCount(ctx context.Context, activityTypes []int) (count int64, err error)
}
diff --git a/internal/service/answer_common/answer.go b/internal/service/answer_common/answer.go
index c84a1d15..ef611351 100644
--- a/internal/service/answer_common/answer.go
+++ b/internal/service/answer_common/answer.go
@@ -20,6 +20,7 @@ type AnswerRepo interface {
SearchList(ctx context.Context, search *entity.AnswerSearch) ([]*entity.Answer, int64, error)
CmsSearchList(ctx context.Context, search *entity.CmsAnswerSearch) ([]*entity.Answer, int64, error)
UpdateAnswerStatus(ctx context.Context, answer *entity.Answer) (err error)
+ GetAnswerCount(ctx context.Context) (count int64, err error)
}
// AnswerCommon user service
diff --git a/internal/service/comment_common/comment_service.go b/internal/service/comment_common/comment_service.go
index dc1f9fc9..19c2b90a 100644
--- a/internal/service/comment_common/comment_service.go
+++ b/internal/service/comment_common/comment_service.go
@@ -12,6 +12,7 @@ import (
// CommentCommonRepo comment repository
type CommentCommonRepo interface {
GetComment(ctx context.Context, commentID string) (comment *entity.Comment, exist bool, err error)
+ GetCommentCount(ctx context.Context) (count int64, err error)
}
// CommentCommonService user service
diff --git a/internal/service/dashboard/dashboard_service.go b/internal/service/dashboard/dashboard_service.go
new file mode 100644
index 00000000..1d0f5400
--- /dev/null
+++ b/internal/service/dashboard/dashboard_service.go
@@ -0,0 +1,85 @@
+package dashboard
+
+import (
+ "context"
+
+ "github.com/answerdev/answer/internal/service/activity_common"
+ answercommon "github.com/answerdev/answer/internal/service/answer_common"
+ "github.com/answerdev/answer/internal/service/comment_common"
+ "github.com/answerdev/answer/internal/service/config"
+ questioncommon "github.com/answerdev/answer/internal/service/question_common"
+ "github.com/answerdev/answer/internal/service/report_common"
+ usercommon "github.com/answerdev/answer/internal/service/user_common"
+ "github.com/davecgh/go-spew/spew"
+)
+
+type DashboardService struct {
+ questionRepo questioncommon.QuestionRepo
+ answerRepo answercommon.AnswerRepo
+ commentRepo comment_common.CommentCommonRepo
+ voteRepo activity_common.VoteRepo
+ userRepo usercommon.UserRepo
+ reportRepo report_common.ReportRepo
+ configRepo config.ConfigRepo
+}
+
+func NewDashboardService(
+ questionRepo questioncommon.QuestionRepo,
+ answerRepo answercommon.AnswerRepo,
+ commentRepo comment_common.CommentCommonRepo,
+ voteRepo activity_common.VoteRepo,
+ userRepo usercommon.UserRepo,
+ reportRepo report_common.ReportRepo,
+ configRepo config.ConfigRepo,
+
+) *DashboardService {
+ return &DashboardService{
+ questionRepo: questionRepo,
+ answerRepo: answerRepo,
+ commentRepo: commentRepo,
+ voteRepo: voteRepo,
+ userRepo: userRepo,
+ reportRepo: reportRepo,
+ configRepo: configRepo,
+ }
+}
+
+// Statistical
+func (ds *DashboardService) Statistical(ctx context.Context) error {
+ questionCount, err := ds.questionRepo.GetQuestionCount(ctx)
+ if err != nil {
+ return err
+ }
+ answerCount, err := ds.answerRepo.GetAnswerCount(ctx)
+ if err != nil {
+ return err
+ }
+ commentCount, err := ds.commentRepo.GetCommentCount(ctx)
+ if err != nil {
+ return err
+ }
+
+ typeKeys := []string{
+ "question.vote_up",
+ "question.vote_down",
+ "answer.vote_up",
+ "answer.vote_down",
+ }
+ var activityTypes []int
+
+ for _, typeKey := range typeKeys {
+ var t int
+ t, err = ds.configRepo.GetConfigType(typeKey)
+ if err != nil {
+ continue
+ }
+ activityTypes = append(activityTypes, t)
+ }
+
+ voteCount, err := ds.voteRepo.GetVoteCount(ctx, activityTypes)
+ if err != nil {
+ return err
+ }
+ spew.Dump(questionCount, answerCount, commentCount, activityTypes, voteCount)
+ return nil
+}
diff --git a/internal/service/dashboard/dashboard_test.go b/internal/service/dashboard/dashboard_test.go
new file mode 100644
index 00000000..cfdd5f81
--- /dev/null
+++ b/internal/service/dashboard/dashboard_test.go
@@ -0,0 +1 @@
+package dashboard
diff --git a/internal/service/provider.go b/internal/service/provider.go
index 3901766e..4562572a 100644
--- a/internal/service/provider.go
+++ b/internal/service/provider.go
@@ -8,6 +8,7 @@ import (
collectioncommon "github.com/answerdev/answer/internal/service/collection_common"
"github.com/answerdev/answer/internal/service/comment"
"github.com/answerdev/answer/internal/service/comment_common"
+ "github.com/answerdev/answer/internal/service/dashboard"
"github.com/answerdev/answer/internal/service/export"
"github.com/answerdev/answer/internal/service/follow"
"github.com/answerdev/answer/internal/service/meta"
@@ -65,4 +66,5 @@ var ProviderSetService = wire.NewSet(
notficationcommon.NewNotificationCommon,
notification.NewNotificationService,
activity.NewAnswerActivityService,
+ dashboard.NewDashboardService,
)
diff --git a/internal/service/question_common/question.go b/internal/service/question_common/question.go
index 3cb944ff..e8b97e18 100644
--- a/internal/service/question_common/question.go
+++ b/internal/service/question_common/question.go
@@ -38,6 +38,7 @@ type QuestionRepo interface {
UpdateLastAnswer(ctx context.Context, question *entity.Question) (err error)
FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error)
CmsSearchList(ctx context.Context, search *schema.CmsQuestionSearch) ([]*entity.Question, int64, error)
+ GetQuestionCount(ctx context.Context) (count int64, err error)
}
// QuestionCommon user service