pgterminate/base/session.go

60 lines
1.3 KiB
Go
Raw Normal View History

2018-06-10 08:44:53 +02:00
package base
import (
"fmt"
"strings"
)
// Session represents a PostgreSQL backend
type Session struct {
Pid int64
User string
Db string
Client string
State string
Query string
StateDuration float64
2018-06-10 08:44:53 +02:00
}
// NewSession instanciates a Session
2018-07-08 23:48:48 +02:00
func NewSession(pid int64, user string, db string, client string, state string, query string, stateDuration float64) *Session {
return &Session{
Pid: pid,
User: user,
Db: db,
Client: client,
State: state,
Query: query,
StateDuration: stateDuration,
2018-06-10 08:44:53 +02:00
}
}
2019-02-16 11:47:30 +01:00
// Format returns a Session as a string by replacing placeholders with their respective value
func (s *Session) Format(format string) string {
definitions := map[string]string{
"%p": fmt.Sprintf("%d", s.Pid),
"%u": s.User,
"%d": s.Db,
"%r": s.Client,
"%s": s.State,
"%m": fmt.Sprintf("%f", s.StateDuration),
"%q": s.Query,
2018-06-10 08:44:53 +02:00
}
2019-02-16 11:47:30 +01:00
output := format
for placeholder, value := range definitions {
output = strings.Replace(output, placeholder, value, -1)
2018-06-10 08:44:53 +02:00
}
2019-02-16 11:47:30 +01:00
return output
2018-06-10 08:44:53 +02:00
}
// IsIdle returns true when a session is doing nothing
2018-07-08 23:48:48 +02:00
func (s *Session) IsIdle() bool {
if s.State == "idle" || s.State == "idle in transaction" || s.State == "idle in transaction (aborted)" {
return true
}
return false
}