From 49e302455bf4f5fe6a9192d833c6fe500bb1a2fc Mon Sep 17 00:00:00 2001 From: muhamad fais aizat Date: Fri, 2 May 2025 02:21:47 +0700 Subject: [PATCH] fix: memperbaiki api predik tomat, riwayat dan setting dan riwayat --- .../__pycache__/predictTomat.cpython-310.pyc | Bin 8017 -> 8155 bytes .../riwayatPengujian.cpython-310.pyc | Bin 3098 -> 3132 bytes .../settingPredict.cpython-310.pyc | Bin 3311 -> 3846 bytes .../__pycache__/testingModel.cpython-310.pyc | Bin 4277 -> 4277 bytes backend/routes/predictTomat.py | 53 +++- backend/routes/riwayatPengujian.py | 2 +- backend/routes/settingPredict.py | 28 +- frontend/src/pages/Panel/Setting/Setting.jsx | 289 +++++++++++++++--- 8 files changed, 307 insertions(+), 65 deletions(-) diff --git a/backend/routes/__pycache__/predictTomat.cpython-310.pyc b/backend/routes/__pycache__/predictTomat.cpython-310.pyc index ea1563ef493090c97cbdcc6610c04132b31a8cef..01c90e66304275d71b852ca1b4852ef0479ef310 100644 GIT binary patch delta 2064 zcmZvcZERCj7{|{!eOt4!Uu72W*_n zlq!N+9XX;g4APPqf}lv2XfP&d;zvH{2mLVOXA=`+)cC;!)aSX>0mdfx*XKO{r_bqm z?*HCzhwcnJryLF&fzQn^JO{oRy5aoUNKY=`HGV}GctBW01rHVsT4kN_B zAlazq6h`jSqXn0!EXIUWxJrx?wO(xy#yri_$H?v)%?){XfM``v(PxGh5=Zs#opriH-jVYP!Nbq6Q3I(@4Uc+LW*iimKrQ$+AK>m5SSVNF|r z2n<@w-?)xsj1M;jlOGfn#T0LsFIRgOI>s3^3&U1XxMFIT2=h*!Dlm9x8h0_R9yT(m z-J+g%6{w$R4Wgmg!@+r4qqol zR|iy!x>2=2c2L&_CmFZ$jRhk-IZi!aQ5=$IJQ!;Ob$&9IE^4A(K-L!1++9>^8 zR{MSnK8sJ}4#ZBx^N3xD7o^L75gtf+-JhVBBB`{d|3RNqCs2S_rd4&&>9 zc|)o#l{6_=F>wuX1@R%`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XlY! mXY&BBzC_9C2(+M#&yee+#~EB6#d`A0E%@BRxPV*@S# diff --git a/backend/routes/__pycache__/riwayatPengujian.cpython-310.pyc b/backend/routes/__pycache__/riwayatPengujian.cpython-310.pyc index 1bf9f9d0596dcd1dde4faa2e504e6c5784c26467..94221e2f1f83c3f72c6b35dd7df3f77e0df1852b 100644 GIT binary patch delta 563 zcmaiuJ4-A<5QV$C$JyCgXLeN(6)q_1T?G~2@7L-gVtPSAP(jBh=t|oLE3=}kfgqSv zCI~aX;3B351{xR$Mh2Uj`UgzZE|nW-a2xuos>3NxomE^_I2p&WnSNz=WovU6&Iz|3 z^mGV&Nep=PY+xza5F3#o8nO^_;IqK?A%q|nvK;ro<44TrX_ki>_Jg=#j#ja)|6ydzucwT75V0}RQz&6>;shIt?GSXX? z3yax;;Vthh;fvun{np3w1I}GY0Dp5ie|1FyT(I;n2kXH7s{B>9;(5_EByrxXGn@6A zY{6r50J`zUeD3HYp`S277$gi4OhQtlU@m1gH`i4omp!C4<5MIJDQvJ#s(MKPLK~r- z@QW~v_g2qP2es9NI>HE{mhk^nIf|)hRqQj7R$6c+Itk{SHo(b$0*& delta 579 zcmaivJ4*vW6ou!`Zq~`_u8W97K?rKV5b;40ABk_RY!z+AEQp1!j4NVHVl;MQHJuMg zS}Vm?P%P~N6a}(1|?7$LH0-YvY)*2U#6J z(l7GVzn_K_$+Hv`Anz@ZG_}E|M z2RSM$(^hRywLT*k)YeYuA=pxrQFcH&H100f9GaxE?uW7trOY*M7L*KtC5mcP_&j^v z9Wo=$Qfh()>Zhq#U;>k03$UzN`_m8JEABk^5D;0dOAAQ}?-7?;93rBuy@iLcF^m{N z#1L_Wh6qTMw1l(1zUBrgk76$*uJmTn%KBkK%(3IPL2Lq|57CckM~sShHncK;S3jZ) zF^1?w{J)ux%efNc&Q8MF-f+1h%b;5IsYXGco=To`Zv>{qlb2h6Qq1d-;I9h-B{Mo3W#=oqyI9ianFCVdJr!t^#0I=E(uXy{5SOUm>mksztmq%I9+>=;G+Il+QV z7TK_*9mdLJl9ZrmOWU8s(=29JPZMmOWv=g)J!lt3*|iuV?xq zB z#YqG|ofKRT;?V@03ob_Ev*tmIqf-~UH3uC0TGRnPg@J4F!PONuFT8$V%!^fj@Uia+ zkz3Br8z^SKv~iljbb0mVuWr*77HWniY(3)AIB=vd;-Z6h!j)`<|r27ml10x zZ_MYGJeCxNWVOv?wavBoOr_o6jMHg8}tp-Z7(A*rCA6F;qRQ$bU3719cK z6s{stIv-QEQY|<%+tba}q;u5_bH~y?#AUw)L7%jnb)fF0v-V!5aVgauJc@q2bQ?{6 zR^bX_PhTncf?reqE#+$~t71>fEeqbG{;3Py)9)7wOME)Wx;>Byw%qYveV+pTO({L1 Z8a<>s{Y4FWKuveQvES76qv~q?(I4vq+TQ>G delta 460 zcmYk1PfHs?7{=$F-OS9+Zk7}wHfpxkgEq#$M-M^-g$l+?Z$0!-Cy}($l+EhFOrSJk zDOfM#Ng`~(9(t0JPvF;hJYq4|ybH1Dl9te-k&he&iYn_H{R~y~tSmr?e68dlAt7|%%MbtwM8->qE$A;;#aGeyHvZ^!8YbHfdMvC%v*fjbYI zX4}oLEn!4IO}IYT)B}%t7NrzMgAKDX$!9DH5?4Vbgk?NZcy5~MTi7=1es1z527T+} oVbr&3AEIzJvG&_~{U9uxBYzg&m@9w7W?&D@y`N=tNO@-P7ne3=SO5S3 diff --git a/backend/routes/__pycache__/testingModel.cpython-310.pyc b/backend/routes/__pycache__/testingModel.cpython-310.pyc index 04daa363b3dd0efeaccaf6dadefe037b395ca16d..45bf28f40949f6a0de134f07f75ac6c02cbc4058 100644 GIT binary patch delta 20 acmdn0xK)umpO=@50SF@Y3UB0IB>(_490chA delta 20 acmdn0xK)umpO=@50SL(_4-~|K# diff --git a/backend/routes/predictTomat.py b/backend/routes/predictTomat.py index 92f6f3bf..132cade7 100644 --- a/backend/routes/predictTomat.py +++ b/backend/routes/predictTomat.py @@ -85,14 +85,25 @@ def predict_price(db: Session = Depends(get_db)): coef0 = float(settings.nilai_coef) if settings.nilai_coef is not None else 0.0 # Inisialisasi Model SVR + # if kernel == "linear": + # svr = SVR(kernel=kernel, C=C, epsilon=epsilon) + # else: + # svr = SVR(kernel=kernel, C=C, gamma=gamma, epsilon=epsilon) + # if kernel in ["poly", "sigmoid"]: + # svr.coef0 = coef0 + # if kernel == "poly": + # svr.degree = degree if kernel == "linear": - svr = SVR(kernel=kernel, C=C, epsilon=epsilon) - else: - svr = SVR(kernel=kernel, C=C, gamma=gamma, epsilon=epsilon) - if kernel in ["poly", "sigmoid"]: - svr.coef0 = coef0 - if kernel == "poly": - svr.degree = degree + svr = SVR(kernel="linear", C=C, epsilon=epsilon) + + elif kernel == "rbf": + svr = SVR(kernel="rbf", C=C, gamma=gamma, epsilon=epsilon) + + elif kernel == "sigmoid": + svr = SVR(kernel="sigmoid", C=C, gamma=gamma, coef0=coef0, epsilon=epsilon) + + elif kernel == "poly": + svr = SVR(kernel="poly", C=C, gamma=gamma, coef0=coef0, degree=degree, epsilon=epsilon) # **Melakukan Prediksi Rolling Window** hasil_prediksi = [] @@ -215,16 +226,28 @@ def get_price_history( # 8. Latih Model SVR dengan Kernel Linear # Inisialisasi Model SVR - if kernel == "linear": - svr = SVR(kernel=kernel, C=C, epsilon=epsilon) - else: - svr = SVR(kernel=kernel, C=C, gamma=gamma, epsilon=epsilon) - if kernel in ["poly", "sigmoid"]: - svr.coef0 = coef0 - if kernel == "poly": - svr.degree = degree + # if kernel == "linear": + # svr = SVR(kernel=kernel, C=C, epsilon=epsilon) + # else: + # svr = SVR(kernel=kernel, C=C, gamma=gamma, epsilon=epsilon) + # if kernel in ["poly", "sigmoid"]: + # svr.coef0 = coef0 + # if kernel == "poly": + # svr.degree = degree # svr = SVR(kernel='linear', C=1.0, epsilon=0.01) # svr = SVR(kernel='rbf', C=1.0, epsilon=0.01, gamma='scale') + if kernel == "linear": + svr = SVR(kernel="linear", C=C, epsilon=epsilon) + + elif kernel == "rbf": + svr = SVR(kernel="rbf", C=C, gamma=gamma, epsilon=epsilon) + + elif kernel == "sigmoid": + svr = SVR(kernel="sigmoid", C=C, gamma=gamma, coef0=coef0, epsilon=epsilon) + + elif kernel == "poly": + svr = SVR(kernel="poly", C=C, gamma=gamma, coef0=coef0, degree=degree, epsilon=epsilon) + svr.fit(X_train, y_train) # 9. Lakukan Prediksi pada Data Uji diff --git a/backend/routes/riwayatPengujian.py b/backend/routes/riwayatPengujian.py index 456eb401..358b9340 100644 --- a/backend/routes/riwayatPengujian.py +++ b/backend/routes/riwayatPengujian.py @@ -20,7 +20,7 @@ async def get_all_riwayat(db: Session = Depends(get_db)): try: query = select(riwayatPengujian, settingPredict.c.nama_kernel).join( settingPredict, riwayatPengujian.c.id_kernel == settingPredict.c.id - ) + ).order_by(riwayatPengujian.c.id.desc()) result = db.execute(query).fetchall() return [dict(row._mapping) for row in result] except SQLAlchemyError as e: diff --git a/backend/routes/settingPredict.py b/backend/routes/settingPredict.py index 5284b823..11e0a5e0 100644 --- a/backend/routes/settingPredict.py +++ b/backend/routes/settingPredict.py @@ -59,7 +59,7 @@ ALLOWED_FIELDS = { "rbf": ["nilai_c", "nilai_gamma", "nilai_epsilon", "status"] } -# ✅ Update Setting by ID tanpa request body (Menggunakan Parameter) + @settingPredict_router.put("/{setting_id}",dependencies=[Depends(verify_token)]) async def update_setting( setting_id: int, @@ -68,7 +68,7 @@ async def update_setting( nilai_epsilon: str = Query(None), nilai_degree: str = Query(None), nilai_coef: str = Query(None), - status: bool = Query(None) + ): # Cek apakah setting dengan ID tersebut ada query = select(settingPredict.c.nama_kernel).where(settingPredict.c.id == setting_id) @@ -87,7 +87,7 @@ async def update_setting( "nilai_epsilon": nilai_epsilon, "nilai_degree": nilai_degree, "nilai_coef": nilai_coef, - "status": status + } # Filter hanya field yang diperbolehkan untuk kernel tersebut @@ -105,3 +105,25 @@ async def update_setting( conn.commit() return {"message": "Setting updated successfully", "updated_fields": update_data} + +@settingPredict_router.put("/update-status/{setting_id}", dependencies=[Depends(verify_token)]) +async def update_status_setting( + setting_id: int, + status: bool = Query(...) +): + # Cek apakah setting dengan ID tersebut ada + query = select(settingPredict.c.id).where(settingPredict.c.id == setting_id) + result = conn.execute(query).fetchone() + + if not result: + raise HTTPException(status_code=404, detail="Setting not found") + + # Update hanya status + query = update(settingPredict).where(settingPredict.c.id == setting_id).values(status=status) + result = conn.execute(query) + + if result.rowcount == 0: + raise HTTPException(status_code=400, detail="Failed to update status") + + conn.commit() + return {"message": "Status updated successfully", "updated_status": status} diff --git a/frontend/src/pages/Panel/Setting/Setting.jsx b/frontend/src/pages/Panel/Setting/Setting.jsx index 0dd9ef96..81a147a4 100644 --- a/frontend/src/pages/Panel/Setting/Setting.jsx +++ b/frontend/src/pages/Panel/Setting/Setting.jsx @@ -1,4 +1,4 @@ -import React,{useState, useEffect} from 'react' +import React, { useState, useEffect } from 'react' import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Button } from '@/components/ui/button' @@ -8,11 +8,51 @@ import { API_URL } from "../../../helpers/networt"; import { useToast } from '@/hooks/use-toast'; const Setting = () => { - const { toast } = useToast(); + const { toast } = useToast(); const getemail = localStorage.getItem("email"); const idUser = localStorage.getItem("idUser"); const [email, setEmail] = useState(getemail); const [password, setPassword] = useState(""); + const [selectedKernel, setSelectedKernel] = useState(""); + const [selectedKernelfalse, setSelectedKernelfalse] = useState(""); + const [nilaiC, setNilaiC] = useState(""); + const [epsilon, setEpsilon] = useState(""); + const [gamma, setGamma] = useState(""); + const [degree, setDegree] = useState(""); + const [coef0, setCoef0] = useState(""); + + + const fetchDataKernel = async () => { + const token = localStorage.getItem("token"); + try { + const response = await axios.get(`${API_URL}/setpredict/`, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + console.log(response.data) + if (response.data.length > 0) { + setSelectedKernel(response.data[0].id) + setSelectedKernelfalse(response.data[0].id) + setNilaiC(response.data[0].nilai_c) + setEpsilon(response.data[0].nilai_epsilon) + setGamma(response.data[0].nilai_gamma) + setDegree(response.data[0].nilai_degree) + setCoef0(response.data[0].nilai_coef) + } + // setDataHarga(response.data) + } catch (error) { + console.error("Error fetching data", error); + } + } + + useEffect(() => { + fetchDataKernel(); + }, []); + + const handleCheckboxChange = (kernel) => { + setSelectedKernel(kernel === selectedKernel ? "" : kernel); + }; const handleSaveData = async () => { @@ -21,28 +61,123 @@ const Setting = () => { const response = await axios.put( `${API_URL}/auth/forgot-password`, { - email: email, - new_password: password + email: email, + new_password: password }, { - headers: { - "Content-Type": "application/json", - Accept: "application/json", - Authorization: `Bearer ${token}`, - }, + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, } - ); - - - toast({ + ); + + + toast({ description: "Pengaturan akun berhasil disimpan", - }); + }); } catch (error) { // console.log(error.response.data.detail) - toast({ - description: `${error.response.data.detail}`, - variant: "destructive", - }); + toast({ + description: `${error.response.data.detail}`, + variant: "destructive", + }); + } + } + + const handleSaveDataKernel = async () => { + const token = localStorage.getItem("token"); + console.log(selectedKernelfalse) + console.log(selectedKernel) + try { + + const updateStatus = await axios.put( + `${API_URL}/setpredict/update-status/${selectedKernelfalse}?status=false`, + null, + { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + } + ); + + const updateStatusnew = await axios.put( + `${API_URL}/setpredict/update-status/${selectedKernel}?status=true`, + null, + { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + } + ); + + if (selectedKernel === 1) { + const linear = await axios.put( + `${API_URL}/setpredict/1?nilai_c=${nilaiC}&nilai_epsilon=${epsilon}`, + null, + { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + } + ); + } else if (selectedKernel ===2 ){ + const poly = await axios.put( + `${API_URL}/setpredict/2?nilai_c=${nilaiC}&nilai_gamma=${gamma}&nilai_epsilon=${epsilon}&nilai_degree=${degree}&nilai_coef=${coef0}`, + null, + { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + } + ); + } else if (selectedKernel ===3 ){ + const sigmoid = await axios.put( + `${API_URL}/setpredict/3?nilai_c=${nilaiC}&nilai_gamma=${gamma}&nilai_epsilon=${epsilon}&nilai_coef=${coef0}`, + null, + { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + } + ); + } else if (selectedKernel ===4 ){ + const rbf = await axios.put( + `${API_URL}/setpredict/4?nilai_c=${nilaiC}&nilai_gamma=${gamma}&nilai_epsilon=${epsilon}`, + null, + { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + } + ); + } + + toast({ + description: "Pengaturan model berhasil disimpan", + }); + + fetchDataKernel(); + + + } catch (error) { + toast({ + description: `${error.response.data.detail}`, + variant: "destructive", + }); } } return ( @@ -53,15 +188,15 @@ const Setting = () => {
- +
setPassword(e.target.value)} - required/> + onChange={(e) => setPassword(e.target.value)} + required />
@@ -78,7 +213,11 @@ const Setting = () => {
- + handleCheckboxChange(1)} + />
- + handleCheckboxChange(2)} + />
- + handleCheckboxChange(3)} + />
- + handleCheckboxChange(4)} + />
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
+
+ + setNilaiC(e.target.value)} + disabled={ + selectedKernel !== 1 && + selectedKernel !== 2 && + selectedKernel !== 3 && + selectedKernel !== 4 + } + + /> +
+
+ + setEpsilon(e.target.value)} + disabled={ + selectedKernel !== 1 && + selectedKernel !== 2 && + selectedKernel !== 3 && + selectedKernel !== 4 + } + /> +
+
+ + setGamma(e.target.value)} + disabled={ + selectedKernel !== 2 && + selectedKernel !== 3 && + selectedKernel !== 4 + } + /> +
+
+ + setDegree(e.target.value)} + disabled={selectedKernel !== 2} /> +
+
+ + setCoef0(e.target.value)} + disabled={ + selectedKernel !== 2 && selectedKernel !== 3 + } + /> +
- +