Skip to content

⚡ Bolt: Redundant GSP Lookup Optimization#183

Open
Moohan wants to merge 2 commits intomainfrom
bolt-redundant-gsp-optimization-6366230867969032865
Open

⚡ Bolt: Redundant GSP Lookup Optimization#183
Moohan wants to merge 2 commits intomainfrom
bolt-redundant-gsp-optimization-6366230867969032865

Conversation

@Moohan
Copy link
Owner

@Moohan Moohan commented Mar 12, 2026

This change introduces a performance optimization to the octopusR package by reducing the number of API calls made during common operations. Specifically, the internal get_meter_details function previously always performed a Grid Supply Point (GSP) lookup via an additional API request. Since the GSP is often not required (e.g., when fetching consumption data), this was redundant.

Key changes:

  • Added include_gsp parameter to get_meter_details.
  • Updated get_consumption to skip GSP lookup.
  • Updated get_meter_gsp's default argument to skip redundant GSP lookup while resolving the MPAN.
  • Updated testing_meter helper to support the new parameter.
  • Documentation updates and in-code comments explaining the optimization.

Performance Impact:
Reduces network overhead by one API request per get_consumption or get_meter_gsp call. Verification showed call counts reduced from 2 to 1 in these scenarios.


PR created automatically by Jules for task 6366230867969032865 started by @Moohan

Summary by Sourcery

Optimize meter detail lookups to avoid unnecessary Grid Supply Point (GSP) API calls when only MPAN and serial number are required.

Enhancements:

  • Add an include_gsp flag to get_meter_details to optionally skip fetching the GSP for electricity meters.
  • Propagate the include_gsp flag through the testing_meter helper to support test scenarios without triggering GSP lookups.
  • Update get_consumption to request meter details without GSP data, reducing redundant network calls.
  • Adjust get_meter_gsp to use get_meter_details with include_gsp disabled when resolving the default MPAN argument.

Documentation:

  • Document the include_gsp parameter and record the optimization rationale in the internal Bolt notes.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added include_gsp parameter to meter detail functions, allowing optional control of Grid Supply Point data retrieval.
  • Documentation

    • Updated examples for meter detail function usage to reflect new parameter option.
  • Chores

    • Updated build configuration files to ignore additional hidden directories.

- Optimized internal `get_meter_details` by adding `include_gsp` parameter to skip expensive API calls when not needed.
- Updated `get_consumption` and `get_meter_gsp` to utilize the optimization.
- Updated documentation and internal helpers.
- Verified performance improvement and package stability with `rcmdcheck`.

Co-authored-by: Moohan <5982260+Moohan@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 12, 2026

Reviewer's Guide

Adds an include_gsp flag to the internal get_meter_details helper and propagates it through consumers so they can skip unnecessary Grid Supply Point (GSP) lookups, thereby reducing API calls for common operations like get_consumption and get_meter_gsp.

Sequence diagram for optimized consumption and GSP lookup

sequenceDiagram
  actor RUser
  participant get_consumption
  participant get_meter_details
  participant get_meter_gsp
  participant OctopusAPI

  RUser->>get_consumption: get_consumption(meter_type, direction, mpan_mprn = NULL, serial_number = NULL)
  alt mpan_mprn or serial_number missing
    get_consumption->>get_meter_details: get_meter_details(meter_type, direction, include_gsp = FALSE)
    alt is_testing()
      get_meter_details-->>get_consumption: testing meter details (gsp = NA)
    else not testing
      get_meter_details->>OctopusAPI: GET meter details (mpan_mprn, serial_number)
      OctopusAPI-->>get_meter_details: meter details (mpan_mprn, serial_number)
      note right of get_meter_details: include_gsp = FALSE so no GSP lookup
      get_meter_details-->>get_consumption: meter details (gsp = NA)
    end
    get_consumption-->>RUser: consumption data (no extra GSP API call)
  end

  RUser->>get_meter_gsp: get_meter_gsp(mpan = missing)
  alt mpan argument missing
    get_meter_gsp->>get_meter_details: get_meter_details("electricity", direction = NULL, include_gsp = FALSE)
    get_meter_details->>OctopusAPI: GET meter details (mpan_mprn, serial_number)
    OctopusAPI-->>get_meter_details: meter details (mpan_mprn, serial_number)
    note right of get_meter_details: GSP not looked up here
    get_meter_details-->>get_meter_gsp: meter details (gsp = NA)
    get_meter_gsp->>OctopusAPI: GET GSP for mpan
    OctopusAPI-->>get_meter_gsp: GSP value
    get_meter_gsp-->>RUser: GSP
  else mpan argument provided
    get_meter_gsp->>OctopusAPI: GET GSP for mpan
    OctopusAPI-->>get_meter_gsp: GSP value
    get_meter_gsp-->>RUser: GSP
  end
