@@ -69,9 +69,12 @@ func (m *Manager) CreateSession(username, ipAddress, userAgent string) error {
6969
7070/* for expiring a session */
7171func (m * Manager ) ExpireSession (username string ) {
72+ /* thread safety for the manager */
7273 m .mutex .Lock ()
7374 defer m .mutex .Unlock ()
7475
76+ /* TODO: Add expired session to REDIS for persistent logging */
77+
7578 /* check if user exists in active sessions */
7679 session , ok := m .sessionsMap [username ]
7780 if ! ok {
@@ -121,6 +124,10 @@ func (m *Manager) RefreshTimer(username string) error {
121124 return fmt .Errorf ("Session not found" )
122125 }
123126
127+ /* thread safety for the session */
128+ session .Mutex .Lock ()
129+ defer session .Mutex .Unlock ()
130+
124131 /* reset the expiry time and last active time */
125132 session .Expiry = time .Now ().Add (time .Duration (config .BackendConfig .AppInfo .SessionTimeout ) * time .Hour )
126133 session .LastActiveAt = time .Now ()
@@ -137,3 +144,34 @@ func (m *Manager) RefreshTimer(username string) error {
137144
138145 return nil
139146}
147+
148+ /* convert session information into frontend safe structure */
149+ func (m * Manager ) ToDashboardView (username string ) (SessionView , error ) {
150+ /* thread safety for the manager */
151+ m .mutex .Lock ()
152+ defer m .mutex .Unlock ()
153+
154+ /* get session from sessionMap */
155+ session , exists := m .sessionsMap [username ]
156+ if ! exists {
157+ return SessionView {}, fmt .Errorf ("Session not found" )
158+ }
159+
160+ /* thread safety for the session */
161+ session .Mutex .Lock ()
162+ defer session .Mutex .Unlock ()
163+
164+ /* can be directly served as JSON in handler */
165+ return SessionView {
166+ ID : session .ID ,
167+ Username : session .Username ,
168+ IP : session .IP ,
169+ UserAgent : session .UserAgent ,
170+ CreatedAt : session .CreatedAt ,
171+ LastActiveAt : session .LastActiveAt ,
172+ Expiry : session .Expiry ,
173+ CompletedCount : session .CompletedCount ,
174+ FailedCount : session .FailedCount ,
175+ PendingCount : session .TransactionQueue .Len (),
176+ }, nil
177+ }
0 commit comments