Skip to content

Commit

Permalink
[Track-305] Add sorting to report factory (#2469)
Browse files Browse the repository at this point in the history
- Add sorting to report factory
- Sort AS items by referral date
  • Loading branch information
tolkamps1 authored Dec 9, 2024
1 parent be731e8 commit 043848d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
10 changes: 9 additions & 1 deletion epictrack-api/src/api/reports/anticipated_schedule_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,16 @@ def __init__(self, filters, color_intensity):
"work_type_id"
]
group_by = "phase_name"
item_sort_key = "referral_date"
template_name = "anticipated_schedule.docx"
super().__init__(data_keys, group_by, template_name, filters, color_intensity)
super().__init__(
data_keys=data_keys,
group_by=group_by,
item_sort_key=item_sort_key,
template_name=template_name,
filters=filters,
color_intensity=color_intensity
)
self.report_title = "Anticipated EA Referral Schedule"

def _fetch_data(self, report_date):
Expand Down
41 changes: 35 additions & 6 deletions epictrack-api/src/api/reports/report_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@
class ReportFactory(ABC):
"""Basic representation of report generator."""

def __init__(self, data_keys, group_by=None, template_name=None, filters=None, color_intensity=None):
def __init__(
self,
data_keys=None,
group_by=None,
group_sort_order=None,
item_sort_key=None,
template_name=None,
filters=None,
color_intensity=None
):
"""Constructor"""
self.data_keys = data_keys
self.group_by = group_by
self.group_sort_order = group_sort_order # Custom group order
self.item_sort_key = item_sort_key # Sort items within groups
if template_name is not None:
self.template_path = Path(__file__, f"../report_templates/{template_name}")
self.filters = filters
Expand All @@ -34,6 +45,7 @@ def _format_data(self, data, report_title=None):
excluded_items = []
if self.filters and "exclude" in self.filters:
excluded_items = self.filters["exclude"]

if report_title == "Anticipated EA Referral Schedule":
for item in data:
obj = {}
Expand All @@ -44,8 +56,8 @@ def _format_data(self, data, report_title=None):
else:
obj[k] = item[k]
if self.group_by:
obj["sl_no"] = len(formatted_data[obj.get(self.group_by)]) + 1
formatted_data[obj.get(self.group_by)].append(obj)
obj["sl_no"] = len(formatted_data[obj.get(self.group_by, -1)]) + 1
formatted_data[obj.get(self.group_by, -1)].append(obj)
else:
formatted_data.append(obj)
else:
Expand All @@ -54,11 +66,28 @@ def _format_data(self, data, report_title=None):
k: getattr(item, k) for k in self.data_keys if k not in excluded_items
}
if self.group_by:
obj["sl_no"] = len(formatted_data[obj.get(self.group_by)]) + 1
formatted_data[obj.get(self.group_by)].append(obj)
obj["sl_no"] = len(formatted_data[obj.get(self.group_by, -1)]) + 1
formatted_data[obj.get(self.group_by, -1)].append(obj)
else:
formatted_data.append(obj)

if self.group_by:
# Sort items within each group
for group, items in formatted_data.items():
if self.item_sort_key:
formatted_data[group] = sorted(
items,
key=lambda x: x[self.item_sort_key]
)
# Sort groups
if self.group_sort_order:
formatted_data = dict(
sorted(
formatted_data.items(),
key=lambda x: self.group_sort_order.index(x[0])
if x[0] in self.group_sort_order
else len(self.group_sort_order), # Move other groups last
)
)
return formatted_data

def _deserialize_work_issues(self, work_issues):
Expand Down
11 changes: 7 additions & 4 deletions epictrack-api/src/api/reports/resource_forecast_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ def __init__(self, filters, color_intensity):
"ea_type_label",
"sector(sub)",
"ea_type_sort_order",
# "responsible_epd",
# "cairt_lead",
# "work_lead",
"fte_positions_construction",
"fte_positions_operation",
"work_type_id",
]
group_by = "work_id"
super().__init__(data_keys, group_by, None, filters, color_intensity)
super().__init__(
data_keys=data_keys,
group_by=group_by,
template_name=None,
filters=filters,
color_intensity=color_intensity
)
self.excluded_items = []
if self.filters and "exclude" in self.filters:
self.excluded_items = self.filters["exclude"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, filters, color_intensity):
"project_id",
"status_date_updated",
]
super().__init__(data_keys, filters=filters, color_intensity=color_intensity)
super().__init__(data_keys=data_keys, filters=filters, color_intensity=color_intensity)
self.report_date = None
self.report_title = "30-60-90"
self.pecp_configuration_ids = (
Expand Down

0 comments on commit 043848d

Please sign in to comment.