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

fix: change data generation according to latest final api spec #59

Merged
merged 1 commit into from
Feb 26, 2025

Conversation

henry-deriv
Copy link
Contributor

@henry-deriv henry-deriv commented Feb 25, 2025

Summary by Sourcery

Updates the data transformation and mock data generation to align with the latest API specification. This includes changes to property names, data types, and the addition of instrument IDs to the transformed data. The changes ensure that the chart data is correctly formatted and displayed according to the API requirements.

Bug Fixes:

  • Fixes data generation and transformation to align with the final API specification, including changes to property names and data types.
  • Fixes the candle close time calculation in the mock data generation.

Enhancements:

  • Adds instrument ID to candle and tick data transformations.
  • Updates the mock data generation to use milliseconds for epoch values.

Copy link

sourcery-ai bot commented Feb 25, 2025

Reviewer's Guide by Sourcery

This pull request updates the data transformation and generation logic to align with the latest API specifications. The changes include modifications to property names, data types, and the structure of the transformed data. Mock data generation and usage have also been updated to reflect these changes.

Updated class diagram for CandleData and TransformedCandle

classDiagram
    class CandleData {
        +open_epoch_ms: string
        +open: string
        +high: string
        +low: string
        +close: string
        +close_epoch_ms: string
    }
    class TransformedCandle {
        +open_time: number
        +open: string
        +high: string
        +low: string
        +close: string
        +epoch: number
    }

    CandleData -- TransformedCandle : transforms to
Loading

Updated class diagram for TickData and TransformedTick

classDiagram
    class TickData {
        +epoch_ms: string
        +ask: string
        +bid: string
        +price: string
    }
    class TransformedTick {
        +epoch: number
        +ask: string
        +bid: string
        +quote: string
    }

    TickData -- TransformedTick : transforms to
Loading

Updated class diagram for TransformedCandleData

classDiagram
    class TransformedCandleDataMultiple {
        +msg_type: string
        +candles: TransformedCandle[]
        +instrument_id: string
    }
    class TransformedCandleDataSingle {
        +msg_type: string
        +ohlc: TransformedCandle
        +instrument_id: string
    }

    TransformedCandleDataMultiple -- TransformedCandle : contains
    TransformedCandleDataSingle -- TransformedCandle : contains
    TransformedCandleDataMultiple --|> TransformedCandleData
    TransformedCandleDataSingle --|> TransformedCandleData
    class TransformedCandleData
    note for TransformedCandleData "Type = TransformedCandleDataMultiple | TransformedCandleDataSingle"
Loading

Updated class diagram for TransformedTickData

classDiagram
    class TransformedTickDataMultiple {
        +msg_type: string
        +instrument_id: string
        +history: TransformedTick[]
    }
    class TransformedTickDataSingle {
        +msg_type: string
        +instrument_id: string
        +tick: TransformedTick
    }

    TransformedTickDataMultiple -- TransformedTick : contains
    TransformedTickDataSingle -- TransformedTick : contains
    TransformedTickDataMultiple --|> TransformedTickData
    TransformedTickDataSingle --|> TransformedTickData
    class TransformedTickData
    note for TransformedTickData "Type = TransformedTickDataMultiple | TransformedTickDataSingle"
Loading

File-Level Changes

Change Details Files
Updated the data transformation functions to align with the latest API specifications, including changes to property names and data types.
  • Renamed openEpochMs to open_epoch_ms and closeEpochMs to close_epoch_ms in the CandleData interface.
  • Renamed epochMs to epoch_ms in the TickData interface.
  • Added instrument_id property to TransformedCandleDataMultiple, TransformedCandleDataSingle, TransformedTickDataMultiple, and TransformedTickDataSingle interfaces.
  • Modified transformCandleData and transformTickData functions to use the new property names and include the instrument_id in the transformed data.
  • Modified the transformCandleData and transformTickData functions to convert epoch milliseconds to epoch seconds.
  • Modified the transformCandleData function to accept the instrument_id as an argument.
