Skip to content

Commit 5a4b537

Browse files
committed
Split up updateCF
1 parent 963359f commit 5a4b537

File tree

1 file changed

+150
-89
lines changed

1 file changed

+150
-89
lines changed

server/recceiver/cfstore.py

Lines changed: 150 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -477,88 +477,9 @@ def create_time_property(owner: str, time: str) -> CFProperty:
477477
return create_property(owner, CFPropertyName.time.name, time)
478478

479479

480-
def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo], records_to_delete, ioc_info: IocInfo):
481-
_log.info("CF Update IOC: %s", ioc_info)
482-
_log.debug("CF Update IOC: %s recordInfoByName %s", ioc_info, recordInfoByName)
483-
# Consider making this function a class methed then 'processor' simply becomes 'self'
484-
client = processor.client
485-
iocs = processor.iocs
486-
cf_config = processor.cf_config
487-
recceiverid = processor.cf_config.recceiverId
488-
new_channels = set(recordInfoByName.keys())
489-
iocid = ioc_info.ioc_id
490-
491-
if iocid not in iocs:
492-
_log.warning("IOC Env Info %s not found in ioc list: %s", ioc_info, iocs)
493-
494-
if ioc_info.hostname is None or ioc_info.iocname is None:
495-
raise Exception(f"Missing hostName {ioc_info.hostname} or iocName {ioc_info.iocname}")
496-
497-
if processor.cancelled:
498-
raise defer.CancelledError("Processor cancelled in __updateCF__")
499-
500-
channels: List[CFChannel] = []
501-
"""A list of channels in channelfinder with the associated hostName and iocName"""
502-
_log.debug("Find existing channels by IOCID: {iocid}".format(iocid=iocid))
503-
old_channels: List[CFChannel] = [
504-
CFChannel(**ch) for ch in client.findByArgs(prepareFindArgs(cf_config, [("iocid", iocid)]))
505-
]
506-
507-
if old_channels is not None:
508-
for cf_channel in old_channels:
509-
if (
510-
len(new_channels) == 0 or cf_channel.name in records_to_delete
511-
): # case: empty commit/del, remove all reference to ioc
512-
_log.debug("Channel %s exists in Channelfinder not in new_channels", cf_channel.name)
513-
else:
514-
if cf_channel.name in new_channels: # case: channel in old and new
515-
"""
516-
Channel exists in Channelfinder with same iocid.
517-
Update the status to ensure it is marked active and update the time.
518-
"""
519-
_log.debug("Channel %s exists in Channelfinder with same iocid %s", cf_channel.name, iocid)
520-
cf_channel.properties = __merge_property_lists(
521-
[
522-
create_active_property(ioc_info.owner),
523-
create_time_property(ioc_info.owner, ioc_info.time),
524-
],
525-
cf_channel,
526-
processor.managed_properties,
527-
)
528-
channels.append(cf_channel)
529-
_log.debug("Add existing channel with same IOC: %s", cf_channel)
530-
new_channels.remove(cf_channel.name)
531-
532-
"""In case, alias exist"""
533-
if cf_config.alias:
534-
if cf_channel.name in recordInfoByName:
535-
for alias_name in recordInfoByName[cf_channel.name].aliases:
536-
if alias_name in old_channels:
537-
_log.debug("Shouldnt happen")
538-
else:
539-
"""alias exists but not part of old list"""
540-
aprops = __merge_property_lists(
541-
[
542-
create_active_property(ioc_info.owner),
543-
create_time_property(ioc_info.owner, ioc_info.time),
544-
create_alias_property(
545-
ioc_info.owner,
546-
cf_channel.name,
547-
),
548-
],
549-
cf_channel,
550-
processor.managed_properties,
551-
)
552-
channels.append(
553-
create_channel(
554-
alias_name,
555-
ioc_info.owner,
556-
aprops,
557-
)
558-
)
559-
new_channels.remove(alias_name)
560-
_log.debug("Add existing alias with same IOC: %s", cf_channel)
561-
# now pvNames contains a list of pv's new on this host/ioc
480+
def fetch_existing_channels(
481+
new_channels: Set[str], client: ChannelFinderClient, cf_config: CFConfig, processor: CFProcessor
482+
) -> Dict[str, CFChannel]:
562483
"""A dictionary representing the current channelfinder information associated with the pvNames"""
563484
existingChannels = {}
564485

