|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "github.com/planetlabs/go-stac/pkg/crawler" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | + "github.com/urfave/cli/v2" |
| 14 | +) |
| 15 | + |
| 16 | +type Stats struct { |
| 17 | + Catalogs uint64 `json:"catalogs"` |
| 18 | + Collections uint64 `json:"collections"` |
| 19 | + Items uint64 `json:"items"` |
| 20 | + Extensions map[string]uint64 `json:"extensions"` |
| 21 | +} |
| 22 | + |
| 23 | +var statsCommand = &cli.Command{ |
| 24 | + Name: "stats", |
| 25 | + Usage: "Generate STAC statistics", |
| 26 | + Description: "Crawls STAC resources and reports on statistics.", |
| 27 | + Flags: []cli.Flag{ |
| 28 | + &cli.StringFlag{ |
| 29 | + Name: flagEntry, |
| 30 | + Usage: "Path to STAC resource (catalog, collection, or item) to stats", |
| 31 | + EnvVars: []string{toEnvVar(flagEntry)}, |
| 32 | + }, |
| 33 | + &cli.IntFlag{ |
| 34 | + Name: flagConcurrency, |
| 35 | + Usage: "Concurrency limit", |
| 36 | + Value: crawler.DefaultOptions.Concurrency, |
| 37 | + EnvVars: []string{toEnvVar(flagConcurrency)}, |
| 38 | + }, |
| 39 | + &cli.GenericFlag{ |
| 40 | + Name: flagRecursion, |
| 41 | + Usage: fmt.Sprintf("Recursion type (%s)", strings.Join(recursionValues, ", ")), |
| 42 | + Value: &Enum{ |
| 43 | + Values: recursionValues, |
| 44 | + Default: string(crawler.DefaultOptions.Recursion), |
| 45 | + }, |
| 46 | + EnvVars: []string{toEnvVar(flagRecursion)}, |
| 47 | + }, |
| 48 | + &cli.GenericFlag{ |
| 49 | + Name: flagLogLevel, |
| 50 | + Usage: fmt.Sprintf("Log level (%s)", strings.Join(logLevelValues, ", ")), |
| 51 | + Value: &Enum{ |
| 52 | + Values: logLevelValues, |
| 53 | + Default: logrus.InfoLevel.String(), |
| 54 | + }, |
| 55 | + EnvVars: []string{toEnvVar(flagLogLevel)}, |
| 56 | + }, |
| 57 | + &cli.GenericFlag{ |
| 58 | + Name: flagLogFormat, |
| 59 | + Usage: fmt.Sprintf("Log format (%s)", strings.Join(logFormatValues, ", ")), |
| 60 | + Value: &Enum{ |
| 61 | + Values: logFormatValues, |
| 62 | + Default: logFormatText, |
| 63 | + }, |
| 64 | + EnvVars: []string{toEnvVar(flagLogFormat)}, |
| 65 | + }, |
| 66 | + }, |
| 67 | + Action: func(ctx *cli.Context) error { |
| 68 | + if err := configureLogger(ctx); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + entryPath := ctx.String(flagEntry) |
| 73 | + if entryPath == "" { |
| 74 | + return fmt.Errorf("missing --%s", flagEntry) |
| 75 | + } |
| 76 | + |
| 77 | + mutext := &sync.Mutex{} |
| 78 | + stats := &Stats{Extensions: map[string]uint64{}} |
| 79 | + |
| 80 | + visitor := func(location string, resource crawler.Resource) error { |
| 81 | + mutext.Lock() |
| 82 | + defer mutext.Unlock() |
| 83 | + |
| 84 | + switch resource.Type() { |
| 85 | + case crawler.Catalog: |
| 86 | + stats.Catalogs += 1 |
| 87 | + case crawler.Collection: |
| 88 | + stats.Collections += 1 |
| 89 | + case crawler.Item: |
| 90 | + stats.Items += 1 |
| 91 | + } |
| 92 | + |
| 93 | + for _, extension := range resource.Extensions() { |
| 94 | + count := stats.Extensions[extension] |
| 95 | + stats.Extensions[extension] = count + 1 |
| 96 | + } |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + c := crawler.NewWithOptions(entryPath, visitor, &crawler.Options{ |
| 101 | + Concurrency: ctx.Int(flagConcurrency), |
| 102 | + Recursion: crawler.RecursionType(ctx.String(flagRecursion)), |
| 103 | + }) |
| 104 | + |
| 105 | + err := c.Crawl(context.Background()) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + return json.NewEncoder(os.Stdout).Encode(stats) |
| 111 | + }, |
| 112 | +} |
0 commit comments