File size: 11,878 Bytes
fa9bd65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
925a137
 
fa9bd65
 
 
 
 
 
 
 
 
58cd75e
fa9bd65
 
 
 
 
 
 
 
 
 
 
 
2c59a16
 
 
 
 
 
fa9bd65
 
 
2c59a16
 
fa9bd65
 
 
 
 
2c59a16
fa9bd65
2c59a16
fa9bd65
 
 
2c59a16
fa9bd65
 
 
2c59a16
fa9bd65
 
 
2c59a16
fa9bd65
 
 
 
2c59a16
fa9bd65
83d1a1d
fa9bd65
83d1a1d
 
 
 
 
2c59a16
a7de93d
 
2c59a16
 
a7de93d
 
 
 
 
 
2c59a16
a7de93d
 
 
2c59a16
 
 
 
 
a7de93d
 
58cd75e
2c59a16
58cd75e
2c59a16
 
 
 
58cd75e
 
2c59a16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa9bd65
2c59a16
 
 
 
 
 
 
 
 
 
 
fa9bd65
 
2c59a16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa9bd65
 
2c59a16
 
 
 
 
 
 
 
a1ea0c3
 
 
 
 
 
2c59a16
a1ea0c3
58cd75e
 
 
 
 
 
 
 
 
 
 
a2f8001
58cd75e
 
 
 
 
 
 
 
 
 
 
 
 
2c59a16
58cd75e
 
2c59a16
 
 
 
 
 
 
 
fa9bd65
2c59a16
58cd75e
 
 
 
fa9bd65
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WASTE-LESS - Reports</title>
    <link rel="stylesheet" href="style.css">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body class="bg-gray-100 min-h-screen">
    <div class="container mx-auto px-4 py-8 max-w-4xl">
        <!-- Header with back button -->
        <header class="bg-blue-600 text-white rounded-lg shadow-lg p-6 mb-8">
            <div class="flex items-center">
                <a href="index.html" class="text-blue-100 hover:text-white mr-4 text-xl"><i class="fas fa-home"></i></a>
<div>
                    <h1 class="text-3xl font-bold">Reports</h1>
                    <p class="text-blue-100">View food waste statistics and analytics</p>
                </div>
            </div>
        </header>
        <!-- Reports Content -->
        <div class="bg-white rounded-lg shadow-md p-6 mb-8">
            <h2 class="text-xl font-semibold mb-4 text-gray-800">Food Waste Statistics</h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
                <!-- Weekly Waste Chart -->
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="text-lg font-medium mb-3">Weekly Food Waste</h3>
                    <canvas id="weeklyWasteChart" height="200"></canvas>
                </div>
                
                <!-- Meal Period Waste -->
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="text-lg font-medium mb-3">Waste by Meal Period</h3>
                    <canvas id="mealWasteChart" height="200"></canvas>
                </div>
            </div>

            <!-- Data Loading Status -->
            <div id="loading-status" class="mb-8 text-center hidden">
                <div class="inline-block animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-blue-500 mb-2"></div>
                <p class="text-gray-600">Loading data from GitHub...</p>
            </div>
            <!-- Top Wasted Foods -->
            <div class="bg-gray-50 p-4 rounded-lg mb-8">
                <h3 class="text-lg font-medium mb-3">Top Wasted Foods</h3>
                <div class="space-y-2" id="topWastedFoods">
                    <div class="text-center py-4 text-gray-500">Loading data...</div>
                </div>
            </div>
            <!-- Attendance Summary -->
            <div class="bg-gray-50 p-4 rounded-lg">
                <h3 class="text-lg font-medium mb-3">Attendance Summary</h3>
                <div class="grid grid-cols-2 md:grid-cols-4 gap-4" id="attendanceSummary">
                    <div class="bg-blue-50 text-blue-800 p-3 rounded-lg text-center">
                        <div class="text-2xl font-bold" id="todayAttendance">--%</div>
                        <div class="text-sm">Today's Attendance</div>
                    </div>
                    <div class="bg-green-50 text-green-800 p-3 rounded-lg text-center">
                        <div class="text-2xl font-bold" id="weeklyAverage">--%</div>
                        <div class="text-sm">Weekly Average</div>
                    </div>
                    <div class="bg-yellow-50 text-yellow-800 p-3 rounded-lg text-center">
                        <div class="text-2xl font-bold" id="absentToday">--</div>
                        <div class="text-sm">Absent Today</div>
                    </div>
                    <div class="bg-purple-50 text-purple-800 p-3 rounded-lg text-center">
                        <div class="text-2xl font-bold" id="presentToday">--</div>
                        <div class="text-sm">Present Today</div>
                    </div>
                </div>
            </div>
</div>
    </div>
    <script src="config.js"></script>
    <script>
// API URLs
        const WANT_TO_EAT_URL = `https://api.github.com/repos/${GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/Data%20Students%20who%20want%20to%20eat`;
        const HAVE_EATEN_URL = `https://api.github.com/repos/${GITHUB_CONFIG.REPOS.CONSUMED}/contents/data%20who%20has%20ate%20at%20dm`;
        const WASTE_DATA_URL = `https://api.github.com/repos/${GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/food_waste_data.json`;
