eventstore: cli "count" command.
This commit is contained in:
42
eventstore/cmd/eventstore/count.go
Normal file
42
eventstore/cmd/eventstore/count.go
Normal 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
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -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,
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user