Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
44a4a50
chore(server): bump version to 1.9.5
anderslindho May 8, 2026
0e05e45
refactor(server): extract recceiver/cf model and config
anderslindho May 8, 2026
f9a9438
refactor(server): collapse module-level functions into CFProcessor
anderslindho May 8, 2026
cce817f
refactor(server): introduce ChannelFinderAdapter, move mock to tests
anderslindho May 8, 2026
586ba2d
refactor(server): move CFProcessor to recceiver/cf, delete cfstore.py
anderslindho May 8, 2026
531c649
refactor(server): clean up cf package: direct imports, f-string, docs…
anderslindho May 8, 2026
3eac508
refactor(server): split adapter query methods by purpose
anderslindho May 8, 2026
1016566
refactor(server): reduce cognitive complexity in processor.py
anderslindho May 8, 2026
e378394
ci(server): generate and upload unit test coverage report
anderslindho May 8, 2026
075f766
fix(server): clean up unused params, dead code, and style in cf package
anderslindho May 8, 2026
e36d2eb
test(server): add unit tests for cf model types
anderslindho May 8, 2026
5b88bec
test(server): cover CFProcessor commit logic with mock adapter
anderslindho May 11, 2026
997e878
refactor(server): convert CFPropertyName and PVStatus to plain enums
anderslindho May 11, 2026
19d2bd4
refactor(server): drop all CFProperty factory methods
anderslindho May 11, 2026
b55c3fa
refactor(server): drop @runtime_checkable from ChannelFinderAdapter
anderslindho May 11, 2026
9f34132
refactor(server): split test_cfstore into test_config and test_processor
anderslindho May 11, 2026
d579ec3
refactor(server): accept List[str] in find_by_names, move chunking in…
anderslindho May 11, 2026
9166abf
refactor(server): rename get_all_properties to get_property_names, re…
anderslindho May 11, 2026
f0d3a8f
refactor(server): rename IocInfo to IOCInfo, ioc_id to id
anderslindho May 11, 2026
fada0da
refactor(server): consolidate test fixtures into conftest hierarchy
anderslindho May 11, 2026
6ea5a5a
fix(server): address review findings from branch audit
anderslindho May 11, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,21 @@ jobs:
- name: Test unit tests
run: |
set -o pipefail
pytest tests/unit -v 2>&1 | tee pytest-unit.log
pytest tests/unit -v --cov=recceiver --cov-report=xml:coverage.xml 2>&1 | tee pytest-unit.log
- name: Upload test log
if: always()
uses: actions/upload-artifact@v4
with:
name: pytest-unit-log
path: server/pytest-unit.log
retention-days: 14
- name: Upload coverage report
if: always()
uses: actions/upload-artifact@v4
with:
name: coverage-xml
path: server/coverage.xml
retention-days: 14

test-integration:
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ requires = [ "setuptools" ]

[project]
name = "recceiver"
version = "1.9.3"
version = "1.9.5"
description = """\
recCeiver is a server component of the recsync protocol. It receives record updates from recsync clients (e.g., \
recCasters) and forwards them to a configurable backend such as ChannelFinder.\
Expand Down Expand Up @@ -36,11 +36,11 @@ dependencies = [
"twisted>=22.10,<23; python_version<'3.8'",
"twisted>=24.11,<24.12; python_version>='3.8'",
]
optional-dependencies.test = [ "pytest>=8.3,<8.4", "testcontainers>=4.8.2,<4.9" ]
optional-dependencies.test = [ "pytest>=8.3,<8.4", "pytest-cov>=6,<7", "testcontainers>=4.8.2,<4.9" ]
urls.Repository = "https://github.com/ChannelFinder/recsync"

[tool.setuptools]
packages = [ "recceiver", "recceiver.protocol", "twisted.plugins" ]
packages = [ "recceiver", "recceiver.cf", "recceiver.protocol", "twisted.plugins" ]
Comment thread
anderslindho marked this conversation as resolved.
include-package-data = true
package-data.twisted = [ "plugins/recceiver_plugin.py" ]

Expand Down
Empty file added server/recceiver/cf/__init__.py
Empty file.
102 changes: 102 additions & 0 deletions server/recceiver/cf/adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from typing import List

try:
from typing import Protocol
except ImportError:
from typing_extensions import Protocol # type: ignore[assignment]

from recceiver.cf.model import CFChannel, CFProperty, CFPropertyName, PVStatus

# CF query URLs break above this length; names are pipe-joined and chunked to stay under it.
_CF_NAME_QUERY_LIMIT = 600


class ChannelFinderAdapter(Protocol):
"""Typed boundary between CFProcessor and the ChannelFinder HTTP client.

