Skip to content

Commit

Permalink
listing active session
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin.daviot authored and moul committed Nov 16, 2018
1 parent e860b60 commit 2ea73a9
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,7 @@ GLOBAL OPTIONS:
Usage: "Lists sessions",
Flags: []cli.Flag{
cli.BoolFlag{Name: "latest, l", Usage: "Show the latest session"},
cli.BoolFlag{Name: "active, a", Usage: "Show only active session"},
cli.BoolFlag{Name: "quiet, q", Usage: "Only display IDs"},
},
Action: func(c *cli.Context) error {
Expand All @@ -2074,7 +2075,14 @@ GLOBAL OPTIONS:
}

var sessions []*Session
query := db.Order("created_at desc").Preload("User").Preload("Host")

limit, offset, status := 60000, -1, []string{string(SessionStatusActive), string(SessionStatusClosed), string(SessionStatusUnknown)}
if c.Bool("active") {
status = status[:1]
}

query := db.Order("created_at desc").Limit(limit).Offset(offset).Where("status in (?)", status).Preload("User").Preload("Host")

if c.Bool("latest") {
var session Session
if err := query.First(&session).Error; err != nil {
Expand All @@ -2085,6 +2093,20 @@ GLOBAL OPTIONS:
if err := query.Find(&sessions).Error; err != nil {
return err
}

factor := 1
for len(sessions) >= limit*factor {

var additionnalSessions []*Session

offset = limit * factor
query := db.Order("created_at desc").Limit(limit).Offset(offset).Where("status in (?)", status).Preload("User").Preload("Host")
if err := query.Find(&additionnalSessions).Error; err != nil {
return err
}
sessions = append(sessions, additionnalSessions...)
factor++
}
}
if c.Bool("quiet") {
for _, session := range sessions {
Expand Down

0 comments on commit 2ea73a9

Please sign in to comment.