Skip to content

Commit 3179632

Browse files
authored
Promote stats command to stable (#3855)
1 parent 6e898fc commit 3179632

File tree

7 files changed

+76
-77
lines changed

7 files changed

+76
-77
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## [Unreleased]
44

5-
- No changes yet.
5+
- Promote `buf beta stats` to `buf stats`.
66

77
## [v1.54.0] - 2025-05-12
88

@@ -11,7 +11,7 @@
1111
- Add RISC-V (64-bit) binaries for Linux to releases.
1212
- Fix type filtering on `buf generate` for empty files, files with no declared types.
1313
- Fix CEL check on `buf lint` for predefined `rules` variables.
14-
- Fix `buf config migrate` to filter out removed rules.
14+
- Fix `buf config migrate` to filter out removed rules.
1515
- Allow users to set examples without constraints in `PROTOVALIDATE` lint rule.
1616
- Add ppc64le binaries for Linux to releases.
1717

@@ -58,7 +58,7 @@
5858
- Fix `buf plugin push --label` to allow pushing a plugin with a label.
5959
- Add `--digest-changes-only` flag to `buf registry {module,plugin} commit list` to filter
6060
out commits that have no digest changes.
61-
- Fix `buf plugin push --source-control-url` to allow pushing a plugin with the source
61+
- Fix `buf plugin push --source-control-url` to allow pushing a plugin with the source
6262
control url.
6363

6464
## [v1.48.0] - 2024-12-19
@@ -67,7 +67,7 @@
6767
- Breaking analysis support for `buf beta lsp`.
6868
- Fix bug when using the `--type` flag filter for `buf build` where import ordering is not
6969
deterministic.
70-
- Add `buf plugin push` command to push a plugin to the Buf Schema Registry.
70+
- Add `buf plugin push` command to push a plugin to the Buf Schema Registry.
7171
Only WebAssembly check plugins are supported at this time.
7272
- Add `buf plugin update` and `buf plugin prune` command to manage plugins in the `buf.lock`
7373
file. Only WebAssembly check plugins are supported at this time.

private/buf/bufprint/stats_printer.go

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"io"
22-
"strconv"
2322

2423
"github.com/bufbuild/buf/private/pkg/protostat"
2524
)
@@ -37,35 +36,32 @@ func newStatsPrinter(writer io.Writer) *statsPrinter {
3736
func (p *statsPrinter) PrintStats(ctx context.Context, format Format, stats *protostat.Stats) error {
3837
switch format {
3938
case FormatText:
40-
return WithTabWriter(
39+
_, err := fmt.Fprintf(
4140
p.writer,
42-
[]string{
43-
"Files",
44-
"Packages",
45-
"Messages",
46-
"Fields",
47-
"Enums",
48-
"Enum Values",
49-
"Extensions",
50-
"Services",
51-
"Methods",
52-
"Files With Errors",
53-
},
54-
func(tabWriter TabWriter) error {
55-
return tabWriter.Write(
56-
strconv.Itoa(stats.NumFiles),
57-
strconv.Itoa(stats.NumPackages),
58-
strconv.Itoa(stats.NumMessages),
59-
strconv.Itoa(stats.NumFields),
60-
strconv.Itoa(stats.NumEnums),
61-
strconv.Itoa(stats.NumEnumValues),
62-
strconv.Itoa(stats.NumExtensions),
63-
strconv.Itoa(stats.NumServices),
64-
strconv.Itoa(stats.NumMethods),
65-
strconv.Itoa(stats.NumFilesWithSyntaxErrors),
66-
)
67-
},
41+
`Files: %d
42+
Types: %d
43+
Packages: %d
44+
Messages: %d
45+
Fields: %d
46+
Enums: %d
47+
Enum Values: %d
48+
Services: %d
49+
RPCs: %d
50+
Extensions: %d
51+
`,
52+
53+
stats.Files,
54+
stats.Types,
55+
stats.Packages,
56+
stats.Messages,
57+
stats.Fields,
58+
stats.Enums,
59+
stats.EnumValues,
60+
stats.Services,
61+
stats.RPCs,
62+
stats.Extensions,
6863
)
64+
return err
6965
case FormatJSON:
7066
return json.NewEncoder(p.writer).Encode(stats)
7167
default:

private/buf/cmd/buf/buf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/webhook/webhookcreate"
4444
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/webhook/webhookdelete"
4545
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/webhook/webhooklist"
46-
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/stats"
4746
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/studioagent"
4847
"github.com/bufbuild/buf/private/buf/cmd/buf/command/breaking"
4948
"github.com/bufbuild/buf/private/buf/cmd/buf/command/build"
@@ -104,6 +103,7 @@ import (
104103
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/registrylogout"
105104
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/sdk/version"
106105
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/whoami"
106+
"github.com/bufbuild/buf/private/buf/cmd/buf/command/stats"
107107
"github.com/bufbuild/buf/private/bufpkg/bufcobra"
108108
"github.com/bufbuild/buf/private/bufpkg/bufconnect"
109109
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
@@ -141,6 +141,7 @@ func NewRootCommand(name string) *appcmd.Command {
141141
breaking.NewCommand("breaking", builder),
142142
generate.NewCommand("generate", builder),
143143
lsfiles.NewCommand("ls-files", builder),
144+
stats.NewCommand("stats", builder),
144145
push.NewCommand("push", builder),
145146
convert.NewCommand("convert", builder),
146147
curl.NewCommand("curl", builder),
@@ -327,7 +328,6 @@ func NewRootCommand(name string) *appcmd.Command {
327328
SubCommands: []*appcmd.Command{
328329
lsp.NewCommand("lsp", builder),
329330
price.NewCommand("price", builder),
330-
stats.NewCommand("stats", builder),
331331
bufpluginv1beta1.NewCommand("buf-plugin-v1beta1", builder),
332332
bufpluginv1.NewCommand("buf-plugin-v1", builder),
333333
bufpluginv2.NewCommand("buf-plugin-v2", builder),

private/buf/cmd/buf/command/beta/price/price.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ and does not interact with our servers.
4848
4949
Your sources have:
5050
51-
- {{.NumMessages}} messages
52-
- {{.NumEnums}} enums
53-
- {{.NumMethods}} methods
51+
- {{.Messages}} messages
52+
- {{.Enums}} enums
53+
- {{.RPCs}} RPCs
5454
55-
This adds up to {{.NumTypes}} types.
55+
This adds up to {{.Types}} types.
5656
5757
Based on this, these sources will cost:
5858
@@ -149,17 +149,15 @@ func run(
149149
type tmplData struct {
150150
*protostat.Stats
151151

152-
NumTypes int
153152
TeamsDollarsPerMonth string
154153
ProDollarsPerMonth string
155154
}
156155

157156
func newTmplData(stats *protostat.Stats) *tmplData {
158157
tmplData := &tmplData{
159-
Stats: stats,
160-
NumTypes: stats.NumMessages + stats.NumEnums + stats.NumMethods,
158+
Stats: stats,
161159
}
162-
tmplData.TeamsDollarsPerMonth = fmt.Sprintf("%.2f", float64(tmplData.NumTypes)*teamsDollarsPerType)
163-
tmplData.ProDollarsPerMonth = fmt.Sprintf("%.2f", math.Max(float64(tmplData.NumTypes)*proDollarsPerType, proDollarsMinimumSpend))
160+
tmplData.TeamsDollarsPerMonth = fmt.Sprintf("%.2f", float64(tmplData.Types)*teamsDollarsPerType)
161+
tmplData.ProDollarsPerMonth = fmt.Sprintf("%.2f", math.Max(float64(tmplData.Types)*proDollarsPerType, proDollarsMinimumSpend))
164162
return tmplData
165163
}
File renamed without changes.
File renamed without changes.

private/pkg/protostat/protostat.go

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ import (
2828
// Note that as opposed to most structs in this codebase, we do not omitempty for
2929
// the fields for JSON or YAML.
3030
type Stats struct {
31-
NumFiles int `json:"num_files" yaml:"num_files"`
32-
NumPackages int `json:"num_packages" yaml:"num_packages"`
33-
NumFilesWithSyntaxErrors int `json:"num_files_with_syntax_errors" yaml:"num_files_with_syntax_errors"`
34-
NumMessages int `json:"num_messages" yaml:"num_messages"`
35-
NumFields int `json:"num_fields" yaml:"num_fields"`
36-
NumEnums int `json:"num_enums" yaml:"num_enums"`
37-
NumEnumValues int `json:"num_enum_values" yaml:"num_enum_values"`
38-
NumExtensions int `json:"num_extensions" yaml:"num_extensions"`
39-
NumServices int `json:"num_services" yaml:"num_services"`
40-
NumMethods int `json:"num_methods" yaml:"num_methods"`
31+
Files int `json:"files" yaml:"files"`
32+
Types int `json:"types" yaml:"types"`
33+
Packages int `json:"packages" yaml:"packages"`
34+
Messages int `json:"messages" yaml:"messages"`
35+
Fields int `json:"fields" yaml:"fields"`
36+
Enums int `json:"enums" yaml:"enums"`
37+
EnumValues int `json:"evalues" yaml:"evalues"`
38+
Services int `json:"services" yaml:"services"`
39+
RPCs int `json:"rpcs" yaml:"rpcs"`
40+
Extensions int `json:"extensions" yaml:"extensions"`
41+
FilesWithSyntaxErrors int `json:"-" yaml:"-"`
4142
}
4243

4344
// FileWalker goes through all .proto files for GetStats.
@@ -75,15 +76,15 @@ func GetStats(ctx context.Context, fileWalker FileWalker) (*Stats, error) {
7576
if err != nil {
7677
// There was a syntax error, but we still have a partial
7778
// AST we can examine.
78-
statsBuilder.NumFilesWithSyntaxErrors++
79+
statsBuilder.FilesWithSyntaxErrors++
7980
}
8081
examineFile(statsBuilder, astRoot)
8182
return nil
8283
},
8384
); err != nil {
8485
return nil, err
8586
}
86-
statsBuilder.NumPackages = len(statsBuilder.packages)
87+
statsBuilder.Packages = len(statsBuilder.packages)
8788
return statsBuilder.Stats, nil
8889
}
8990

@@ -93,16 +94,17 @@ func GetStats(ctx context.Context, fileWalker FileWalker) (*Stats, error) {
9394
func MergeStats(statsSlice ...*Stats) *Stats {
9495
resultStats := &Stats{}
9596
for _, stats := range statsSlice {
96-
resultStats.NumFiles += stats.NumFiles
97-
resultStats.NumPackages += stats.NumPackages
98-
resultStats.NumFilesWithSyntaxErrors += stats.NumFilesWithSyntaxErrors
99-
resultStats.NumMessages += stats.NumMessages
100-
resultStats.NumFields += stats.NumFields
101-
resultStats.NumEnums += stats.NumEnums
102-
resultStats.NumEnumValues += stats.NumEnumValues
103-
resultStats.NumExtensions += stats.NumExtensions
104-
resultStats.NumServices += stats.NumServices
105-
resultStats.NumMethods += stats.NumMethods
97+
resultStats.Files += stats.Files
98+
resultStats.FilesWithSyntaxErrors += stats.FilesWithSyntaxErrors
99+
resultStats.Packages += stats.Packages
100+
resultStats.Types += stats.Types
101+
resultStats.Messages += stats.Messages
102+
resultStats.Fields += stats.Fields
103+
resultStats.Enums += stats.Enums
104+
resultStats.EnumValues += stats.EnumValues
105+
resultStats.Services += stats.Services
106+
resultStats.RPCs += stats.RPCs
107+
resultStats.Extensions += stats.Extensions
106108
}
107109
return resultStats
108110
}
@@ -121,7 +123,7 @@ func newStatsBuilder() *statsBuilder {
121123
}
122124

123125
func examineFile(statsBuilder *statsBuilder, fileNode *ast.FileNode) {
124-
statsBuilder.NumFiles++
126+
statsBuilder.Files++
125127
for _, decl := range fileNode.Decls {
126128
switch decl := decl.(type) {
127129
case *ast.PackageNode:
@@ -133,33 +135,35 @@ func examineFile(statsBuilder *statsBuilder, fileNode *ast.FileNode) {
133135
case *ast.ExtendNode:
134136
examineExtend(statsBuilder, decl)
135137
case *ast.ServiceNode:
136-
statsBuilder.NumServices++
138+
statsBuilder.Services++
137139
for _, decl := range decl.Decls {
138140
_, ok := decl.(*ast.RPCNode)
139141
if ok {
140-
statsBuilder.NumMethods++
142+
statsBuilder.RPCs++
143+
statsBuilder.Types++
141144
}
142145
}
143146
}
144147
}
145148
}
146149

147150
func examineMessage(statsBuilder *statsBuilder, messageBody *ast.MessageBody) {
148-
statsBuilder.NumMessages++
151+
statsBuilder.Messages++
152+
statsBuilder.Types++
149153
for _, decl := range messageBody.Decls {
150154
switch decl := decl.(type) {
151155
case *ast.FieldNode, *ast.MapFieldNode:
152-
statsBuilder.NumFields++
156+
statsBuilder.Fields++
153157
case *ast.GroupNode:
154-
statsBuilder.NumFields++
158+
statsBuilder.Fields++
155159
examineMessage(statsBuilder, &decl.MessageBody)
156160
case *ast.OneofNode:
157161
for _, ooDecl := range decl.Decls {
158162
switch ooDecl := ooDecl.(type) {
159163
case *ast.FieldNode:
160-
statsBuilder.NumFields++
164+
statsBuilder.Fields++
161165
case *ast.GroupNode:
162-
statsBuilder.NumFields++
166+
statsBuilder.Fields++
163167
examineMessage(statsBuilder, &ooDecl.MessageBody)
164168
}
165169
}
@@ -174,11 +178,12 @@ func examineMessage(statsBuilder *statsBuilder, messageBody *ast.MessageBody) {
174178
}
175179

176180
func examineEnum(statsBuilder *statsBuilder, enumNode *ast.EnumNode) {
177-
statsBuilder.NumEnums++
181+
statsBuilder.Enums++
182+
statsBuilder.Types++
178183
for _, decl := range enumNode.Decls {
179184
_, ok := decl.(*ast.EnumValueNode)
180185
if ok {
181-
statsBuilder.NumEnumValues++
186+
statsBuilder.EnumValues++
182187
}
183188
}
184189
}
@@ -187,9 +192,9 @@ func examineExtend(statsBuilder *statsBuilder, extendNode *ast.ExtendNode) {
187192
for _, decl := range extendNode.Decls {
188193
switch decl := decl.(type) {
189194
case *ast.FieldNode:
190-
statsBuilder.NumExtensions++
195+
statsBuilder.Extensions++
191196
case *ast.GroupNode:
192-
statsBuilder.NumExtensions++
197+
statsBuilder.Extensions++
193198
examineMessage(statsBuilder, &decl.MessageBody)
194199
}
195200
}

0 commit comments

Comments
 (0)