Skip to content

Commit

Permalink
Merge pull request #154 from eye-on-surveillance/AI/request-flow-fix
Browse files Browse the repository at this point in the history
AI/request-flow-fix
  • Loading branch information
ayyubibrahimi authored Nov 14, 2023
2 parents 77dae6a + e58edb1 commit 6a536d5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
7 changes: 4 additions & 3 deletions packages/googlecloud/functions/getanswer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
supabase_key = os.environ.get("SUPABASE_SERVICE_KEY_STAGING")
supabase = create_client(supabase_url, supabase_key)

def update_supabase(response, query, response_type):
def update_supabase(response, query, response_type, card_id):
try:
response = supabase.table('cards').insert({'title': query, 'card_type': response_type, 'responses': response}).execute()
response = supabase.table('cards').update({'title': query, 'card_type': response_type, 'responses': response}).eq('id', card_id).execute()
logging.info("Data successfully inserted into Supabase")
except Exception as e:
logging.error(f"Failed to update Supabase: {e}")
Expand Down Expand Up @@ -68,6 +68,7 @@ def getanswer(request):

query = parse_field(request_json, "query")
response_type = parse_field(request_json, "response_type")
card_id = parse_field(request_json, "card_id")
else:
raise ValueError("Unknown content type: {}".format(content_type))

Expand All @@ -76,7 +77,7 @@ def getanswer(request):
answer = answer_query(query, response_type, voting_roll_df, db_general, db_in_depth)

# Update Supabase instead of returning the answer to the client
update_supabase(answer, query, response_type)
update_supabase(answer, query, response_type, card_id)

end = time.time()
elapsed = math.ceil(end - start)
Expand Down
23 changes: 14 additions & 9 deletions packages/web/components/NewQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export default function NewQuery() {
// Start processing question
const answerResp = await fetch(apiEndpoint, {
method: "POST",
// Pass responseMode to your API endpoint
body: JSON.stringify({ query, response_type: cardType }),
// Pass responseMode to your API endpoint
body: JSON.stringify({ query, response_type: cardType, card_id: newCard.id }),
mode: "cors",
headers: {
"Content-Type": "application/json",
Expand Down Expand Up @@ -117,26 +117,31 @@ export default function NewQuery() {
if (!card) {
return;
}

const channel = (supabase.channel(`cards:id=eq.${card.id}`) as any)
.on(
"postgres_changes",
{
event: "INSERT",
event: "UPDATE",
schema: "public",
},
(payload: SupabaseRealtimePayload<ICard>) => {
if (payload.new.id === card.id) {
setCard((prevCard) => ({ ...prevCard, ...payload.new }));
if (payload.new.responses !== payload.old.responses) {
setCard(prevCard => {
if (!prevCard) {
return null;
}
return { ...prevCard, responses: payload.new.responses };
});
}
}
)
})
.subscribe();

return () => {
channel.unsubscribe();
};
}, [card]);


const submitQuery = async (e?: React.FormEvent<HTMLFormElement>) => {
e?.preventDefault();
Expand Down

0 comments on commit 6a536d5

Please sign in to comment.