async function fetchContributionData() {
try {
const response = await fetch('https://ejfox-allvals.web.val.run');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return processContributionData(data.vals);
} catch (error) {
console.error('Error fetching contribution data:', error);
throw error;
}
}
function processContributionData(vals) {
try {
const today = new Date();
const oneYearAgo = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate());
const data = {};
for (let d = new Date(oneYearAgo); d <= today; d.setDate(d.getDate() + 1)) {
data[d.toISOString().split('T')[0]] = 0;
}
vals.forEach(val => {
const date = new Date(val.createdAt).toISOString().split('T')[0];
if (date in data) {
data[date]++;
}
});
return data;
} catch (error) {
console.error('Error processing contribution data:', error);
throw error;
}
}
function getColorIntensity(count) {
if (count === 0) return '#ebedf0';
if (count < 5) return '#9be9a8';
if (count < 10) return '#40c463';
if (count < 15) return '#30a14e';
return '#216e39';
}
function drawContributionChart(ctx, contributionData, width, height) {
try {
const cellSize = 10;
const cellGap = 2;
const weekGap = 4;
const days = Object.keys(contributionData).sort();
const weeks = Math.ceil(days.length / 7);
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, width, height);
for (let week = 0; week < weeks; week++) {
for (let day = 0; day < 7; day++) {
const index = week * 7 + day;
if (index >= days.length) break;
const date = days[index];
const count = contributionData[date];
const x = week * (cellSize + weekGap);
const y = day * (cellSize + cellGap);
ctx.fillStyle = getColorIntensity(count);
ctx.fillRect(x, y, cellSize, cellSize);
}
}
} catch (error) {
console.error('Error drawing contribution chart:', error);
throw error;
}
}
async function server(request: Request): Promise<Response> {
try {
console.log('Fetching contribution data...');
const contributionData = await fetchContributionData();
console.log('Contribution data fetched successfully');
const width = 53 * (10 + 4) - 4;
const height = 7 * (10 + 2) - 2;
console.log('Creating canvas...');
const { createCanvas } = await import("https://deno.land/x/canvas/mod.ts");
const canvas = createCanvas(width, height);
if (!canvas) {
throw new Error('Failed to create canvas');
}
const ctx = canvas.getContext("2d");
if (!ctx) {
throw new Error('Failed to get 2D context from canvas');
}