Skip to content

Add target_path flag option for edr files and logs#747

Merged
noaKurman merged 1 commit intomasterfrom
ele-303-add-target-path
Mar 16, 2023
Merged

Add target_path flag option for edr files and logs#747
noaKurman merged 1 commit intomasterfrom
ele-303-add-target-path

Conversation

@noaKurman
Copy link
Copy Markdown
Contributor

No description provided.

@noaKurman noaKurman requested a review from elongl March 13, 2023 15:32
@linear
Copy link
Copy Markdown

linear bot commented Mar 13, 2023

ELE-407 edr should have a target folder for all outputs

"It seems quite messy for to have the 3 files created for the report in the main project folder e.g.

edr.log
elementary_output.json
elementary_report.html

In most other packages where documentation has been built, all the artifacts created from the build have been added to a separate sub-folder (e.g. docs or build or dist or something along those lines). What I would like to be able to do is define within my project folder where the three edr files are built, so that I can ensure that is not included in source control, and to ensure that folder is consistent across all developers.
As it is, the behaviour of target-path in the config file doesn't quite do what I want it to, as the log is still written to the main project folder."

Slack thread:
https://elementary-community.slack.com/archives/C02CTC89LAX/p1674490912619819

@github-actions
Copy link
Copy Markdown
Contributor

👋 @noaKurman
Thank you for raising your pull request.
Please make sure to add tests and document all user-facing changes.
You can do this by editing the docs files in this pull request.

@noaKurman noaKurman removed the request for review from elongl March 13, 2023 15:32
@noaKurman noaKurman force-pushed the ele-303-add-target-path branch 3 times, most recently from cfc280b to f0467d8 Compare March 13, 2023 15:59
@RoiTabach
Copy link
Copy Markdown
Contributor

Looks good, added one small comment
Do we want to add user-facing documentation for it?

@noaKurman noaKurman force-pushed the ele-303-add-target-path branch from eb701e7 to dbac505 Compare March 14, 2023 09:12
@Maayan-s
Copy link
Copy Markdown
Contributor

Maayan-s commented Mar 15, 2023

Looks good, added one small comment
Do we want to add user-facing documentation for it?

@noaKurman
If you don't document no one will use it, so it's not a question 🙃
Let me know if you need help with docs.
You can do it in different PR

@noaKurman
Copy link
Copy Markdown
Contributor Author

noaKurman commented Mar 15, 2023

Looks good, added one small comment
Do we want to add user-facing documentation for it?

@noaKurman If you don't document no one will use it, so it's not a question 🙃 Let me know if you need help with docs. You can do it in different PR

@Maayan-s @RoiTabach
I added documentation in docs/understand-elementary/cli-commands.mdx, is that enough? If not I'll add it in a different PR :)

