Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
List streams
Browse files Browse the repository at this point in the history
  • Loading branch information
scoiatael committed Mar 9, 2017
1 parent ecec5b8 commit a7cf80b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions actions/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ func (hs HttpServer) Run(c Context) error {
handler.Get("/_check", func(ctx http.GetContext) {
ctx.SendJson("OK")
})
handler.Get("/streams", func(ctx http.GetContext) {
session, err := c.Persistence().Session()
err = errors.Wrap(err, "Obtaining session failed")
if err != nil {
c.HandleErr(err)
ctx.ServerErr(err)
return
}
streams, err := session.ListStreams()
view := make(simplejson.Object)
view["streams"] = streams
ctx.SendJson(view)
})
handler.Get("/stream/:id", func(ctx http.GetContext) {
stream := ctx.GetSegment("id")
action := ReadEvents{Stream: stream}
Expand Down
26 changes: 26 additions & 0 deletions actions/list_streams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package actions

import (
"fmt"

"github.com/pkg/errors"
)

type ListStreams struct {
}

func (re ListStreams) Run(c Context) error {
session, err := c.Persistence().Session()
if err != nil {
return errors.Wrap(err, "Obtaining session failed")
}
streams, err := session.ListStreams()
for _, s := range streams {
println(s)
}
return errors.Wrap(err, fmt.Sprintf("Error listing streams"))
}

func (re ListStreams) MarshalJSON() ([]byte, error) {
return []byte(`"List streams"`), nil
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func main() {
Value: "",
Usage: "Cassandra keyspace replication options",
},
cli.BoolFlag{
Name: "list-streams",
Usage: "List streams in Cassandra",
},
}
app.Action = func(c *cli.Context) error {
config := Config{}
Expand All @@ -64,6 +68,9 @@ func main() {
if c.Bool("migrate") {
config.Append(actions.Migrate{})
}
if c.Bool("list-streams") {
config.Append(actions.ListStreams{})
}
if c.Bool("dev-logger") {
config.Features["dev_logger"] = true
}
Expand Down
14 changes: 14 additions & 0 deletions persistence/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Session interface {
WriteEvent(string, []byte, map[string]string) error
ReadEvents(string, string, int) ([]types.Event, error)
ListStreams() ([]string, error)
Close()
}

Expand Down Expand Up @@ -59,3 +60,16 @@ func (sess *CassandraSession) ReadEvents(stream string, cursor string, amount in
err = iter.Close()
return events, errors.Wrap(err, "Failed readEvent")
}

const listStreams = `SELECT DISTINCT stream FROM events`

func (sess *CassandraSession) ListStreams() ([]string, error) {
iter := sess.Query(listStreams).Iter()
rows, err := iter.SliceMap()
streams := make([]string, len(rows))
for i, r := range rows {
streams[i] = r["stream"].(string)
}
err = iter.Close()
return streams, errors.Wrap(err, "Failed listStreams")
}

0 comments on commit a7cf80b

Please sign in to comment.