-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathanalytics.py
More file actions
285 lines (238 loc) · 10.3 KB
/
Copy pathanalytics.py
File metadata and controls
285 lines (238 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
Blockchain Analytics Module
Provides advanced analytics and reporting for the blockchain
"""
import datetime as dt
from collections import defaultdict, Counter
import json
def get_attendance_analytics(blockchain):
"""
Generate comprehensive attendance analytics
"""
analytics = {
"overview": {
"total_blocks": len(blockchain),
"attendance_blocks": 0,
"total_students_recorded": 0,
"unique_teachers": set(),
"unique_courses": set(),
"date_range": {"start": None, "end": None}
},
"by_teacher": defaultdict(lambda: {
"total_classes": 0,
"total_students": 0,
"courses": set(),
"dates": []
}),
"by_course": defaultdict(lambda: {
"total_classes": 0,
"total_students": 0,
"teachers": set(),
"dates": []
}),
"by_date": defaultdict(lambda: {
"classes": 0,
"students": 0,
"teachers": set(),
"courses": set()
}),
"student_attendance": defaultdict(int),
"trends": {
"daily_attendance": [],
"course_popularity": [],
"teacher_activity": []
}
}
dates = []
for block in blockchain:
if block.index == 0: # Skip genesis block
continue
if block.data.get('type') == 'attendance':
analytics["overview"]["attendance_blocks"] += 1
teacher = block.data.get('teacher_name', 'Unknown')
course = block.data.get('course', 'Unknown')
date = block.data.get('date', '')
students = block.data.get('present_students', [])
# Update overview
analytics["overview"]["total_students_recorded"] += len(students)
analytics["overview"]["unique_teachers"].add(teacher)
analytics["overview"]["unique_courses"].add(course)
if date:
dates.append(date)
# Update teacher analytics
analytics["by_teacher"][teacher]["total_classes"] += 1
analytics["by_teacher"][teacher]["total_students"] += len(students)
analytics["by_teacher"][teacher]["courses"].add(course)
analytics["by_teacher"][teacher]["dates"].append(date)
# Update course analytics
analytics["by_course"][course]["total_classes"] += 1
analytics["by_course"][course]["total_students"] += len(students)
analytics["by_course"][course]["teachers"].add(teacher)
analytics["by_course"][course]["dates"].append(date)
# Update date analytics
analytics["by_date"][date]["classes"] += 1
analytics["by_date"][date]["students"] += len(students)
analytics["by_date"][date]["teachers"].add(teacher)
analytics["by_date"][date]["courses"].add(course)
# Update student attendance count
for student in students:
analytics["student_attendance"][student] += 1
# Convert sets to lists for JSON serialization
analytics["overview"]["unique_teachers"] = list(analytics["overview"]["unique_teachers"])
analytics["overview"]["unique_courses"] = list(analytics["overview"]["unique_courses"])
# Set date range
if dates:
analytics["overview"]["date_range"]["start"] = min(dates)
analytics["overview"]["date_range"]["end"] = max(dates)
# Convert defaultdicts to regular dicts and sets to lists
analytics["by_teacher"] = {k: {
"total_classes": v["total_classes"],
"total_students": v["total_students"],
"courses": list(v["courses"]),
"dates": v["dates"],
"avg_students_per_class": v["total_students"] / v["total_classes"] if v["total_classes"] > 0 else 0
} for k, v in analytics["by_teacher"].items()}
analytics["by_course"] = {k: {
"total_classes": v["total_classes"],
"total_students": v["total_students"],
"teachers": list(v["teachers"]),
"dates": v["dates"],
"avg_students_per_class": v["total_students"] / v["total_classes"] if v["total_classes"] > 0 else 0
} for k, v in analytics["by_course"].items()}
analytics["by_date"] = {k: {
"classes": v["classes"],
"students": v["students"],
"teachers": list(v["teachers"]),
"courses": list(v["courses"])
} for k, v in analytics["by_date"].items()}
# Generate trends
analytics["trends"]["course_popularity"] = sorted(
[(course, data["total_students"]) for course, data in analytics["by_course"].items()],
key=lambda x: x[1], reverse=True
)
analytics["trends"]["teacher_activity"] = sorted(
[(teacher, data["total_classes"]) for teacher, data in analytics["by_teacher"].items()],
key=lambda x: x[1], reverse=True
)
analytics["trends"]["daily_attendance"] = sorted(
[(date, data["students"]) for date, data in analytics["by_date"].items()]
)
return analytics
def get_blockchain_health(blockchain):
"""
Analyze blockchain health and integrity metrics
"""
health = {
"integrity": {
"valid_blocks": 0,
"invalid_blocks": 0,
"broken_links": 0,
"hash_mismatches": 0
},
"performance": {
"avg_block_size": 0,
"total_data_size": 0,
"block_creation_rate": 0
},
"security": {
"unique_hashes": set(),
"hash_distribution": {},
"potential_issues": []
}
}
total_size = 0
timestamps = []
for i, block in enumerate(blockchain):
# Check block validity
if block.is_valid():
health["integrity"]["valid_blocks"] += 1
else:
health["integrity"]["invalid_blocks"] += 1
# Check linkage
if i > 0:
if block.prev_hash != blockchain[i-1].hash:
health["integrity"]["broken_links"] += 1
# Calculate block size (approximate)
block_size = len(json.dumps(block.to_dict(), default=str))
total_size += block_size
# Collect timestamps
timestamps.append(block.timestamp)
# Check hash uniqueness
if block.hash in health["security"]["unique_hashes"]:
health["security"]["potential_issues"].append(f"Duplicate hash found in block {i}")
health["security"]["unique_hashes"].add(block.hash)
# Analyze hash distribution
hash_prefix = block.hash[:2]
health["security"]["hash_distribution"][hash_prefix] = \
health["security"]["hash_distribution"].get(hash_prefix, 0) + 1
# Calculate performance metrics
health["performance"]["total_data_size"] = total_size
health["performance"]["avg_block_size"] = total_size / len(blockchain) if blockchain else 0
# Calculate block creation rate (blocks per day)
if len(timestamps) > 1:
time_span = (timestamps[-1] - timestamps[0]).total_seconds() / 86400 # days
health["performance"]["block_creation_rate"] = len(blockchain) / time_span if time_span > 0 else 0
# Convert set to count for JSON serialization
health["security"]["unique_hashes"] = len(health["security"]["unique_hashes"])
return health
def generate_attendance_report(blockchain, format="text"):
"""
Generate a comprehensive attendance report
"""
analytics = get_attendance_analytics(blockchain)
if format == "json":
return json.dumps(analytics, indent=2, default=str)
# Text format report
report = []
report.append("=" * 60)
report.append("BLOCKCHAIN ATTENDANCE REPORT")
report.append("=" * 60)
# Overview
overview = analytics["overview"]
report.append(f"\nOVERVIEW:")
report.append(f" Total Blocks: {overview['total_blocks']}")
report.append(f" Attendance Records: {overview['attendance_blocks']}")
report.append(f" Total Students Recorded: {overview['total_students_recorded']}")
report.append(f" Unique Teachers: {len(overview['unique_teachers'])}")
report.append(f" Unique Courses: {len(overview['unique_courses'])}")
if overview['date_range']['start']:
report.append(f" Date Range: {overview['date_range']['start']} to {overview['date_range']['end']}")
# Top teachers
report.append(f"\nTOP TEACHERS BY ACTIVITY:")
for teacher, classes in analytics["trends"]["teacher_activity"][:5]:
avg_students = analytics["by_teacher"][teacher]["avg_students_per_class"]
report.append(f" {teacher}: {classes} classes, {avg_students:.1f} avg students")
# Top courses
report.append(f"\nTOP COURSES BY ATTENDANCE:")
for course, total_students in analytics["trends"]["course_popularity"][:5]:
classes = analytics["by_course"][course]["total_classes"]
report.append(f" {course}: {total_students} total students, {classes} classes")
# Most active students
report.append(f"\nMOST ACTIVE STUDENTS:")
top_students = sorted(analytics["student_attendance"].items(),
key=lambda x: x[1], reverse=True)[:10]
for student, count in top_students:
report.append(f" {student}: {count} attendances")
return "\n".join(report)
def export_analytics(blockchain, filename="blockchain_analytics.json"):
"""
Export comprehensive analytics to file
"""
try:
analytics = get_attendance_analytics(blockchain)
health = get_blockchain_health(blockchain)
export_data = {
"generated_at": str(dt.datetime.now()),
"blockchain_stats": {
"total_blocks": len(blockchain),
"genesis_hash": blockchain[0].hash if blockchain else None,
"latest_hash": blockchain[-1].hash if blockchain else None
},
"analytics": analytics,
"health": health
}
with open(filename, 'w') as f:
json.dump(export_data, f, indent=2, default=str)
return True, f"Analytics exported to {filename}"
except Exception as e:
return False, f"Error exporting analytics: {str(e)}"