fix charts to use local time
This commit is contained in:
parent
e82866f39b
commit
55ca8d3260
4 changed files with 18 additions and 10 deletions
|
|
@ -2,6 +2,7 @@ import { error, json } from '@sveltejs/kit';
|
|||
import { db } from '$lib/server/db';
|
||||
import { coin, user, priceHistory, transaction } from '$lib/server/db/schema';
|
||||
import { eq, desc } from 'drizzle-orm';
|
||||
import { timeToLocal } from '$lib/utils';
|
||||
|
||||
function aggregatePriceHistory(priceData: any[], intervalMinutes: number = 60) {
|
||||
if (priceData.length === 0) return [];
|
||||
|
|
@ -19,7 +20,7 @@ function aggregatePriceHistory(priceData: any[], intervalMinutes: number = 60) {
|
|||
|
||||
if (!candlesticks.has(intervalStart)) {
|
||||
candlesticks.set(intervalStart, {
|
||||
time: Math.floor(intervalStart / 1000),
|
||||
time: timeToLocal(Math.floor(intervalStart / 1000)),
|
||||
open: point.price,
|
||||
high: point.price,
|
||||
low: point.price,
|
||||
|
|
@ -87,7 +88,7 @@ function aggregateVolumeData(transactionData: any[], intervalMinutes: number = 6
|
|||
|
||||
if (!volumeMap.has(intervalStart)) {
|
||||
volumeMap.set(intervalStart, {
|
||||
time: Math.floor(intervalStart / 1000),
|
||||
time: timeToLocal(Math.floor(intervalStart / 1000)),
|
||||
volume: 0
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { db } from '$lib/server/db';
|
|||
import { predictionQuestion, user, predictionBet } from '$lib/server/db/schema';
|
||||
import { eq, desc, sum, and, asc } from 'drizzle-orm';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { timeToLocal } from '$lib/utils';
|
||||
|
||||
export const GET: RequestHandler = async ({ params, request }) => {
|
||||
const questionId = parseInt(params.id!);
|
||||
|
|
@ -84,7 +85,7 @@ export const GET: RequestHandler = async ({ params, request }) => {
|
|||
if (probabilityHistory.length > 0) {
|
||||
const firstBetTime = Math.floor(new Date(probabilityHistory[0].createdAt).getTime() / 1000);
|
||||
probabilityData.push({
|
||||
time: firstBetTime - 3600, // 1 hour before first bet
|
||||
time: timeToLocal(firstBetTime - 3600),
|
||||
value: 50
|
||||
});
|
||||
}
|
||||
|
|
@ -100,7 +101,7 @@ export const GET: RequestHandler = async ({ params, request }) => {
|
|||
const yesPercentage = total > 0 ? (runningYesTotal / total) * 100 : 50;
|
||||
|
||||
probabilityData.push({
|
||||
time: Math.floor(new Date(bet.createdAt).getTime() / 1000),
|
||||
time: timeToLocal(Math.floor(new Date(bet.createdAt).getTime() / 1000)),
|
||||
value: Number(yesPercentage.toFixed(1))
|
||||
});
|
||||
}
|
||||
|
|
@ -108,7 +109,7 @@ export const GET: RequestHandler = async ({ params, request }) => {
|
|||
// Add current point if no recent bets
|
||||
if (probabilityData.length > 0) {
|
||||
const lastPoint = probabilityData[probabilityData.length - 1];
|
||||
const currentTime = Math.floor(Date.now() / 1000);
|
||||
const currentTime = timeToLocal(Math.floor(Date.now() / 1000));
|
||||
|
||||
// Only add current point if last bet was more than 1 hour ago
|
||||
if (currentTime - lastPoint.time > 3600) {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@
|
|||
import CoinIcon from '$lib/components/self/CoinIcon.svelte';
|
||||
import { USER_DATA } from '$lib/stores/user-data';
|
||||
import { fetchPortfolioData } from '$lib/stores/portfolio-data';
|
||||
import { getPublicUrl, getTimeframeInSeconds } from '$lib/utils.js';
|
||||
import { getPublicUrl, getTimeframeInSeconds, timeToLocal } from '$lib/utils.js';
|
||||
import { websocketController, type PriceUpdate, isConnectedStore } from '$lib/stores/websocket';
|
||||
import SEO from '$lib/components/self/SEO.svelte';
|
||||
|
||||
|
|
@ -159,12 +159,13 @@
|
|||
const currentTime = Math.floor(Date.now() / 1000);
|
||||
|
||||
const currentCandleTime = Math.floor(currentTime / timeframeSeconds) * timeframeSeconds;
|
||||
const localCandleTime = timeToLocal(currentCandleTime);
|
||||
|
||||
const lastCandle = chartData[chartData.length - 1];
|
||||
|
||||
if (lastCandle && lastCandle.time === currentCandleTime) {
|
||||
if (lastCandle && lastCandle.time === localCandleTime) {
|
||||
const updatedCandle = {
|
||||
time: currentCandleTime,
|
||||
time: localCandleTime,
|
||||
open: lastCandle.open,
|
||||
high: Math.max(lastCandle.high, newPrice),
|
||||
low: Math.min(lastCandle.low, newPrice),
|
||||
|
|
@ -173,9 +174,9 @@
|
|||
|
||||
candlestickSeries.update(updatedCandle);
|
||||
chartData[chartData.length - 1] = updatedCandle;
|
||||
} else if (currentCandleTime > (lastCandle?.time || 0)) {
|
||||
} else if (localCandleTime > (lastCandle?.time || 0)) {
|
||||
const newCandle = {
|
||||
time: currentCandleTime,
|
||||
time: localCandleTime,
|
||||
open: newPrice,
|
||||
high: newPrice,
|
||||
low: newPrice,
|
||||
|
|
|
|||
Reference in a new issue