Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertion failed: data must be asc ordered by time. TIME DIPLICATES #1700

Open
Yaroslav-Bulavin opened this issue Sep 23, 2024 · 1 comment

Comments

@Yaroslav-Bulavin
Copy link

Lightweight Charts™ Version: ^4.2.0

Steps/code to reproduce:

From the error I understand that I have the same time property for a few entries:
Assertion failed: data must be asc ordered by time, index=3, time=1727077163.929, prev time=1727077163.929;

I using data from the backend to get coin price history, approximately 400 items in the array.
Array item:

currency_code: "BTC"
high24hr: "64146.2"
id: 1161674
low24hr: "62603.6"
price: "62890.13"
timestamp: "2024-09-20T13:57:28.947Z"
vol24hr: "238901766.70551"

I have an endpoint that saves data in storage:

getCoinPriceHistory: builder.query<GetCoinPriceHistory, { coin: CoinEnum }>(
      {
        query: ({ coin }) => ({
          url: `/prices/${coin}`,
        }),
        async onQueryStarted(id, { dispatch, queryFulfilled }) {
          try {
            const { data } = await queryFulfilled;
            console.log('DUPLICATES', findDuplicateDatesWithTime(data?.prices));
            dispatch(SET_COIN_PRICES(data?.prices || []));
          } catch (err) {}
        },
        providesTags: [ApiTagsEnum.GET_COIN_PRICE_HISTORY],
      },
    ),

I added console.log to find duplications in the response data.
The findDuplicateDatesWithTime function:

function findDuplicateDatesWithTime(data: CurrencyType[]) {
  const duplicates = data.filter((x, i, arr) => {
    return arr.some(
      (y) =>
        new Date(y.timestamp).getTime() / 1000 === new Date(x.timestamp).getTime() / 1000 &&
        y.id !== x.id,
    );
  });
  return duplicates;
}

But it logs me empty array every time(No duplicates have been found). So BE response is valid.
Screenshot 2024-09-23 at 10 59 39

This is how I use timestamp from the response:

const formattedInitPrices = coinPrices.map((info) => ({
      time: (new Date(info.timestamp).getTime() / 1000) as Time,
      value: Number(info?.price),
    }));

The strangest thing is that sometimes it works without any errors after the page refresh, but sometimes it crashes.

Tech stack: react, vite, lightweight-charts, typescript, redux-toolkit

Actual behavior:

Error Assertion failed: data must be asc ordered by time, index=3, time=1727077163.929, prev time=1727077163.929;

Expected behavior:

It should work whenever BE response has unique entries in the array

Screenshots:

CodeSandbox/JSFiddle/etc link:

@Yaroslav-Bulavin Yaroslav-Bulavin changed the title Assertion failed: data must be asc ordered by time. Assertion failed: data must be asc ordered by time. TIME DIPLICATES Sep 23, 2024
@SlicedSilver
Copy link
Contributor

I suspect that your findDuplicateDatesWithTime isn't correctly identifying duplicate timestamps. Any two items which have the same time value would be considered a duplicate even if everything else was different.
In your case, you code is checking if the timestamp values match but also check that the id values match. So perhaps try removing && y.id !== x.id and you'll find the duplicate values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants