Skip to content

Commit

Permalink
Wire through last-updated to the similarCards endpoint from client.
Browse files Browse the repository at this point in the history
Part of #670. Part of #646.
  • Loading branch information
jkomoros committed Nov 12, 2023
1 parent 1142e60 commit 847eea8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
4 changes: 3 additions & 1 deletion functions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,16 @@ export type LegalResponseData = {
reason: string
};

type MillisecondsSinceEpoch = number;

//Replicated in `src/actions/similarity.ts`
export type SimilarCardsRequestData = {
card_id: CardID

//timestamp in milliseconds since epoch. If provided, results will only be
//provided if the Vector point has a last-updated since then, otherwise
//error of `stale`.
last_updated? : number
last_updated? : MillisecondsSinceEpoch

//TODO: include a limit

Expand Down
28 changes: 20 additions & 8 deletions src/actions/similarity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '../config.GENERATED.SECRET.js';

import {
selectCardSimilarity
selectCardSimilarity, selectRawCards
} from '../selectors.js';

import {
Expand All @@ -38,14 +38,17 @@ import {
//Replicated in src/actions/similarity.ts
type EmbeddableCard = Pick<Card, 'body' | 'title' | 'subtitle' | 'card_type' | 'created' | 'id'>;

//Replicated in src/actions/similarity.ts
type MillisecondsSinceEpoch = number;

//Replicated in `src/actions/similarity.ts`
type SimilarCardsRequestData = {
card_id: CardID

//timestamp in milliseconds since epoch. If provided, results will only be
//provided if the Vector point has a last-updated since then, otherwise
//error of `stale`.
last_updated? : number
last_updated? : MillisecondsSinceEpoch

//TODO: include a limit

Expand All @@ -70,25 +73,30 @@ type SimilarCardsResponseData = {

const similarCardsCallable = httpsCallable<SimilarCardsRequestData, SimilarCardsResponseData>(functions, 'similarCards');

const similarCards = async (cardID : CardID) : Promise<SimilarCardsResponseData> => {
const similarCards = async (cardID : CardID, lastUpdated : MillisecondsSinceEpoch) : Promise<SimilarCardsResponseData> => {
if (!QDRANT_ENABLED) {
return {
success: false,
code: 'qdrant-disabled',
error: 'Qdrant isn\'t enabled'
};
}
const request = {
card_id: cardID

//TODO: there's a massive problem with this flow currently... if a card is updated but
//doesn't have an embedding updated.

const request : SimilarCardsRequestData = {
card_id: cardID,
last_updated: lastUpdated
};
const result = await similarCardsCallable(request);
return result.data;
};

const fetchSimilarCards = (cardID : CardID) : ThunkSomeAction => async (dispatch) => {
const fetchSimilarCards = (cardID : CardID, lastUpdated: MillisecondsSinceEpoch) : ThunkSomeAction => async (dispatch) => {
if (!cardID) return;

const result = await similarCards(cardID);
const result = await similarCards(cardID, lastUpdated);

if (result.success == false) {

Expand Down Expand Up @@ -124,7 +132,11 @@ export const fetchSimilarCardsIfEnabled = (cardID : CardID) : boolean => {
if (similarity[cardID]) {
return false;
}

const cards = selectRawCards(state);
const card = cards[cardID];
if (!card) throw new Error(`Couldn't find card ${cardID}`);
//This will return immediately.
store.dispatch(fetchSimilarCards(cardID));
store.dispatch(fetchSimilarCards(cardID, card.updated.toMillis()));
return true;
};

0 comments on commit 847eea8

Please sign in to comment.