@@ -585,9 +506,96 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
585506
existingChannels[cf_channel.name] = cf_channel
586507
if processor.cancelled:
587508
raise defer.CancelledError()
509+
return existingChannels
510+
511+
512+
def create_alias_channel(
513+
ioc_info: IocInfo, cf_channel: CFChannel, managed_properties: Set[str], channels: List[CFChannel], alias_name: str
514+
) -> CFChannel:
515+
"""alias exists but not part of old list"""
516+
aprops = __merge_property_lists(
517+
[
518+
create_active_property(ioc_info.owner),
519+
create_time_property(ioc_info.owner, ioc_info.time),
520+
create_alias_property(
521+
ioc_info.owner,
522+
cf_channel.name,
523+
),
524+
],
525+
cf_channel,
526+
managed_properties,
527+
)
528+
return create_channel(
529+
alias_name,
530+
ioc_info.owner,
531+
aprops,
532+
)
533+
588534

535+
def handle_old_channels(
536+
old_channels: List[CFChannel],
537+
new_channels: Set[str],
538+
records_to_delete: List[str],
539+
ioc_info: IocInfo,
540+
managed_properties: Set[str],
541+
channels: List[CFChannel],
542+
alias_enabled: bool,
543+
recordInfoByName: Dict[str, RecordInfo],
544+
):
545+
for cf_channel in old_channels:
546+
if (
547+
len(new_channels) == 0 or cf_channel.name in records_to_delete
548+
): # case: empty commit/del, remove all reference to ioc
549+
_log.debug("Channel %s exists in Channelfinder not in new_channels", cf_channel.name)
550+
else:
551+
if cf_channel.name in new_channels: # case: channel in old and new
552+
"""
553+
Channel exists in Channelfinder with same iocid.
554+
Update the status to ensure it is marked active and update the time.
555+
"""
556+
_log.debug("Channel %s exists in Channelfinder with same ioc %s", cf_channel.name, ioc_info)
557+
new_channel = create_channel(
558+
cf_channel.name,
559+
ioc_info.owner,
560+
__merge_property_lists(
561+
[
562+
create_active_property(ioc_info.owner),
563+
create_time_property(ioc_info.owner, ioc_info.time),
564+
],
565+
cf_channel,
566+
managed_properties,
567+
),
568+
)
569+
channels.append(new_channel)
570+
_log.debug("Add existing channel with same IOC: %s", cf_channel)
571+
new_channels.remove(cf_channel.name)
572+
573+
"""In case, alias exist"""
574+
if alias_enabled and cf_channel.name in recordInfoByName:
575+
for alias_name in recordInfoByName[cf_channel.name].aliases:
576+
if alias_name in old_channels:
577+
_log.debug("Shouldnt happen")
578+
else:
579+
alias_channel = create_alias_channel(
580+
ioc_info, cf_channel, managed_properties, channels, alias_name
581+
)
582+
channels.append(alias_channel)
583+
new_channels.remove(alias_name)
584+
_log.debug("Add existing alias with same IOC: %s", cf_channel)
585+
586+
587+
def handle_new_and_existing_channels(
588+
new_channels: Set[str],
589+
ioc_info: IocInfo,
590+
recceiverid: str,
591+
cf_config: CFConfig,
592+
recordInfoByName: Dict[str, RecordInfo],
593+
existingChannels: Dict[str, CFChannel],
594+
managed_properties: Set[str],
595+
channels: List[CFChannel],
596+
):
589597
for channel_name in new_channels:
590-
newProps = create_properties(
598+
newProps = create_ioc_properties(
591599
ioc_info.owner,
592600
ioc_info.time,
593601
recceiverid,
@@ -602,13 +610,13 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
602610
newProps = newProps + recordInfoByName[channel_name].infoProperties
603611

604612
if channel_name in existingChannels:
605-
_log.debug("update existing channel %s: exists but with a different iocid from %s", channel_name, iocid)
613+
_log.debug("update existing channel %s: exists but with a different iocid from %s", channel_name, ioc_info)
606614

607615
existingChannel = existingChannels[channel_name]
608616
existingChannel.properties = __merge_property_lists(
609617
newProps,
610618
existingChannel,
611-
processor.managed_properties,
619+
managed_properties,
612620
)
613621
channels.append(existingChannel)
614622
_log.debug("Add existing channel with different IOC: %s", existingChannel)
@@ -624,13 +632,13 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
624632
ach.properties = __merge_property_lists(
625633
alProps,
626634
ach,
627-
processor.managed_properties,
635+
managed_properties,
628636
)
629637
channels.append(ach)
630638
else:
631639
channels.append(create_channel(alias_name, ioc_info.owner, alProps))
632640
_log.debug(
633-
"Add existing alias %s of %s with different IOC from %s", alias_name, channel_name, iocid
641+
"Add existing alias %s of %s with different IOC from %s", alias_name, channel_name, ioc_info
634642
)
635643

636644
else:
@@ -645,6 +653,57 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
645653
for alias in recordInfoByName[channel_name].aliases:
646654
channels.append(CFChannel(alias, ioc_info.owner, alProps))
647655
_log.debug("Add new alias: %s from %s", alias, channel_name)
656+
return alias
657+
658+
659+
def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo], records_to_delete, ioc_info: IocInfo):
660+
_log.info("CF Update IOC: %s", ioc_info)
661+
_log.debug("CF Update IOC: %s recordInfoByName %s", ioc_info, recordInfoByName)
662+
# Consider making this function a class methed then 'processor' simply becomes 'self'
663+
client = processor.client
664+
iocs = processor.iocs
665+
cf_config = processor.cf_config
666+
recceiverid = processor.cf_config.recceiverId
667+
new_channels = set(recordInfoByName.keys())
668+
iocid = ioc_info.ioc_id
669+
670+
if iocid not in iocs:
671+
_log.warning("IOC Env Info %s not found in ioc list: %s", ioc_info, iocs)
672+
673+
if ioc_info.hostname is None or ioc_info.iocname is None:
674+
raise Exception(f"Missing hostName {ioc_info.hostname} or iocName {ioc_info.iocname}")
675+
676+
if processor.cancelled:
677+
raise defer.CancelledError("Processor cancelled in __updateCF__")
678+
679+
channels: List[CFChannel] = []
680+
"""A list of channels in channelfinder with the associated hostName and iocName"""
681+
_log.debug("Find existing channels by IOCID: {iocid}".format(iocid=iocid))
682+
old_channels: List[CFChannel] = [
683+
CFChannel(**ch) for ch in client.findByArgs(prepareFindArgs(cf_config, [("iocid", iocid)]))
684+
]
685+
handle_old_channels(
686+
old_channels,
687+
new_channels,
688+
records_to_delete,
689+
ioc_info,
690+
processor.managed_properties,
691+
channels,
692+
cf_config.alias,
693+
recordInfoByName,
694+
)
695+
# now pvNames contains a list of pv's new on this host/ioc
696+
existingChannels = fetch_existing_channels(new_channels, client, cf_config, processor)
697+
handle_new_and_existing_channels(
698+
new_channels,
699+
ioc_info,
700+
recceiverid,
701+
cf_config,
702+
recordInfoByName,
703+
existingChannels,
704+
processor.managed_properties,
705+
channels,
706+
)
648707
_log.info("Total channels to update: %s for ioc: %s", len(channels), ioc_info)
649708

650709
if len(channels) != 0:
@@ -665,7 +724,9 @@ def cf_set_chunked(client, channels: List[CFChannel], chunk_size=10000):
665724
client.set(channels=chunk)
666725

667726

668-
def create_properties(owner: str, iocTime: str, recceiverid: str, hostName: str, iocName: str, iocIP: str, iocid: str):
727+
def create_ioc_properties(
728+
owner: str, iocTime: str, recceiverid: str, hostName: str, iocName: str, iocIP: str, iocid: str
729+
):
669730
return [
670731
create_property(owner, CFPropertyName.hostName.name, hostName),
671732
create_property(owner, CFPropertyName.iocName.name, iocName),
@@ -682,7 +743,7 @@ def create_default_properties(
682743
):
683744
channel_name = cf_channel.name
684745
last_ioc_info = iocs[channels_iocs[channel_name][-1]]
685-
return create_properties(
746+
return create_ioc_properties(
686747
ioc_info.owner,
687748
ioc_info.time,
688749
recceiverid,

0 commit comments

Comments
 (0)