Scripts for exporting poll data and simulation results from Firebase Firestore to CSV files.
export_charts_data.js- Export data matching results page charts- Poll Votes by Party - Party rankings with vote counts and percentages
- Daily Trends - Timeline data (top 10 parties, pivot table format)
- Policy Statistics - Most selected policies with rankings
- Party Winners - Top 20 parties by match count
- Raw Poll Votes - Individual vote records with timestamps (27K+ records)
- Uses same calculation logic as
results.js
-
export_all_polls_sdk.js- Export using Firebase SDK (RECOMMENDED)- Uses Firebase SDK (same as website) to avoid rate limits
- Exports Poll Votes successfully
- Simulation Results may timeout for large datasets
-
export_sim_results_batched.js- Export Simulation Results in batches- Use this if Simulation Results timeout in the main script
- Uses REST API with pagination and delays
- Handles large collections better
-
export_all_polls.js- Export using REST API (may have rate limits)- Original version using Firebase REST API
- May encounter rate limiting issues
-
export_policy_stats_local.js- Export policy statistics only- Exports "Most Selected Policies" data from
sim_results_v7 - Generates aggregated policy rankings
- Exports "Most Selected Policies" data from
cleanup-empty-policies.js- Utility script for cleaning up empty policiesdebug_data.js- Debug script for analyzing Firestore data structure (for development)
Exports data that exactly matches the charts displayed on the results page.
-
Install dependencies:
npm install dotenv firebase
-
Run the script:
node export_charts_data.js
-
CSV files will be saved in
exports/folder:poll_votes_by_party_{timestamp}.csv- Party rankings with counts and percentagesdaily_trends_{timestamp}.csv- Daily vote timeline (top 10 parties, pivot format)policy_statistics_{timestamp}.csv- Policy selection rankingsparty_winners_{timestamp}.csv- Top 20 parties by match countraw_poll_votes_{timestamp}.csv- Individual vote records with timestamps
Output formats:
- Poll Votes by Party: Rank, Party ID, Party Name, Vote Count, Percentage
- Daily Trends: Date, Party1, Party2, ... (top 10 parties as columns)
- Policy Statistics: Rank, Policy Name, Count, Percentage
- Party Winners: Rank, Party ID, Party Name, Win Count, Percentage
- Raw Poll Votes: Timestamp, Party ID, Party Name (27K+ individual records)
Exports both Poll Votes and Simulation Results to CSV files.
-
Install dependencies:
npm install dotenv
-
Configure .env file:
- The script automatically reads Firebase configuration from
../.env - No manual .env creation needed if using the project root .env
- The script automatically reads Firebase configuration from
-
Run the script:
node export_all_polls.js
-
CSV files will be saved in
exports/folder:poll_votes_{timestamp}.csv- All poll votes from userssim_results_{timestamp}.csv- All simulation results
Exports aggregated policy rankings from simulation results.
-
Run the script:
node export_policy_stats_local.js
-
CSV file will be saved in the current directory with name:
policy_stats_{timestamp}.csv
poll_votes_{timestamp}.csv:
Timestamp,Party,Party Name
2025-01-30T10:30:00,PP,ประชาชน
2025-01-30T10:31:00,PTP,เพื่อไทย
...sim_results_{timestamp}.csv:
Timestamp,Winner,Party Name,Policy Choices,Eco,Soc,Lib,Budget
2025-01-30T10:30:00,PP,ประชาชน,"คืนครูให้นักเรียน|ยกเลิกเกณฑ์ทหาร",65,75,80,-26
...CSV file format:
Rank,Policy Name,Count,Percentage
1,"คืนครูให้นักเรียน",111,73.5%
2,"ยกเลิกเกณฑ์ทหาร",97,64.2%
3,"ทักษะอนาคต (Wellness/Tech)",94,62.3%
...-
Export All Poll Data:
- Fetches from both
poll_votes_v7andsim_results_v7collections - Handles pagination automatically for large datasets
- CSV files include BOM for proper Thai character display in Excel on Windows
- Uses Firebase REST API for reliable data fetching
- Fetches from both
-
Export Policy Statistics:
- Fetches from the
sim_results_v7collection only - Data is aggregated and sorted by the most selected policies
- Processes Firestore's nested map structure for policy choices
- Fetches from the
-
Firebase configuration is loaded securely from project root .env file
If errors occur:
Firebase configuration not found- Check your .env file in the project rootpermission-denied- Check Firebase security rules and make sure Firestore is in test modenot-foundor404- The collection name might have changed or the collection path is incorrect- General issues - Check your internet connection and ensure Firestore is enabled
The scripts use environment variables from the .env file in the project root. Create a .env file based on .env.example:
# Firebase Configuration
VITE_FIREBASE_API_KEY=your_api_key_here
VITE_FIREBASE_AUTH_DOMAIN=your_auth_domain.firebaseapp.com
VITE_FIREBASE_PROJECT_ID=your_project_id
VITE_FIREBASE_STORAGE_BUCKET=your_storage_bucket.appspot.com
VITE_FIREBASE_MESSAGING_SENDER_ID=your_sender_id
VITE_FIREBASE_APP_ID=your_app_id
VITE_FIREBASE_MEASUREMENT_ID=your_measurement_id