|
| 1 | +package dashboard |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/DoPlan-dev/CLI/pkg/models" |
| 8 | +) |
| 9 | + |
| 10 | +// ActivityData represents activity data (to avoid import cycle) |
| 11 | +type ActivityData struct { |
| 12 | + Last24Hours ActivityPeriodData |
| 13 | + Last7Days ActivityPeriodData |
| 14 | + RecentActivity []ActivityItemData |
| 15 | +} |
| 16 | + |
| 17 | +// ActivityPeriodData represents activity for a time period |
| 18 | +type ActivityPeriodData struct { |
| 19 | + Commits int |
| 20 | + TasksCompleted int |
| 21 | + FilesChanged int |
| 22 | +} |
| 23 | + |
| 24 | +// ActivityItemData represents a single activity item |
| 25 | +type ActivityItemData struct { |
| 26 | + Type string |
| 27 | + Message string |
| 28 | + Timestamp string |
| 29 | +} |
| 30 | + |
| 31 | +// ActivityGenerator generates activity feed from various sources |
| 32 | +type ActivityGenerator struct { |
| 33 | + state *models.State |
| 34 | + githubData *models.GitHubData |
| 35 | + progressData map[string]*ProgressData |
| 36 | +} |
| 37 | + |
| 38 | +// NewActivityGenerator creates a new activity generator |
| 39 | +func NewActivityGenerator(state *models.State, githubData *models.GitHubData, progressData map[string]*ProgressData) *ActivityGenerator { |
| 40 | + return &ActivityGenerator{ |
| 41 | + state: state, |
| 42 | + githubData: githubData, |
| 43 | + progressData: progressData, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// GenerateActivityFeed generates a comprehensive activity feed |
| 48 | +func (a *ActivityGenerator) GenerateActivityFeed() ActivityData { |
| 49 | + now := time.Now() |
| 50 | + last24Hours := now.Add(-24 * time.Hour) |
| 51 | + last7Days := now.Add(-7 * 24 * time.Hour) |
| 52 | + |
| 53 | + activity := ActivityData{ |
| 54 | + Last24Hours: ActivityPeriodData{}, |
| 55 | + Last7Days: ActivityPeriodData{}, |
| 56 | + RecentActivity: []ActivityItemData{}, |
| 57 | + } |
| 58 | + |
| 59 | + // Collect activities from different sources |
| 60 | + var allActivities []ActivityItemData |
| 61 | + |
| 62 | + // Add commits |
| 63 | + if a.githubData != nil { |
| 64 | + commits24h := 0 |
| 65 | + commits7d := 0 |
| 66 | + for _, commit := range a.githubData.Commits { |
| 67 | + commitTime, err := parseTime(commit.Date) |
| 68 | + if err != nil { |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + if commitTime.After(last24Hours) { |
| 73 | + commits24h++ |
| 74 | + } |
| 75 | + if commitTime.After(last7Days) { |
| 76 | + commits7d++ |
| 77 | + } |
| 78 | + |
| 79 | + // Add to recent activity (last 10) |
| 80 | + if len(allActivities) < 10 { |
| 81 | + allActivities = append(allActivities, ActivityItemData{ |
| 82 | + Type: "commit", |
| 83 | + Message: fmt.Sprintf("📝 commit: %s", commit.Message), |
| 84 | + Timestamp: commit.Date, |
| 85 | + }) |
| 86 | + } |
| 87 | + } |
| 88 | + activity.Last24Hours.Commits = commits24h |
| 89 | + activity.Last7Days.Commits = commits7d |
| 90 | + } |
| 91 | + |
| 92 | + // Add task completions from progress data |
| 93 | + tasks24h := 0 |
| 94 | + tasks7d := 0 |
| 95 | + for _, progress := range a.progressData { |
| 96 | + if progress.LastUpdated.IsZero() { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + if progress.LastUpdated.After(last24Hours) { |
| 101 | + tasks24h++ |
| 102 | + } |
| 103 | + if progress.LastUpdated.After(last7Days) { |
| 104 | + tasks7d++ |
| 105 | + } |
| 106 | + |
| 107 | + // Add feature status changes |
| 108 | + if progress.Status != "" && progress.LastUpdated.After(last7Days) { |
| 109 | + statusIcon := "✨" |
| 110 | + if progress.Status == "complete" { |
| 111 | + statusIcon = "✅" |
| 112 | + } else if progress.Status == "in-progress" { |
| 113 | + statusIcon = "🚧" |
| 114 | + } |
| 115 | + |
| 116 | + featureName := progress.FeatureName |
| 117 | + if featureName == "" { |
| 118 | + featureName = progress.FeatureID |
| 119 | + } |
| 120 | + |
| 121 | + allActivities = append(allActivities, ActivityItemData{ |
| 122 | + Type: "feature", |
| 123 | + Message: fmt.Sprintf("%s feature: %s %s", statusIcon, featureName, progress.Status), |
| 124 | + Timestamp: progress.LastUpdated.Format(time.RFC3339), |
| 125 | + }) |
| 126 | + } |
| 127 | + } |
| 128 | + activity.Last24Hours.TasksCompleted = tasks24h |
| 129 | + activity.Last7Days.TasksCompleted = tasks7d |
| 130 | + |
| 131 | + // Add PR merges |
| 132 | + if a.githubData != nil { |
| 133 | + for _, pr := range a.githubData.PRs { |
| 134 | + if pr.Status == "merged" || pr.Status == "closed" { |
| 135 | + // Try to parse PR timestamp (would need to be added to GitHubData) |
| 136 | + allActivities = append(allActivities, ActivityItemData{ |
| 137 | + Type: "pr", |
| 138 | + Message: fmt.Sprintf("🔀 PR #%d: %s merged", pr.Number, pr.Title), |
| 139 | + Timestamp: time.Now().Format(time.RFC3339), // TODO: Get actual merge time |
| 140 | + }) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Add phase completions from state |
| 146 | + if a.state != nil { |
| 147 | + for _, phase := range a.state.Phases { |
| 148 | + if phase.Status == "complete" { |
| 149 | + allActivities = append(allActivities, ActivityItemData{ |
| 150 | + Type: "phase", |
| 151 | + Message: fmt.Sprintf("🎯 phase: %s completed", phase.Name), |
| 152 | + Timestamp: time.Now().Format(time.RFC3339), // TODO: Get actual completion time |
| 153 | + }) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Sort activities by timestamp (newest first) |
| 159 | + activity.RecentActivity = sortActivitiesByTime(allActivities) |
| 160 | + |
| 161 | + // Limit to last 10 |
| 162 | + if len(activity.RecentActivity) > 10 { |
| 163 | + activity.RecentActivity = activity.RecentActivity[:10] |
| 164 | + } |
| 165 | + |
| 166 | + return activity |
| 167 | +} |
| 168 | + |
| 169 | +// FormatTimeAgo formats a timestamp as relative time |
| 170 | +func FormatTimeAgo(timestamp string) string { |
| 171 | + t, err := parseTime(timestamp) |
| 172 | + if err != nil { |
| 173 | + return timestamp |
| 174 | + } |
| 175 | + |
| 176 | + now := time.Now() |
| 177 | + diff := now.Sub(t) |
| 178 | + |
| 179 | + if diff < time.Minute { |
| 180 | + return "just now" |
| 181 | + } |
| 182 | + |
| 183 | + if diff < time.Hour { |
| 184 | + minutes := int(diff.Minutes()) |
| 185 | + return fmt.Sprintf("%dm ago", minutes) |
| 186 | + } |
| 187 | + |
| 188 | + if diff < 24*time.Hour { |
| 189 | + hours := int(diff.Hours()) |
| 190 | + return fmt.Sprintf("%dh ago", hours) |
| 191 | + } |
| 192 | + |
| 193 | + days := int(diff.Hours() / 24) |
| 194 | + return fmt.Sprintf("%dd ago", days) |
| 195 | +} |
| 196 | + |
| 197 | +// parseTime parses various time formats |
| 198 | +func parseTime(timeStr string) (time.Time, error) { |
| 199 | + // Try RFC3339 first |
| 200 | + if t, err := time.Parse(time.RFC3339, timeStr); err == nil { |
| 201 | + return t, nil |
| 202 | + } |
| 203 | + |
| 204 | + // Try ISO 8601 |
| 205 | + if t, err := time.Parse(time.RFC3339Nano, timeStr); err == nil { |
| 206 | + return t, nil |
| 207 | + } |
| 208 | + |
| 209 | + // Try common formats |
| 210 | + formats := []string{ |
| 211 | + "2006-01-02 15:04:05", |
| 212 | + "2006-01-02T15:04:05", |
| 213 | + "2006-01-02", |
| 214 | + } |
| 215 | + |
| 216 | + for _, format := range formats { |
| 217 | + if t, err := time.Parse(format, timeStr); err == nil { |
| 218 | + return t, nil |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + return time.Time{}, fmt.Errorf("unable to parse time: %s", timeStr) |
| 223 | +} |
| 224 | + |
| 225 | +// sortActivitiesByTime sorts activities by timestamp (newest first) |
| 226 | +func sortActivitiesByTime(activities []ActivityItemData) []ActivityItemData { |
| 227 | + // Simple bubble sort (can be optimized if needed) |
| 228 | + for i := 0; i < len(activities); i++ { |
| 229 | + for j := i + 1; j < len(activities); j++ { |
| 230 | + timeI, errI := parseTime(activities[i].Timestamp) |
| 231 | + timeJ, errJ := parseTime(activities[j].Timestamp) |
| 232 | + |
| 233 | + if errI != nil || errJ != nil { |
| 234 | + continue |
| 235 | + } |
| 236 | + |
| 237 | + if timeJ.After(timeI) { |
| 238 | + activities[i], activities[j] = activities[j], activities[i] |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + return activities |
| 244 | +} |
| 245 | + |
| 246 | +// CalculateActivityPeriods calculates activity for specific time periods |
| 247 | +func CalculateActivityPeriods(activities []ActivityItemData, since time.Time) ActivityPeriodData { |
| 248 | + period := ActivityPeriodData{} |
| 249 | + |
| 250 | + for _, activity := range activities { |
| 251 | + activityTime, err := parseTime(activity.Timestamp) |
| 252 | + if err != nil { |
| 253 | + continue |
| 254 | + } |
| 255 | + |
| 256 | + if activityTime.After(since) { |
| 257 | + switch activity.Type { |
| 258 | + case "commit": |
| 259 | + period.Commits++ |
| 260 | + case "task", "feature": |
| 261 | + period.TasksCompleted++ |
| 262 | + } |
| 263 | + // FilesChanged would need to be tracked separately |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + return period |
| 268 | +} |
0 commit comments