eventstore: cli "count" command.

This commit is contained in:
fiatjaf
2025-08-22 14:08:24 -03:00
parent 3eaf02ff6a
commit 6737398375
3 changed files with 45 additions and 2 deletions

View File

@@ -0,0 +1,42 @@
package main
import (
"context"
"fmt"
"os"
"fiatjaf.com/nostr"
"github.com/mailru/easyjson"
"github.com/urfave/cli/v3"
)
var count = &cli.Command{
Name: "count",
ArgsUsage: "[<filter-json>]",
Usage: "counts all events that match a given filter",
Description: "applies the filter to the currently open eventstore, counting the results",
Action: func(ctx context.Context, c *cli.Command) error {
hasError := false
for line := range getStdinLinesOrFirstArgument(c) {
filter := nostr.Filter{}
if err := easyjson.Unmarshal([]byte(line), &filter); err != nil {
fmt.Fprintf(os.Stderr, "invalid filter '%s': %s\n", line, err)
hasError = true
continue
}
res, err := db.CountEvents(filter)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to count '%s': %s\n", filter, err)
hasError = true
continue
}
fmt.Println(res)
}
if hasError {
os.Exit(123)
}
return nil
},
}

View File

@@ -115,6 +115,7 @@ var app = &cli.Command{
Commands: []*cli.Command{ Commands: []*cli.Command{
queryOrSave, queryOrSave,
query, query,
count,
save, save,
delete_, delete_,
neg, neg,

View File

@@ -14,7 +14,7 @@ var query = &cli.Command{
Name: "query", Name: "query",
ArgsUsage: "[<filter-json>]", ArgsUsage: "[<filter-json>]",
Usage: "queries an eventstore for events, takes a filter as argument", Usage: "queries an eventstore for events, takes a filter as argument",
Description: "applies the filter to the currently open eventstore, returning up to a million events.\n takes either a filter as an argument or reads a stream of filters from stdin.", Description: "applies the filter to the currently open eventstore, returning up to 10 million events.\n takes either a filter as an argument or reads a stream of filters from stdin.",
Action: func(ctx context.Context, c *cli.Command) error { Action: func(ctx context.Context, c *cli.Command) error {
hasError := false hasError := false
for line := range getStdinLinesOrFirstArgument(c) { for line := range getStdinLinesOrFirstArgument(c) {
@@ -25,7 +25,7 @@ var query = &cli.Command{
continue continue
} }
for evt := range db.QueryEvents(filter, 1_000_000) { for evt := range db.QueryEvents(filter, 10_000_000) {
fmt.Println(evt) fmt.Println(evt)
} }
} }