Skip to content

Commit

Permalink
fix(app): if unable to load detailed score points, rescore (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianHymer authored Oct 10, 2023
1 parent 39e0dfc commit 2c37423
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions app/context/scorerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,36 @@ export const ScorerContextProvider = ({ children }: { children: any }) => {
const [stampWeights, setStampWeights] = useState<Partial<Weights>>({});
const [scoredPlatforms, setScoredPlatforms] = useState<PlatformScoreSpec[]>([]);

const loadScore = async (address: string | undefined, dbAccessToken: string): Promise<string> => {
const loadScore = async (
address: string | undefined,
dbAccessToken: string,
rescore: boolean = false
): Promise<string> => {
try {
setScoreState("APP_INITIAL");
const response = await axios.get(`${scorerApiGetScore}/${address}`, {
headers: {
Authorization: `Bearer ${dbAccessToken}`,
},
});
let response;
try {
response = await axios({
url: `${scorerApiGetScore}/${address}`,
method: rescore ? "post" : "get",
headers: {
Authorization: `Bearer ${dbAccessToken}`,
},
});
} catch (e) {
// Rethrow if this is already rescoring, to avoid loop
if (rescore) throw e;
else return loadScore(address, dbAccessToken, true);
}
setScoreState(response.data.status);
if (response.data.status === "DONE") {
setScore(response.data.score);

const numRawScore = Number.parseFloat(response.data.evidence.rawScore);
const numThreshold = Number.parseFloat(response.data.evidence.threshold);
const numScore = Number.parseFloat(response.data.score);

if (needsRescore(rescore, numRawScore, response.data.stamp_scores))
return loadScore(address, dbAccessToken, true);

setRawScore(numRawScore);
setThreshold(numThreshold);
setScore(numScore);
Expand All @@ -109,6 +123,16 @@ export const ScorerContextProvider = ({ children }: { children: any }) => {
}
};

const needsRescore = (currentlyRescoring: boolean, rawScore: number, stamp_scores: any): boolean => {
let needsRescore = rawScore > 0;
if (!currentlyRescoring && needsRescore) {
try {
if (Object.keys(stamp_scores).length > 0) needsRescore = false;
} catch {}
}
return needsRescore;
};

const fetchStampWeights = async () => {
try {
const response = await axios.get(`${scorerApiGetWeights}`);
Expand Down

0 comments on commit 2c37423

Please sign in to comment.