Skip to content

feat(backend,frontend,client,common): implementation#1

Merged
melancholiai merged 42 commits intomasterfrom
implementation
Aug 13, 2024
Merged

feat(backend,frontend,client,common): implementation#1
melancholiai merged 42 commits intomasterfrom
implementation

Conversation

@melancholiai
Copy link
Copy Markdown
Contributor

Question Answer
Bug fix
New feature
Breaking change
Deprecations
Documentation
Tests added
Chore

@melancholiai melancholiai requested a review from syncush January 23, 2024 15:22
@melancholiai melancholiai changed the title feat(backend,client,common): implementation feat(backend,frontend,client,common): implementation Jun 4, 2024
@melancholiai melancholiai marked this pull request as ready for review June 4, 2024 12:06
fetch: {
method: 'GET',
headers: {
'x-api-key': `eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJNYXBDb2xvbmllc0RldiIsImlhdCI6MTUxNjIzOTAyMiwiZCI6WyJyYXN0ZXIiLCJyYXN0ZXJXbXMiLCJyYXN0ZXJFeHBvcnQiLCJkZW0iLCJ2ZWN0b3IiLCIzZCJdfQ.GvTQ_yLjnioxxFrNgGQiuarhJxLpe8AhTTtrWE3LHoUED48CFKBEOfKqOyEWSDVZjx1jHkDvZAL1iyEvi5FHNys7UBRXCiJvVlG-muJZ6ycS9PGKauzL-eggXqTqGsXh4FBkqvHUEElXEnu7ARsMCm5eIC66U2i_eHFU3PLcOc67qJvS1IQjAI2oj9Pd5mGaI_HlDaf3B4PFOb0AHdY-r_MDGwck3asm1G_InVzsvCXt36vImyn1Z4HYaN4YiDfaMLBF0-GGrlLE84PObzGGtt66EIuQ4OneEZSzoQNusBt5-SFs0EQXsfsDc_RMRTz3DZseqkNIKiXEsEBBPjMr7w`,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a real token ? it shouldnt be here

@@ -0,0 +1,13 @@
export class TileDetailsNotFoundError extends Error {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
I think you should implement HttpError object from @map-colonies/error-express-handler on this error.
Then, add public readonly status = httpStatus.NOT_FOUND; - instead of catching the error and setting the returned status code manually.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree.
TileDetailsNotFoundError is being used in TileDetailsManager which shouldn't be decoupled to an http server. thus implementing errors with http properties would not be best practice.
for example, this becomes handy when we're unit testing this function and are aware of the exact error that was thrown in the relevant case.


const tilesDetails = result.documents.map((document) => document.value[0]);

return tilesDetails as unknown as TileDetails[];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to avoid using 'as unkown as x'? I think it's not type-safe

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sadly no, the return type of redis.ft.search isn't type aware of the documents and they might be of a primitive type.
you cannot cast from a custom to a primitive without erasing the type first - casting to unknown erases the type checking.


const keys = await this.redis.keys(`${REDIS_KITS_HASH_PREFIX}:*`);

const kits = (await Promise.all(keys.map(async (key) => this.redis.hGetAll(key)))) as KitMetadata[];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question:
If one fetch fail, do you want this promise to totally reject?
Else, I think you need Promise.allSettled.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the client is determining its logic by the provided result. an incomplete result might lead to a client mistakes.
if the server couldn't provide the "whole picture" the request should fail.

@@ -0,0 +1,6 @@
export class KitAlreadyExistsError extends Error {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here I suggest you to implement HttpError object from @map-colonies/error-express-handler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Comment on lines +41 to +44
export const bboxToWktPolygon = (bbox: BoundingBox): string => {
const { west, south, east, north } = bbox;
return `POLYGON ((${west} ${north}, ${west} ${south}, ${east} ${south}, ${east} ${north}, ${west} ${north}))`;
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just me but I would destructure the bbox in the parameter and remove the return statement like in keyfy function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's just a styling preference. I find it more readable

Comment thread packages/client/src/client/index.ts Outdated
const { logger, ...config } = options;
this.logger = logger;
this.config = config;
this.axios = axios.create({ timeout: options.timeout });
Copy link
Copy Markdown

@NivGreenstein NivGreenstein Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the backend is going to be protected with an api-key. If so, you should add here the relevant header (or support it if you get it as part of the options).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Comment on lines +155 to +171
useEffect(() => {
async function kitsFetch(): Promise<void> {
try {
const [result, duration] = await timerify<Awaited<ReturnType<typeof client.getKits>>, never[]>(client.getKits.bind(client));
kits = result;
setStatsTable((prevStatsTable) => {
const nextHttpStat = calcHttpStat(prevStatsTable.httpInvokes.kits, duration);
return { ...prevStatsTable, httpInvokes: { ...prevStatsTable.httpInvokes, kits: nextHttpStat } };
});
} catch (err) {
logger.error({ msg: 'error fetching kits', err });
enqueueSnackbar(JSON.stringify(err), { variant: 'error' });
}
}
void kitsFetch();
setIntervalAsync(kitsFetch, FETCH_KITS_INTERVAL);
}, []);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add cleanup for the setInterval if this component will unmount.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Comment thread packages/frontend/src/app.tsx Outdated
}
}
void kitsFetch();
setIntervalAsync(kitsFetch, FETCH_KITS_INTERVAL);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I prefer CRON over intervals as it makes it more predictable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using an async function thus implementation is done with set-interval-async

@melancholiai melancholiai merged commit 0815c2e into master Aug 13, 2024
@melancholiai melancholiai deleted the implementation branch August 13, 2024 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants