@@ -24,6 +24,7 @@ import (
2424 "github.com/pkg/errors"
2525
2626 "gitlab.com/postgres-ai/database-lab/v3/internal/provision/databases/postgres"
27+ "gitlab.com/postgres-ai/database-lab/v3/internal/provision/databases/postgres/pgconfig"
2728 "gitlab.com/postgres-ai/database-lab/v3/internal/provision/docker"
2829 "gitlab.com/postgres-ai/database-lab/v3/internal/provision/pool"
2930 "gitlab.com/postgres-ai/database-lab/v3/internal/provision/resources"
@@ -620,12 +621,15 @@ func (p *Provisioner) LastSessionActivity(session *resources.Session, minimumTim
620621 ctx , cancel := context .WithCancel (p .ctx )
621622 defer cancel ()
622623
623- fileSelector := pglog .NewSelector (fsm .Pool ().ClonePath (session .Port ))
624+ clonePath := fsm .Pool ().ClonePath (session .Port )
625+ fileSelector := pglog .NewSelector (clonePath )
624626
625627 if err := fileSelector .DiscoverLogDir (); err != nil {
626628 return nil , errors .Wrap (err , "failed to init file selector" )
627629 }
628630
631+ location := detectLogsTimeZone (clonePath )
632+
629633 fileSelector .SetMinimumTime (minimumTime )
630634 fileSelector .FilterOldFilesInList ()
631635
@@ -639,7 +643,7 @@ func (p *Provisioner) LastSessionActivity(session *resources.Session, minimumTim
639643 return nil , errors .Wrap (err , "failed get CSV log filenames" )
640644 }
641645
642- activity , err := p .scanCSVLogFile (ctx , filename , minimumTime )
646+ activity , err := p .scanCSVLogFile (ctx , filename , minimumTime , location )
643647 if err == io .EOF {
644648 continue
645649 }
@@ -650,12 +654,39 @@ func (p *Provisioner) LastSessionActivity(session *resources.Session, minimumTim
650654 return nil , pglog .ErrNotFound
651655}
652656
653- const csvMessageLogFieldsLength = 14
657+ const (
658+ csvMessageLogFieldsLength = 14
659+ logTZ = "log_timezone"
660+ )
661+
662+ func detectLogsTimeZone (dataDir string ) * time.Location {
663+ userCfg , err := pgconfig .ReadUserConfig (dataDir )
664+ if err != nil {
665+ log .Msg ("unable to read user-defined config of clone:" , err .Error ())
666+
667+ return time .UTC
668+ }
669+
670+ if tz , ok := userCfg [logTZ ]; ok {
671+ location , err := time .LoadLocation (tz )
672+
673+ if err != nil {
674+ log .Msg (fmt .Sprintf ("unable to load location (%q) defined in config: %s" , tz , err .Error ()))
675+
676+ return time .UTC
677+ }
678+
679+ return location
680+ }
681+
682+ return time .UTC
683+ }
654684
655- func (p * Provisioner ) scanCSVLogFile (ctx context.Context , filename string , availableTime time.Time ) (* time.Time , error ) {
685+ func (p * Provisioner ) scanCSVLogFile (ctx context.Context , filename string , availableTime time.Time ,
686+ location * time.Location ) (* time.Time , error ) {
656687 csvFile , err := os .Open (filename )
657688 if err != nil {
658- return nil , errors .Wrap (err , "failed to open a CSV log file" )
689+ return nil , errors .Wrap (err , "failed to open CSV log file" )
659690 }
660691
661692 defer func () {
@@ -683,13 +714,13 @@ func (p *Provisioner) scanCSVLogFile(ctx context.Context, filename string, avail
683714 logTime := entry [0 ]
684715 logMessage := entry [13 ]
685716
686- lastActivity , err := pglog .ParsePostgresLastActivity (logTime , logMessage )
717+ lastActivity , err := pglog .ParsePostgresLastActivity (logTime , logMessage , location )
687718 if err != nil {
688- return nil , errors .Wrapf (err , "failed to get the time of last activity" )
719+ return nil , errors .Wrapf (err , "failed to determine last activity timestamp " )
689720 }
690721
691722 // Filter invalid and non-recent activity.
692- if lastActivity == nil || lastActivity .Before (availableTime ) {
723+ if lastActivity == nil || lastActivity .In ( time . UTC ). Before (availableTime ) {
693724 continue
694725 }
695726
0 commit comments