// Chart variables
        let weeklyChart, mealChart;

        // Fetch data from GitHub
        async function fetchData(url) {
            try {
                const response = await fetch(url, {
                    headers: {
                        'Accept': 'application/vnd.github.v3+json',
                        'Authorization': `token ${GITHUB_CONFIG.TOKEN}`
}
                });

                if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
                const data = await response.json();
                return JSON.parse(atob(data.content));
            } catch (error) {
                console.error('Error fetching data:', error);
                return null;
            }
        }

        // Calculate attendance stats
        function calculateAttendanceStats(records) {
            const today = new Date().toISOString().split('T')[0];
            const thisWeekRecords = records.filter(r => {
                const recordDate = r.timestamp.split('T')[0];
                const dateDiff = (new Date(today) - new Date(recordDate)) / (1000 * 60 * 60 * 24);
                return dateDiff <= 7;
            });

            // Today's stats
            const todayRecords = records.filter(r => r.timestamp.includes(today));
            const presentToday = todayRecords.filter(r => r.attendance === 'yes').length;
            const absentToday = todayRecords.filter(r => r.attendance === 'no').length;
            const todayAttendanceRate = todayRecords.length ? Math.round((presentToday / todayRecords.length) * 100) : 0;

            // Weekly stats
            const weeklyPresent = thisWeekRecords.filter(r => r.attendance === 'yes').length;
            const weeklyTotal = thisWeekRecords.length;
            const weeklyAverage = weeklyTotal ? Math.round((weeklyPresent / weeklyTotal) * 100) : 0;

            return {
                todayAttendance: todayAttendanceRate,
                weeklyAverage: weeklyAverage,
                absentToday: absentToday,
                presentToday: presentToday
            };
        }

        // Calculate food waste stats
        function calculateFoodWasteStats(wasteData) {
            const weeklyWaste = wasteData.last_week_daily_waste;
            const mealWaste = wasteData.meal_period_waste;
            const topWasted = wasteData.top_wasted_foods;

            return { weeklyWaste, mealWaste, topWasted };
        }

        // Initialize charts with data
        function initCharts(weeklyData, mealData) {
            // Weekly Waste Chart
            const weeklyCtx = document.getElementById('weeklyWasteChart').getContext('2d');
            weeklyChart = new Chart(weeklyCtx, {
                type: 'line',
                data: {
                    labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
                    datasets: [{
                        label: 'Food Waste (kg)',
                        data: weeklyData,
                        backgroundColor: 'rgba(59, 130, 246, 0.2)',
                        borderColor: 'rgba(59, 130, 246, 1)',
                        borderWidth: 2,
                        tension: 0.3
                    }]
                },
                options: {
                    responsive: true,
                    plugins: {
                        legend: {
                            display: false
                        }
                    },
                    scales: {
                        y: {
                            beginAtZero: true
                        }
                    }
                }
            });

            // Meal Waste Chart
            const mealCtx = document.getElementById('mealWasteChart').getContext('2d');
            mealChart = new Chart(mealCtx, {
                type: 'doughnut',
                data: {
                    labels: ['Breakfast', 'Lunch', 'Dinner', 'Supper'],
                    datasets: [{
                        data: mealData,
                        backgroundColor: [
                            'rgba(234, 179, 8, 0.7)',
                            'rgba(22, 163, 74, 0.7)',
                            'rgba(99, 102, 241, 0.7)',
                            'rgba(168, 85, 247, 0.7)'
                        ],
                        borderWidth: 1
                    }]
                },
                options: {
                    responsive: true,
                    plugins: {
                        legend: {
                            position: 'bottom'
                        }
                    }
                }
            });
        }

        // Update UI with fetched data
        async function updateUI() {
            document.getElementById('loading-status').classList.remove('hidden');

            try {
                // Fetch all data in parallel
                const [wantToEatData, haveEatenData, wasteData] = await Promise.all([
                    fetchData(WANT_TO_EAT_URL),
                    fetchData(HAVE_EATEN_URL),
                    fetchData(WASTE_DATA_URL)
                ]);

                // Process attendance data
                const attendanceStats = calculateAttendanceStats([...wantToEatData, ...haveEatenData]);
                document.getElementById('todayAttendance').textContent = `${attendanceStats.todayAttendance}%`;
                document.getElementById('weeklyAverage').textContent = `${attendanceStats.weeklyAverage}%`;
                document.getElementById('absentToday').textContent = attendanceStats.absentToday;
                document.getElementById('presentToday').textContent = attendanceStats.presentToday;

                // Process food waste data
                if (wasteData) {
                    const { weeklyWaste, mealWaste, topWasted } = calculateFoodWasteStats(wasteData);
                    
                    // Initialize charts
                    initCharts(weeklyWaste, mealWaste);
                    
                    // Update top wasted foods
                    const topWastedContainer = document.getElementById('topWastedFoods');
                    topWastedContainer.innerHTML = '';
                    topWasted.forEach(food => {
                        const item = document.createElement('div');
                        item.className = 'flex justify-between items-center p-2 bg-white rounded';
                        item.innerHTML = `
                            <span>${food.name}</span>
                            <span class="font-medium">${food.percentage}% wasted</span>
                        `;
                        topWastedContainer.appendChild(item);
                    });
                }

            } catch (error) {
                console.error('Error updating UI:', error);
                // Show error message
                const errorMsg = document.createElement('div');
                errorMsg.className = 'bg-red-50 text-red-700 p-4 rounded-lg mb-4';
                errorMsg.textContent = 'Failed to load data. Please try again later.';
                document.getElementById('reports-content').prepend(errorMsg);
            } finally {
                document.getElementById('loading-status').classList.add('hidden');
            }
        }

        // Initialize when page loads
        document.addEventListener('DOMContentLoaded', updateUI);
    </script>
</body>
</html>