Skip to content

Commit

Permalink
Merge pull request #159 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 15, 2023
2 parents 37c1034 + 635158d commit 061d356
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
12 changes: 6 additions & 6 deletions packages/googlecloud/functions/getanswer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
supabase_key = os.environ.get("SUPABASE_SERVICE_KEY_STAGING")
supabase = create_client(supabase_url, supabase_key)


def update_supabase(responses, citations, card_id):
def update_supabase(responses, citations, card_id, processing_time_ms):
transformed_citations = []
for citation in citations:
transformed_citations.append({
Expand All @@ -36,13 +35,16 @@ def update_supabase(responses, citations, card_id):

try:
supabase.table("cards").update(
{"responses": responses, "citations": transformed_citations}
{"responses": responses,
"citations": transformed_citations,
"processing_time_ms": processing_time_ms} # Update this line
).eq("id", card_id).execute()
logging.info("Data successfully updated in Supabase")
except Exception as e:
logging.error(f"Failed to update Supabase: {e}")



@functions_framework.http
def getanswer(request):
"""HTTP Cloud Function.
Expand Down Expand Up @@ -96,13 +98,11 @@ def getanswer(request):
return ("Failed to process answer", 500, headers)

responses_data = answer.get("responses")

citations_data = answer.get("citations")

update_supabase(responses_data, citations_data, card_id)

end = time.time()
elapsed = math.ceil(end - start)
update_supabase(responses_data, citations_data, card_id, elapsed)
logging.info(f"Completed getanswer in {elapsed} seconds")
print(f"\n\t--------- Completed getanswer in {elapsed} seconds --------\n")

Expand Down
31 changes: 15 additions & 16 deletions packages/web/components/NewQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,33 +123,32 @@ export default function NewQuery() {
event: "UPDATE",
schema: "public",
},
(payload: SupabaseRealtimePayload<typeof card>) => {
setCard((prevCard) => {
if (!prevCard || payload.new.id !== prevCard.id) return prevCard;
(payload: SupabaseRealtimePayload<ICard>) => {
if (payload.new.id === card.id) {
setCard((prevCard) => {
// Update only if there are changes in responses or citations
const hasNewResponse = payload.new.responses !== prevCard?.responses;
const hasNewCitations = payload.new.citations !== prevCard?.citations;

const updatedCard = payload.new;
const hasNewResponse = updatedCard.responses !== prevCard.responses;
const hasNewCitations = updatedCard.citations !== prevCard.citations;

if (hasNewResponse || hasNewCitations) {
return {
...prevCard,
responses: hasNewResponse ? updatedCard.responses || [] : prevCard.responses,
citations: hasNewCitations ? updatedCard.citations || [] : prevCard.citations,
};
}
if (hasNewResponse || hasNewCitations) {
return { ...prevCard, ...payload.new };
}

return prevCard;
});
return prevCard;
});
}
}
)
.subscribe();

// Cleanup subscription on component unmount
return () => {
channel.unsubscribe();
};
}, [card]);



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

0 comments on commit 061d356

Please sign in to comment.