|
1
|
import json |
|
2
|
|
|
3
|
from django.contrib.auth.decorators import login_required |
|
4
|
from django.shortcuts import render |
|
5
|
|
|
6
|
|
|
7
|
@login_required |
|
8
|
def dashboard(request): |
|
9
|
from fossil.models import FossilRepository |
|
10
|
from fossil.reader import FossilReader |
|
11
|
from projects.models import Project |
|
12
|
|
|
13
|
# Aggregate stats across all projects |
|
14
|
total_projects = Project.objects.count() |
|
15
|
total_checkins = 0 |
|
16
|
total_tickets = 0 |
|
17
|
total_wiki = 0 |
|
18
|
system_activity = [] # weekly commit counts across all repos |
|
19
|
heatmap_data = {} # {date_string: count} -- daily commit counts across all repos |
|
20
|
recent_across_all = [] |
|
21
|
|
|
22
|
# NOTE: For large installations with many repos, this per-request aggregation |
|
23
|
# could become slow. Consider caching heatmap_data with a short TTL (e.g. 5 min) |
|
24
|
# via Django's cache framework if this becomes a bottleneck. |
|
25
|
repos = FossilRepository.objects.filter(deleted_at__isnull=True) |
|
26
|
for repo in repos: |
|
27
|
if not repo.exists_on_disk: |
|
28
|
continue |
|
29
|
try: |
|
30
|
with FossilReader(repo.full_path) as reader: |
|
31
|
meta = reader.get_metadata() |
|
32
|
total_checkins += meta.checkin_count |
|
33
|
total_tickets += meta.ticket_count |
|
34
|
total_wiki += meta.wiki_page_count |
|
35
|
|
|
36
|
activity = reader.get_commit_activity(weeks=26) |
|
37
|
if not system_activity: |
|
38
|
system_activity = [c["count"] for c in activity] |
|
39
|
else: |
|
40
|
for i, c in enumerate(activity): |
|
41
|
if i < len(system_activity): |
|
42
|
system_activity[i] += c["count"] |
|
43
|
|
|
44
|
# Aggregate daily activity for heatmap (single pass per repo) |
|
45
|
daily = reader.get_daily_commit_activity(days=365) |
|
46
|
for entry in daily: |
|
47
|
date = entry["date"] |
|
48
|
heatmap_data[date] = heatmap_data.get(date, 0) + entry["count"] |
|
49
|
|
|
50
|
commits = reader.get_timeline(limit=3, event_type="ci") |
|
51
|
for c in commits: |
|
52
|
recent_across_all.append({"project": repo.project, "entry": c}) |
|
53
|
except Exception: |
|
54
|
continue |
|
55
|
|
|
56
|
# Sort recent across all by timestamp, take top 10 |
|
57
|
recent_across_all.sort(key=lambda x: x["entry"].timestamp, reverse=True) |
|
58
|
recent_across_all = recent_across_all[:10] |
|
59
|
|
|
60
|
# Convert heatmap to sorted list for the template |
|
61
|
heatmap_json = json.dumps(sorted([{"date": d, "count": c} for d, c in heatmap_data.items()], key=lambda x: x["date"])) |
|
62
|
|
|
63
|
return render( |
|
64
|
request, |
|
65
|
"dashboard.html", |
|
66
|
{ |
|
67
|
"total_projects": total_projects, |
|
68
|
"total_checkins": total_checkins, |
|
69
|
"total_tickets": total_tickets, |
|
70
|
"total_wiki": total_wiki, |
|
71
|
"total_repos": repos.count(), |
|
72
|
"system_activity_json": json.dumps(system_activity), |
|
73
|
"heatmap_json": heatmap_json, |
|
74
|
"recent_across_all": recent_across_all, |
|
75
|
}, |
|
76
|
) |
|
77
|
|