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