Use Denver local time for scrape dates instead of UTC
Some checks failed
CI/CD Pipeline - Apartment API / Scan Dependencies (push) Successful in 18s
CI/CD Pipeline - Apartment API / Lint & Test (push) Successful in 1m27s
CI/CD Pipeline - Apartment API / Send Webhook Notification (push) Has been skipped
CI/CD Pipeline - Apartment API / Build & Push Image (push) Successful in 3m17s
CI/CD Pipeline - Apartment API / Deploy to Production (push) Failing after 27s
Some checks failed
CI/CD Pipeline - Apartment API / Scan Dependencies (push) Successful in 18s
CI/CD Pipeline - Apartment API / Lint & Test (push) Successful in 1m27s
CI/CD Pipeline - Apartment API / Send Webhook Notification (push) Has been skipped
CI/CD Pipeline - Apartment API / Build & Push Image (push) Successful in 3m17s
CI/CD Pipeline - Apartment API / Deploy to Production (push) Failing after 27s
Change date helper functions to use toLocaleDateString with the configured SCRAPER_TIMEZONE (America/Denver) so that scrape date records (date_checked, daily summaries) reflect local time. A late-night manual scrape and early-morning automated scrape will no longer both report as the same UTC day. ISO timestamps for last_scraped, first_seen, startedAt etc. remain in UTC as they represent precise moments.
This commit is contained in:
14
server.js
14
server.js
@ -107,8 +107,8 @@ async function connectToMongoDB() {
|
|||||||
// Register scraper signal handlers for graceful shutdown
|
// Register scraper signal handlers for graceful shutdown
|
||||||
registerSignalHandlers();
|
registerSignalHandlers();
|
||||||
|
|
||||||
// Helper function to get today's date in UTC
|
// Helper function to get today's date in configured timezone
|
||||||
const getTodayDateUTC = () => new Date().toISOString().split('T')[0];
|
const getTodayDate = () => new Date().toLocaleDateString('en-CA', { timeZone: scraperConfig.SCRAPER_TIMEZONE });
|
||||||
|
|
||||||
// Cache for the most recent date with data
|
// Cache for the most recent date with data
|
||||||
let cachedLatestDate = null;
|
let cachedLatestDate = null;
|
||||||
@ -143,7 +143,7 @@ const getLatestDateWithData = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to UTC today if no data found
|
// Fallback to UTC today if no data found
|
||||||
return getTodayDateUTC();
|
return getTodayDate();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper function to get yesterday relative to the latest date with data
|
// Helper function to get yesterday relative to the latest date with data
|
||||||
@ -641,10 +641,10 @@ app.get('/analytics', requireAuth, async (req, res) => {
|
|||||||
]).toArray();
|
]).toArray();
|
||||||
|
|
||||||
// 4. Historical Comparison - current vs 30/90/365 day averages
|
// 4. Historical Comparison - current vs 30/90/365 day averages
|
||||||
const now = new Date();
|
const tz = scraperConfig.SCRAPER_TIMEZONE;
|
||||||
const date30 = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
|
const date30 = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toLocaleDateString('en-CA', { timeZone: tz });
|
||||||
const date90 = new Date(now.getTime() - 90 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
|
const date90 = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000).toLocaleDateString('en-CA', { timeZone: tz });
|
||||||
const date365 = new Date(now.getTime() - 365 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
|
const date365 = new Date(Date.now() - 365 * 24 * 60 * 60 * 1000).toLocaleDateString('en-CA', { timeZone: tz });
|
||||||
|
|
||||||
const [currentAvg, avg30, avg90, avg365] = await Promise.all([
|
const [currentAvg, avg30, avg90, avg365] = await Promise.all([
|
||||||
db.collection(PRICES_COLLECTION).aggregate([
|
db.collection(PRICES_COLLECTION).aggregate([
|
||||||
|
|||||||
@ -626,11 +626,11 @@ async function updateDailySummary(db, summaryData, logger) {
|
|||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get today's date in YYYY-MM-DD format (UTC).
|
* Get today's date in YYYY-MM-DD format using the configured timezone.
|
||||||
* @returns {string} Today's date string
|
* @returns {string} Today's date string
|
||||||
*/
|
*/
|
||||||
function getTodayUTC() {
|
function getToday() {
|
||||||
return new Date().toISOString().split('T')[0];
|
return new Date().toLocaleDateString('en-CA', { timeZone: config.SCRAPER_TIMEZONE });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -820,7 +820,7 @@ async function runScrape(db, options = {}) {
|
|||||||
result.unitsProcessed = units.length;
|
result.unitsProcessed = units.length;
|
||||||
|
|
||||||
// Step 4: Database operations
|
// Step 4: Database operations
|
||||||
const today = getTodayUTC();
|
const today = getToday();
|
||||||
|
|
||||||
// Get yesterday's unit codes for comparison
|
// Get yesterday's unit codes for comparison
|
||||||
const yesterdayUnits = await getYesterdayUnitCodes(db, today);
|
const yesterdayUnits = await getYesterdayUnitCodes(db, today);
|
||||||
@ -904,7 +904,7 @@ module.exports = {
|
|||||||
recordScraperRun,
|
recordScraperRun,
|
||||||
createScraperIndexes,
|
createScraperIndexes,
|
||||||
// Export helpers for testing
|
// Export helpers for testing
|
||||||
getTodayUTC,
|
getToday,
|
||||||
getYesterdayUnitCodes,
|
getYesterdayUnitCodes,
|
||||||
getYesterday,
|
getYesterday,
|
||||||
parseInteger,
|
parseInteger,
|
||||||
|
|||||||
Reference in New Issue
Block a user