pgterminate/base/session.go

81 lines
1.9 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
ApplicationName string
2018-06-10 08:44:53 +02:00
}
// NewSession instanciates a Session
func NewSession(pid int64, user string, db string, client string, state string, query string, stateDuration float64, applicationName string) *Session {
2018-07-08 23:48:48 +02:00
return &Session{
Pid: pid,
User: user,
Db: db,
Client: client,
State: state,
Query: query,
StateDuration: stateDuration,
ApplicationName: applicationName,
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,
"%a": s.ApplicationName,
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
}
// Equal returns true when two sessions share the same process id
func (s *Session) Equal(session *Session) bool {
if s.Pid == 0 {
return s.User == session.User && s.Db == session.Db && s.Client == session.Client
}
return s.Pid == session.Pid
}
// InSlice returns true when this sessions in present in the slice
func (s *Session) InSlice(sessions []*Session) bool {
for _, session := range sessions {
if s.Equal(session) {
return true
}
}
return false
}