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

refactor: added field for supply-change endpoint #41

Merged
merged 1 commit into from
Jun 24, 2024

Conversation

yevhen-burkovskyi
Copy link
Contributor

No description provided.

Copy link

coderabbitai bot commented Jun 24, 2024

Walkthrough

This update introduces a new caching system for supply change data using Redis. A new environment variable, SUPPLY_CHANGE_CACHE_TTL, is added to manage cache timeouts. Updates to the SupplyService and SupplyCache classes include methods for setting and getting cached supply changes and calculations for caching mechanisms. Configuration changes ensure the correct types and settings are applied throughout the system.

Changes

Files Change Summary
.env.example Added SUPPLY_CHANGE_CACHE_TTL variable with a value of 120 for cache time in minutes.
src/core/config/config.dto.ts Added supplyChange property of type number to CacheConfig interface.
src/core/config/config.schema.ts Converted import statement to double quotes; added SUPPLY_CHANGE_CACHE_TTL with a required number type to ConfigSchema.
src/core/config/config.ts Updated import to double quotes; added supplyChange to the cache section of config object with process environment variable.
src/modules/supply/services/supply.cache.ts Added setSupplyChange and getSupplyChange methods; updated createRedisKey to use variadic arguments; added a private changePrefix property.
src/modules/supply/services/supply.service.ts Updated getSupplyChange to include caching logic; cacheSupplyChange updated to calculate, update cache, and return supply change based on past and current data.

Sequence Diagram(s)

sequenceDiagram
    actor User
    participant SupplyService
    participant SupplyCache
    participant Redis
    User ->> SupplyService: getSupplyChange(range)
    SupplyService ->> SupplyCache: getSupplyChange(range)
    SupplyCache ->> Redis: Fetch supply change data
    Redis -->> SupplyCache: Return cached data (if exists)
    SupplyCache -->> SupplyService: Supply change data or null
    alt Cache Hit
        SupplyService -->> User: Return cached supply change
    else Cache Miss
        SupplyService ->> SupplyService: Calculate supply change
        SupplyService ->> SupplyCache: setSupplyChange(range, data)
        SupplyCache ->> Redis: Store supply change data
        SupplyService -->> User: Return calculated supply change
    end
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 Configration 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.

Copy link

@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 df96ca1 and a729610.

Files selected for processing (6)
  • .env.example (1 hunks)
  • src/core/config/config.dto.ts (1 hunks)
  • src/core/config/config.schema.ts (2 hunks)
  • src/core/config/config.ts (3 hunks)
  • src/modules/supply/services/supply.cache.ts (2 hunks)
  • src/modules/supply/services/supply.service.ts (1 hunks)
Files skipped from review due to trivial changes (4)
  • .env.example
  • src/core/config/config.dto.ts
  • src/core/config/config.schema.ts
  • src/core/config/config.ts
Additional comments not posted (7)
src/modules/supply/services/supply.cache.ts (4)

11-11: Initialization of changePrefix property.

The addition of the changePrefix property to manage keys specifically for supply changes is a good practice as it helps in distinguishing different types of cached data.


26-28: Ensure proper use of TTL in setSupplyChange method.

The method correctly sets the TTL for caching supply change data, which aligns with the new configuration parameter SUPPLY_CHANGE_CACHE_TTL. This should help in managing cache expiration effectively.


30-38: Review of getSupplyChange method for handling cache retrieval.

This method properly checks for the existence of serialized data and returns null if not found, which is a good practice for cache handling. The JSON parsing is done safely with a type assertion, which is also commendable.


40-41: Review of createRedisKey method.

The method uses a functional approach to concatenate IDs with a prefix, which is a clean and efficient way to generate keys for Redis operations.

src/modules/supply/services/supply.service.ts (3)

130-137: Review of caching logic in getSupplyChange.

The method effectively checks the cache before computing the supply change, which is a good performance optimization. The use of null to indicate the absence of cache data is clear and follows common practices.


139-157: Review of cacheSupplyChange method.

This method is well-implemented with clear logic for computing supply changes. It handles both the scenario where previous supply data exists and when it does not, ensuring robustness. The use of Big for arithmetic operations is appropriate to avoid precision errors with floating-point calculations.


Line range hint 204-228: Review of calculatePastDateByRange and its usage.

This method provides a flexible way to compute past dates based on different ranges, which is crucial for the functionality of this service. The implementation is straightforward and uses standard date manipulation techniques.

@yevhen-burkovskyi yevhen-burkovskyi merged commit 5e1cb6b into main Jun 24, 2024
11 checks passed
@yevhen-burkovskyi yevhen-burkovskyi deleted the refactor/supply-change branch June 24, 2024 13:56
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