Commit b5b4e1a2 authored by Maik Messerschmidt's avatar Maik Messerschmidt
Browse files

Made Result controller log server errors on the submit route

parent be564368
......@@ -196,69 +196,79 @@ class ResultController extends Controller
* API route for submitting results for the experiment.
*/
public function submit(Request $request) {
$data = $request->data;
$run_id = $request->id;
$isPartial = $request->partial ? true : false;
try {
fdsfds();
$data = $request->data;
$run_id = $request->id;
$isPartial = $request->partial ? true : false;
// In case the client didn't provide a id, we add a new one.
$run_id = $run_id ?? Str::random(60);
if ($data === null)
return [
'code' => 3,
'message' => 'Invalid data format. Please provide valid JSON.'
];
// Mark the experiment as done, if this is final data.
$experiment = Experiment::firstWhere('run_id', $run_id);
if ($experiment && ! $isPartial) {
$experiment->status = Experiment::STATUS_DONE;
if (! config('experiment.store-session-id'))
$experiment->session_id = null;
$experiment->save();
}
// Check, if we already have partial data lying around.
// NOTE: Result::firstOrNew() somehow doesn't work here...
$result = Result::where('run_id', $run_id)
->where('partial', true)
->first();
$result = $result ?? Result::make([
'run_id' => $run_id,
]);
$result->partial = $isPartial;
$result->data = $data;
if (config('experiment.store-ip')) {
$result->ip = IPAnonymizer::anonymize($request->ip());
Log::info("Storing data from (anonymized) ip '{$result->ip}'.");
}
else
$result->ip = null;
if ($experiment && $isPartial) {
$result->session_id = $experiment->session_id;
}
else if (! config('experiment.store-session-id')) {
$result->session_id = null;
}
// Note: even though results can reference there Experiments
// via the experiment_id key, we don't set it here, because
// results should probably keep existing, when the Experiment
// is deleted, but we configured the database to cascade delete.
// I think, the 'experiment_id' key maybe should be removed.
$result->save();
// In case the client didn't provide a id, we add a new one.
$run_id = $run_id ?? Str::random(60);
if ($data === null)
return [
'code' => 3,
'message' => 'Invalid data format. Please provide valid JSON.'
'code' => 0,
'message' => 'Result submitted.',
'session' => $run_id,
];
// Mark the experiment as done, if this is final data.
$experiment = Experiment::firstWhere('run_id', $run_id);
if ($experiment && ! $isPartial) {
$experiment->status = Experiment::STATUS_DONE;
if (! config('experiment.store-session-id'))
$experiment->session_id = null;
$experiment->save();
}
// Check, if we already have partial data lying around.
// NOTE: Result::firstOrNew() somehow doesn't work here...
$result = Result::where('run_id', $run_id)
->where('partial', true)
->first();
$result = $result ?? Result::make([
'run_id' => $run_id,
]);
$result->partial = $isPartial;
$result->data = $data;
if (config('experiment.store-ip')) {
$result->ip = IPAnonymizer::anonymize($request->ip());
Log::info("Storing data from (anonymized) ip '{$result->ip}'.");
}
else
$result->ip = null;
if ($experiment && $isPartial) {
$result->session_id = $experiment->session_id;
}
else if (! config('experiment.store-session-id')) {
$result->session_id = null;
catch (\Throwable $e) {
Log::error("Submit request failed:\n{$request}\nError was:\n{$e}");
return [
'code' => 500,
'message' => 'Your request caused a server error. It has been logged.'
];
}
// Note: even though results can reference there Experiments
// via the experiment_id key, we don't set it here, because
// results should probably keep existing, when the Experiment
// is deleted, but we configured the database to cascade delete.
// I think, the 'experiment_id' key maybe should be removed.
$result->save();
return [
'code' => 0,
'message' => 'Result submitted.',
'session' => $run_id,
];
}
public function results() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment