update dashboard analytic
This commit is contained in:
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"eslogad-be/internal/appcontext"
|
||||
@@ -37,7 +38,7 @@ func (s *AnalyticsServiceImpl) GetDashboard(ctx context.Context, req *contract.A
|
||||
// Default to last 30 days
|
||||
startDate = time.Now().AddDate(0, 0, -30)
|
||||
}
|
||||
|
||||
|
||||
if req.EndDate != "" {
|
||||
if date, err := time.Parse("2006-01-02", req.EndDate); err == nil {
|
||||
endDate = date.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
|
||||
@@ -48,7 +49,7 @@ func (s *AnalyticsServiceImpl) GetDashboard(ctx context.Context, req *contract.A
|
||||
|
||||
// Apply user context filters if not admin
|
||||
var userID *uuid.UUID
|
||||
|
||||
|
||||
appCtx := appcontext.FromGinContext(ctx)
|
||||
if appCtx != nil && appCtx.UserRole != "admin" && appCtx.UserRole != "superadmin" {
|
||||
userID = &appCtx.UserID
|
||||
@@ -61,8 +62,9 @@ func (s *AnalyticsServiceImpl) GetDashboard(ctx context.Context, req *contract.A
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("[DEBUG] summaryData: %v\n", summaryData)
|
||||
response.Summary = s.mapSummaryStats(summaryData)
|
||||
|
||||
|
||||
// Calculate growth metrics
|
||||
response.Summary.WeekOverWeekGrowth = s.calculateWeekOverWeekGrowth(ctx)
|
||||
response.Summary.MonthOverMonthGrowth = s.calculateMonthOverMonthGrowth(ctx)
|
||||
@@ -99,7 +101,7 @@ func (s *AnalyticsServiceImpl) GetDashboard(ctx context.Context, req *contract.A
|
||||
response.InstitutionStats = s.mapInstitutionStats(instData)
|
||||
|
||||
// Get daily activity (last 7 days)
|
||||
dailyData, err := s.analyticsRepo.GetDailyActivity(ctx, 7)
|
||||
dailyData, err := s.analyticsRepo.GetDailyActivityByUserID(ctx, userID, 7)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -143,17 +145,17 @@ func (s *AnalyticsServiceImpl) calculateWeekOverWeekGrowth(ctx context.Context)
|
||||
// Get this week's data
|
||||
thisWeekStart := time.Now().AddDate(0, 0, -int(time.Now().Weekday()))
|
||||
thisWeekEnd := time.Now()
|
||||
|
||||
|
||||
// Get last week's data
|
||||
lastWeekStart := thisWeekStart.AddDate(0, 0, -7)
|
||||
lastWeekEnd := thisWeekStart.AddDate(0, 0, -1)
|
||||
|
||||
|
||||
thisWeekData, _ := s.analyticsRepo.GetLetterSummaryStats(ctx, thisWeekStart, thisWeekEnd, nil, nil)
|
||||
lastWeekData, _ := s.analyticsRepo.GetLetterSummaryStats(ctx, lastWeekStart, lastWeekEnd, nil, nil)
|
||||
|
||||
|
||||
thisWeekTotal := getInt64Value(thisWeekData["total_incoming"]) + getInt64Value(thisWeekData["total_outgoing"])
|
||||
lastWeekTotal := getInt64Value(lastWeekData["total_incoming"]) + getInt64Value(lastWeekData["total_outgoing"])
|
||||
|
||||
|
||||
if lastWeekTotal > 0 {
|
||||
return float64((thisWeekTotal - lastWeekTotal) * 100 / lastWeekTotal)
|
||||
}
|
||||
@@ -166,17 +168,17 @@ func (s *AnalyticsServiceImpl) calculateMonthOverMonthGrowth(ctx context.Context
|
||||
now := time.Now()
|
||||
thisMonthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||
thisMonthEnd := now
|
||||
|
||||
|
||||
// Get last month's data
|
||||
lastMonthStart := thisMonthStart.AddDate(0, -1, 0)
|
||||
lastMonthEnd := thisMonthStart.AddDate(0, 0, -1)
|
||||
|
||||
|
||||
thisMonthData, _ := s.analyticsRepo.GetLetterSummaryStats(ctx, thisMonthStart, thisMonthEnd, nil, nil)
|
||||
lastMonthData, _ := s.analyticsRepo.GetLetterSummaryStats(ctx, lastMonthStart, lastMonthEnd, nil, nil)
|
||||
|
||||
|
||||
thisMonthTotal := getInt64Value(thisMonthData["total_incoming"]) + getInt64Value(thisMonthData["total_outgoing"])
|
||||
lastMonthTotal := getInt64Value(lastMonthData["total_incoming"]) + getInt64Value(lastMonthData["total_outgoing"])
|
||||
|
||||
|
||||
if lastMonthTotal > 0 {
|
||||
return float64((thisMonthTotal - lastMonthTotal) * 100 / lastMonthTotal)
|
||||
}
|
||||
@@ -190,7 +192,7 @@ func (s *AnalyticsServiceImpl) getSimpleDepartmentStats(ctx context.Context, sta
|
||||
if err != nil {
|
||||
return []contract.SimpleDepartmentStats{}
|
||||
}
|
||||
|
||||
|
||||
result := make([]contract.SimpleDepartmentStats, 0, len(deptData))
|
||||
for _, item := range deptData {
|
||||
deptIDStr := getStringValue(item["department_id"])
|
||||
@@ -198,17 +200,17 @@ func (s *AnalyticsServiceImpl) getSimpleDepartmentStats(ctx context.Context, sta
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
// Calculate total letter count (incoming + outgoing)
|
||||
letterCount := getInt64Value(item["incoming_count"]) + getInt64Value(item["outgoing_count"])
|
||||
|
||||
|
||||
result = append(result, contract.SimpleDepartmentStats{
|
||||
DepartmentID: deptID,
|
||||
Department: getStringValue(item["department_name"]),
|
||||
LetterCount: letterCount,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -303,11 +305,11 @@ func (s *AnalyticsServiceImpl) mapInstitutionStats(data []map[string]interface{}
|
||||
OutgoingCount: getInt64Value(item["outgoing_count"]),
|
||||
TotalCount: getInt64Value(item["total_count"]),
|
||||
}
|
||||
|
||||
|
||||
if lastActivity, ok := item["last_activity"].(time.Time); ok {
|
||||
stat.LastActivity = lastActivity
|
||||
}
|
||||
|
||||
|
||||
result = append(result, stat)
|
||||
}
|
||||
}
|
||||
@@ -410,4 +412,4 @@ func getFloat64Value(v interface{}) float64 {
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user