Skip to content

Commit

Permalink
Add total downloads badge
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Aug 27, 2020
1 parent f04b94f commit d40efa5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion internal/handlers/api/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ func (h *Handler) download(c *clevergo.Context) error {
return err
}

badge := shields.New("downloads", fmt.Sprintf("%s/%s", stringhelper.ShortScale(count), interval))
var downloads string
if interval == "total" {
downloads = stringhelper.ShortScale(count)
} else {
downloads = fmt.Sprintf("%s/%s", stringhelper.ShortScale(count), interval)
}

badge := shields.New("downloads", downloads)
badge.Color = shields.ColorBrightGreen
if err := badge.ParseRequest(c.Request); err != nil {
return err
Expand All @@ -53,6 +60,8 @@ func (h *Handler) getDownloads(ctx context.Context, interval string, id int64) (
fromDate = fromDate.AddDate(0, 0, -6)
case "month":
fromDate = fromDate.AddDate(0, 0, -29)
case "total":
fromDate = time.Time{}
default:
return 0, fmt.Errorf("invalid interval parameter")
}
Expand Down
1 change: 1 addition & 0 deletions views/badge/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<option>day</option>
<option>week</option>
<option selected>month</option>
<option>total</option>
</select>

<input class="form-control mb-2 mr-2" name="package" placeholder="Package" onblur="loadBadge()">
Expand Down
1 change: 1 addition & 0 deletions views/package/view.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<img src="{{ siteURL }}/badges/downloads/day/{{ .pkg.Prefix() }}" />
<img src="{{ siteURL }}/badges/downloads/week/{{ .pkg.Prefix() }}" />
<img src="{{ siteURL }}/badges/downloads/month/{{ .pkg.Prefix() }}" />
<img src="{{ siteURL }}/badges/downloads/total/{{ .pkg.Prefix() }}" />
</p>
</h1>
{{ if .pkg.Description }}
Expand Down

0 comments on commit d40efa5

Please sign in to comment.