Skip to content

Commit 5c64534

Browse files
committed
Split up updateCF
1 parent 8b118df commit 5c64534

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
@@ -473,88 +473,9 @@ def create_time_property(owner: str, time: str) -> CFProperty:
473473
return create_property(owner, CFPropertyName.time.name, time)
474474

475475

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

@@ -581,9 +502,96 @@ def __updateCF__(processor: CFProcessor, recordInfoByName: Dict[str, RecordInfo]
581502
existingChannels[cf_channel.name] = cf_channel
582503
if processor.cancelled:
583504
raise defer.CancelledError()
505+
return existingChannels
506+
507+
508+
def create_alias_channel(
509+
ioc_info: IocInfo, cf_channel: CFChannel, managed_properties: Set[str], channels: List[CFChannel], alias_name: str
510+
) -> CFChannel:
511+
"""alias exists but not part of old list"""
512+
aprops = __merge_property_lists(
513+
[
514+
create_active_property(ioc_info.owner),
515+
create_time_property(ioc_info.owner, ioc_info.time),
516+
create_alias_property(
517+
ioc_info.owner,
518+
cf_channel.name,
519+
),
520+
],
521+
cf_channel,
522+
managed_properties,
523+
)
524+
return create_channel(
525+
alias_name,
526+
ioc_info.owner,
527+
aprops,
528+
)
529+
530+
531+
def handle_old_channels(
532+
old_channels: List[CFChannel],
533+
new_channels: Set[str],
534+
records_to_delete: List[str],
535+
ioc_info: IocInfo,
536+
managed_properties: Set[str],
537+
channels: List[CFChannel],
538+
alias_enabled: bool,
539+
recordInfoByName: Dict[str, RecordInfo],
540+
):
541+
for cf_channel in old_channels:
542+
if (
543+
len(new_channels) == 0 or cf_channel.name in records_to_delete
544+
): # case: empty commit/del, remove all reference to ioc
545+
_log.debug("Channel %s exists in Channelfinder not in new_channels", cf_channel.name)
546+
else:
547+
if cf_channel.name in new_channels: # case: channel in old and new
548+
"""
549+
Channel exists in Channelfinder with same iocid.
550+
Update the status to ensure it is marked active and update the time.
551+
"""
552+
_log.debug("Channel %s exists in Channelfinder with same ioc %s", cf_channel.name, ioc_info)
553+
new_channel = create_channel(
554+
cf_channel.name,
555+
ioc_info.owner,
556+
__merge_property_lists(
557+
[
558+
create_active_property(ioc_info.owner),
559+
create_time_property(ioc_info.owner, ioc_info.time),
560+
],
561+
cf_channel,
562+
managed_properties,
563+
),
564+
)
565+
channels.append(new_channel)
566+
_log.debug("Add existing channel with same IOC: %s", cf_channel)
567+
new_channels.remove(cf_channel.name)
568+
569+
"""In case, alias exist"""
570+
if alias_enabled and cf_channel.name in recordInfoByName:
571+
for alias_name in recordInfoByName[cf_channel.name].aliases:
572+
if alias_name in old_channels:
573+
_log.debug("Shouldnt happen")
574+
else:
575+
alias_channel = create_alias_channel(
576+
ioc_info, cf_channel, managed_properties, channels, alias_name
577+
)
578+
channels.append(alias_channel)
579+
new_channels.remove(alias_name)
580+
_log.debug("Add existing alias with same IOC: %s", cf_channel)
581+
584582

583+
def handle_new_and_existing_channels(
584+
new_channels: Set[str],
585+
ioc_info: IocInfo,
586+
recceiverid: str,
587+
cf_config: CFConfig,
588+
recordInfoByName: Dict[str, RecordInfo],
589+
existingChannels: Dict[str, CFChannel],
590+
managed_properties: Set[str],
591+
channels: List[CFChannel],
592+
):
585593
for channel_name in new_channels:
586-
newProps = create_properties(
594+
newProps = create_ioc_properties(
587595
ioc_info.owner,
588596
ioc_info.time,
589597
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)