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

Small refactoring CR23 - File deletion #1732

Conversation

nikolaredstork
Copy link
Collaborator

@nikolaredstork nikolaredstork commented Oct 31, 2023

Excluding deletion of deprecated files from load functions.
Comment from the main PR #1470 (comment)

@@ -393,7 +393,14 @@ class JobSaveStudy final : public Toolbox::Jobs::Job
} // save as

Copy link
Collaborator Author

@nikolaredstork nikolaredstork Oct 31, 2023

Choose a reason for hiding this comment

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

@guilpier-code This is obviously code duplication. Do you have proposal where potential function should be defined?

Copy link
Contributor

Choose a reason for hiding this comment

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

@guilpier-code This is obviously code duplication. Do you have proposal where potential function should be defined?

As you know, we don't want to enhance the UI code, we think it's not worth it.
Besides, it's not exactly code we touched for this CR 23.
But you're right, there is a code duplication.

@@ -449,6 +449,12 @@ class Study: public Yuni::NonCopyable<Study>, public IObject, public LayerData
void removeTimeseriesIfTSGeneratorEnabled();
//@}

/*!
Copy link
Contributor

@guilpier-code guilpier-code Oct 31, 2023

Choose a reason for hiding this comment

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

Class Study is large enough, we plan to split it in many pieces, and we don't want it to grow.

  • For now, we should make this function a free function. We could add it as a couple of source files file-removal.h/.cpp
  • What it actually does is removing deprecated hydro max power input files, right ? Don't hesitate renaming it removeDeprecatedHydroMaxPowerTSfiles.
  • this function should take 3 arguments :
    • a list of areas (AreaList&),
    • a input folder path (as an std::string or in a standard format, we don't want dependencies on Yuni strings, we want to get rid of it)
    • a current study version (as a unsigned int)

It could be something like :

void removeDeprecatedHydroMaxPowerTSfiles(AreaList& areas, 
                                          std::string inputFolder, 
                                           unsigned int currentStudyVersion)
{
    if (currentStudyVersion >= 870)
        return;
    
    vector<std::string> areasNames;
    areas.each( [](const Area& area) { areasNames.push_back(area.id.to<std::string>(); });
    
    for (const name :  areasNames)
    {
        std::string filePath = folderInput + "/hydro/common/capacity/" + name + ".txt"
        removeFile(filePath); // To be coded in the same source file, in the .cpp file.
    }
}

On the client side, it would be :

removeDeprecatedHydroMaxPowerTSfiles(study.areas, study.folderInput.to<std::string>, study.header.version),

Note : about removeFile(filePath), it probably already exists in the unit tests code area

Copy link
Collaborator Author

@nikolaredstork nikolaredstork Oct 31, 2023

Choose a reason for hiding this comment

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

The idea is to first read currentStudyVerstion and save it value to temporary variable, after that we are calling study->saveToFolder. Why we need temporary variable? We need it because, if we are upgrading a study, variable study->.header.version will be changed (after study->saveToFolder function call) to a current version of our software, and if we are passing value of the study->.header.version variable to the deleteDeprecatedHydroMaxPowerTSFiles function we will be passing already updated value of our study version (currently 870). If we are upgrading for example from 8.2 version than we need to pass 820 value to the deleteDeprecatedHydroMaxPowerTSFiles function, and we are doing that via temporary variable.
Condition if (currentStudyVersion >= 870) must be changed to if(previousVersion < 870) . Why? Because currentStudyVersion after saving a study will have 870 value, and deletion won't be performed.

Copy link
Collaborator Author

@nikolaredstork nikolaredstork Oct 31, 2023

Choose a reason for hiding this comment

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

Also, we need to delete files just if study->saveToFolder() function call return true, than we are sure that our new files are saved and all data from deprecated files are stored in our new files.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok. Your last comments regard more the updater/main.cpp than the src/libs/antares/study/study.h.
So I answer you in updater/main.cpp (below)

@@ -93,7 +93,12 @@ class MyStudyFinder final : public Data::StudyFinder
study->parameters.readonly = true;

logs.info() << "Saving...";
study->saveToFolder(folder);

uint tempVersionVar = (uint)study->header.version;
Copy link
Contributor

Choose a reason for hiding this comment

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

tempVersionVar : we should be more clear. This variable is the current study version, isn't it ?
So let's call it currentStudyVersion

Copy link
Contributor

Choose a reason for hiding this comment

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

An answer to the previous comment from @nikolaredstork was given here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here is what think, eventually about tempVersionVar and this added piece of code.
I think we should have :

  ...
  bool isSaved = study->saveToFolder(folder);
  if (isSaved)
      deleteDeprecatedHydroMaxPowerTSFiles(study->areas, 
                                           study->folderInput.to<std::string>, 
                                           study->header.version);

Note we don't need the source (what you called tmp) study version here.
Why ?
When get here, a study was loaded, and it may have input deprecated files.
Then we upgrade it, meaning that we erase input data with fresh updated one.
Deprecated files (hydro max power TS in this case) can still remain.
Calling deleteDeprecatedHydroMaxPowerTSFiles will delete them if they exists and if the new study version requires it.
Note that the small removeFile function suggested in this comment removes a file only if it exists of course.

It's more simple like this, don't you think ?

@@ -93,7 +93,12 @@ class MyStudyFinder final : public Data::StudyFinder
study->parameters.readonly = true;

logs.info() << "Saving...";
study->saveToFolder(folder);

Copy link
Contributor

Choose a reason for hiding this comment

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

Normally, we should add this to the src/libs/antares/study/cleaner/cleaner.cpp, which is supposed to clean any study from unsuitable files.

Copy link
Collaborator Author

@nikolaredstork nikolaredstork Nov 2, 2023

Choose a reason for hiding this comment

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

You are right. I've looked up cleaner.cpp file in details, and apparently after upgrading and saving our new files, we will delete deprecated files with clean command, so I don't think that we need automatic deletion after upgrade. With that been said, this PR shouldn't be merged and code for deletion of files should be excluded.

@@ -393,7 +393,14 @@ class JobSaveStudy final : public Toolbox::Jobs::Job
} // save as

Copy link
Contributor

Choose a reason for hiding this comment

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

@guilpier-code This is obviously code duplication. Do you have proposal where potential function should be defined?

As you know, we don't want to enhance the UI code, we think it's not worth it.
Besides, it's not exactly code we touched for this CR 23.
But you're right, there is a code duplication.

@pull-request-size pull-request-size bot added size/XS and removed size/M labels Nov 2, 2023
@@ -449,6 +449,12 @@ class Study: public Yuni::NonCopyable<Study>, public IObject, public LayerData
void removeTimeseriesIfTSGeneratorEnabled();
//@}

/*!
Copy link
Contributor

Choose a reason for hiding this comment

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

ok. Your last comments regard more the updater/main.cpp than the src/libs/antares/study/study.h.
So I answer you in updater/main.cpp (below)

@@ -93,7 +93,12 @@ class MyStudyFinder final : public Data::StudyFinder
study->parameters.readonly = true;

logs.info() << "Saving...";
study->saveToFolder(folder);

uint tempVersionVar = (uint)study->header.version;
Copy link
Contributor

Choose a reason for hiding this comment

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

An answer to the previous comment from @nikolaredstork was given here.

@@ -93,7 +93,12 @@ class MyStudyFinder final : public Data::StudyFinder
study->parameters.readonly = true;

logs.info() << "Saving...";
study->saveToFolder(folder);

uint tempVersionVar = (uint)study->header.version;
Copy link
Contributor

Choose a reason for hiding this comment

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

Here is what think, eventually about tempVersionVar and this added piece of code.
I think we should have :

  ...
  bool isSaved = study->saveToFolder(folder);
  if (isSaved)
      deleteDeprecatedHydroMaxPowerTSFiles(study->areas, 
                                           study->folderInput.to<std::string>, 
                                           study->header.version);

Note we don't need the source (what you called tmp) study version here.
Why ?
When get here, a study was loaded, and it may have input deprecated files.
Then we upgrade it, meaning that we erase input data with fresh updated one.
Deprecated files (hydro max power TS in this case) can still remain.
Calling deleteDeprecatedHydroMaxPowerTSFiles will delete them if they exists and if the new study version requires it.
Note that the small removeFile function suggested in this comment removes a file only if it exists of course.

It's more simple like this, don't you think ?

Copy link

sonarcloud bot commented Nov 2, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@guilpier-code guilpier-code merged commit 3da3b13 into feature/max-gen-pump-cr23-develop-merge Nov 2, 2023
7 checks passed
@guilpier-code guilpier-code deleted the fix/cr-23-deprecated-file-deletion branch November 2, 2023 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants