Skip to content

Commit c2978c1

Browse files
authored
Merge pull request #52 from planetlabs/stats
Match the structure of the stats extension
2 parents f2c2ece + fa898bc commit c2978c1

File tree

2 files changed

+81
-24
lines changed

2 files changed

+81
-24
lines changed

cmd/stac/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const (
2323
// version flags
2424
flagVerbose = "verbose"
2525

26+
// stats flags
27+
flagExcludeEntry = "exclude-entry"
28+
2629
// common flags
2730
flagLogLevel = "log-level"
2831
flagEntry = "entry"

cmd/stac/stats.go

+78-24
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ import (
1414
)
1515

1616
type Stats struct {
17-
Catalogs uint64 `json:"catalogs"`
18-
Collections uint64 `json:"collections"`
19-
Items uint64 `json:"items"`
20-
Assets map[string]uint64 `json:"assets"`
21-
Extensions map[string]uint64 `json:"extensions"`
22-
Conformance map[string]uint64 `json:"conformance"`
17+
Catalogs *ResourceStats `json:"catalogs,omitempty"`
18+
Collections *ResourceStats `json:"collections,omitempty"`
19+
Items *ResourceStats `json:"items,omitempty"`
20+
}
21+
22+
type ResourceStats struct {
23+
Count uint64 `json:"count"`
24+
Versions map[string]uint64 `json:"versions,omitempty"`
25+
Extensions map[string]uint64 `json:"extensions,omitempty"`
26+
Conformance map[string]uint64 `json:"conformance,omitempty"`
27+
Assets map[string]uint64 `json:"assets,omitempty"`
2328
}
2429

2530
var statsCommand = &cli.Command{
@@ -32,6 +37,12 @@ var statsCommand = &cli.Command{
3237
Usage: "Path to STAC resource (catalog, collection, or item) to crawl",
3338
EnvVars: []string{toEnvVar(flagEntry)},
3439
},
40+
&cli.BoolFlag{
41+
Name: flagExcludeEntry,
42+
Usage: "Do not count the entry itself",
43+
Value: false,
44+
EnvVars: []string{toEnvVar(flagExcludeEntry)},
45+
},
3546
&cli.IntFlag{
3647
Name: flagConcurrency,
3748
Usage: "Concurrency limit",
@@ -55,11 +66,7 @@ var statsCommand = &cli.Command{
5566
}
5667

5768
mutext := &sync.Mutex{}
58-
stats := &Stats{
59-
Assets: map[string]uint64{},
60-
Extensions: map[string]uint64{},
61-
Conformance: map[string]uint64{},
62-
}
69+
stats := &Stats{}
6370

6471
bar := progressbar.NewOptions64(
6572
-1,
@@ -74,35 +81,82 @@ var statsCommand = &cli.Command{
7481
progressbar.OptionClearOnFinish(),
7582
)
7683

84+
skip := ctx.Bool(flagExcludeEntry)
7785
visitor := func(location string, resource crawler.Resource) error {
86+
if skip {
87+
skip = false
88+
return nil
89+
}
90+
7891
mutext.Lock()
7992
defer mutext.Unlock()
8093

94+
var resourceStats *ResourceStats
95+
8196
switch resource.Type() {
8297
case crawler.Catalog:
83-
stats.Catalogs += 1
84-
case crawler.Collection:
85-
stats.Collections += 1
86-
case crawler.Item:
87-
stats.Items += 1
88-
}
98+
if stats.Catalogs == nil {
99+
stats.Catalogs = &ResourceStats{}
100+
}
101+
for _, conformance := range resource.ConformsTo() {
102+
if stats.Catalogs.Conformance == nil {
103+
stats.Catalogs.Conformance = map[string]uint64{}
104+
}
105+
stats.Catalogs.Conformance[conformance] += 1
106+
}
107+
resourceStats = stats.Catalogs
89108

90-
_ = bar.Add(1)
91-
bar.Describe(fmt.Sprintf("catalogs: %d; collections: %d; items: %d", stats.Catalogs, stats.Collections, stats.Items))
109+
case crawler.Collection:
110+
if stats.Collections == nil {
111+
stats.Collections = &ResourceStats{}
112+
}
113+
resourceStats = stats.Collections
92114

93-
if resource.Type() == crawler.Item {
115+
case crawler.Item:
116+
if stats.Items == nil {
117+
stats.Items = &ResourceStats{}
118+
}
94119
for _, asset := range resource.Assets() {
95-
stats.Assets[asset.Type()] += 1
120+
if stats.Items.Assets == nil {
121+
stats.Items.Assets = map[string]uint64{}
122+
}
123+
stats.Items.Assets[asset.Type()] += 1
96124
}
125+
resourceStats = stats.Items
97126
}
98127

99128
for _, extension := range resource.Extensions() {
100-
stats.Extensions[extension] += 1
129+
if resourceStats.Extensions == nil {
130+
resourceStats.Extensions = map[string]uint64{}
131+
}
132+
resourceStats.Extensions[extension] += 1
133+
}
134+
135+
if resourceStats.Versions == nil {
136+
resourceStats.Versions = map[string]uint64{}
137+
}
138+
resourceStats.Versions[resource.Version()] += 1
139+
140+
resourceStats.Count += 1
141+
142+
catalogs := uint64(0)
143+
if stats.Catalogs != nil {
144+
catalogs = stats.Catalogs.Count
145+
}
146+
147+
collections := uint64(0)
148+
if stats.Collections != nil {
149+
collections = stats.Collections.Count
101150
}
102151

103-
for _, conformance := range resource.ConformsTo() {
104-
stats.Conformance[conformance] += 1
152+
items := uint64(0)
153+
if stats.Items != nil {
154+
items = stats.Items.Count
105155
}
156+
157+
_ = bar.Add(1)
158+
bar.Describe(fmt.Sprintf("catalogs: %d; collections: %d; items: %d", catalogs, collections, items))
159+
106160
return nil
107161
}
108162

0 commit comments

Comments
 (0)