Skip to content

GS Onboarding (backend and frontend)#10

Closed
Jiatao7 wants to merge 17 commits intoUWOrbital:mainfrom
Jiatao7:main
Closed

GS Onboarding (backend and frontend)#10
Jiatao7 wants to merge 17 commits intoUWOrbital:mainfrom
Jiatao7:main

Conversation

@Jiatao7
Copy link

@Jiatao7 Jiatao7 commented Jan 26, 2025

No description provided.

Copy link
Contributor

@Yarik-Popov Yarik-Popov left a comment

Choose a reason for hiding this comment

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

Overall good start on the backend. Ill review the frontend later. Some changes are needed

num_commands = len(items)

command = Command (
id=1+num_commands,
Copy link
Contributor

Choose a reason for hiding this comment

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

You dont need this as its error prone. Let the orm handle the auto increment

Comment on lines 62 to 72
try:
query = select(Command).where(Command.id == id)
command = db.exec(query).one()
db.delete(command)
db.commit()

query2 = select(Command).where(Command.id != id)
items = db.exec(query2).all()
return {"data": items}
except:
raise HTTPException(status_code=404, detail="Item does not exist") No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

The orm will not raise an exception if an item doesnt exist. It will return None and you should check against it

from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware

from ...utils.logging import logger
Copy link
Contributor

Choose a reason for hiding this comment

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

Use absolute imports

# TODO: (Member) Implement this method
return self

if (self.params == None and self.format == None):
Copy link
Contributor

Choose a reason for hiding this comment

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

Dont compare using == against None use is None instead. Also you dont need the extra () for if and elif, while statements

if (self.params == None and self.format == None):
return self
elif (self.params == None or self.format == None):
raise(ValueError)
Copy link
Contributor

Choose a reason for hiding this comment

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

The proper syntax is raise ValueError() as your case throws the class and not the instance.

elif (len(self.params.split(",")) == len(self.format.split(","))):
return self
else:
raise(ValueError)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing here

Comment on lines 28 to +32
# TODO: Figure out a better way to check the times
assert result.get("created_on")
assert result.get("updated_on")
created_on_dt = datetime.fromisoformat(result.get("created_on"))
updated_on_dt = datetime.fromisoformat(result.get("updated_on"))
assert isinstance(created_on_dt, datetime)
assert isinstance(updated_on_dt, datetime)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks. Not a part of the onboarding but definitely something that can be improved. Ill link this in the task to improve the gs onboarding.

@Jiatao7 Jiatao7 requested a review from Yarik-Popov February 1, 2025 21:10
Copy link
Contributor

@Yarik-Popov Yarik-Popov left a comment

Choose a reason for hiding this comment

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

Good step in the right direction. Looks like I forgot to submit my requested changes for the frontend earlier. Sorry about that but all the requested changes are here for both the frontend and backend.

Comment on lines 73 to 76
let paramsObject : { [key: string]: string } = {}
for(const param of parameters) {
paramsObject[param] = ""
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Theres a bug if you input data into a text field then switch the type. The text field still has the data displayed but then you click submit it says missing parameter. It might be due to this and the fact that the input does have a value property


{/* TODO: (Member) Add input handling here if the selected command has a param input*/}
{commandType &&
commandType.params?.split(",").map((param) => (<input id={param} placeholder={`Enter ${param}`} onChange={changeParam}/>))}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should have a value property that is an individual param. It should fix the bug i described above

setParams(paramsObject);
}

const changeParam = (e: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use a specific type here insteas of any or if you dont know the type use unknown

//console.log(`Command: `, commandType)
//console.log(`Parameters: `, params)

const changeCommandType = (e: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use a specific type here instead of any. Or if you dont know the type, use unknown as it is safer

Comment on lines 62 to 68
/*
<input value={params} onChange={(e) => setParams(e.target.value)}/>
{// Use parameters of command type to output input boxes}
{ .params.map(param => (<input id={} value={}>{cmd.name}</option>))}
*/
//console.log(`Command: `, commandType)
//console.log(`Parameters: `, params)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not leave code commented out. If its not needed remove it. This also applies to line 52

elif len(self.params.split(",")) == len(self.format.split(",")):
return self
else:
raise ValueError()
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a descriptive error message


setCommandType(data.data[0])

const parameters = commandType?.params?.split(",") || []
Copy link
Contributor

Choose a reason for hiding this comment

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

commandType will be null on the first render but not sure if it matters here

// TODO: (Member) Setup state and useEffect calls here
const [mainCommands, setMainCommands] = useState<MainCommandResponse[]>([])
const [commandType, setCommandType] = useState<MainCommandResponse | null>(null)
const [params, setParams] = useState<{[key: string] : string}>({})
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably use a custom type instead of {[key: string]: string} maybe look into StringMap

const paramsList = []
for (const param of allParams) {
if (!params[param]) {
alert("Parameter missing")
Copy link
Contributor

Choose a reason for hiding this comment

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

From a UX perspective probably best to indicate which parameter is missing

@Jiatao7 Jiatao7 requested a review from Yarik-Popov February 3, 2025 00:05
Copy link
Contributor

@Yarik-Popov Yarik-Popov left a comment

Choose a reason for hiding this comment

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

Backend lgtm. Some changes are needed on the frontend.

Comment on lines 16 to 18
if(!data.data) {
alert("Error occured. Please try again later.")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This code will never run because an empty array in Typescript is truthy. So !data.data will always be false. Also add a return at the end of the if block. You should be comparing against the length

const allParams = commandType?.params?.split(",") || [];
const paramsList = []

let sendAlert = false
Copy link
Contributor

Choose a reason for hiding this comment

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

The sendAlert variable is redundant you can just use the length of the missingParams array to check if there is something missing

const CommandInput = () => {
// TODO: (Member) Setup state and useEffect calls here
const [mainCommands, setMainCommands] = useState<MainCommandResponse[]>([])
const [commandType, setCommandType] = useState<MainCommandResponse>({id: 0, name: "", params: null, format: null, data_size: 0, total_size: 0})
Copy link
Contributor

Choose a reason for hiding this comment

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

I think having null as the default value and checking against it in the code would probably be better than having a default object.

}

const changeCommandType = (e: React.ChangeEvent<HTMLSelectElement>) => {
const type : MainCommandResponse = mainCommands.find(cmd => cmd.id == +e.target.value) || {id: 0, name: "", params: null, format: null, data_size: 0, total_size: 0}
Copy link
Contributor

Choose a reason for hiding this comment

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

Just let type be undefined and show an alert that an error occurred and return from the function. Also, probably not a good idea to use type as a variable name as it is a soft keyword in Typescript

@Jiatao7 Jiatao7 requested a review from Yarik-Popov February 3, 2025 18:42
Copy link
Contributor

@Yarik-Popov Yarik-Popov left a comment

Choose a reason for hiding this comment

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

Do not use loose equals. Should be last set of changes

useEffect(() => {
const setMainCommandsFn = async () => {
const data = await getMainCommands()
if(data.data.length == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not use double equals == in comparisons. Use ===

Copy link
Contributor

Choose a reason for hiding this comment

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

Because == is loosely equal. Example:
0 == [] // true
0 == '0' //true
But:
[] == '0' // false

Copy link
Contributor

Choose a reason for hiding this comment

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

Here's even more cursed things about javascript:
0 < null // false
0 > null // false
0 == null // false
But
0 <= null // true
0 >= null // true
But if you were to replace all of the null above with undefined, they would all be false

Copy link
Contributor

Choose a reason for hiding this comment

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

You can disregard the last 2 comments in the thread, they are just me showing you some trivia about js and why we use Typescript

}
paramsList.push(params.get(param))
}
if(missingParams.length != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not use != in comparisons. Use !== instead

@Jiatao7 Jiatao7 requested a review from Yarik-Popov February 4, 2025 01:09
@Yarik-Popov
Copy link
Contributor

Lgtm. Dm me your email and ill add u to the notion

@Yarik-Popov Yarik-Popov closed this Feb 4, 2025
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.

2 participants