```

- **Configuration target directory path**
The path where all output files will be saved, default will create "edr_files" directory in your path
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@noaKurman
Can they change the default?
If not I would change to:
The path where edr should create an edr_files directory where all output files will be saved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not completely sure what you meant:
You can either run --target-path ./noa-target and it would create noa-target in the current directory and save all files there, or you can ignore the flag and it would create edr_files in your current directory and save all files there, so should I change it to the phrase you suggested?
Also should I mention it's an absolute path in the doc?
@Maayan-s

Copy link
Copy Markdown
Contributor

@Maayan-s Maayan-s Mar 15, 2023

Choose a reason for hiding this comment

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

Ok I get it now! @noaKurman

So I would write like this:
The absolute path where all output files such as logs and reports will be saved. If not configured, the default is ./edr_files.

@noaKurman noaKurman force-pushed the ele-303-add-target-path branch from dbac505 to 93530e2 Compare March 15, 2023 10:51
@noaKurman noaKurman requested a review from Maayan-s March 15, 2023 11:14
@Maayan-s Maayan-s linked an issue Mar 15, 2023 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

@RoiTabach RoiTabach left a comment

Choose a reason for hiding this comment

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

Looks good. Found 2 small typos, amazingly both don't have bad side effects but still best to remove them. Also wondering if we should rename Customer->Custom .

Afterwards - Yalla merge (Also write something in slack #tech so the team knows since it's a behavioural change)

Comment thread elementary/config/config.py Outdated
Comment thread elementary/config/config.py Outdated
config = self._load_configuration()

self.target_dir = self._first_not_none(
self.target_dir = self.target_dir = self._first_not_none(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Typo without side effects but still - let's fix:

Suggested change
self.target_dir = self.target_dir = self._first_not_none(
self.target_dir = self._first_not_none(

Comment thread elementary/utils/log.py Outdated
except Exception:
pass
finally:
os.makedirs(os.path.abspath(os.path.abspath(file_path)), exist_ok=True)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we don't need to double abspath , right?

Suggested change
os.makedirs(os.path.abspath(os.path.abspath(file_path)), exist_ok=True)
os.makedirs(os.path.abspath(file_path), exist_ok=True)

Comment thread elementary/utils/log.py Outdated
LOG_FILE = os.getcwd() + "/edr_files/edr.log"


class CustomerFileHandler(logging.FileHandler):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this customer and not custom intentionaly? I don't know English well enough to be sure

@noaKurman noaKurman force-pushed the ele-303-add-target-path branch from 9c874fc to c7ac650 Compare March 15, 2023 12:24
Comment thread elementary/config/config.py Outdated

DEFAULT_CONFIG_DIR = str(Path.home() / ".edr")

DEFAULT_FILES_PATH = os.getcwd() + "/edr_files"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe edr_outputs would be a better name?
Not sure

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Or asked for "edr_target" since it's compliant with dbt target dir

Comment thread elementary/monitor/cli.py Outdated
"-tp",
type=str,
default=Config.DEFAULT_FILES_PATH,
help="Absolut target path for saving edr files such as logs and reports",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
help="Absolut target path for saving edr files such as logs and reports",
help="Absolute target path for saving edr files such as logs and reports",

Comment thread elementary/utils/log.py Outdated
target_path_flag = "--target-path"
ctx = click.get_current_context()
ctx_args = ctx.args
if target_path_flag in ctx_args:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do we need to check by both the flag and the param?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because in the context of the cli/cli.py it's an arg, but in the context of some other files it's in the params. I guess its based on if it's before or after the get_cli_properties function @haritamar

Comment thread elementary/utils/log.py Outdated
file_path = os.getcwd() + "/edr_files"
try:
target_path_param = "target_path"
target_path_flag = "--target-path"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What if the user passes it as -tp rather than --target-path

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's a good point
I think I'll remove the -tp, it felt redundant from the start and also kind of confusing with the -t option
Is that ok? @haritamar

Comment thread elementary/utils/log.py Outdated
LOG_FILE = os.getcwd() + "/edr_files/edr.log"


class CustomerFileHandler(logging.FileHandler):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We normally use the term User rather than Customer in OSS, so let's do that here as well :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should have been Custom 😅

Comment thread elementary/utils/log.py Outdated

def get_file_handler():
file_handler = logging.FileHandler(LOG_FILE)
file_handler = CustomFileHandler(LOG_FILE, delay=True)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we should add the file handler dynamically with the correct log file output, initializing it with LOG_FILE and then changing it in emit feels a bit hacky...

I think it's better that we'll add a line of the format:
logger.addHandler(get_file_handler(log_path))

to elementary/monitor/cli.py (where log_path is constructed from the target path parameter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Writing here too -
Basically we're initializing a different logger in every file we have - (Which is a best practice in python),
So we need to somehow from every file to set the file handler to be ontarget_path - at that point I thought about getting the context in the log file itself, but then realized we're initializing it in the imports of the cli.py - which is before they have the context.

Resolution after huddle with Itamar I'll remove the custom handler and use the root logger (remove the "propagate = False") and set it's handler after the context is already set (this can happen even after the rest of the loggers are already initialized and they would be updated)

Comment thread elementary/monitor/cli.py
full_refresh_dbt_package = params.get("full_refresh_dbt_package")

return {
"target_path": target_path,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this necessary?
What does it affect?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Are you asking if we need it in the return or if we need the "target_path": target_path instead of just returning target_path itself?

For the first question - I'm pretty sure that's how we set the params (instead of it being in the args)

@noaKurman noaKurman requested review from elongl and haritamar March 15, 2023 16:22
Comment thread elementary/utils/log.py Outdated
return logger


def set_root_logger_file_handler(logger_name):
Copy link
Copy Markdown
Collaborator

@haritamar haritamar Mar 15, 2023

Choose a reason for hiding this comment

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

I think you should add both handlers only to the root logger, otherwise you'll get duplicate console outputs (outcome of removing propagate=False).
So maybe we can have set_root_logger_handlers that handles both?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I actually don't see it log twice to the console so it's interesting
But anyway I'll move the console handler to the root as well, I agree it's more sorted that way

Comment thread elementary/cli/cli.py Outdated
self.format_epilog(ctx, formatter)

def invoke(self, ctx: click.Context) -> Any:
set_root_logger_file_handler("elementary")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it may be good that set_root_logger_file_handler will explicitly get the target path rather than relying on the click context.
WDYT about moving the call to config.py below and then doing set_root_logger_file_handler("elementary", target_path)?

I think it makes sense to do it right after you're creating the directory there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm I see at that point in the code the config still points to the default edr_files path, maybe you can show me why
but I can also get it from the click context in the cli.py instead of the log (unless what you dislike here is the context itself and not the location of it)

Comment thread elementary/utils/log.py Outdated
return console_handler


def get_log_path():
Copy link
Copy Markdown
Collaborator

@haritamar haritamar Mar 15, 2023

Choose a reason for hiding this comment

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

See my note above regarding getting the target_path as a parameter, in that case I think this function can simply be joining it with "edr.log" :)

Comment thread elementary/utils/log.py Outdated
file_path = ctx_args[ctx_args.index(target_path_flag) + 1]
finally:
os.makedirs(os.path.abspath(file_path), exist_ok=True)
return file_path + "/edr.log"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

it's a good practice in python usually to use os.path.join(file_path, "edr.log") rather than adding strings
(handles well edge cases, like if the path ends or doesn't end with "/")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ha good to know, I saw both in the code so just used one of them 👍

with:
name: edr.log
path: edr.log
path: edr_files/edr.log
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should this be changed to edr_target? (also for other cases below)

@noaKurman noaKurman force-pushed the ele-303-add-target-path branch from e10b1ee to 75bc7e4 Compare March 16, 2023 09:39
@noaKurman noaKurman merged commit ba09d5e into master Mar 16, 2023
@noaKurman noaKurman deleted the ele-303-add-target-path branch March 16, 2023 09:55
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.

[ELE-303] edr should have a target folder for all outputs

5 participants