All methods accept and return domain objects (CFChannel, CFProperty).
Dict serialisation is handled inside the implementation, not at callsites.
"""

def find_by_ioc_id(self, iocid: str) -> List[CFChannel]:
"""Return all channels registered under the given IOC ID."""
...

def find_by_names(self, names: List[str]) -> List[CFChannel]:
"""Return channels whose names are in the given list."""
...

def find_active_for_recceiver(self, recceiverid: str) -> List[CFChannel]:
"""Return all channels marked Active for the given recceiver."""
...

def set_channels(self, channels: List[CFChannel]) -> None:
"""Create or overwrite channels."""
...

def update_property(self, prop: CFProperty, channel_names: List[str]) -> None:
"""Update a single property value across the named channels."""
...

def get_property_names(self) -> List[str]:
"""Return the names of all property definitions registered in ChannelFinder."""
...

def set_property(self, name: str, owner: str) -> None:
"""Register a property definition if it does not already exist."""
...


class PyCFClientAdapter:
"""Wraps pyCFClient's ChannelFinderClient to implement ChannelFinderAdapter."""

def __init__(self, client, size_limit: int = 0):
self._client = client
self._size_limit = size_limit

def _find(self, args: List) -> List[CFChannel]:
if self._size_limit > 0:
args = args + [("~size", self._size_limit)]
return [CFChannel.from_dict(ch) for ch in self._client.findByArgs(args)]

def find_by_ioc_id(self, iocid: str) -> List[CFChannel]:
return self._find([(CFPropertyName.IOC_ID.value, iocid)])

def find_by_names(self, names: List[str]) -> List[CFChannel]:
if not names:
return []
chunks, buf = [], ""
for name in names:
if not buf:
buf = name
elif len(buf) + len(name) < _CF_NAME_QUERY_LIMIT:
buf = buf + "|" + name
else:
chunks.append(buf)
buf = name
if buf:
chunks.append(buf)
results = []
for chunk in chunks:
results.extend(self._find([("~name", chunk)]))
return results

def find_active_for_recceiver(self, recceiverid: str) -> List[CFChannel]:
return self._find(
[
(CFPropertyName.PV_STATUS.value, PVStatus.ACTIVE.value),
(CFPropertyName.RECCEIVER_ID.value, recceiverid),
]
)

def set_channels(self, channels: List[CFChannel]) -> None:
self._client.set(channels=[ch.as_dict() for ch in channels])

def update_property(self, prop: CFProperty, channel_names: List[str]) -> None:
self._client.update(property=prop.as_dict(), channelNames=channel_names)

def get_property_names(self) -> List[str]:
return [p["name"] for p in self._client.getAllProperties()]

def set_property(self, name: str, owner: str) -> None:
self._client.set(property={"name": name, "owner": owner})
66 changes: 66 additions & 0 deletions server/recceiver/cf/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import socket
from dataclasses import dataclass, fields
from typing import Optional

from recceiver.processors import ConfigAdapter

RECCEIVERID_DEFAULT = socket.gethostname()
DEFAULT_QUERY_LIMIT = 10_000


@dataclass
class CFConfig:
"""Configuration options for the CF Processor."""

alias_enabled: bool = False
record_type_enabled: bool = False
environment_variables: str = ""
info_tags: str = ""
ioc_connection_info: bool = True
record_description_enabled: bool = False
clean_on_start: bool = True
clean_on_stop: bool = True
username: str = "cfstore"
env_owner_variable: str = "ENGINEER"
recceiver_id: str = RECCEIVERID_DEFAULT
timezone: Optional[str] = None
cf_query_limit: int = DEFAULT_QUERY_LIMIT
base_url: Optional[str] = None
cf_username: Optional[str] = None
cf_password: Optional[str] = None
verify_ssl: Optional[bool] = None
push_max_retries: int = 10
push_always_retry: bool = True

