Skip to content

Commit a3be596

Browse files
committed
feat: chart data consistency
1 parent 3c3af57 commit a3be596

File tree

5 files changed

+56
-32
lines changed

5 files changed

+56
-32
lines changed

apps/insights/src/components/PriceFeed/Chart/chart.tsx

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,35 @@ const fetchHistory = async ({ symbol, range, cluster, from, until }: { symbol: s
6464
return fetch(url).then(async (data) => historySchema.parse(await data.json()));
6565
}
6666

67+
// const checkPriceData = (data: {time: UTCTimestamp}[]) => {
68+
// const chartData = [...data].sort((a, b) => a.time - b.time);
69+
// if(chartData.length < 2) {
70+
// return;
71+
// }
72+
// const firstElement = chartData.at(-2);
73+
// const secondElement = chartData.at(-1);
74+
// if(!firstElement || !secondElement ) {
75+
// return;
76+
// }
77+
// const detectedInterval = secondElement.time - firstElement.time
78+
// for(let i = 0; i < chartData.length - 1; i++) {
79+
// const currentElement = chartData[i];
80+
// const nextElement = chartData[i + 1];
81+
// if(!currentElement || !nextElement) {
82+
// return;
83+
// }
84+
// const interval = nextElement.time - currentElement.time
85+
// if(interval !== detectedInterval) {
86+
// console.warn("Price chartData is not consistent", {
87+
// current: currentElement,
88+
// next: nextElement,
89+
// detectedInterval,
90+
// });
91+
// }
92+
// }
93+
// return detectedInterval;
94+
// }
95+
6796
const useChartElem = (symbol: string, feedId: string) => {
6897
const logger = useLogger();
6998
const { current } = useLivePriceData(Cluster.Pythnet, feedId);
@@ -80,9 +109,9 @@ const [interval] = useQueryState(
80109
if (!isBackfilling.current && earliestDateRef.current) {
81110
isBackfilling.current = true;
82111
// seconds to date
83-
console.log("backfilling", new Date(Number(earliestDateRef.current) * 1000));
84112
const range = interval === "Live" ? "1H" : interval;
85-
fetchHistory({ symbol, range, cluster: "pythnet", from: earliestDateRef.current - 100n, until: earliestDateRef.current -2n })
113+
console.log("backfilling", new Date(Number(earliestDateRef.current) * 1000), {from: earliestDateRef.current - 100n, until: earliestDateRef.current});
114+
fetchHistory({ symbol, range, cluster: "pythnet", from: earliestDateRef.current - 100n, until: earliestDateRef.current })
86115
.then((data) => {
87116
const firstPoint = data[0];
88117
if (firstPoint) {
@@ -109,28 +138,28 @@ const [interval] = useQueryState(
109138
value: price,
110139
}))
111140
});
112-
chartRef.current.price.setData([
113-
...convertedData.map(({ time, price }) => ({
141+
const newPriceData = [...convertedData.map(({ time, price }) => ({
114142
time,
115143
value: price,
116144
})),
117-
...chartRef.current.price.data(),
118-
]);
119-
120-
chartRef.current.confidenceHigh.setData([
121-
...convertedData.map(({ time, price, confidence }) => ({
145+
...chartRef.current.price.data(),]
146+
const newConfidenceHighData = [...convertedData.map(({ time, price, confidence }) => ({
122147
time,
123148
value: price + confidence,
124149
})),
125-
...chartRef.current.confidenceHigh.data(),
126-
]);
127-
chartRef.current.confidenceLow.setData([
128-
...convertedData.map(({ time, price, confidence }) => ({
150+
...chartRef.current.confidenceHigh.data(),]
151+
const newConfidenceLowData = [...convertedData.map(({ time, price, confidence }) => ({
152+
time,
153+
value: price - confidence,
154+
})), ...chartRef.current.confidenceLow.data(),]
155+
checkPriceData(convertedData.map(({ time, price }) => ({
129156
time,
130-
value: price - confidence,
131-
})),
132-
...chartRef.current.confidenceLow.data(),
133-
]);
157+
value: price,
158+
})));
159+
console.log(newPriceData)
160+
chartRef.current.price.setData(newPriceData);
161+
chartRef.current.confidenceHigh.setData(newConfidenceHighData);
162+
chartRef.current.confidenceLow.setData(newConfidenceLowData);
134163
}
135164
isBackfilling.current = false;
136165
})

apps/insights/src/services/clickhouse.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,6 @@ export const getHistory = async ({
388388
}) => {
389389

390390
// Calculate interval parameters based on range
391-
const start = (range === "1H" || range === "1D") ? "1" : (range === "1W") ? "7" : "30";
392-
const range_unit = range === "1H" ? "HOUR" : "DAY";
393-
const interval_number = range === "1H" ? 5 : 1;
394391
const interval_unit = range === "1H" ? "SECOND" : (range === "1D") ? "MINUTE" : "HOUR";
395392

396393
let additional_cluster_clause = "";
@@ -400,7 +397,7 @@ export const getHistory = async ({
400397

401398
const query = `
402399
SELECT
403-
toUnixTimestamp(toStartOfInterval(publishTime, INTERVAL {interval_number: UInt32} ${interval_unit})) AS timestamp,
400+
toUnixTimestamp(toStartOfInterval(publishTime, INTERVAL 1 ${interval_unit})) AS timestamp,
404401
argMin(price, slot) AS openPrice,
405402
min(price) AS lowPrice,
406403
argMax(price, slot) AS closePrice,
@@ -418,8 +415,8 @@ export const getHistory = async ({
418415
AND (version = 2)
419416
AND (publishTime > fromUnixTimestamp(toInt64({from: String})))
420417
AND (publishTime <= fromUnixTimestamp(toInt64({until: String})))
421-
AND (time > (fromUnixTimestamp(toInt64({from: String})) - INTERVAL 5 SECOND))
422-
AND (time <= (fromUnixTimestamp(toInt64({until: String})) + INTERVAL 5 SECOND))
418+
AND (time > (fromUnixTimestamp(toInt64({from: String}))))
419+
AND (time <= (fromUnixTimestamp(toInt64({until: String}))))
423420
AND (publisher = {publisher: String})
424421
AND (status = 1)
425422
GROUP BY timestamp
@@ -449,8 +446,6 @@ console.log(query)
449446
base_quote_symbol: symbol.split(".")[-1],
450447
from,
451448
until,
452-
start,
453-
interval_number,
454449
},
455450
});
456451
console.log("from", new Date(from * 1000), from, "until", new Date(until * 1000), until, data[0], data[data.length - 1]);

apps/insights/src/services/pyth/get-feeds.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Cluster, priceFeedsSchema } from ".";
2-
import { getPythMetadataCached } from "./get-metadata";
2+
import { getPythMetadata } from "./get-metadata";
33
import { redisCache } from "../../cache";
44

55
const _getFeeds = async (cluster: Cluster) => {
6-
const unfilteredData = await getPythMetadataCached(cluster);
6+
const unfilteredData = await getPythMetadata(cluster);
77
const filtered = unfilteredData.symbols
88
.filter(
99
(symbol) =>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { clients, Cluster } from ".";
22
import { memoryOnlyCache } from "../../cache";
33

4-
const getPythMetadata = async (cluster: Cluster) => {
4+
const _getPythMetadata = async (cluster: Cluster) => {
55
return clients[cluster].getData();
66
};
77

8-
export const getPythMetadataCached = memoryOnlyCache.define(
8+
export const getPythMetadata = memoryOnlyCache.define(
99
"getPythMetadata",
10-
getPythMetadata,
10+
_getPythMetadata,
1111
).getPythMetadata;

apps/insights/src/services/pyth/get-publishers-for-cluster.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Cluster } from ".";
2-
import { getPythMetadataCached } from "./get-metadata";
2+
import { getPythMetadata } from "./get-metadata";
33
import { redisCache } from "../../cache";
44

55
const _getPublishersForCluster = async (cluster: Cluster) => {
6-
const data = await getPythMetadataCached(cluster);
6+
const data = await getPythMetadata(cluster);
77
const result: Record<string, string[]> = {};
88
for (const key of data.productPrice.keys()) {
99
const price = data.productPrice.get(key);

0 commit comments

Comments
 (0)