ResultController.php 8.59 KB
Newer Older
1
2
3
4
5
6
7
8
<?php

namespace App\Http\Controllers;

use App\Experiment;
use App\Result;
use App\Logic\IPAnonymizer;

9
10
use League\Csv\Writer;

11
12
13
14
use Illuminate\Http\Request;
use Str;
use Log;

15
16
17
18

use ZipStream\Option\Archive as ZipOptions;
use ZipStream\ZipStream;

19

20
21
class ResultController extends Controller
{
22
23
24
25
26
    /**
     * Fields provided to the index view.
     *
     * Needed to avoid including the 'data' field, which can get very large.
     */
27
    const ROWS_FOR_INDEX = [
28
29
30
31
32
33
        "id",
        "created_at",
        "ip",
        "session_id",
    ];

34
35
36
37
38
39
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
40
        $results = Result::select(self::ROWS_FOR_INDEX)
41
42
43
44
            ->where('partial', false)
            ->get();

        return view('results.index', ['results' => $results]);
45
46
47
    }

    public function indexPartial() {
48
        $results = Result::select(self::ROWS_FOR_INDEX)
49
50
51
52
            ->where('partial', true)
            ->get();

        return view('results.index', ['results' => $results]);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Result $result) {
        $header = collect($result->data ?? [])
            ->map(function($entry) {
                return array_keys($entry);
            })
            ->flatten()
            ->unique();

        $rows = collect($result->data ?? [])
            ->map(function($row) use ($header) {
                foreach ($header as $key) {
                    // Fill empty columns with N/A
                    $row[$key] = $row[$key] ?? config('experiment.n-a-text');

                    // Replace arrays with json
                    if (is_array($row[$key]))
                        $row[$key] = json_encode($row[$key]);
                }

                return $row;
            });

        return view('results.show', [
            'header' => $header,
            'rows' => $rows,
        ]);
    }

110
111
112
113
114
115
116
117
118
119
120
121
122
123
    // Returns object obj with obj->header: array with header info
    //                         obj->rows: arrays with data.
    //
    // @param $results Collection of Result
    private function getResults($results) {
        $header = collect($results->first()->data ?? [])
            ->map(function($entry) {
                return array_keys($entry);
            })
            ->flatten()
            ->unique();

        $all_rows = collect();

124
125
126
127
        $id = config('experiment.csv-id-header-start');
        $id_format = config('experiment.csv-id-header-format');
        $id_header = config('experiment.csv-id-header');

128
129
        foreach ($results as $result) {
            $rows = collect($result->data ?? [])
130
                ->map(function($row) use ($header, $id_header, $id_format, $id) {
131
132
133
134
135
136
137
138
139
                    foreach ($header as $key) {
                        // Fill empty columns with N/A
                        $row[$key] = $row[$key] ?? config('experiment.n-a-text');

                        // Replace arrays with json
                        if (is_array($row[$key]))
                            $row[$key] = json_encode($row[$key]);
                    }

140
141
142
143
144
145
                    if ($id_header)
                        return array_merge(
                            [$id_header => sprintf($id_format, $id)], $row);
                    else
                        return $row;

146
147
148
                });

            $all_rows = $all_rows->concat($rows);
149
150
151
152
153
            $id += 1;
        }

        if ($id_header) {
            $header->prepend($id_header);
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
        }

        return (object) [
            'header' => $header->toArray(),
            'rows' => $all_rows->toArray(),
        ];
    }

    public function showAllCSV() {
        header('Content-Type: text/csv; charset=utf-8');
        header('Content-Type: application/force-download');
        header('Content-Disposition: attachment; filename=results.csv');

        $data = $this->getResults(Result::all());

        $csv = Writer::createFromString('');
        $csv->insertOne($data->header);
        $csv->insertAll($data->rows);

        return $csv->getContent();
    }

176
    public function downloadZip() {
177
178
179
180
        $options = new ZipOptions();
        $options->setSendHttpHeaders(true);

        $zip = new ZipStream('results.zip', $options);
181

182
183
184
185
186
        Result::chunk(5, function($results) use ($zip) {
            foreach ($results as $result) {
                $filename = $result->created_at . ".json";
                $zip->addFile($filename, $result->getOriginal("data"));
            }
187
188
        });

189
        $zip->finish();
190
191
    }

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    /**
     * Display the result as json.
     */
    public function showJson(Result $result) {
        return $result->data;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Result $result) {
        $result->delete();
        return back();
    }

    /**
     * API route for submitting results for the experiment.
     */
    public function submit(Request $request) {
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
        try {
            $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();
295
296

            return [
297
298
299
                'code' => 0,
                'message' => 'Result submitted.',
                'session' => $run_id,
300
301
            ];
        }
302
303
304
305
306
307
        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.'
            ];
308
309
310
311
312
313
314
        }
    }

    public function results() {
        return Result::all()->values();
    }
}