Loading

File-Level Changes

Change Details Files
Parameterize get_meter_details with an include_gsp flag and gate GSP lookups on it, including in test-mode helpers.
  • Extend get_meter_details signature with an include_gsp boolean parameter defaulting to TRUE.
  • Update get_meter_details implementation to compute meter_gsp only when meter_type is electricity and include_gsp is TRUE, otherwise default to NA_character_.
  • Pass include_gsp through to testing_meter when in testing mode.
  • Update testing_meter to accept include_gsp, and only perform the GSP lookup or test stub mapping when include_gsp is TRUE, otherwise set GSP to NA_character_.
R/meter_details.R
Ensure callers that only need MPAN/serial skip redundant GSP lookups by using include_gsp = FALSE.
  • Change get_consumption to call get_meter_details with include_gsp = FALSE when inferring meter details.
  • Change get_meter_gsp default mpan argument to use get_meter_details("electricity", include_gsp = FALSE) so that retrieving the MPAN does not itself trigger a GSP lookup.
R/get_consumption.R
R/get_meter_gsp.R
man/get_meter_gsp.Rd
Document the optimization and capture it in project automation metadata.
  • Add roxygen documentation for the new include_gsp parameter on get_meter_details, including rationale about avoiding an extra API call.
  • Add a Bolt note summarizing the redundant GSP lookup optimization and its impact.
R/meter_details.R
.jules/bolt.md

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!

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 12, 2026

Warning

Rate limit exceeded

@Moohan has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 2 minutes and 34 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1438de3e-5e76-46d5-a8fb-7386c2337f2b

📥 Commits

Reviewing files that changed from the base of the PR and between 7de8d6f and 0cb57e4.

📒 Files selected for processing (1)
  • R/get_consumption.R

Walkthrough

The pull request introduces an include_gsp parameter to the get_meter_details function (default TRUE) to allow callers to skip unnecessary Grid Supply Point (GSP) API requests. The parameter is propagated through call sites in get_consumption and get_meter_gsp, with conditional logic added to skip GSP retrieval when the flag is FALSE, reducing network overhead.

Changes

Cohort / File(s) Summary
Build Configuration
.Rbuildignore
Added new ignore pattern ^\.jules$ to exclude Jules-related directory from build process.
Documentation
.jules/bolt.md, man/get_meter_gsp.Rd
Updated documentation to describe the new include_gsp parameter and its performance benefits; revised usage example in get_meter_gsp.Rd to demonstrate the parameter in action.
Function Implementations
R/meter_details.R
Extended get_meter_details and testing_meter signatures with include_gsp parameter (default TRUE); added conditional logic to skip GSP API call when include_gsp = FALSE, otherwise sets gsp to NA.
Call Sites
R/get_consumption.R, R/get_meter_gsp.R
Updated function calls to get_meter_details to explicitly pass include_gsp = FALSE, preventing redundant GSP lookups at these call sites.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main performance optimization: making GSP lookups optional to eliminate redundant API calls, which is the primary objective of the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bolt-redundant-gsp-optimization-6366230867969032865

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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 - I've left some high level feedback:

  • The include_gsp handling logic is duplicated between get_meter_details() and testing_meter(); consider extracting the GSP resolution into a small internal helper to keep behavior consistent and reduce maintenance overhead.
  • For non-electricity meters the new include_gsp parameter is effectively a no-op; it might be worth explicitly short-circuiting or documenting that include_gsp is only meaningful for meter_type == "electricity" to avoid confusion for future callers.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `include_gsp` handling logic is duplicated between `get_meter_details()` and `testing_meter()`; consider extracting the GSP resolution into a small internal helper to keep behavior consistent and reduce maintenance overhead.
