func (app *App) handleModel(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": vars := mux.Vars(r) name := vars["name"] user, ok := app.getUser(r) if !ok { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } tx, err := app.db.Begin() if err != nil { app.dbError(w, r, err) return } defer tx.Rollback() model, err := db.GetModel(tx, user.Name, name) if err != nil { if db.IsNotFound(err) { http.Error(w, "Model not found", http.StatusNotFound) } else { app.dbError(w, r, err) } return } v := model.ActiveVersion activeVersion, err := db.GetModelVersion(tx, user.Name, name, v) if err != nil { if db.IsNotFound(err) { http.Error(w, "Model not found", http.StatusNotFound) } else { app.dbError(w, r, err) } return } app.writeJson(w, map[string]interface{}{ "Model": model, "Version": activeVersion, }) default: http.Error(w, "I only respond to GETs", http.StatusNotImplemented) } }
func (app *App) handleModelLogs(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": vars := mux.Vars(r) name := vars["name"] user, ok := app.getUser(r) if !ok { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } tx, err := app.db.Begin() if err != nil { app.dbError(w, r, err) return } defer tx.Rollback() model, err := db.GetModel(tx, user.Name, name) if err != nil { if db.IsNotFound(err) { http.Error(w, "Model not found", http.StatusNotFound) } else { app.dbError(w, r, err) } return } logLines, err := alb.ReadDeploymentLogs(app.modelLogsDir, user.Name, name, model.LastDeployment) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } app.writeJson(w, logLines) default: http.Error(w, "I only respond to GETs", http.StatusNotImplemented) } }
// - [x] create N users // - [x] create N apikeys & read-only apikeys // - [x] create M models for each user // - [x] create J versions for model // - [x] create model status for each model // - [ ] associate model w/ MPS (?) // - [x] give (some) models JSON/HTML examples // - [x] share model with K users func seedFunc(conn *sql.DB) error { tx, err := conn.Begin() if err != nil { return fmt.Errorf("cannot begin transaction %v", err) } defer tx.Rollback() log.Println("truncating tables in db") tx.Exec(`use scienceops;`) tx.Exec(`SET FOREIGN_KEY_CHECKS=0;`) rows, err := tx.Query(`show tables;`) if err != nil { return fmt.Errorf("could not truncate database: ", err) } var tables []string defer rows.Close() for rows.Next() { var t string rows.Scan(&t) tables = append(tables, t) } for _, t := range tables { q := fmt.Sprintf("TRUNCATE TABLE %s;", t) if _, err := tx.Exec(q); err != nil { fmt.Println(err) return fmt.Errorf("Could not truncate table %s: %v", t, err) } } tx.Exec(`SET FOREIGN_KEY_CHECKS=1;`) log.Println("seeding db") nUsers := 3 nModels := 10 nVersions := 15 os.MkdirAll("/tmp/bundles/", 0777) hashedPass := "******" for _, user := range []string{"eric", "ryan", "greg", "sush", "colin", "brandon", "austin", "charlie"} { _, err := db.NewUser(tx, user, hashedPass, user+"@yhathq.com", true) if err != nil { return fmt.Errorf("could not create user: %v", err) } } for _, user := range []string{"bigdatabob"} { _, err := db.NewUser(tx, user, hashedPass, user+"@yhathq.com", false) if err != nil { return fmt.Errorf("could not create user: %v", err) } } for i := 0; i < nUsers; i++ { username := fmt.Sprintf("user-%d", i) email := fmt.Sprintf("*****@*****.**", username) user, err := db.NewUser(tx, username, hashedPass, email, true) if err != nil { return fmt.Errorf("could not create user: %v", err) } log.Printf("Created user %s", user.Name) for j := 0; j < nModels; j++ { name := randomdata.SillyName() params := &db.NewVersionParams{ UserId: user.Id, Model: name, Lang: db.LangPython2, SourceCode: "print HI!", BundleFilename: "/foobar/bundle.json", } for v := 0; v < nVersions; v++ { if _, err := db.NewModelVersion(tx, params); err != nil { return fmt.Errorf("could not create version: %v", err) } } model, err := db.GetModel(tx, username, name) if err != nil { return fmt.Errorf("could not get model %s/%s: %v", username, name, err) } err = db.SetModelStatus(tx, model.Id, "online") if err != nil { fmt.Println(err) return fmt.Errorf("could not insert model status: %v", err) } } } log.Printf("added %d users to db\n", nUsers) return tx.Commit() }