Exemplo n.º 1
0
func (c SecuredApplication) SaveProgram(mpNumString string) revel.Result {
	var program string
	var mp models.MachineProblem

	mpNum, err := strconv.Atoi(mpNumString)
	if err != nil {
		c.Flash.Error("Invalid machine problem")
		return c.Render(routes.PublicApplication.Index())
	}

	user := c.connected()

	c.Params.Bind(&program, "program")

	stats.Incr("App", "ProgramSave")

	if mp, err = models.FindOrCreateMachineProblemByUser(user, mpNum); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"data":   "Could not create mp.",
		})
	}

	if prog, err := models.CreateProgram(mp, program); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"data":   "Could not save program.",
		})
	} else {
		return c.RenderJson(map[string]interface{}{
			"status": "success",
			"id":     prog.Id,
		})
	}
}
Exemplo n.º 2
0
func revertMachineProblemProgram(user models.User, mp models.MachineProblem) (models.Program, error) {
	conf, err := ReadMachineProblemConfig(mp.Number)
	if err != nil {
		return models.Program{}, err
	}
	templateProgram := conf.CodeTemplate
	return models.CreateProgram(mp, templateProgram)
}
Exemplo n.º 3
0
func (c SecuredApplication) SubmitProgram(mpNumString string) (res revel.Result) {
	var program string
	var datasetId int

	mpNum, err := strconv.Atoi(mpNumString)
	if err != nil {
		c.Flash.Error("Invalid machine problem")
		return c.Render(routes.PublicApplication.Index())
	}

	user := c.connected()

	c.Params.Bind(&program, "program")
	c.Params.Bind(&datasetId, "datasetId")

	mp, err := models.FindOrCreateMachineProblemByUser(user, mpNum)
	if err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error finding MP record",
			"data":   "System was not able to save program.",
		})
	}

	if lastAttempt, err := models.FindLastAttemptByMachineProblem(mp); err == nil && time.Since(lastAttempt.Created).Seconds() < 5 {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Attempt limiter",
			"data":   "Too many attempts. Please wait 10 seconds between attempts.",
		})
	}

	if _, err := models.CreateProgram(mp, program); err != nil {
		return c.RenderJson(map[string]interface{}{
			"status": "error",
			"title":  "Error Saving Program",
			"data":   "System was not able to save program.",
		})
	}

	runId := generateRunId(user)

	conf, _ := ReadMachineProblemConfig(mpNum)

	stats.Incr("App", "ProgramSubmission")

	server.SubmitProgram(mp, program, datasetId, conf.Language, runId, false)
	return c.RenderJson(map[string]interface{}{
		"status":  "success",
		"runId":   runId,
		"attempt": "Attempt submitted",
	})
}
Exemplo n.º 4
0
// TODO: This should not live here
func CreateAttemptWithStates(states []WorkerState) (attempts []models.Attempt, grade models.Grade, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(fmt.Sprint(r))
		}
	}()

	if len(states) == 0 {
		err = errors.New("Invalid attempt")
		return
	}

	firstState := states[0]

	mp, err := models.FindMachineProblem(firstState.MachineProblemId)
	if err != nil {
		return
	}

	prog, err := models.CreateProgram(mp, firstState.Program)
	if err != nil {
		return
	}

	toGrade := false

	for _, ws := range states {

		jsInternalCData, _ := json.Marshal(ws.InternalCData)

		attempt := models.Attempt{
			MachineProblemInstanceId: mp.Id,
			ProgramInstanceId:        prog.Id,
			RunId:                    ws.RunId,
			DatasetId:                ws.DatasetId,
			CompilationFailed:        ws.CompilationFailed,
			CompileStderr:            strings.TrimSpace(ws.CompileStderr),
			CompileStdout:            strings.TrimSpace(ws.CompileStdout),
			RunFailed:                ws.RunFailed,
			RunStdout:                strings.TrimSpace(ws.RunStdout),
			RunStderr:                strings.TrimSpace(ws.RunStderr),
			TimeoutError:             ws.TimeoutError,
			TimeoutValue:             ws.TimeoutValue,
			Sandboxed:                ws.Sandboxed,
			SandboxKeyword:           ws.SandboxKeyword,
			CompileElapsedTime:       ws.CompileEndTime.Sub(ws.CompileStartTime).Nanoseconds(),
			RunElapsedTime:           ws.RunEndTime.Sub(ws.RunStartTime).Nanoseconds(),
			CompileStartTime:         ws.CompileStartTime,
			CompileEndTime:           ws.CompileEndTime,
			RunStartTime:             ws.RunStartTime,
			RunEndTime:               ws.RunEndTime,
			RequestStartTime:         ws.RequestStartTime,
			RequestEndTime:           ws.RequestEndTime,
			SolutionCorrect:          ws.SolutionCorrect,
			SolutionMessage:          ws.SolutionMessage,
			UserOutput:               ws.UserOutput,
			Language:                 ws.Language,
			OnAllDatasets:            ws.OnAllDatasets,
			InternalCData:            string(jsInternalCData),
			Created:                  time.Now(),
			Updated:                  time.Now(),
		}

		if ws.ForGrading {
			attempt.GradedQ = true
			toGrade = true
		}

		err = models.DB.Save(&attempt).Error
		if err != nil {
			revel.TRACE.Println("Failed saving attempt..  ", err)
			stats.Incr("Master", "FailedAttemptStore")
		} else {
			attempts = append(attempts, attempt)
		}
	}

	if toGrade {
		stats.Incr("Master", "UpdatingGrade")
		//revel.TRACE.Println("Updating grade...")
		grade, err = models.UpdateGradeWithAttempts(attempts)
		if err != nil {
			revel.TRACE.Println("Error updating grade  ", err)
		}
	}

	return
}