- For non-electricity meters the new `include_gsp` parameter is effectively a no-op; it might be worth explicitly short-circuiting or documenting that `include_gsp` is only meaningful for `meter_type == "electricity"` to avoid confusion for future callers.

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.

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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.jules/bolt.md:
- Line 1: Add a top-level H1 heading at the very top of the file to satisfy
markdownlint (MD041); for example insert a single line starting with "#" above
the existing "## 2025-05-15..." entry so the file begins with a document-level
heading while keeping the existing content (references: get_meter_details,
include_gsp, get_consumption) intact.

In `@R/meter_details.R`:
- Around line 160-163: The testing_meter() helper currently returns a different
shape for gas meters than production because get_meter_details() always includes
a gsp field; update testing_meter() so both branches (electricity and gas)
include the same gsp element (and any nested octopus_meter-point structure) as
get_meter_details() does — locate the gas branch inside testing_meter() and add
the missing gsp key/value (matching the shape and keys used by
get_meter_details() and the octopus_meter-point structure) so tests mirror
production.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 83fb95dc-6735-4d75-bd07-b7763af3b601

📥 Commits

Reviewing files that changed from the base of the PR and between 74f7003 and 7de8d6f.

📒 Files selected for processing (6)
  • .Rbuildignore
  • .jules/bolt.md
  • R/get_consumption.R
  • R/get_meter_gsp.R
  • R/meter_details.R
  • man/get_meter_gsp.Rd

@@ -0,0 +1 @@
## 2025-05-15 - Redundant GSP Lookup Optimization **Learning:** Internal helper `get_meter_details` was performing an expensive API call to retrieve the Grid Supply Point (GSP) even when only MPAN/Serial identification was needed. **Action:** Introduced `include_gsp` flag (default `TRUE`) in `get_meter_details` to allow callers like `get_consumption` to skip redundant secondary API calls, reducing network overhead by one request per call.
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Add a top-level heading to satisfy markdownlint.

Line 1 starts with an H2-style heading, which triggers MD041. Please add a file-level # ... heading above this entry.

🧰 Tools
🪛 markdownlint-cli2 (0.21.0)

[warning] 1-1: First line in a file should be a top-level heading

(MD041, first-line-heading, first-line-h1)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.jules/bolt.md at line 1, Add a top-level H1 heading at the very top of the
file to satisfy markdownlint (MD041); for example insert a single line starting
with "#" above the existing "## 2025-05-15..." entry so the file begins with a
document-level heading while keeping the existing content (references:
get_meter_details, include_gsp, get_consumption) intact.

Comment on lines +160 to +163
testing_meter <- function(
meter_type = c("electricity", "gas"),
include_gsp = TRUE
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Keep testing_meter() aligned with the production object shape.

get_meter_details() now always includes gsp in the non-testing path, but the gas branch in testing_meter() still omits it. That means tests see a different octopus_meter-point shape from production for gas meters.

Suggested fix
   } else if (meter_type == "gas") {
     mprn <- safe_decrypt(
       "z-BpI17a6UVNWT8ByPzue_XI5j2zU547vi0",
       "sk_test_mprn"
     )
     serial_number <- safe_decrypt(
       "d06raLRtC5JWyQkh64mZOtWFDOUCQlojLAyfMUk-",
       "sk_test_serial"
     )

     structure(
       list(
         type = "gas",
         mpan_mprn = mprn,
-        serial_number = serial_number
+        serial_number = serial_number,
+        direction = NULL,
+        gsp = NA_character_
       ),
       class = "octopus_meter-point"
     )
   }

Also applies to: 176-183

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@R/meter_details.R` around lines 160 - 163, The testing_meter() helper
currently returns a different shape for gas meters than production because
get_meter_details() always includes a gsp field; update testing_meter() so both
branches (electricity and gas) include the same gsp element (and any nested
octopus_meter-point structure) as get_meter_details() does — locate the gas
branch inside testing_meter() and add the missing gsp key/value (matching the
shape and keys used by get_meter_details() and the octopus_meter-point
structure) so tests mirror production.

- Optimized internal `get_meter_details` by adding `include_gsp` parameter to skip expensive API calls when not needed.
- Updated `get_consumption` and `get_meter_gsp` to utilize the optimization.
- Fixed line length linting issue in `R/get_consumption.R`.
- Updated documentation and internal helpers.
- Verified performance improvement and package stability with `rcmdcheck`.

Co-authored-by: Moohan <5982260+Moohan@users.noreply.github.com>
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