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 current block #1170

Merged
merged 6 commits into from
Aug 15, 2024
Merged

Fix current block #1170

merged 6 commits into from
Aug 15, 2024

Conversation

alex-Arc
Copy link
Collaborator

@alex-Arc alex-Arc commented Aug 5, 2024

No description provided.

Copy link
Contributor

coderabbitai bot commented Aug 5, 2024

Walkthrough

The update streamlines the RuntimeState type in runtimeState.ts by removing the _prevCurrentBlock property, thereby simplifying related functions. The load and loadNow functions now accept an optional prevBlock parameter to retain essential timing information. Simultaneously, the broadcastResult function in RuntimeService has been improved to merge clock synchronization logic with new block change conditions. These changes enhance clarity, efficiency, and historical data retention.

Changes

File Change Summary
apps/server/src/stores/runtimeState.ts Removed _prevCurrentBlock from RuntimeState. Updated load and loadNow functions to add a new prevBlock parameter for better timing management. Adjusted roll and introduced loadBlock function for enhanced clarity.
apps/server/src/services/runtime-service/RuntimeService.ts Refactored broadcastResult function to integrate clock update logic with new synchronization conditions based on block changes. Minor formatting change in checkTimerUpdate method.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant RuntimeState
    participant LoadFunctions
    participant RuntimeService

    User->>RuntimeState: Request state load
    RuntimeState->>LoadFunctions: Call load(prevBlock)
    LoadFunctions->>RuntimeState: Preserve timing info
    RuntimeState-->>User: State loaded with timing data

    User->>RuntimeService: Request broadcast result
    RuntimeService->>RuntimeService: Check if block changed
    RuntimeService->>RuntimeService: Update clock if necessary
    RuntimeService-->>User: Result broadcasted
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@alex-Arc alex-Arc requested a review from cpvalente August 5, 2024 19:55
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Outside diff range, codebase verification and nitpick comments (1)
apps/server/src/stores/runtimeState.ts (1)

169-171: Verify the correctness of the new prevBlock parameter.

The prevBlock parameter is not being passed in the load function calls. Ensure that this parameter is correctly passed and utilized in all instances of the load function.

  • apps/server/src/stores/runtimeState.ts: Lines where load is called without prevBlock.
Analysis chain

Verify the correctness of the new prevBlock parameter.

The introduction of the prevBlock parameter appears to be correctly handled. Ensure that this parameter is correctly passed and utilized in the function.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correctness of the new `prevBlock` parameter.

# Test: Search for the function usage. Expect: Correct usage of the new parameter.
rg --type python -A 5 $'load('

Length of output: 92


Script:

#!/bin/bash
# Description: Verify the correctness of the new `prevBlock` parameter.

# Test: Search for the function usage. Expect: Correct usage of the new parameter.
rg --type ts -A 5 'load('

Length of output: 88


Script:

#!/bin/bash
# Description: Verify the correctness of the new `prevBlock` parameter.

# Test: Search for the function usage. Expect: Correct usage of the new parameter.
rg --type ts -A 5 'load\('

Length of output: 13780

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a26aa9e and 688d76e.

Files selected for processing (1)
  • apps/server/src/stores/runtimeState.ts (11 hunks)
Additional context used
Learnings (2)
Common learnings
Learnt from: cpvalente
PR: cpvalente/ontime#1100
File: apps/server/src/stores/runtimeState.ts:195-200
Timestamp: 2024-07-22T18:57:03.714Z
Learning: In the `runtimeState.ts` file, ensure that `runtimeState.currentBlock.block` is not null before accessing its `id` property to prevent potential null reference errors.
apps/server/src/stores/runtimeState.ts (1)
Learnt from: cpvalente
PR: cpvalente/ontime#1100
File: apps/server/src/stores/runtimeState.ts:195-200
Timestamp: 2024-07-22T18:57:03.714Z
Learning: In the `runtimeState.ts` file, ensure that `runtimeState.currentBlock.block` is not null before accessing its `id` property to prevent potential null reference errors.
Additional comments not posted (4)
apps/server/src/stores/runtimeState.ts (4)

215-220: Verify the correctness of the new prevBlock parameter.

The introduction of the prevBlock parameter appears to be correctly handled. Ensure that this parameter is correctly passed and utilized in the function.

Verification successful

The prevBlock parameter is correctly utilized in the loadNow function.

