📊 Statistics
Get overall statistics for Packagist repository: total downloads, total packages, total versions. Suitable for ecosystem overview, growth trend monitoring, homepage data display.
When to Use
- 📈 Display Packagist ecosystem scale on dashboard homepage ("N packages indexed, M cumulative downloads").
- 📊 Periodically collect statistics, plot ecosystem growth curve.
- 🧪 Health check: Quickly verify if Packagist is accessible.
- 🔍 Compare with self-hosted mirror's local data, confirm sync completeness.
Data Models
StatisticsResponse
pkg/domain/statistics.go, corresponds to https://packagist.org/statistics.json response.
type StatisticsResponse struct {
Totals Totals `json:"totals"`
}| Field | Type | Description |
|---|---|---|
Totals | Totals | Repository total statistics |
Totals
type Totals struct {
Downloads int64 `json:"downloads"`
Packages int `json:"packages"`
Versions int `json:"versions"`
}| Field | Type | Description |
|---|---|---|
Downloads | int64 | Total download count for all packages (large value, use int64 to hold) |
Packages | int | Total packages in repository |
Versions | int | Total versions for all packages |
Why Downloads Uses int64?
Packagist cumulative downloads have long exceeded int32 upper limit (about 2.1 billion), so this field uses int64 to avoid overflow on 32-bit platforms.
GetStatistics
📊 Get overall statistics for Packagist repository. Corresponds to GET https://packagist.org/statistics.json.
Signature
func (c *ComposerClient) GetStatistics() (*domain.StatisticsResponse, error)Parameters
None.
Return Values
| Value | Type | Description |
|---|---|---|
| Result | *domain.StatisticsResponse | Contains Totals (downloads/packages/versions totals) |
| Error | error | Returned on HTTP failure, non-200, or JSON parsing failure |
Example
package main
import (
"fmt"
"log"
"time"
"github.com/scagogogo/composer-skills/pkg/client"
)
func main() {
c := client.NewComposerClient(30 * time.Second)
stats, err := c.GetStatistics()
if err != nil {
log.Fatalf("Failed to get statistics: %v", err)
}
fmt.Printf("Cumulative downloads: %d\n", stats.Totals.Downloads)
fmt.Printf("Total packages: %d\n", stats.Totals.Packages)
fmt.Printf("Total versions: %d\n", stats.Totals.Versions)
}Sample output:
Cumulative downloads: 12345678901
Total packages: 395421
Total versions: 2876543Advanced Topics
As Health Probe
GetStatistics has no parameters, small response, stable endpoint, very suitable as a "can access Packagist" probe:
func checkPackagist(c *client.ComposerClient) bool {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Note: GetStatistics itself doesn't accept ctx, here only timeout demo;
// If need ctx control, use lower-level Repository.Statistics(ctx).
_, err := c.GetStatistics()
return err == nil
}No context Accepted
ComposerClient.GetStatistics() signature doesn't include context.Context, timeout is controlled by overall timeout passed to NewComposerClient(timeout). If you need request-level cancellation / timeout, use lower-level Repository.Statistics(ctx), which accepts context.Context.
🔗 Related
- 📦 Single package download statistics see GetPackageStats.
- 🏗️ Lower-level implementation see Repository.Statistics.