Skip to content

Commit

Permalink
bug/pypi bq api change (#215)
Browse files Browse the repository at this point in the history
* Use a JSON encoder instead of sprintf-ing JSON

* More useful error messages

* "downloads" field is now a structure
  • Loading branch information
blast-hardcheese committed Jan 19, 2024
1 parent 9baa501 commit 3bbe9a1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions internal/backends/python/gen_pypi_map/download_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func GetPypiStats(projectID string) (map[string]int, error) {
ctx := context.Background()
client, err := bigquery.NewClient(ctx, projectID)
if err != nil {
return map[string]int{}, fmt.Errorf("Failed to connect to bigquery: %v", err)
return map[string]int{}, fmt.Errorf("Failed to connect to bigquery [%s]: %v", projectID, err)
}

q := client.Query(
Expand All @@ -88,7 +88,7 @@ func GetPypiStats(projectID string) (map[string]int, error) {
if err != nil {
return packages, err
}
packages[info.Name] = info.Downloads
packages[info.Name] = info.Downloads.LastMonth
}

return packages, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/backends/python/gen_pypi_map/test_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestModules(packages PackageIndex, bigqueryFile string, cacheDir string, pk
packageInfo, err := ProcessPackage(packageName, cache, cacheDir, distMods, force)
packageInfo.Name = packageName
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to process package: %s\n", err.Error())
fmt.Fprintf(os.Stderr, "Failed to process package [%v]: %v\n", packageName, err)
packageInfo.Error = err.Error()
}
resultQueue <- packageInfo
Expand Down
34 changes: 27 additions & 7 deletions internal/backends/python/gen_pypi_map/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package main

import "fmt"
import (
"encoding/json"
"fmt"
)

type PackageCache = map[string]PackageInfo

type DownloadsInfo struct {
LastDay int `json:"last_day"`
LastWeek int `json:"last_week"`
LastMonth int `json:"last_month"`
}

type PackageInfo struct {
Name string `json:"name,omitempty"`
Downloads int `json:"downloads,string,omitempty"`
Version string `json:"version,omitempty"`
RequiresDist []string `json:"requires_dist,omitempty"`
Name string `json:"name,omitempty"`
Downloads DownloadsInfo `json:"downloads,omitempty"`
Version string `json:"version,omitempty"`
RequiresDist []string `json:"requires_dist,omitempty"`

// Specific to the dist we use to get modules from
Modules []string `json:"modules,omitempty"`
Expand All @@ -31,7 +40,7 @@ type PackageURL struct {
}

type PackageData struct {
Info PackageInfo
Info PackageInfo `json:"info"`
Releases map[string][]PackageURL
}

Expand Down Expand Up @@ -60,5 +69,16 @@ func (e PypiError) Error() string {
"Failed to download: " + e.Info,
"Failed to install: " + e.Info,
}[e.Class]
return fmt.Sprintf("{\"type\": %v, \"message\": \"%v\"}", e.Class, message)
jsonError := map[string]interface{}{
"type": e.Class,
"message": message,
}
if e.WrappedError != nil {
jsonError["wrapped"] = e.WrappedError.Error()
}
encodedError, err := json.Marshal(jsonError)
if err != nil {
return fmt.Sprintf("{\"error\": \"%v\"}", err)
}
return string(encodedError)
}

0 comments on commit 3bbe9a1

Please sign in to comment.