The implementation of the loadNow function includes appropriate handling of the prevBlock parameter. Specifically, the code checks if prevBlock is defined and if its id matches the current block's id, then it uses prevBlock.startedAt to maintain the startedAt time.

  • File: apps/server/src/stores/runtimeState.ts
    • Lines:
      • 236: if (prevBlock && prevBlock.id === runtimeState.currentBlock.block?.id) {
      • 237: runtimeState.currentBlock.startedAt = prevBlock.startedAt;
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correctness of the new `prevBlock` parameter in `loadNow`.

# Test: Search for the function usage. Expect: Correct usage of the new parameter.
rg --type python -A 5 $'loadNow('

Length of output: 101


Script:

#!/bin/bash
# Description: Verify the correctness of the new `prevBlock` parameter in `loadNow`.

# Test: Search for the function usage. Expect: Correct usage of the new parameter.
rg --type typescript -A 5 'loadNow('

Length of output: 105


Script:

#!/bin/bash
# Description: Verify the correctness of the new `prevBlock` parameter in `loadNow`.

# Test: Search for the function usage. Expect: Correct usage of the new parameter.
rg --type typescript -A 5 'loadNow'

Length of output: 70


Script:

#!/bin/bash
# Description: Verify the correctness of the new `prevBlock` parameter in `loadNow`.

# Test: Search for the function usage. Expect: Correct usage of the new parameter.
rg --type ts -A 5 'loadNow'

Length of output: 1810


Script:

#!/bin/bash
# Description: Verify the implementation of the `loadNow` function for the correct usage of the `prevBlock` parameter.

# Test: Search for the `loadNow` function implementation. Expect: Correct usage of the `prevBlock` parameter.
ast-grep --lang ts --pattern $'function loadNow($_, $_, $_, $_) {
  $$$
}'

Length of output: 3852


375-377: Verify the correctness of the updated loadNow function usage.

The usage of the updated loadNow function appears to be correctly handled. Ensure that this function is correctly passed and utilized.


210-211: Verify the correctness of the loadNext function usage.

The usage of the loadNext function appears to be correctly handled. Ensure that this function is correctly passed and utilized.

Verification successful

The loadNext function is correctly used in the codebase.

The function is defined in apps/server/src/stores/runtimeState.ts and is utilized in multiple locations, including RuntimeService.ts and integration.controller.ts. The usage appears to be consistent and correct.

  • apps/server/src/stores/runtimeState.ts
  • apps/server/src/services/runtime-service/RuntimeService.ts
  • apps/server/src/api-integration/integration.controller.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correctness of the `loadNext` function usage in `loadNow`.

# Test: Search for the function usage. Expect: Correct usage of the function.
rg --type python -A 5 $'loadNext('

Length of output: 104


Script:

#!/bin/bash
# Description: Verify the correctness of the `loadNext` function usage in `loadNow`.

# Test: Search for the function usage. Expect: Correct usage of the function.
rg --type js -A 5 'loadNext\('

Length of output: 32


Script:

#!/bin/bash
# Description: Find the definition of the `loadNext` function and trace its usage in the codebase.

# Search for the definition of the `loadNext` function
rg --type js 'function loadNext' -A 5

# Search for the usage of the `loadNext` function
rg --type js 'loadNext' -A 5

Length of output: 68


Script:

#!/bin/bash
# Description: Search for any occurrence of `loadNext` in the codebase.

# Search for any occurrence of `loadNext`
rg 'loadNext' -A 5

Length of output: 5091


614-617: Verify the correctness of the new prevBlock parameter.

The introduction of the prevBlock parameter appears to be correctly handled. Ensure that this parameter is correctly passed and utilized in the function.

apps/server/src/stores/runtimeState.ts Outdated Show resolved Hide resolved
apps/server/src/stores/runtimeState.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 688d76e and 47de5b2.

Files selected for processing (1)
  • apps/server/src/stores/runtimeState.ts (11 hunks)
Additional context used
Learnings (2)
Common learnings
Learnt from: cpvalente
PR: cpvalente/ontime#1100
File: apps/server/src/stores/runtimeState.ts:195-200
Timestamp: 2024-07-22T18:57:03.714Z
Learning: In the `runtimeState.ts` file, ensure that `runtimeState.currentBlock.block` is not null before accessing its `id` property to prevent potential null reference errors.
apps/server/src/stores/runtimeState.ts (1)
Learnt from: cpvalente
PR: cpvalente/ontime#1100
File: apps/server/src/stores/runtimeState.ts:195-200
Timestamp: 2024-07-22T18:57:03.714Z
Learning: In the `runtimeState.ts` file, ensure that `runtimeState.currentBlock.block` is not null before accessing its `id` property to prevent potential null reference errors.
Biome
apps/server/src/stores/runtimeState.ts

[error] 663-663: Unexpected label.

Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.

(lint/suspicious/noConfusingLabels)


[error] 663-663: Unused label.

The label is not used by any break statement and continue statement.
Safe fix: Remove the unused label.

(lint/correctness/noUnusedLabels)

Additional comments not posted (2)
apps/server/src/stores/runtimeState.ts (2)

