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

refactor: daily now uses the fact api can take multiple packages #3

Merged
merged 1 commit into from
Jan 24, 2022

Conversation

dpastoor
Copy link
Contributor

@dpastoor dpastoor commented Jan 21, 2022

This refactors daily to take advantage of don't need to make multiple requests as its valid to include a comma separated package list

This is also why (to your question/comment in the code) why even for a single package it returns an array, to keep the return structure consistent whether single or multiple packages (i presume at least)

In addition, I added the conditional capability to return json - this would make it easier to interact with this in programmatic settings/compose with other tools

…turn json

api docs noted that you can provide a comma separated list
of packages and it'll return the array
@dpastoor
Copy link
Contributor Author

some tests:

regular:

❯ go run main.go daily dplyr ggplot2
Package:  dplyr 
2022-01-13: 55223
2022-01-14: 50035
2022-01-15: 28902
2022-01-16: 28955
2022-01-17: 46963
2022-01-18: 55283
2022-01-19: 59652
Package:  ggplot2 
2022-01-13: 73446
2022-01-14: 70471
2022-01-15: 50345
2022-01-16: 50184
2022-01-17: 67054
2022-01-18: 74793
2022-01-19: 81060

with json:

❯ go run main.go daily dplyr ggplot2 --json
[
  {
    "downloads": [
      {
        "day": "2022-01-13",
        "downloads": 55223
      },
      {
        "day": "2022-01-14",
        "downloads": 50035
      },
      {
        "day": "2022-01-15",
        "downloads": 28902
      },
      {
        "day": "2022-01-16",
        "downloads": 28955
      },
      {
        "day": "2022-01-17",
        "downloads": 46963
      },
      {
        "day": "2022-01-18",
        "downloads": 55283
      },
      {
        "day": "2022-01-19",
        "downloads": 59652
      }
    ],
    "start": "2022-01-13",
    "end": "2022-01-19",
    "package": "dplyr"
  },
  {
    "downloads": [
      {
        "day": "2022-01-13",
        "downloads": 73446
      },
      {
        "day": "2022-01-14",
        "downloads": 70471
      },
      {
        "day": "2022-01-15",
        "downloads": 50345
      },
      {
        "day": "2022-01-16",
        "downloads": 50184
      },
      {
        "day": "2022-01-17",
        "downloads": 67054
      },
      {
        "day": "2022-01-18",
        "downloads": 74793
      },
      {
        "day": "2022-01-19",
        "downloads": 81060
      }
    ],
    "start": "2022-01-13",
    "end": "2022-01-19",
    "package": "ggplot2"
  }
]

nonsense package names just return 0 downloads, rather than an error, so "error checking" for invalid packages isn't really a thing. r-hub/cranlogs.app#18

I also filed: r-hub/cranlogs.app#41 which came up when I originally removed a "/" to do a gut check on the status code properly erroring (it didn't) then was getting mysterious marshalling errors instead

@dpastoor
Copy link
Contributor Author

kk the 404 is now added in: r-hub/cranlogs.app#42

@@ -18,31 +19,36 @@ type DailyDownload struct {
Downloads int `json:"downloads"`
}

func GetDaily(period, pkg string) (Daily, error) {
func GetDaily(period Period, pkgs []string) ([]Daily, error) {
// weirdly the API returns an array of length 1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is because of that comma separated packages - keeps the same data structure if 1 package or many..... note: need to update to remove the comment now

Copy link
Member

Choose a reason for hiding this comment

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

I did not know we could pass multiple packages

Copy link
Contributor Author

@dpastoor dpastoor left a comment

Choose a reason for hiding this comment

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

some comments

// https://github.com/r-hub/cranlogs.app/issues/41 is resolved but at least
// its there
if resp.StatusCode != 200 {
return daily, err
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is.... bare minimum ok - ideally would consider more about what alternative responses could come in then handle them. One huge issue is essentially this will currently swallow the result that isn't an error. So for example, the boy json with the error message currently is being swallowed - so this needs to get refactored to likely generate a better, processed error.

}

err = json.Unmarshal(body, &daily)

// if this errors given the current 200 status code returning an error response
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is now (partially) fixed due to the new deployment that does 404 (at least for some cases)

@JohnCoene JohnCoene merged commit cf1595d into devOpifex:master Jan 24, 2022
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