Skip to content

Commit 688a83b

Browse files
committed
refactor: update count incrementing logic to rpc call and coderabbit fixes
1 parent 71f7be7 commit 688a83b

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

backend/app/agents/devrel/nodes/generate_response.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ async def _store_interaction_to_db(state: AgentState, final_response: str) -> No
119119

120120
# Extract classification data
121121
classification = state.context.get("classification", {})
122-
intent = classification.get("intent")
122+
# TODO: intent key not present in classification schema (contains: needs_devrel, priority, reasoning)
123+
# Modify prompt to include intent key
124+
intent = classification.get("reasoning") # Fallback to reasoning for intent
123125

124126
# Store the interaction
125127
await store_interaction(

backend/app/database/supabase/scripts/create_db.sql

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,27 @@ $$ language 'plpgsql';
116116
CREATE TRIGGER update_conversation_context_updated_at
117117
BEFORE UPDATE ON conversation_context
118118
FOR EACH ROW
119-
EXECUTE FUNCTION update_updated_at_column();
119+
EXECUTE FUNCTION update_updated_at_column();
120+
121+
-- Migration: Add atomic increment function for user interaction count
122+
-- This function safely increments the total_interactions_count for a user
123+
124+
CREATE OR REPLACE FUNCTION increment_user_interaction_count(user_uuid UUID)
125+
RETURNS INTEGER AS $$
126+
DECLARE
127+
new_count INTEGER;
128+
BEGIN
129+
-- Atomically increment the counter and return the new value
130+
UPDATE users
131+
SET total_interactions_count = total_interactions_count + 1
132+
WHERE id = user_uuid
133+
RETURNING total_interactions_count INTO new_count;
134+
135+
-- Return the new count (NULL if user not found)
136+
RETURN new_count;
137+
END;
138+
$$ LANGUAGE plpgsql;
139+
140+
-- Optional: Add a comment for documentation
141+
COMMENT ON FUNCTION increment_user_interaction_count(UUID) IS
142+
'Atomically increments the total_interactions_count for a user. Returns the new count or NULL if user not found.';

backend/app/database/supabase/services.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,18 @@ async def store_interaction(
139139
if response.data:
140140
logger.info(f"Interaction stored successfully for user {user_uuid}")
141141

142-
# Increment user's total_interactions_count
143-
# First get the current count
144-
user_response = await supabase.table("users").select("total_interactions_count").eq("id", user_uuid).limit(1).execute()
145-
if user_response.data:
146-
current_count = user_response.data[0].get("total_interactions_count", 0)
147-
await supabase.table("users").update({
148-
"total_interactions_count": current_count + 1
149-
}).eq("id", user_uuid).execute()
150-
142+
# Atomically increment user's total_interactions_count
143+
try:
144+
rpc_response = await supabase.rpc("increment_user_interaction_count", {"user_uuid": user_uuid}).execute()
145+
if rpc_response.data is not None:
146+
logger.debug(f"Updated interaction count for user {user_uuid}: {rpc_response.data}")
147+
else:
148+
logger.warning(f"User {user_uuid} not found when incrementing interaction count")
149+
except Exception as e:
150+
logger.exception("Error incrementing user interaction count")
151+
152+
# Not failing the entire operation if incrementing the interaction count fails
151153
return True
152-
else:
153-
logger.error(f"Failed to store interaction: {response}")
154-
return False
155154

156155
except Exception as e:
157156
logger.error(f"Error storing interaction: {str(e)}")

0 commit comments

Comments
 (0)