Skip to content

Commit

Permalink
Add sort capability to list adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
craig8 committed Nov 2, 2023
1 parent 7c9c7f9 commit 9383f0c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
26 changes: 25 additions & 1 deletion ieee_2030_5/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,38 @@ def get_resource_list(self,
list_uri: str,
start: int = 0,
after: int = 0,
limit: int = 0) -> Union[m.List_type, m.SubscribableList]:
limit: int = 0,
sort_by: List[str] = [],
reverse: bool = False) -> Union[m.List_type, m.SubscribableList]:
if isinstance(sort_by, str):
sort_by = [sort_by]
cls = self.get_type(list_uri)
if cls is None:
raise KeyError(f"Resource list {list_uri} not found in adapter")

thelist = eval(f"m.{cls.__name__}List()")
try:
thecontainerlist = list(self._container_dict[list_uri].values())
for sort in sort_by:
subobj = sort.split('.')
if len(subobj) == 2:
try:
thecontainerlist = sorted(
thecontainerlist,
key=lambda o: getattr(getattr(o, subobj[0]), subobj[1]),
reverse=reverse)
except AttributeError:
# happens when value is none
pass
elif len(subobj) == 1:
thecontainerlist = sorted(thecontainerlist,
key=lambda o: getattr(o, sort),
reverse=reverse)
else:
raise ValueError("Can only sort through single nested properties.")
# if "." in sort:

# thecontainerlist = sorted(thecontainerlist, key=sort)
thelist.href = list_uri
thelist.all = len(thecontainerlist)
if start == after == limit == 0:
Expand Down
11 changes: 6 additions & 5 deletions ieee_2030_5/adapters/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,33 @@ def create_mirror_meter_reading(
location = mr_list_href

# Current instantanious values.
if mmr.Reading is not None:
if mmr_input.Reading is not None:
mmr.Reading.href = hrefs.SEP.join([mmr.href, "r"])
mmr.Reading.href = mmr.Reading.href.replace("mup", "upt")
add_href(mmr.Reading.href, mmr.Reading)
mr.ReadingLink = m.ReadingLink(mmr.Reading.href)

# Mirror reading sets
if mmr.MirrorReadingSet:
if mmr_input.MirrorReadingSet:

mrs_list_href = hrefs.SEP.join([mmr.href, "rs"])
rs_list_href = hrefs.SEP.join([mmr.href, "rs"]).replace("mup", "upt")
mr.ReadingSetListLink = m.ReadingSetListLink(href=rs_list_href)
ListAdapter.initialize_uri(mr.ReadingSetListLink.href, m.ReadingSet)

for mrs in mmr.MirrorReadingSet:
for mrs in mmr_input.MirrorReadingSet:
found_rs = False
try:
mrs_item = ListAdapter.get_by_mrid(mrs_list_href, mrs.mRID)
mrs_item_index = ListAdapter.get_list(mrs_list_href).index(mrs_item)
was_updated = True
found_rs = True
except NotFoundError:
mrs_item = mrs
mrs_item_index = ListAdapter.list_size(mrs_list_href)
mrs_item.href = hrefs.SEP.join([mrs_list_href, str(mrs_item_index)])
ListAdapter.append(mrs_list_href, mrs_item)

if was_updated:
if found_rs:
rs_item = ListAdapter.get(rs_list_href, mrs_item_index)
rs_item.description = mrs_item.description
rs_item.timePeriod = mrs_item.timePeriod
Expand Down
2 changes: 1 addition & 1 deletion ieee_2030_5/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class ServerConfiguration:

log_event_list_poll_rate: int = 900
device_capability_poll_rate: int = 900
usage_point_post_rate: int = 300
mirror_usage_point_post_rate: int = 300
end_device_list_poll_rate: int = 86400 # daily check-in

generate_admin_cert: bool = False
Expand Down
19 changes: 16 additions & 3 deletions ieee_2030_5/server/meteringfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ def get(self) -> Response:
parsed = hrefs.ParsedUsagePointHref(request.path)

handled = False
sort_by = []
reversed = True

if parsed.has_reading_list():
sort_by = "timePeriod.start"
if handled := parsed.reading_index is not None:
obj = adpt.ListAdapter.get(parsed.last_list(), parsed.reading_index)

elif parsed.has_reading_set_list():
sort_by = "timePeriod.start"
if handled := parsed.reading_set_index is not None:
obj = adpt.ListAdapter.get(parsed.last_list(), parsed.reading_set_index)
obj = sorted(obj, key="timePeriod.start")
elif parsed.has_meter_reading_list():
if handled := parsed.has_reading_type():
obj = get_href(request.path)
Expand All @@ -56,13 +61,16 @@ def get(self) -> Response:
obj = adpt.ListAdapter.get_resource_list(request.path,
start=start,
limit=limit,
after=after)
after=after,
reverse=reversed)

if not handled:
obj = adpt.ListAdapter.get_resource_list(request.path,
start=start,
limit=limit,
after=after)
after=after,
sort_by=sort_by,
reverse=reversed)
# # /upt
# if not parsed.has_usage_point_index():
# obj = adpt.UsagePointAdapter.fetch_all(m.UsagePointList(request.path),
Expand Down Expand Up @@ -99,7 +107,10 @@ def get(self) -> Response:

if not mup_href.has_usage_point_index():
# /mup
mup = adpt.ListAdapter.get_resource_list(request.path)
mup: m.MirrorUsagePointList = adpt.ListAdapter.get_resource_list(request.path)
# Because our resource_list doesn't include other properties than the list we set
# them here before returning.
mup.pollRate = self.server_config.mirror_usage_point_post_rate
else:
# /mup_0
mup = adpt.ListAdapter.get(hrefs.DEFAULT_MUP_ROOT, mup_href.usage_point_index)
Expand All @@ -121,6 +132,8 @@ def post(self) -> Response:

# Creating a new mup
if data_type == m.MirrorUsagePoint:
if data.postRate is None:
data.postRate = self.server_config.mirror_usage_point_post_rate
result = adpt.create_mirror_usage_point(mup=data)
#result = adpt.MirrorUsagePointAdapter.create(mup=data)
else:
Expand Down

0 comments on commit 9383f0c

Please sign in to comment.