Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEAT-235] Excel Importer/Exporter Support Last 🛟 #444

Merged
merged 19 commits into from
May 10, 2024
Merged

Conversation

doctrino
Copy link
Collaborator

@doctrino doctrino commented May 8, 2024

from cognite.neat.rules.exporter  import ExcelExporter

ExcelExporter(dump_as="last").export_to_file(rules, Path("dumped.xlsx"))

image

Ended up simplifying the logic in both the Importer and Exporter.

Copy link

github-actions bot commented May 8, 2024

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
19828 13452 68% 60% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
cognite/neat/rules/exporters/_rules2excel.py 85% 🟢
cognite/neat/rules/importers/_spreadsheet2rules.py 77% 🟢
cognite/neat/rules/models/dms/_rules_input.py 97% 🟢
cognite/neat/rules/models/dms/_serializer.py 97% 🟢
cognite/neat/rules/models/domain.py 98% 🟢
cognite/neat/rules/models/information/_converter.py 98% 🟢
cognite/neat/workflows/steps/lib/current/rules_exporter.py 25% 🟢
TOTAL 82% 🟢

updated for commit: ec36a32 by action🐍

Comment on lines -219 to -226
elif reference_result:
sheets = reference_result.sheets
original_role = reference_result.role
read_info_by_sheet = reference_result.read_info_by_sheet
else:
raise ValueError(
"No rules were generated. This should have been caught earlier. " f"Bug in {type(self).__name__}."
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case could not happen due to the logic above, so I removed it and thus simplified the logic.

@@ -241,25 +257,29 @@ def _create_metadata_sheet_user_rules(self, rules: Rules) -> dict[str, Any]:
# Excel does not support timezone in datetime strings
now_iso = datetime.now().replace(tzinfo=None).isoformat()
is_info = isinstance(rules, InformationRules)
is_dms = not is_info
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is dangerous, as it will break if we add asset rules

is_dms = not is_info
is_extension = self.new_model_id is not None
is_solution = is_extension and self.new_model_id != existing_model_id
is_dms = isinstance(rules, DMSRules)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole function needs a rewrite, have captured it in https://cognitedata.atlassian.net/browse/NEAT-240

sheets: user, last, and reference. The user sheets are used for inputting rules from a user. The last sheets
are used for the last version of the same model as the user, while the reference sheets are used for
the model the user is building on. The options are:
* "user": The rules are written to the user sheets. This is used when you want to modify the rules
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@@ -170,17 +189,14 @@ def _write_sheets(self, workbook: Workbook, dumped_rules: dict[str, Any], rules:
for cell in sheet["2"]:
cell.font = Font(bold=True, size=14)

def _write_metadata_sheet(self, workbook: Workbook, metadata: dict[str, Any], is_reference: bool = False) -> None:
def _write_metadata_sheet(self, workbook: Workbook, metadata: dict[str, Any], sheet_prefix: str = "") -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes us future proof with sheet_prefix , nice !

metadata["prefix" if is_info else "space"] = self.new_model_id[0] # type: ignore[index]
metadata["title" if is_info else "externalId"] = self.new_model_id[1] # type: ignore[index]
metadata["version"] = self.new_model_id[2] # type: ignore[index]
elif is_solution and self.dump_as == "reference" and rules.reference:
metadata["prefix" if is_info else "space"] = "YOUR_PREFIX"
metadata["title" if is_info else "externalId"] = "YOUR_TITLE"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

title is converted to name

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my thinking is that prefix is turned into both external_id and space, alt would be to remove all illegal characters from title and turn it to external_id

@@ -292,7 +292,8 @@ def run(self, rules: MultiRuleData) -> FlowMessage: # type: ignore[override, sy
if role != "input" and role is not None:
output_role = RoleTypes[role]

excel_exporter = exporters.ExcelExporter(styling=styling, output_role=output_role, is_reference=is_reference)
dump_as = "reference" if is_reference else "user"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not mistaken this cannot currently export user-last-ref ?

@doctrino doctrino merged commit cb6500f into main May 10, 2024
7 checks passed
@doctrino doctrino deleted the excel-support-last branch May 10, 2024 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants