|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "evolve/db/connection" |
| 6 | + "evolve/modules" |
| 7 | + "evolve/util" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | +) |
| 12 | + |
| 13 | +func CreateMOBO(res http.ResponseWriter, req *http.Request) { |
| 14 | + logger := util.NewLogger() |
| 15 | + logger.Info("CreateMOBO API called.") |
| 16 | + |
| 17 | + // ----------------------------- |
| 18 | + // AUTHENTICATE USER |
| 19 | + // ----------------------------- |
| 20 | + user, err := modules.Auth(req) |
| 21 | + if err != nil { |
| 22 | + util.JSONResponse(res, http.StatusUnauthorized, err.Error(), nil) |
| 23 | + return |
| 24 | + } |
| 25 | + // For local testing: |
| 26 | + // user := map[string]any{ |
| 27 | + // "email": "local@test.com", |
| 28 | + // "fullName": "Local Test User", |
| 29 | + // "id": "00000000-0000-0000-0000-000000000000", |
| 30 | + // "role": "user", |
| 31 | + // "userName": "localtester", |
| 32 | + // } |
| 33 | + logger.Info(fmt.Sprintf("User: %v", user)) |
| 34 | + |
| 35 | + // ----------------------------- |
| 36 | + // PARSE JSON BODY |
| 37 | + // ----------------------------- |
| 38 | + data, err := util.Body(req) |
| 39 | + if err != nil { |
| 40 | + util.JSONResponse(res, http.StatusBadRequest, err.Error(), nil) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + mobo, err := modules.MOBOFromJSON(data) |
| 45 | + if err != nil { |
| 46 | + util.JSONResponse(res, http.StatusBadRequest, err.Error(), nil) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + // ----------------------------- |
| 51 | + // GENERATE PYTHON CODE |
| 52 | + // ----------------------------- |
| 53 | + code, err := mobo.Code() |
| 54 | + if err != nil { |
| 55 | + util.JSONResponse(res, http.StatusBadRequest, err.Error(), nil) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + // ----------------------------- |
| 60 | + // CONNECT DB |
| 61 | + // ----------------------------- |
| 62 | + db, err := connection.PoolConn(req.Context()) |
| 63 | + if err != nil { |
| 64 | + logger.Error(fmt.Sprintf("CreateMOBO.PoolConn: %s", err.Error())) |
| 65 | + util.JSONResponse(res, http.StatusInternalServerError, "something went wrong", nil) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + // ----------------------------- |
| 70 | + // INSERT RUN METADATA |
| 71 | + // ----------------------------- |
| 72 | + runName := fmt.Sprintf("MOBO-%s-%dD", mobo.Problem, len(mobo.Bounds)) |
| 73 | + |
| 74 | + row := db.QueryRow(req.Context(), ` |
| 75 | + INSERT INTO run (name, description, type, command, createdBy) |
| 76 | + VALUES ($1, $2, $3, $4, $5) |
| 77 | + RETURNING id |
| 78 | + `, runName, "Multi-Objective Bayesian Optimization", "bo", "python mobo.py", user["id"]) |
| 79 | + |
| 80 | + var runID string |
| 81 | + err = row.Scan(&runID) |
| 82 | + if err != nil { |
| 83 | + logger.Error(fmt.Sprintf("CreateMOBO.row.Scan: %s", err.Error())) |
| 84 | + util.JSONResponse(res, http.StatusInternalServerError, "something went wrong", nil) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + logger.Info(fmt.Sprintf("MOBO RunID: %s", runID)) |
| 89 | + |
| 90 | + // ----------------------------- |
| 91 | + // ACCESS TABLE ENTRY |
| 92 | + // ----------------------------- |
| 93 | + _, err = db.Exec(req.Context(), ` |
| 94 | + INSERT INTO access (runID, userID, mode) |
| 95 | + VALUES ($1, $2, $3) |
| 96 | + `, runID, user["id"], "write") |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + logger.Error(fmt.Sprintf("CreateMOBO.db.Exec: %s", err.Error())) |
| 100 | + util.JSONResponse(res, http.StatusInternalServerError, "something went wrong", nil) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + // ----------------------------- |
| 105 | + // UPLOAD PYTHON CODE TO MINIO |
| 106 | + // ----------------------------- |
| 107 | + os.Mkdir("mobo", 0755) |
| 108 | + pythonPath := fmt.Sprintf("mobo/%v.py", runID) |
| 109 | + |
| 110 | + if err := os.WriteFile(pythonPath, []byte(code), 0644); err != nil { |
| 111 | + logger.Error(fmt.Sprintf("CreateMOBO.WriteFile: %s", err.Error())) |
| 112 | + util.JSONResponse(res, http.StatusInternalServerError, "something went wrong", nil) |
| 113 | + return |
| 114 | + } |
| 115 | + if err := util.UploadFile(req.Context(), runID, "mobo", "py"); err != nil { |
| 116 | + util.JSONResponse(res, http.StatusInternalServerError, err.Error(), nil) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + // ----------------------------- |
| 121 | + // UPLOAD INPUT JSON |
| 122 | + // ----------------------------- |
| 123 | + inputParams, err := json.Marshal(data) |
| 124 | + if err != nil { |
| 125 | + logger.Error(fmt.Sprintf("CreateMOBO.Marshal: %s", err.Error())) |
| 126 | + util.JSONResponse(res, http.StatusInternalServerError, "something went wrong", nil) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + os.Mkdir("input", 0755) |
| 131 | + inputPath := fmt.Sprintf("input/%v.json", runID) |
| 132 | + |
| 133 | + if err := os.WriteFile(inputPath, inputParams, 0644); err != nil { |
| 134 | + logger.Error(fmt.Sprintf("CreateMOBO.WriteFile(input): %s", err.Error())) |
| 135 | + util.JSONResponse(res, http.StatusInternalServerError, "something went wrong", nil) |
| 136 | + return |
| 137 | + } |
| 138 | + if err := util.UploadFile(req.Context(), runID, "input", "json"); err != nil { |
| 139 | + util.JSONResponse(res, http.StatusInternalServerError, err.Error(), nil) |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + // ----------------------------- |
| 144 | + // CLEAN UP LOCAL FILES |
| 145 | + // ----------------------------- |
| 146 | + os.Remove(pythonPath) |
| 147 | + os.Remove(inputPath) |
| 148 | + |
| 149 | + // ----------------------------- |
| 150 | + // PUSH TO REDIS QUEUE |
| 151 | + // ----------------------------- |
| 152 | + if err := util.EnqueueRunRequest(req.Context(), runID, "mobo", "py"); err != nil { |
| 153 | + util.JSONResponse(res, http.StatusInternalServerError, err.Error(), nil) |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + // Attach runID to response |
| 158 | + data["runID"] = runID |
| 159 | + |
| 160 | + util.JSONResponse(res, http.StatusOK, "MOBO Created Successfully 🎉", data) |
| 161 | +} |
0 commit comments