Skip to content

Commit

Permalink
basic cache for electoral districts #74
Browse files Browse the repository at this point in the history
  • Loading branch information
Scobiform committed Oct 20, 2024
1 parent 2a61de5 commit 3f861f7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
26 changes: 16 additions & 10 deletions src/components/Map/DynamicMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ const DynamicMap = ({ polygons = [],
mapClassName = `${mapClassName} ${className}`;
}

// Event handler for the dropdown
const handleSwitch = (event) => {
setSelectedMap(event.target.value);
};



// Function to fetch district data from the API
const fetchDistrictData = async (code, state) => {
try {
const response = await fetch(`/api/district?code=${code}&state=${state}`);
Expand Down Expand Up @@ -211,20 +211,26 @@ const DynamicMap = ({ polygons = [],
};

const onEachFeature = (feature, layer, state) => {
if (state === 'sh' || state === 'hamburg'
|| state === 'bremen' || state === 'berlin'
|| state === 'brandenburg') {
electIT(feature, layer, state);
} else if (state === 'hessen' || state === 'rlp') {
rlphessen(feature, layer, state);
} else {
const stateHandlers = {
sh: electIT,
bremen: electIT,
berlin: electIT,
brandenburg: electIT,
hessen: rlphessen,
rlp: rlphessen
};

const handler = stateHandlers[state] || ((feature, layer) => {
const popupContent = ReactDOMServer.renderToString(
<DistrictCard district={feature.properties} />
);
layer.bindPopup(popupContent);
}
});

handler(feature, layer, state);
};


return (
<>
{/* Conditionally render LoadingSpinner */}
Expand Down
32 changes: 25 additions & 7 deletions src/pages/api/download.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import puppeteer from 'puppeteer';
import fs from 'fs';
import path from 'path';

async function scrapeElectionStatistics(page) {
try {
Expand Down Expand Up @@ -177,8 +178,21 @@ async function scrapeVoterTurnoutChart(page) {
export default async function handler(req, res) {
let browser;
const { electoralDistrict, state } = req.query;
const cacheDir = 'public/cache';

const cacheDir = path.join(process.cwd(), 'public', 'cache');
const cacheFile = path.join(cacheDir, `${electoralDistrict}_${state}.json`);

// Check if cache file exists
if (fs.existsSync(cacheFile)) {
try {
// Read and return cached data
const cachedData = fs.readFileSync(cacheFile, 'utf8');
return res.status(200).json(JSON.parse(cachedData));
} catch (error) {
console.error('Error reading cache file:', error);
return res.status(500).json({ error: 'Failed to read cached data' });
}
}

try {
// Launch Puppeteer
browser = await puppeteer.launch({ headless: true });
Expand Down Expand Up @@ -236,7 +250,7 @@ export default async function handler(req, res) {

// Scrape all SVG elements
let svgData = null;
if(state === 'sh') {
if(state !== 'berlin') {
svgData = await scrapeAllSVGs(page);
}

Expand Down Expand Up @@ -264,11 +278,15 @@ export default async function handler(req, res) {
voterTurnoutChart,
};

// Return the scraped data as JSON
res.status(200).json(combinedData);
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, { recursive: true });
}

// Cache the data by writing it to a file
fs.writeFileSync(cacheFile, JSON.stringify(combinedData, null, 2));

// write the data to a file
fs.writeFileSync(cacheDir+'/'+electoralDistrict+state+'.json', JSON.stringify(combinedData, null, 2));
// Return the scraped data
res.status(200).json(combinedData);

} catch (error) {
console.error('Error scraping table:', error);
Expand Down

0 comments on commit 3f861f7

Please sign in to comment.