Skip to content

📊 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.

go
type StatisticsResponse struct {
    Totals Totals `json:"totals"`
}
FieldTypeDescription
TotalsTotalsRepository total statistics

Totals

go
type Totals struct {
    Downloads int64 `json:"downloads"`
    Packages  int   `json:"packages"`
    Versions  int   `json:"versions"`
}
FieldTypeDescription
Downloadsint64Total download count for all packages (large value, use int64 to hold)
PackagesintTotal packages in repository
VersionsintTotal 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

go
func (c *ComposerClient) GetStatistics() (*domain.StatisticsResponse, error)

Parameters

None.

Return Values

ValueTypeDescription
Result*domain.StatisticsResponseContains Totals (downloads/packages/versions totals)
ErrorerrorReturned on HTTP failure, non-200, or JSON parsing failure

Example

go
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:

text
Cumulative downloads: 12345678901
Total packages:   395421
Total versions: 2876543

Advanced Topics

As Health Probe

GetStatistics has no parameters, small response, stable endpoint, very suitable as a "can access Packagist" probe:

go
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.

Released under the MIT License