169-170: Ensure runtimeState.currentBlock.block is not null before accessing its id.

To prevent potential null reference errors, ensure that runtimeState.currentBlock.block is not null before accessing its id property.

-  const prevBlockId = runtimeState.currentBlock.block?.id;
-  const prevBlockStartAt = runtimeState.currentBlock.startedAt;
+  const prevBlockId = runtimeState.currentBlock.block ? runtimeState.currentBlock.block.id : undefined;
+  const prevBlockStartAt = runtimeState.currentBlock.block ? runtimeState.currentBlock.startedAt : null;
Skipped due to learnings
Learnt from: cpvalente
PR: cpvalente/ontime#1100
File: apps/server/src/stores/runtimeState.ts:195-200
Timestamp: 2024-07-22T18:57:03.714Z
Learning: In the `runtimeState.ts` file, ensure that `runtimeState.currentBlock.block` is not null before accessing its `id` property to prevent potential null reference errors.

596-597: Ensure runtimeState.currentBlock.block is not null before accessing its id.

To prevent potential null reference errors, ensure that runtimeState.currentBlock.block is not null before accessing its id property.

-  const prevBlockId = runtimeState.currentBlock.block?.id;
-  const prevBlockStartAt = runtimeState.currentBlock.startedAt;
+  const prevBlockId = runtimeState.currentBlock.block ? runtimeState.currentBlock.block.id : undefined;
+  const prevBlockStartAt = runtimeState.currentBlock.block ? runtimeState.currentBlock.startedAt : null;
Skipped due to learnings
Learnt from: cpvalente
PR: cpvalente/ontime#1100
File: apps/server/src/stores/runtimeState.ts:195-200
Timestamp: 2024-07-22T18:57:03.714Z
Learning: In the `runtimeState.ts` file, ensure that `runtimeState.currentBlock.block` is not null before accessing its `id` property to prevent potential null reference errors.

apps/server/src/stores/runtimeState.ts Outdated Show resolved Hide resolved
apps/server/src/stores/runtimeState.ts Outdated Show resolved Hide resolved
apps/server/src/stores/runtimeState.ts Outdated Show resolved Hide resolved
apps/server/src/stores/runtimeState.ts Outdated Show resolved Hide resolved
apps/server/src/stores/runtimeState.ts Show resolved Hide resolved
apps/server/src/stores/runtimeState.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 47de5b2 and 5a3b221.

Files selected for processing (3)
  • apps/server/src/services/runtime-service/RuntimeService.ts (1 hunks)
  • apps/server/src/stores/runtimeState.ts (12 hunks)
  • packages/utils/src/rundown-utils/rundownUtils.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • apps/server/src/services/runtime-service/RuntimeService.ts
Additional context used
Learnings (2)
Common learnings
Learnt from: cpvalente
PR: cpvalente/ontime#1100
File: apps/server/src/stores/runtimeState.ts:195-200
Timestamp: 2024-07-22T18:57:03.714Z
Learning: In the `runtimeState.ts` file, ensure that `runtimeState.currentBlock.block` is not null before accessing its `id` property to prevent potential null reference errors.
apps/server/src/stores/runtimeState.ts (1)
Learnt from: cpvalente
PR: cpvalente/ontime#1100
File: apps/server/src/stores/runtimeState.ts:195-200
Timestamp: 2024-07-22T18:57:03.714Z
Learning: In the `runtimeState.ts` file, ensure that `runtimeState.currentBlock.block` is not null before accessing its `id` property to prevent potential null reference errors.
Additional comments not posted (7)
packages/utils/src/rundown-utils/rundownUtils.ts (1)

306-320: Improved variable naming enhances readability.

The renaming of inBlock to foundCurrentEvent clarifies the purpose of the variable, improving the readability of the getRelevantBlock function. The logic remains intact.

apps/server/src/stores/runtimeState.ts (6)

169-172: Preserving block state across loads is a good practice.

The changes ensure that the current block state is preserved across loads, which is essential for maintaining accurate timing information.


169-172: Enhanced function signature improves timing management.

The addition of the prevBlock parameter to loadNow allows for better management of block timing, improving clarity and functionality.


360-360: Consistent use of updated functions enhances state management.

The integration of loadNow and loadBlock in reloadAll ensures consistent state management and event loading.


384-384: Conditional block start time update is efficient.

Updating the block start time only when necessary prevents unnecessary state changes, improving efficiency.


597-601: Streamlined rolling process with accurate state management.

Persisting the current block state and using updated functions in roll ensures a streamlined process and accurate state management.


659-675: Correct block state update based on current event.

The loadBlock function correctly updates the block state based on the current event, ensuring consistency and accuracy.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5a3b221 and ad419b2.

