|
| 1 | +package databases |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "matching-service/models" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/redis/go-redis/v9" |
| 12 | +) |
| 13 | + |
| 14 | +// Print existing users in the matching queue |
| 15 | +func PrintMatchingQueue(tx *redis.Tx, status string, ctx context.Context) { |
| 16 | + users, err := GetAllQueuedUsers(tx, ctx) |
| 17 | + if err != nil { |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + var concatenatedUsers strings.Builder |
| 22 | + for i, user := range users { |
| 23 | + concatenatedUsers.WriteString(user) |
| 24 | + if i != len(users)-1 { |
| 25 | + concatenatedUsers.WriteString(", ") |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + log.Println("Redis Queue (" + status + "): " + concatenatedUsers.String()) |
| 30 | +} |
| 31 | + |
| 32 | +func IsQueueEmpty(tx *redis.Tx, ctx context.Context) (bool, error) { |
| 33 | + queueLength, err := tx.LLen(ctx, MatchmakingQueueRedisKey).Result() |
| 34 | + if err != nil { |
| 35 | + log.Println("Error checking queue length:", err) |
| 36 | + return false, err |
| 37 | + } |
| 38 | + // No users in the queue, so no need to perform matching |
| 39 | + return queueLength == 0, nil |
| 40 | +} |
| 41 | + |
| 42 | +// Enqueue a user into the matchmaking queue |
| 43 | +func EnqueueUser(tx *redis.Tx, username string, ctx context.Context) { |
| 44 | + err := tx.LPush(ctx, MatchmakingQueueRedisKey, username).Err() |
| 45 | + if err != nil { |
| 46 | + log.Println("Error enqueuing user:", err) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// Remove user from the matchmaking queue |
| 51 | +func DequeueUser(tx *redis.Tx, username string, ctx context.Context) { |
| 52 | + err := tx.LRem(ctx, MatchmakingQueueRedisKey, 1, username).Err() |
| 53 | + if err != nil { |
| 54 | + log.Println("Error dequeuing user:", err) |
| 55 | + return |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Returns the first user's username from the queue. |
| 60 | +func GetFirstUser(tx *redis.Tx, ctx context.Context) (string, error) { |
| 61 | + // Peek at the user queue |
| 62 | + username, err := tx.LIndex(ctx, MatchmakingQueueRedisKey, 0).Result() |
| 63 | + if err != nil { |
| 64 | + log.Println("Error peeking user from queue:", err) |
| 65 | + return "", err |
| 66 | + } |
| 67 | + return username, nil |
| 68 | +} |
| 69 | + |
| 70 | +func GetAllQueuedUsers(tx *redis.Tx, ctx context.Context) ([]string, error) { |
| 71 | + users, err := tx.LRange(ctx, MatchmakingQueueRedisKey, 0, -1).Result() |
| 72 | + if err != nil { |
| 73 | + log.Println("Error retrieving users from queue:", err) |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + return users, nil |
| 77 | +} |
| 78 | + |
| 79 | +// Add user details into hashset in Redis |
| 80 | +func StoreUserDetails(tx *redis.Tx, request models.MatchRequest, ctx context.Context) { |
| 81 | + topicsJSON, err := json.Marshal(request.Topics) |
| 82 | + if err != nil { |
| 83 | + log.Println("Error marshalling topics:", err) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + difficultiesJSON, err := json.Marshal(request.Difficulties) |
| 88 | + if err != nil { |
| 89 | + log.Println("Error marshalling difficulties:", err) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + err = tx.HSet(ctx, request.Username, map[string]interface{}{ |
| 94 | + "topics": topicsJSON, |
| 95 | + "difficulty": difficultiesJSON, |
| 96 | + "username": request.Username, |
| 97 | + }).Err() |
| 98 | + if err != nil { |
| 99 | + log.Println("Error storing user details:", err) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Retrieve user details from hashset in Redis |
| 104 | +func GetUserDetails(tx *redis.Tx, username string, ctx context.Context) (models.MatchRequest, error) { |
| 105 | + userDetails, err := tx.HGetAll(ctx, username).Result() |
| 106 | + if err != nil { |
| 107 | + return models.MatchRequest{}, err |
| 108 | + } |
| 109 | + |
| 110 | + if len(userDetails) == 0 { |
| 111 | + return models.MatchRequest{}, fmt.Errorf("user not found in hashset: %s", username) |
| 112 | + } |
| 113 | + |
| 114 | + topicsJSON, topicsExist := userDetails["topics"] |
| 115 | + difficultiesJSON, difficultiesExist := userDetails["difficulty"] |
| 116 | + |
| 117 | + if !topicsExist || !difficultiesExist { |
| 118 | + return models.MatchRequest{}, fmt.Errorf("incomplete user details for: %s", username) |
| 119 | + } |
| 120 | + |
| 121 | + var topics []string |
| 122 | + err = json.Unmarshal([]byte(topicsJSON), &topics) |
| 123 | + if err != nil { |
| 124 | + return models.MatchRequest{}, fmt.Errorf("error unmarshalling topics: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + var difficulties []string |
| 128 | + err = json.Unmarshal([]byte(difficultiesJSON), &difficulties) |
| 129 | + if err != nil { |
| 130 | + return models.MatchRequest{}, fmt.Errorf("error unmarshalling difficulties: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + matchRequest := models.MatchRequest{ |
| 134 | + Topics: topics, |
| 135 | + Difficulties: difficulties, |
| 136 | + Username: username, |
| 137 | + } |
| 138 | + |
| 139 | + return matchRequest, nil |
| 140 | +} |
| 141 | + |
| 142 | +// Remove user details from HashSet |
| 143 | +func RemoveUserDetails(tx *redis.Tx, username string, ctx context.Context) { |
| 144 | + err := tx.Del(ctx, username).Err() |
| 145 | + if err != nil { |
| 146 | + log.Println("Error removing user details:", err) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// Find the first matching user based on topics |
| 151 | +// TODO: match based on available questions |
| 152 | +func FindMatchingUser(tx *redis.Tx, username string, ctx context.Context) (*models.MatchFound, error) { |
| 153 | + user, err := GetUserDetails(tx, username, ctx) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + |
| 158 | + for _, topic := range user.Topics { |
| 159 | + users, err := tx.SMembers(ctx, strings.ToLower(topic)).Result() |
| 160 | + if err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + |
| 164 | + for _, potentialMatch := range users { |
| 165 | + if potentialMatch == username { |
| 166 | + continue |
| 167 | + } |
| 168 | + |
| 169 | + matchedUser, err := GetUserDetails(tx, potentialMatch, ctx) |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + |
| 174 | + commonDifficulty := models.GetCommonDifficulty(user.Difficulties, matchedUser.Difficulties) |
| 175 | + |
| 176 | + matchFound := models.MatchFound{ |
| 177 | + Type: "match_found", |
| 178 | + MatchedUser: potentialMatch, |
| 179 | + Topic: topic, |
| 180 | + Difficulty: commonDifficulty, |
| 181 | + } |
| 182 | + |
| 183 | + return &matchFound, nil |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return nil, nil |
| 188 | +} |
| 189 | + |
| 190 | +func PopAndInsertUser(tx *redis.Tx, username string, ctx context.Context) { |
| 191 | + // Pop user |
| 192 | + username, err := tx.LPop(ctx, MatchmakingQueueRedisKey).Result() |
| 193 | + if err != nil { |
| 194 | + log.Println("Error popping user from queue:", err) |
| 195 | + } |
| 196 | + |
| 197 | + // Insert back in queue |
| 198 | + err = tx.LPush(ctx, MatchmakingQueueRedisKey, username).Err() |
| 199 | + if err != nil { |
| 200 | + log.Println("Error enqueuing user:", err) |
| 201 | + } |
| 202 | +} |
0 commit comments