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

ability to download the calculation by id or value #2

Merged
merged 1 commit into from
Jul 28, 2021

Conversation

odvarkadaniel
Copy link
Contributor

@odvarkadaniel odvarkadaniel commented Jun 28, 2021

Hi,
don't know if we want to create just some kind of txt file or something. For now I just create new file with the name of calculation or it's teff and logG values.
@droslean

Signed-off-by: Daniel Odvarka odvarkaadaniel@gmail.com


fileName := fmt.Sprintf("%0.1f___%0.1f", teff, logG)

file, err := os.Create(fileName)
Copy link
Member

Choose a reason for hiding this comment

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

First, here we should check the error. Second, this will create the file in the current directory. We should have a default location for the downloaded results. Maybe in the same path where we create the configs for this tool?

Copy link
Contributor Author

@odvarkadaniel odvarkadaniel Jun 28, 2021

Choose a reason for hiding this comment

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

Somehow, I am unable to create the file in another directory. Tried all sorts of functions, different permission..
os.Chdir is not doing the same thing as terminal for me. Couldnt find anything that would help me solve this.

'~/.config/ccboc' nor any other is not working. Only in $pwd. I even tried to jump between the $pwd but that didnt work either.

Copy link
Member

Choose a reason for hiding this comment

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

This should be related to your environment only. I guess permissions? You should be able to write in the ~/.config/ccboc since we already wrote the configuration files.


_, err = file.WriteString(string(body))
if err != nil {
logrus.WithError(err).Fatal("Couldn't write the calculation into the file.")
Copy link
Member

Choose a reason for hiding this comment

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

The error message is wrong. Those data are not a calculation but bytes from a tar file.

args := os.Args
calcID := args[len(args)-1]

fmt.Println(globalConfig.APIURL + "/calculations/results/" + calcID)
Copy link
Member

Choose a reason for hiding this comment

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

debugging leftover too?

logrus.WithFields(logrus.Fields{"message": responseError.Message, "status_code": responseError.StatusCode}).Fatal("errors occurred")
}

file, err := os.Create(calcID)
Copy link
Member

Choose a reason for hiding this comment

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

Same here. The file is a tar.gz.

Copy link
Member

Choose a reason for hiding this comment

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

Also you are not checking the error here.


defer file.Close()

_, err = file.WriteString(string(body))
Copy link
Member

Choose a reason for hiding this comment

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

file.Write(body)

@odvarkadaniel
Copy link
Contributor Author

Hi,
added a new flag for user to enter a specific path for the download.
Also not entirely sure about the files. Its just not working on my system so I let it be this way.

if path != "" {
defaultPath = filepath.Join(path, filepath.Base(key))
} else {
defaultPath = filepath.Join("~/.config/ccboc/", filepath.Base("text.txt"))
Copy link
Member

@droslean droslean Jul 3, 2021

Choose a reason for hiding this comment

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

We should try to get the $HOME path like

	user, err := user.Current()
	if err != nil {
		logrus.WithError(err).Fatal("couldn't get $HOME directory from current user")
	}
	home := user.HomeDir 

Also, filepath.Base("text.txt") will always return text.txt but it is also wrong since the file will never be just a text file.

}

