Skip to content

refactor: Make BlueapiClient logic more modular, split devices and plans - #1667

Merged
oliwenmandiamond merged 17 commits into
mainfrom
Split_client_devices_and_plans
Oct 13, 2026
Merged

oliwenmandiamond merged 17 commits into
mainfrom
Split_client_devices_and_plans

Conversation

@oliwenmandiamond

@oliwenmandiamond oliwenmandiamond commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

BlueapiClient contains all of the logic for the client, devices and plans. This made the code hard to read as the file was very large and relied on circular dependencies.

I have split this logic up so we now have the addition of blueapi.client.protocols.py, blueapi.client.plan_cache.py, and blueapi.client.device_cache.py along with mirrored tests. This breaks the circular dependencies and makes the logic more modular and easier to maintain for future additions.

Additional doc strings and typing have been added to methods and classes.

@oliwenmandiamond
oliwenmandiamond requested a review from a team as a code owner September 11, 2026 09:47
@codecov

codecov Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.36%. Comparing base (ac3e6e5) to head (92ec084).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1667      +/-   ##
==========================================
+ Coverage   96.34%   96.36%   +0.02%     
==========================================
  Files          46       49       +3     
  Lines        3665     3686      +21     
==========================================
+ Hits         3531     3552      +21     
  Misses        134      134              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@oliwenmandiamond oliwenmandiamond changed the title Make Blueapiclient logic more modular, split devices and plans refactor: Make Blueapiclient logic more modular, split devices and plans Sep 11, 2026
@oliwenmandiamond oliwenmandiamond changed the title refactor: Make Blueapiclient logic more modular, split devices and plans refactor: Make BlueapiClient logic more modular, split devices and plans Sep 11, 2026
@tpoliaw

tpoliaw commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

See also changes in #1338. They need updating after recent changes but might solve some of the concerns here.

@oliwenmandiamond

Copy link
Copy Markdown
Contributor Author

See also changes in #1338. They need updating after recent changes but might solve some of the concerns here.

This PR is 9 months old and still needs to be rebased. It also only splits into cache rather than device cache and plan cache. This PR would make it more modular, e.g you could then add fixture cache, suspender cache, baseline cache in future PRs. It is also tied up with all other logic of generating the cache client side.

Does it not make more sense to merge this first and then that can be rebased on this and then that should reduce the size of that PR a lot as well? Then it can focus solely on the logic for generating client side cache rather than splitting logic at the same time?

@ZohebShaikh ZohebShaikh left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

First of all Thank you for the PR.
I do like the test changes you have made.

The only logic that has changed is to remove the redundant name for Plan and DeviceRef as the model.name attribute shares the exact same one.

About this, Can we keep the behaviour as it is, because I think this is used to refer to a device
like bc.devices.stage.x error
If you really want this, Can this be put in a different PR and we can discuss the pros and cons of it there.

There is no integration tests for the scripting interface so I have created this
It might become more useful when scripting is doing a lot more stuff

Comment on lines +9 to +13
class ClientProtocol(Protocol):
def run_task(
self,
task: TaskRequest,
on_event: OnAnyEvent | None = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I didn't get the value in making this a protocol. I'm not against it, just want to know why you have added this. Is this out of habit of Java interfaces ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately I had to do this. The previous code had everything in one file which made it hard to understand the logic. When separating the logic out, their were a lot of circular references to each other. This was one of them. So the only way to break this circular dependency was to create a protocol

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So for example, Plans and PlanCache takes in the Client and the Client has references to the plans. The DeviceCache has reference to the rest client and the rest client had reference to the DeviceCache. So we can break the circular dependencies with Protocols

Comment thread src/blueapi/client/rest.py Resolved
Comment thread src/blueapi/client/protocols.py Resolved
Comment thread src/blueapi/client/protocols.py Resolved
oliwenmandiamond and others added 4 commits October 13, 2026 13:08
Co-authored-by: Zoheb Shaikh <zoheb.shaikh@diamond.ac.uk>
Co-authored-by: Zoheb Shaikh <zoheb.shaikh@diamond.ac.uk>
Co-authored-by: Zoheb Shaikh <zoheb.shaikh@diamond.ac.uk>
@oliwenmandiamond
oliwenmandiamond added this pull request to the merge queue Oct 13, 2026
Merged via the queue into main with commit 91ee209 Oct 13, 2026
20 checks passed
@oliwenmandiamond
oliwenmandiamond deleted the Split_client_devices_and_plans branch October 13, 2026 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.

3 participants