Skip to content

Commit d56c61f

Browse files
ToDashboardView function added
1 parent f3cdc81 commit d56c61f

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

internal/session/manager.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ func (m *Manager) CreateSession(username, ipAddress, userAgent string) error {
6969

7070
/* for expiring a session */
7171
func (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+
}

internal/session/model.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,27 @@ type Session struct {
3838
/* transactions issued by the user */
3939
TransactionQueue *list.List
4040

41-
/* mutex for thread safety */
42-
Mutex sync.Mutex
43-
4441
/*
4542
listElem stores it's node address in sessionOrder
4643
this is done to maintain O(1) runtime performance while deleting session
4744
*/
4845
listElem *list.Element
46+
47+
/* mutex for thread safety */
48+
Mutex sync.Mutex
49+
}
50+
51+
/* sessionView is a frontend-safe representation of a session */
52+
type SessionView struct {
53+
CompletedCount int `json:"completedCount"`
54+
FailedCount int `json:"failedCount"`
55+
PendingCount int `json:"pendingCount"`
56+
ID string `json:"id"`
57+
Username string `json:"username"`
58+
IP string `json:"ip"`
59+
UserAgent string `json:"userAgent"`
60+
Status string `json:"status"`
61+
CreatedAt time.Time `json:"createdAt"`
62+
LastActiveAt time.Time `json:"lastActiveAt"`
63+
Expiry time.Time `json:"expiry"`
4964
}

0 commit comments

Comments
 (0)