Skip to content

Commit

Permalink
feat(cli)!: remove ecalc show results command
Browse files Browse the repository at this point in the history
Refs: ECALC-1700

BREAKING CHANGE: Removed `ecalc show results` from the cli. This feature
is assumed to be of little use and causes additional maintenance.
  • Loading branch information
jsolaas committed Oct 8, 2024
1 parent 29cdaab commit 6d1f104
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 522 deletions.
4 changes: 3 additions & 1 deletion docs/docs/changelog/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ sidebar_position: -1002
- Economics has been removed. TAX, PRICE and QUOTA keywords will now give an error.
- Misplaced keywords will now cause an error instead of being ignored.

- H2O is no longer supported in composition. Use 'water' instead.
- H2O is no longer supported in composition. Use 'water' instead.

- The CLI command `ecalc show results` has been removed. The reason being additional maintenance for something that did not seem to be used a lot.
8 changes: 0 additions & 8 deletions src/ecalc_cli/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import libecalc.version
from ecalc_cli.errors import EcalcCLIError
from ecalc_cli.infrastructure.file_resource_service import FileResourceService
from ecalc_cli.io.cache import Cache
from ecalc_cli.io.output import (
write_flow_diagram,
write_json,
Expand Down Expand Up @@ -148,13 +147,6 @@ def run(

run_info.end = datetime.now()

cache = Cache(user_specified_output_path=output_folder)
cache.write_run_info(run_info)
cache.write_results(
results=results_core,
component_dto=model.dto,
)

output_prefix: Path = output_folder / name_prefix

results_dto = get_asset_result(results_core)
Expand Down
90 changes: 0 additions & 90 deletions src/ecalc_cli/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@

import typer

import libecalc.common.time_utils
import libecalc.version
from ecalc_cli.io.cache import Cache
from ecalc_cli.io.output import write_output
from ecalc_cli.logger import logger
from ecalc_cli.types import DateFormat, Frequency
from libecalc.infrastructure.file_utils import (
OutputFormat,
get_component_output,
get_result_output,
)
from libecalc.presentation.yaml.yaml_entities import ResourceStream
from libecalc.presentation.yaml.yaml_models.pyyaml_yaml_model import PyYamlYamlModel

Expand All @@ -38,83 +28,3 @@ def show_yaml(
ResourceStream(name=model_file.name, stream=model_file), base_dir=model_filepath.parent
)
write_output(read_model, output_file)


@app.command("results")
def show_results(
component_name: str = typer.Option(
None,
"-n",
"--name",
help="Filter the results to only show the component with this name",
),
output_format: OutputFormat = typer.Option(
OutputFormat.JSON.value,
"--output-format",
help="Show the data in this format.",
),
output_file: Path = typer.Option(
None,
"--file",
help="Write the data to a file with the specified name.",
),
output_folder: Path = typer.Option(
Path.cwd(),
"--output-folder",
help="Output folder. Defaults to current working directory",
show_default=False,
),
detailed_output: bool = typer.Option(
False,
"--detailed-output",
help="Output detailed output." " When False you will get basic energy usage and emissions results",
),
date_format_option: DateFormat = typer.Option(
DateFormat.ISO_8601.value,
"--date-format-option",
help='Date format option. 0: "YYYY-MM-DD HH:MM:SS" (Accepted variant of ISO8601), 1: "YYYYMMDD HH:MM:SS" (ISO8601), 2: "DD.MM.YYYY HH:MM:SS". Default 0 (ISO 8601)',
),
output_frequency: Frequency = typer.Option(
libecalc.common.time_utils.Frequency.NONE.name,
"--output-frequency",
"-f",
help="Frequency of output. Options are DAY, MONTH, YEAR. If not specified, it will give"
" time steps equal to the union of all input given with INFLUENCE_TIME_VECTOR set to True."
" Down-sampling the result may lead to loss of data, and rates such as MW may not add up to cumulative values",
),
):
"""Show results. You need to run eCalc™ before this will be available."""
cache = Cache(user_specified_output_path=output_folder)
results = cache.load_results()
run_info = cache.load_run_info()

if run_info.version != libecalc.version.current_version():
logger.warning(
f"Your version of eCalc™ '{libecalc.version.current_version()}' is different to the one used to create the results '{run_info.version}'."
)

if output_frequency != Frequency.NONE:
results_resampled = results.resample(libecalc.common.time_utils.Frequency[output_frequency.name])
else:
results_resampled = results.model_copy()

component_name = component_name

if component_name is None:
# Default to full result if no specified component.
text = get_result_output(
results_resampled,
output_format=output_format,
simple_output=not detailed_output,
date_format_option=int(date_format_option.value),
)
else:
text = get_component_output(
results_resampled,
component_name,
output_format=output_format,
simple_output=not detailed_output,
date_format_option=int(date_format_option.value),
)

write_output(text, output_file)
107 changes: 0 additions & 107 deletions src/ecalc_cli/io/cache.py

This file was deleted.

52 changes: 0 additions & 52 deletions src/libecalc/infrastructure/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import enum
import sys
from datetime import datetime
from typing import Any, Callable, Optional, Union

Expand Down Expand Up @@ -129,54 +128,3 @@ def get_result_output(
raise ValueError(
f"Invalid output format. Expected {OutputFormat.CSV} or {OutputFormat.JSON}, got '{output_format}'"
)


def get_component_output(
results: EcalcModelResult,
component_name: str,
output_format: OutputFormat,
simple_output: bool,
date_format_option: int,
) -> str:
"""Get eCalc output for a single component by name
Args:
results: Complete from eCalc model
component_name: Name of component to output results from
output_format: Format of output file, CSV and JSON is currently supported
simple_output: If true, will provide a simplified output format. Only supported for json format
date_format_option:
Returns:
"""
components = [component for component in results.components if component.name == component_name]

if len(components) == 0:
msg = f"Unable to find component with name '{component_name}'"
logger.error(msg)
raise ValueError(msg)
elif len(components) == 1:
component = components[0]
else:
print("Several components match this name\n")
format_str = "{:<5} {:<18} {:<10}"
print(format_str.format("index", "type", "name"))
for index, component in enumerate(components):
print(format_str.format(index, component.componentType.value, component.name))
print()
selected_component_index = input("Enter the index of the component you want to select (q to quit): ")
if selected_component_index == "q":
sys.exit(0)

component = components[int(selected_component_index)]

if output_format == OutputFormat.JSON:
return to_json(component, simple_output=simple_output, date_format_option=date_format_option)
elif output_format == OutputFormat.CSV:
df = component.to_dataframe()
return dataframe_to_csv(df, date_format=DateTimeFormats.get_format(date_format_option))
else:
raise ValueError(
f"Invalid output format. Expected {OutputFormat.CSV} or {OutputFormat.JSON}, got '{output_format}'"
)
Loading

0 comments on commit 6d1f104

Please sign in to comment.