src/utils/transformChartData.ts
src/utils/__tests__/transformChartData.test.ts
Modified the mock data generation and usage to align with the updated data structures and API specifications.
  • Updated the mock candle and tick data in useChartData hook to use the new property names (open_epoch_ms, close_epoch_ms, epoch_ms) and string types.
  • Modified the time increment logic in useChartData to increment in milliseconds instead of seconds.
  • Updated the generateHistoricalCandles and generateHistoricalTicks functions to use the new property names and return the instrument_id.
  • Modified the generateHistoricalCandles function to generate epoch milliseconds instead of epoch seconds.
  • Added instrumentId argument to the generateHistoricalCandles function.
src/hooks/useChartData.ts
src/utils/generateHistoricalData.ts
Updated the chart component to use the updated mock data.
  • Removed unused historicalData1 and streamingData1 variables.
  • Removed unused generateHistoricalCandles import.
src/components/Chart/Chart.tsx

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Manifest Files

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @henry-deriv - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a unit test for the transformTickData function to ensure correct transformation of tick data.
  • The openTime ref in useChartData is initialized with Date.now() but later used as seconds - be consistent with milliseconds.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

const transformedCandles = candles.map(candle => ({
open_time: candle.openEpochMs,
const transformedCandles = candles.map((candle) => ({
open_time: Math.floor(parseInt(candle.open_epoch_ms) / 1000),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Specify the radix in parseInt for clarity.

Consider using parseInt(candle.open_epoch_ms, 10) to avoid potential issues with different number formats.

Suggested implementation:

    open_time: Math.floor(parseInt(candle.open_epoch_ms, 10) / 1000),
    epoch: Math.floor(parseInt(candle.close_epoch_ms, 10) / 1000),

const transformedTicks = ticks.map(tick => ({
epoch: Math.floor(tick.epochMs / 1000),
const transformedTicks = ticks.map((tick) => ({
epoch: Math.floor(parseInt(tick.epoch_ms) / 1000),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Include radix parameter when parsing string timestamps.

Changing parseInt(tick.epoch_ms) to parseInt(tick.epoch_ms, 10) would be more explicit and robust.

Suggested change
epoch: Math.floor(parseInt(tick.epoch_ms) / 1000),
epoch: Math.floor(parseInt(tick.epoch_ms, 10) / 1000),

Copy link

@devloai devloai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reviewed the changes adapting the chart data to the latest API spec. The implementation looks solid with proper field name changes and timestamp handling. I've left a few suggestions regarding error handling, unused code cleanup, and type safety improvements. Feel free to reach out if you need any clarification on the comments.

Follow-up suggestion: @devloai please verify the changes after the suggested improvements are implemented.

ohlc: TransformedCandle;
instrument_id: string;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling for parseInt() calls when parsing epoch_ms strings. A malformed string could cause the Math.floor operation to produce NaN. Consider adding a validation or fallback:

Suggested change
instrument_id: string;
open_time: Math.floor(Number.isNaN(parseInt(candle.open_epoch_ms)) ? Date.now() : parseInt(candle.open_epoch_ms) / 1000),

// const data = generateHistoricalCandles(100, 60);
// return transformCandleData(data);
// }, []);
const historicalData1 = useMemo(() => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable historicalData1 and streamingData1 are defined but never used. Either remove them or use them as intended. Keeping unused code makes maintenance harder.

@@ -1,21 +1,28 @@
export const generateHistoricalCandles = (count: number = 100, durationInSeconds: number = 60) => {
const currentTime = Math.floor(Date.now() / 1000); // Convert to seconds
export const generateHistoricalCandles = (
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider importing and using the CandleData and TickData interfaces from transformChartData.ts to ensure type consistency between generated and transformed data.

@henry-deriv henry-deriv merged commit 41c2a41 into master Feb 26, 2025
3 checks passed
@henry-deriv henry-deriv deleted the henry/adapt-to-chart-data-change-v3 branch February 26, 2025 03:29
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

Successfully merging this pull request may close these issues.

1 participant