Skip to content

feat: Add key metrics to the dashboard #1176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions i18n/en_US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,8 @@ ui:
welcome: Welcome to Admin!
site_statistics: Site statistics
questions: "Questions:"
resolved: "Resolved:"
unanswered: "Unanswered:"
answers: "Answers:"
comments: "Comments:"
votes: "Votes:"
Expand Down
2 changes: 2 additions & 0 deletions i18n/zh_CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,8 @@ ui:
welcome: 欢迎来到管理后台!
site_statistics: 站点统计
questions: "问题:"
resolved: "已解决:"
unanswered: "未回复:"
answers: "回答:"
comments: "评论:"
votes: "投票:"
Expand Down
23 changes: 23 additions & 0 deletions internal/repo/question/question_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,29 @@ func (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, err
return count, nil
}

func (qr *questionRepo) GetUnansweredQuestionCount(ctx context.Context) (count int64, err error) {
session := qr.data.DB.Context(ctx)
session.Where(builder.Lt{"status": entity.QuestionStatusDeleted}).
And(builder.Eq{"answer_count": 0})
count, err = session.Count(&entity.Question{Show: entity.QuestionShow})
if err != nil {
return 0, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return count, nil
}

func (qr *questionRepo) GetResolvedQuestionCount(ctx context.Context) (count int64, err error) {
session := qr.data.DB.Context(ctx)
session.Where(builder.Lt{"status": entity.QuestionStatusDeleted}).
And(builder.Neq{"answer_count": 0}).
And(builder.Neq{"accepted_answer_id": 0})
count, err = session.Count(&entity.Question{Show: entity.QuestionShow})
if err != nil {
return 0, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return count, nil
}

func (qr *questionRepo) GetUserQuestionCount(ctx context.Context, userID string, show int) (count int64, err error) {
session := qr.data.DB.Context(ctx)
session.Where(builder.Lt{"status": entity.QuestionStatusDeleted})
Expand Down
4 changes: 4 additions & 0 deletions internal/schema/dashboard_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const (

type DashboardInfo struct {
QuestionCount int64 `json:"question_count"`
ResolvedCount int64 `json:"resolved_count"`
ResolvedRate string `json:"resolved_rate"`
UnansweredCount int64 `json:"unanswered_count"`
UnansweredRate string `json:"unanswered_rate"`
AnswerCount int64 `json:"answer_count"`
CommentCount int64 `json:"comment_count"`
VoteCount int64 `json:"vote_count"`
Expand Down
29 changes: 28 additions & 1 deletion internal/service/dashboard/dashboard_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func (ds *dashboardService) Statistical(ctx context.Context) (*schema.DashboardI
dashboardInfo := ds.getFromCache(ctx)
if dashboardInfo == nil {
dashboardInfo = &schema.DashboardInfo{}
dashboardInfo.QuestionCount = ds.questionCount(ctx)
dashboardInfo.AnswerCount = ds.answerCount(ctx)
dashboardInfo.CommentCount = ds.commentCount(ctx)
dashboardInfo.UserCount = ds.userCount(ctx)
Expand All @@ -121,6 +120,18 @@ func (ds *dashboardService) Statistical(ctx context.Context) (*schema.DashboardI
dashboardInfo.DatabaseSize = ds.GetDatabaseSize()
}

dashboardInfo.QuestionCount = ds.questionCount(ctx)
dashboardInfo.UnansweredCount = ds.unansweredQuestionCount(ctx)
dashboardInfo.ResolvedCount = ds.resolvedQuestionCount(ctx)

if dashboardInfo.QuestionCount == 0 {
dashboardInfo.ResolvedRate = "0.00"
dashboardInfo.UnansweredRate = "0.00"
} else {
dashboardInfo.ResolvedRate = fmt.Sprintf("%.2f", float64(dashboardInfo.ResolvedCount)/float64(dashboardInfo.QuestionCount)*100)
dashboardInfo.UnansweredRate = fmt.Sprintf("%.2f", float64(dashboardInfo.UnansweredCount)/float64(dashboardInfo.QuestionCount)*100)
}

dashboardInfo.ReportCount = ds.reportCount(ctx)
dashboardInfo.SMTP = ds.smtpStatus(ctx)
dashboardInfo.HTTPS = ds.httpsStatus(ctx)
Expand Down Expand Up @@ -170,6 +181,22 @@ func (ds *dashboardService) questionCount(ctx context.Context) int64 {
return questionCount
}

func (ds *dashboardService) unansweredQuestionCount(ctx context.Context) int64 {
unansweredQuestionCount, err := ds.questionRepo.GetUnansweredQuestionCount(ctx)
if err != nil {
log.Errorf("get unanswered question count failed: %s", err)
}
return unansweredQuestionCount
}

func (ds *dashboardService) resolvedQuestionCount(ctx context.Context) int64 {
resolvedQuestionCount, err := ds.questionRepo.GetResolvedQuestionCount(ctx)
if err != nil {
log.Errorf("get resolved question count failed: %s", err)
}
return resolvedQuestionCount
}

func (ds *dashboardService) answerCount(ctx context.Context) int64 {
answerCount, err := ds.answerRepo.GetAnswerCount(ctx)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/service/question_common/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type QuestionRepo interface {
FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error)
AdminQuestionPage(ctx context.Context, search *schema.AdminQuestionPageReq) ([]*entity.Question, int64, error)
GetQuestionCount(ctx context.Context) (count int64, err error)
GetUnansweredQuestionCount(ctx context.Context) (count int64, err error)
GetResolvedQuestionCount(ctx context.Context) (count int64, err error)
GetUserQuestionCount(ctx context.Context, userID string, show int) (count int64, err error)
SitemapQuestions(ctx context.Context, page, pageSize int) (questionIDList []*schema.SiteMapQuestionInfo, err error)
RemoveAllUserQuestion(ctx context.Context, userID string) (err error)
Expand Down
4 changes: 4 additions & 0 deletions ui/src/common/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ export interface SearchRes extends ListResult<SearchResItem> {
export interface AdminDashboard {
info: {
question_count: number;
resolved_count: number;
resolved_rate: string;
unanswered_count: number;
unanswered_rate: string;
answer_count: number;
comment_count: number;
vote_count: number;
Expand Down
22 changes: 22 additions & 0 deletions ui/src/pages/Admin/Dashboard/components/Statistics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ const Statistics: FC<IProps> = ({ data }) => {
<span className="text-secondary me-1">{t('questions')}</span>
<strong>{data.question_count}</strong>
</Col>
<Col xs={6} className="mb-1">
<span className="text-secondary me-1">{t('resolved')}</span>
<strong>{data.resolved_count}</strong>
{data.resolved_count > 0 ? (
<span className="text-secondary m-1">
({data.resolved_rate}%)
</span>
) : (
''
)}
</Col>
<Col xs={6} className="mb-1">
<span className="text-secondary me-1">{t('unanswered')}</span>
<strong>{data.unanswered_count}</strong>
{data.unanswered_count > 0 ? (
<span className="text-secondary m-1">
({data.unanswered_rate}%)
</span>
) : (
''
)}
</Col>
<Col xs={6} className="mb-1">
<span className="text-secondary me-1">{t('answers')}</span>
<strong>{data.answer_count}</strong>
Expand Down