Files selected for processing (1)
  • apps/server/src/services/runtime-service/RuntimeService.ts (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • apps/server/src/services/runtime-service/RuntimeService.ts

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ad419b2 and 48e5ff8.

Files selected for processing (2)
  • apps/server/src/services/runtime-service/RuntimeService.ts (3 hunks)
  • apps/server/src/stores/runtimeState.ts (12 hunks)
Files skipped from review as they are similar to previous changes (1)
  • apps/server/src/services/runtime-service/RuntimeService.ts
Additional context used
Learnings (2)
Common learnings
Learnt from: cpvalente
PR: cpvalente/ontime#1100
File: apps/server/src/stores/runtimeState.ts:195-200
Timestamp: 2024-07-22T18:57:03.714Z
Learning: In the `runtimeState.ts` file, ensure that `runtimeState.currentBlock.block` is not null before accessing its `id` property to prevent potential null reference errors.
apps/server/src/stores/runtimeState.ts (1)
Learnt from: cpvalente
PR: cpvalente/ontime#1100
File: apps/server/src/stores/runtimeState.ts:195-200
Timestamp: 2024-07-22T18:57:03.714Z
Learning: In the `runtimeState.ts` file, ensure that `runtimeState.currentBlock.block` is not null before accessing its `id` property to prevent potential null reference errors.
Additional comments not posted (5)
apps/server/src/stores/runtimeState.ts (5)

169-172: Verify the persistence logic for currentBlock.

The logic to persist the currentBlock state across loads by copying it to prevCurrentBlock seems correct. Ensure this change does not introduce unintended side effects.

Verification successful

Persistence logic for currentBlock is correctly implemented.

The logic to persist the currentBlock state across loads by copying it to prevCurrentBlock and restoring it appears to be correctly implemented. No unintended side effects were identified in the current usage.

  • Lines where currentBlock is copied and restored:
    • const prevCurrentBlock = { ...runtimeState.currentBlock };
    • runtimeState.currentBlock = prevCurrentBlock;
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the persistence logic for `currentBlock` in the `load` function.

# Test: Check for any potential side effects introduced by the persistence logic.
rg --type-add 'typescript' --type typescript 'runtimeState.currentBlock' -A 5

Length of output: 135


Script:

#!/bin/bash
# Description: Verify the persistence logic for `currentBlock` in the `load` function.

# Test: Check for any potential side effects introduced by the persistence logic.
rg 'runtimeState\.currentBlock' -A 5

Length of output: 3264


659-675: Verify the logic in the new loadBlock function.

The loadBlock function centralizes block loading logic effectively. Ensure the logic for updating currentBlock is correct and doesn't introduce errors.

Verification successful

The loadBlock function logic is correctly implemented.

The loadBlock function is effectively managing the current block state and is invoked appropriately within the application logic. The function's integration with state management and timers appears correct and does not introduce errors.

  • The function is called in contexts that ensure state updates are consistent with application requirements.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the logic in the new `loadBlock` function.

# Test: Check for any potential errors introduced by the new logic.
rg --type typescript 'loadBlock' -A 5

Length of output: 72


Script:

#!/bin/bash
# Description: Search for the usage of the `loadBlock` function across the codebase.

# Test: Find occurrences of the `loadBlock` function to verify its logic.
rg 'loadBlock' -A 10

Length of output: 3112


Line range hint 207-207:
Verify the function signature change for loadNow.

The function signature change to include parameters for the previous block's id and startedAt values seems appropriate. Ensure all calls to loadNow are updated to match the new signature.


384-384: Verify the update logic for currentBlock start time in start.

The logic to update the currentBlock start time in the start function appears correct. Ensure this update doesn't conflict with other parts of the code.


597-607: Verify the integration of loadBlock in the roll function.

The integration of loadBlock in the roll function seems to improve clarity and efficiency. Ensure this integration is seamless and doesn't introduce errors.

Copy link
Owner

@cpvalente cpvalente left a comment

Choose a reason for hiding this comment

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

small suggestion, thank you alex!

}

const shouldUpdateClock = getShouldClockUpdate(RuntimeService.previousClockUpdate, state.clock) || syncBlockStartAt;
Copy link
Owner

Choose a reason for hiding this comment

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

tip: how about having syncBlockStartAt first? if that is true, we want dont need to run the function getShouldClockUpdate

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 48e5ff8 and e7b21a2.

Files selected for processing (1)
  • apps/server/src/services/runtime-service/RuntimeService.ts (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • apps/server/src/services/runtime-service/RuntimeService.ts

@alex-Arc alex-Arc merged commit f708dc0 into master Aug 15, 2024
3 checks passed
@alex-Arc alex-Arc deleted the fix-current-block branch August 15, 2024 14:25
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.

2 participants