@classmethod
def loads(cls, conf: ConfigAdapter) -> "CFConfig":
"""Load configuration from a ConfigAdapter instance."""
return CFConfig(
alias_enabled=conf.getboolean("alias", False),
record_type_enabled=conf.getboolean("recordType", False),
environment_variables=conf.get("environment_vars", ""),
info_tags=conf.get("infotags", ""),
ioc_connection_info=conf.getboolean("iocConnectionInfo", True),
record_description_enabled=conf.getboolean("recordDesc", False),
clean_on_start=conf.getboolean("cleanOnStart", True),
clean_on_stop=conf.getboolean("cleanOnStop", True),
username=conf.get("username", "cfstore"),
recceiver_id=conf.get("recceiverId", RECCEIVERID_DEFAULT),
timezone=conf.get("timezone", ""),
cf_query_limit=conf.get("findSizeLimit", DEFAULT_QUERY_LIMIT),
base_url=conf.get("baseUrl"),
cf_username=conf.get("cfUsername"),
cf_password=conf.get("cfPassword"),
verify_ssl=conf.getboolean("verifySSL"),
push_max_retries=conf.getint("pushMaxRetries", 10),
push_always_retry=conf.getboolean("pushAlwaysRetry", True),
)

def __repr__(self) -> str:
parts = []
for f in fields(self):
value = getattr(self, f.name)
if f.name == "cf_password":
value = "***" if value else None
parts.append(f"{f.name}={value!r}")
return f"CFConfig({', '.join(parts)})"
111 changes: 111 additions & 0 deletions server/recceiver/cf/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import enum
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional


class PVStatus(enum.Enum):
"""Active/Inactive status values as used in the pvStatus CF property."""

ACTIVE = "Active"
INACTIVE = "Inactive"


class CFPropertyName(enum.Enum):
"""Canonical property names registered and managed in Channelfinder."""

HOSTNAME = "hostName"
IOC_NAME = "iocName"
IOC_ID = "iocid"
IOC_IP = "iocIP"
PV_STATUS = "pvStatus"
TIME = "time"
RECCEIVER_ID = "recceiverID"
ALIAS = "alias"
RECORD_TYPE = "recordType"
RECORD_DESC = "recordDesc"
CA_PORT = "caPort"
PVA_PORT = "pvaPort"


@dataclass
class CFProperty:
"""A single named property attached to a Channelfinder channel."""

name: str
owner: str
value: Optional[str] = None

def as_dict(self) -> Dict[str, str]:
"""Serialise to the dict shape expected by pyCFClient."""
return {"name": self.name, "owner": self.owner, "value": self.value or ""}

@classmethod
def from_dict(cls, prop_dict: Dict[str, str]) -> "CFProperty":
"""Deserialise from the dict shape returned by pyCFClient."""
return cls(
name=prop_dict.get("name", ""),
owner=prop_dict.get("owner", ""),
value=prop_dict.get("value"),
)


@dataclass
class CFChannel:
"""A Channelfinder channel with its associated properties."""

name: str
owner: str
properties: List[CFProperty]

def as_dict(self) -> Dict[str, Any]:
"""Serialise to the dict shape expected by pyCFClient."""
return {
"name": self.name,
"owner": self.owner,
"properties": [p.as_dict() for p in self.properties],
}

@classmethod
def from_dict(cls, channel_dict: Dict[str, Any]) -> "CFChannel":
"""Deserialise from the dict shape returned by pyCFClient."""
return cls(
name=channel_dict.get("name", ""),
owner=channel_dict.get("owner", ""),
properties=[CFProperty.from_dict(p) for p in channel_dict.get("properties", [])],
)


@dataclass
class IOCInfo:
"""Runtime state for a connected IOC. The .id property is the primary key."""

host: str
hostname: str
ioc_name: str
ioc_ip: str
owner: str
time: str
port: int
channelcount: int = 0

@property
def id(self) -> str:
return f"{self.host}:{self.port}"


@dataclass
class RecordInfo:
"""Per-record data extracted from a transaction before pushing to CF."""

pv_name: str
record_type: Optional[str] = None
info_properties: List[CFProperty] = field(default_factory=list)
aliases: List[str] = field(default_factory=list)


class IOCMissingInfoError(Exception):
"""Raised when an IOC is missing required information."""

def __init__(self, ioc_info: IOCInfo):
super().__init__(f"Missing hostName {ioc_info.hostname} or iocName {ioc_info.ioc_name}")
self.ioc_info = ioc_info
Loading
Loading