Skip to content

Commit 40972ec

Browse files
Created complete Redis hash to websocket message compliant function
1 parent c674aa0 commit 40972ec

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

internal/session/ws_utils.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package session
2+
3+
import (
4+
"strconv"
5+
"time"
6+
)
7+
8+
/* convert redis hash to session */
9+
func convertRedisHashToSession(hash map[string]string) SessionStreamData {
10+
session := SessionStreamData{}
11+
12+
if val, ok := hash["id"]; ok {
13+
session.ID = val
14+
}
15+
16+
if val, ok := hash["username"]; ok {
17+
session.Username = val
18+
}
19+
20+
if val, ok := hash["ip"]; ok {
21+
session.IP = val
22+
}
23+
24+
if val, ok := hash["user_agent"]; ok {
25+
session.UserAgent = val
26+
}
27+
28+
if val, ok := hash["status"]; ok {
29+
session.Status = val
30+
}
31+
32+
if val, ok := hash["created_at"]; ok {
33+
if t, err := time.Parse(time.RFC3339, val); err == nil {
34+
session.CreatedAt = t
35+
}
36+
}
37+
38+
if val, ok := hash["last_active_at"]; ok {
39+
if t, err := time.Parse(time.RFC3339, val); err == nil {
40+
session.LastActiveAt = t
41+
}
42+
}
43+
44+
if val, ok := hash["expiry"]; ok {
45+
if t, err := time.Parse(time.RFC3339, val); err == nil {
46+
session.Expiry = t
47+
}
48+
}
49+
50+
if val, ok := hash["completed"]; ok {
51+
if i, err := strconv.Atoi(val); err == nil {
52+
session.CompletedCount = i
53+
}
54+
}
55+
56+
if val, ok := hash["failed"]; ok {
57+
if i, err := strconv.Atoi(val); err == nil {
58+
session.FailedCount = i
59+
}
60+
}
61+
62+
return session
63+
}

0 commit comments

Comments
 (0)