if _, err := os.Stat(defaultPath); os.IsNotExist(err) {
logrus.WithError(err).Fatal("The desired file path does not exist.")
Copy link
Member

Choose a reason for hiding this comment

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

Let us have a better error message here. Also, I think it will be better to just print a warning here and then download the file to the default path.

if path != "" {
defaultPath = filepath.Join(path, filepath.Base(calcID))
} else {
defaultPath = filepath.Join("~/.config/ccboc/", filepath.Base(calcID))
Copy link
Member

Choose a reason for hiding this comment

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

same here

@@ -222,6 +304,11 @@ func main() {
Name: "logG",
Usage: "specifies the logG value when creating a calculation.",
},
&cli.StringFlag{
Name: "path",
Copy link
Member

Choose a reason for hiding this comment

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

argument named path is unclear what it does. Let us have a better name, like results-download-path

@@ -249,6 +336,20 @@ func main() {
Usage: "",
Action: getCalculations,
},

{
Copy link
Member

Choose a reason for hiding this comment

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

lets just have one command that belongs in the calculation command. I would expect for the user to just type ccboc get calculation results {id} or ccboc --logG=4.0 --teff=1000 get calculation results

Copy link
Member

Choose a reason for hiding this comment

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

@gszasz We could use your insight as a user in this one...

@odvarkadaniel odvarkadaniel force-pushed the download-calc branch 2 times, most recently from c5bac40 to 40cb614 Compare July 3, 2021 19:05
@odvarkadaniel
Copy link
Contributor Author

@droslean Not sure about the implementation of the cli commands. Couldnt find some kind of example of this kind of thing.
Also, is the "filename" header OK? I cant even recieve for example Content-Type just to check.

@droslean
Copy link
Member

droslean commented Jul 3, 2021

@odvarkadaniel

Not sure about the implementation of the cli commands. Couldnt find some kind of example of this kind of thing.

You can check the library in godoc.

Also, is the "filename" header OK?

Let's have some tests that can check that.

@odvarkadaniel
Copy link
Contributor Author

odvarkadaniel commented Jul 4, 2021

@droslean Its just not letting me get to the headers locally. Dont know what I can do about it.

error="Get "https://apiserver-vega.router.default.svc.cluster.local/calculations/results?teff=9000.0&logg=4.0\": x509: certificate signed by unknown authority"

Hopefully, this isnt some stupid way to get to the headers:)
Could we schedule a meeting about all this + dont understand some of this process - the files we are creating etc.

Also, is the "filename" header OK?

Let's have some tests that can check that.

@droslean
Copy link
Member

droslean commented Jul 4, 2021

You still trying to reach the API server in the cluster. Why not just testing it locally?

@odvarkadaniel
Copy link
Contributor Author

You still trying to reach the API server in the cluster. Why not just testing it locally?

How do I reach it locally? I have no clue what you would mean by that..

@droslean
Copy link
Member

droslean commented Jul 6, 2021

How do I reach it locally? I have no clue what you would mean by that..

Run the apiserver locally. It has the dry-run, for the get/create/delete calculations but you don't even care about that. You can just start it by pointing to your results folder, add the fort.7 and fort.8 files in there, and expect to get them.

Example results folder structure:
results/9000.0___4.00/fort.7
results/9000.0___4.00/fort.8

@odvarkadaniel odvarkadaniel force-pushed the download-calc branch 2 times, most recently from 03dc837 to b3d39ea Compare July 23, 2021 16:17
@odvarkadaniel
Copy link
Contributor Author

Hi,
if user wants to download the result by teff and logG, or just by calcID, the tar.gz file get created as the name of the header (10071.0_4.00-results.tar.gz). There are the two fort files - fort.7 and fort.8
@droslean

if path != "" {
defaultPath = filepath.Join(path, filepath.Base(newKey[1]))
} else {
logrus.Info("Desired path to download the calculations was not included. The calculations are set to be downloaded into: $HOME/.config/ccboc/{id}")
Copy link
Member

@droslean droslean Jul 23, 2021

Choose a reason for hiding this comment

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

no need for this log. Instead, just log where we downloaded the results.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think the user should know, that the path was not added properly and was not found.

}

key := resp.Header.Get("Content-Disposition")
newKey := regexp.MustCompile("=").Split(key, 2)
Copy link
Member

Choose a reason for hiding this comment

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

Please use strings.Split() instead.


var defaultPath string

user, err := user.Current()
Copy link
Member

Choose a reason for hiding this comment

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

just use conig.GetDefaultConfigPath()

Copy link
Member

Choose a reason for hiding this comment

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

We should be able to just have

defaultPath := path
if defaultPath == "" {
  defaultPath, err = config.GetDefaultConfigPath()
  if err != nil{...}
}

Copy link
Contributor Author

@odvarkadaniel odvarkadaniel Jul 23, 2021

Choose a reason for hiding this comment

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

This rewrites the config file, which causes errors.
DefalultConfigName gets thrown in and generates at defaultPath.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just created a new function really which is pretty much the same thing as this one, but with different fileName.

defaultPath = filepath.Join(home, "/.config/ccboc/", filepath.Base(newKey[1]))
}

if _, err := os.Stat(defaultPath); !errors.Is(err, os.ErrNotExist) {
Copy link
Member

Choose a reason for hiding this comment

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

lets have a helper func that creates the results folder in the config package

@@ -49,3 +50,28 @@ func GetDefaultConfigPath() (string, error) {

return filepath.Join(home, DefaultConfigPath, DefaultConfigFileName), nil
}

func GetDefaultPath(fileName string) (string, error) {
Copy link
Member

@droslean droslean Jul 26, 2021

Choose a reason for hiding this comment

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

This new function is a duplicate with GetDefaultConfigPath. Let's refactor it to get the desired location instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just had to change the name of the file, because 'DefaultConfigName' is set to 'config' and it brakes the config file.

Copy link
Member

Choose a reason for hiding this comment

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

Yea, but we still have duplicated code in this function. Instead refactor GetDefaultConfigPath.

logrus.WithError(err).Fatal("Couldn't retrieve the data from headers.")
}

key := resp.Header.Get("Content-Disposition")
Copy link
Member

Choose a reason for hiding this comment

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

lets use better names rather than key and newKey

@odvarkadaniel odvarkadaniel force-pushed the download-calc branch 2 times, most recently from 02b5f14 to 1c2a615 Compare July 26, 2021 11:12
@odvarkadaniel
Copy link
Contributor Author

@droslean Function is refactored, variables renamed. Everything seems to be working. Exception errors handled.


fileNameHeader := resp.Header.Get("Content-Disposition")
splitFileNameHeader := strings.Split(fileNameHeader, "=")
if len(splitFileNameHeader) == 1 {
Copy link
Member

Choose a reason for hiding this comment

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

Header.Get() will return "" if there is no header. You can make the check earlier.

See https://pkg.go.dev/net/http#Header.Get


if defaultPath == "" {
return filepath.Join(home, DefaultConfigPath, fileName), nil
} else {
Copy link
Member

Choose a reason for hiding this comment

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

There is no need for the else here.

logrus.Info("Desired path to download the calculations does not exist. The calculations are set to be downloaded into: $HOME/.config/ccboc/{id}")
return filepath.Join(home, "/.config/ccboc/", filepath.Base(fileName)), nil
}
defaultPath = filepath.Join(defaultPath, filepath.Base(fileName))
Copy link
Member

Choose a reason for hiding this comment

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

There is no need to manipulate this variable that was passed in the function already.

return filepath.Join(home, DefaultConfigPath, fileName), nil
} else {
if _, err := os.Stat(defaultPath); os.IsNotExist(err) {
logrus.Info("Desired path to download the calculations does not exist. The calculations are set to be downloaded into: $HOME/.config/ccboc/{id}")
Copy link
Member

Choose a reason for hiding this comment

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

When logging, the user should know all the information about the problem. The desired path is unclear here. We need to give him the exact path. Also, by handling only the not exists error, we eat all the other potential errors. Instead lets check for all errors and output the error in the screen. There is no need to fallback to the default location, since the user didn't ask for that.

Copy link
Contributor Author

@odvarkadaniel odvarkadaniel Jul 27, 2021

Choose a reason for hiding this comment

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

Changed it to errors.Is(err, os.ErrNotExist). Hopefully that's enough.

Copy link
Member

Choose a reason for hiding this comment

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

No I mean we should check all the errors, and output the error to the user. Don't fall back to the default location.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, thats what I did in the latest commit. When there is an error, then we fail. I dont fall to the def. location anymore.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but you are checking only one error case. We need to fail in any error.

@odvarkadaniel odvarkadaniel force-pushed the download-calc branch 2 times, most recently from 2f0021c to e8bc6cf Compare July 27, 2021 13:13

_, err = os.Stat(defaultPath)
if err != nil {
return "", fmt.Errorf("something is wrong with the download path of the calculation %v", defaultPath)
Copy link
Member

Choose a reason for hiding this comment

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

There is no reason to eat the actual error message with a something is wrong message. We need the user to know exactly why his actions failed. We need something like:

Suggested change
return "", fmt.Errorf("something is wrong with the download path of the calculation %v", defaultPath)
return "", fmt.Errorf("couldn't stat path %s: %v", defaultPath, err)


fileNameHeader := resp.Header.Get("Content-Disposition")
if fileNameHeader == "" {
logrus.WithError(err).Fatal("Unable to get the headers")
Copy link
Member

Choose a reason for hiding this comment

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

The log here is wrong. The err will be nil since there is no error. Also please be more detailed about which header we failed to get.


fileNameHeader := resp.Header.Get("Content-Disposition")
if fileNameHeader == "" {
logrus.WithError(err).Fatal("Unable to get the headers")
Copy link
Member

Choose a reason for hiding this comment

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

Same here

Signed-off-by: Daniel Odvarka <odvarkaadaniel@gmail.com>
@droslean droslean merged commit b2e9d58 into vega-project:main Jul 28, 2021
@odvarkadaniel odvarkadaniel deleted the download-calc branch July 28, 2021 11:09
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