-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.js
More file actions
176 lines (150 loc) · 5.7 KB
/
Copy pathanalytics.js
File metadata and controls
176 lines (150 loc) · 5.7 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
"use strict";
// Timezone-safe daily streak calculation
function calculateStreak(submissions) {
if (!submissions || submissions.length === 0) {
return { currentStreak: 0, maxStreak: 0, streakAtRisk: false };
}
// Get unique local dates (YYYY-MM-DD) of submissions
const dates = new Set(
submissions.map((s) => {
const date = new Date(s.timestamp || s.detectedAt);
return toLocalISOString(date).slice(0, 10);
})
);
const sortedDates = Array.from(dates).sort((a, b) => new Date(b) - new Date(a)); // Descending order
const todayStr = toLocalISOString(new Date()).slice(0, 10);
const yesterdayStr = toLocalISOString(new Date(Date.now() - 86400000)).slice(0, 10);
let currentStreak = 0;
let maxStreak = 0;
let runningStreak = 0;
// Calculate current streak
let checkDate = new Date(todayStr);
if (!dates.has(todayStr) && !dates.has(yesterdayStr)) {
currentStreak = 0;
} else {
let dateToVerify = dates.has(todayStr) ? todayStr : yesterdayStr;
let tempDate = new Date(dateToVerify);
while (dates.has(toLocalISOString(tempDate).slice(0, 10))) {
currentStreak++;
tempDate.setDate(tempDate.getDate() - 1);
}
}
// Calculate max streak historically
const chronologicalDates = Array.from(dates).sort((a, b) => new Date(a) - new Date(b));
if (chronologicalDates.length > 0) {
runningStreak = 1;
maxStreak = 1;
for (let i = 1; i < chronologicalDates.length; i++) {
const prev = new Date(chronologicalDates[i - 1]);
const curr = new Date(chronologicalDates[i]);
const diffTime = Math.abs(curr - prev);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays === 1) {
runningStreak++;
} else if (diffDays > 1) {
runningStreak = 1;
}
if (runningStreak > maxStreak) {
maxStreak = runningStreak;
}
}
}
const streakAtRisk = currentStreak > 0 && !dates.has(todayStr);
return { currentStreak, maxStreak, streakAtRisk };
}
// Convert date to timezone-safe local YYYY-MM-DD
function toLocalISOString(date) {
const tzo = -date.getTimezoneOffset();
const dif = tzo >= 0 ? '+' : '-';
const pad = (num) => (num < 10 ? '0' : '') + num;
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds()) +
dif + pad(Math.floor(Math.abs(tzo) / 60)) +
':' + pad(Math.floor(Math.abs(tzo) % 60));
}
// Get heatmap submissions count for last 365 days
function generateHeatmapData(submissions) {
const heatmap = {};
const today = new Date();
// Initialize last 365 days with 0
for (let i = 365; i >= 0; i--) {
const d = new Date(today);
d.setDate(today.getDate() - i);
const key = toLocalISOString(d).slice(0, 10);
heatmap[key] = 0;
}
// Populate actual counts
submissions.forEach((s) => {
const date = new Date(s.timestamp || s.detectedAt);
const key = toLocalISOString(date).slice(0, 10);
if (heatmap[key] !== undefined) {
heatmap[key]++;
}
});
return heatmap;
}
// Generate topic statistics and identify strengths/weaknesses
function generateTopicAnalytics(submissions) {
const topicsCount = {};
submissions.forEach((s) => {
const payload = s.payload || s;
const topics = payload.topics || [];
topics.forEach((topic) => {
const clean = topic.trim();
topicsCount[clean] = (topicsCount[clean] || 0) + 1;
});
});
const sortedTopics = Object.entries(topicsCount).sort((a, b) => b[1] - a[1]);
const strongest = sortedTopics.length > 0 ? sortedTopics[0][0] : "None";
const weakest = sortedTopics.length > 1 ? sortedTopics[sortedTopics.length - 1][0] : "None";
return { topicsCount, strongest, weakest };
}
// Language statistics
function generateLanguageAnalytics(submissions) {
const langCount = {};
submissions.forEach((s) => {
const payload = s.payload || s;
const lang = payload.language || "Unknown";
langCount[lang] = (langCount[lang] || 0) + 1;
});
return langCount;
}
// Insights Engine
function generateInsights(submissions, streakData, topicData) {
const insights = [];
if (submissions.length === 0) {
insights.push("No submissions synced yet. Solve a problem to start generating insights!");
return insights;
}
// 1. Streak check
if (streakData.streakAtRisk) {
insights.push("🔥 Your daily streak is at risk! Submit a solution today to keep it active.");
} else if (streakData.currentStreak > 0) {
insights.push(`💪 Awesome! You are on a ${streakData.currentStreak}-day streak. Keep it up!`);
}
// 2. Strongest topic check
if (topicData.strongest && topicData.strongest !== "None" && topicData.strongest !== "Uncategorized") {
insights.push(`🎯 Your strongest domain is ${topicData.strongest}, where you have solved the most problems.`);
}
// 3. Inactive areas check
// Check if graphs or trees haven't been solved in 5 days
const now = Date.now();
const graphSubmissions = submissions.filter(s => {
const p = s.payload || s;
return (p.topics || []).some(t => t.toLowerCase().includes("graph") || t.toLowerCase().includes("tree"));
});
if (graphSubmissions.length > 0) {
const lastGraphDate = Math.max(...graphSubmissions.map(s => s.timestamp || new Date(s.detectedAt).getTime()));
const daysSince = (now - lastGraphDate) / (1000 * 60 * 60 * 24);
if (daysSince >= 5) {
insights.push(`⚠️ It's been ${Math.floor(daysSince)} days since you solved a Graph or Tree problem. Keep your skills sharp!`);
}
} else {
insights.push("💡 Try solving some Graph or Tree problems to diversify your skills!");
}
return insights.slice(0, 3); // Max 3 insights
}