-
Notifications
You must be signed in to change notification settings - Fork 2
/
CsvReportGenerator.php
269 lines (235 loc) · 8.48 KB
/
CsvReportGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
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
<?php
namespace Biigle\Modules\Reports\Support\Reports\Volumes\VideoAnnotations;
use Biigle\Label;
use Biigle\LabelTree;
use Biigle\Modules\Reports\Support\CsvFile;
use Biigle\Modules\Reports\Support\Reports\MakesZipArchives;
use Biigle\Modules\Reports\Support\Reports\Volumes\VolumeReportGenerator;
use Biigle\Modules\Reports\Traits\RestrictsToNewestLabels;
use Biigle\User;
use DB;
use Illuminate\Support\Str;
class CsvReportGenerator extends VolumeReportGenerator
{
use MakesZipArchives, RestrictsToNewestLabels;
/**
* Name of the report for use in text.
*
* @var string
*/
public $name = 'CSV video annotation report';
/**
* Name of the report for use as (part of) a filename.
*
* @var string
*/
public $filename = 'csv_video_annotation_report';
/**
* File extension of the report file.
*
* @var string
*/
public $extension = 'zip';
/**
* Get the report name.
*
* @return string
*/
public function getName()
{
$restrictions = [];
if ($this->isRestrictedToAnnotationSession()) {
$name = $this->getAnnotationSessionName();
$restrictions[] = "annotation session {$name}";
}
if ($this->isRestrictedToNewestLabel()) {
$restrictions[] = 'newest label of each annotation';
}
if (!empty($restrictions)) {
$suffix = implode(', ', $restrictions);
return "{$this->name} (restricted to {$suffix})";
}
return $this->name;
}
/**
* Get the filename.
*
* @return string
*/
public function getFilename()
{
$restrictions = [];
if ($this->isRestrictedToAnnotationSession()) {
$name = Str::slug($this->getAnnotationSessionName());
$restrictions[] = "annotation_session_{$name}";
}
if ($this->isRestrictedToNewestLabel()) {
$restrictions[] = 'newest_label';
}
if (!empty($restrictions)) {
$suffix = implode('_', $restrictions);
return "{$this->filename}_restricted_to_{$suffix}";
}
return $this->filename;
}
/**
* Generate the report.
*
* @param string $path Path to the report file that should be generated
*/
public function generateReport($path)
{
$rows = $this->query()->get();
$toZip = [];
if ($this->shouldSeparateLabelTrees() && $rows->isNotEmpty()) {
$rows = $rows->groupBy('label_tree_id');
$trees = LabelTree::whereIn('id', $rows->keys())->pluck('name', 'id');
foreach ($trees as $id => $name) {
$csv = $this->createCsv($rows->get($id));
$this->tmpFiles[] = $csv;
$toZip[$csv->getPath()] = $this->sanitizeFilename("{$id}-{$name}", 'csv');
}
} elseif ($this->shouldSeparateUsers() && $rows->isNotEmpty()) {
$rows = $rows->groupBy('user_id');
$users = User::whereIn('id', $rows->keys())
->selectRaw("id, concat(firstname, ' ', lastname) as name")
->pluck('name', 'id');
foreach ($users as $id => $name) {
$csv = $this->createCsv($rows->get($id));
$this->tmpFiles[] = $csv;
$toZip[$csv->getPath()] = $this->sanitizeFilename("{$id}-{$name}", 'csv');
}
} else {
$csv = $this->createCsv($rows);
$this->tmpFiles[] = $csv;
$toZip[$csv->getPath()] = $this->sanitizeFilename("{$this->source->id}-{$this->source->name}", 'csv');
}
$this->makeZip($toZip, $path);
}
/**
* Assembles the part of the DB query that is the same for all annotation reports.
*
* @param mixed $columns The columns to select
* @return \Illuminate\Database\Query\Builder
*/
public function initQuery($columns = [])
{
$query = DB::table('video_annotation_labels')
->join('video_annotations', 'video_annotation_labels.annotation_id', '=', 'video_annotations.id')
->join('videos', 'video_annotations.video_id', '=', 'videos.id')
->join('labels', 'video_annotation_labels.label_id', '=', 'labels.id')
->where('videos.volume_id', $this->source->id)
->when($this->isRestrictedToAnnotationSession(), [$this, 'restrictToAnnotationSessionQuery'])
->when($this->isRestrictedToNewestLabel(), function ($query) {
return $this->restrictToNewestLabelQuery($query, 'video_annotation_labels');
})
->when($this->isRestrictedToLabels(), function ($query) {
return $this->restrictToLabelsQuery($query, 'video_annotation_labels');
})
->select($columns);
if ($this->shouldSeparateLabelTrees()) {
$query->addSelect('labels.label_tree_id');
} elseif ($this->shouldSeparateUsers()) {
$query->addSelect('video_annotation_labels.user_id');
}
return $query;
}
/**
* Callback to be used in a `when` query statement that restricts the resulting annotation labels to the annotation session of this report.
*
* @param \Illuminate\Database\Query\Builder $query
* @return \Illuminate\Database\Query\Builder
*/
public function restrictToAnnotationSessionQuery($query)
{
$session = $this->getAnnotationSession();
return $query->where(function ($query) use ($session) {
// take only annotations that belong to the time span...
$query->where('video_annotations.created_at', '>=', $session->starts_at)
->where('video_annotations.created_at', '<', $session->ends_at)
// ...and to the users of the session
->whereIn('video_annotation_labels.user_id', function ($query) use ($session) {
$query->select('user_id')
->from('annotation_session_user')
->where('annotation_session_id', $session->id);
});
});
}
/**
* Assemble a new DB query for the video of this report.
*
* @return \Illuminate\Database\Query\Builder
*/
protected function query()
{
$query = $this->initQuery([
'video_annotation_labels.id as video_annotation_label_id',
'video_annotation_labels.label_id',
'labels.name as label_name',
'users.id as user_id',
'users.firstname',
'users.lastname',
'videos.id as video_id',
'videos.filename as video_filename',
'shapes.id as shape_id',
'shapes.name as shape_name',
'video_annotations.points',
'video_annotations.frames',
'video_annotations.id as annotation_id',
'video_annotation_labels.created_at',
])
->join('shapes', 'video_annotations.shape_id', '=', 'shapes.id')
->leftJoin('users', 'video_annotation_labels.user_id', '=', 'users.id')
->orderBy('video_annotation_labels.id');
return $query;
}
/**
* Create a CSV file for this report.
*
* @param \Illuminate\Support\Collection $rows The rows for the CSV
* @return CsvFile
*/
protected function createCsv($rows)
{
$csv = CsvFile::makeTmp();
// column headers
$csv->put([
'video_annotation_label_id',
'label_id',
'label_name',
'label_hierarchy',
'user_id',
'firstname',
'lastname',
'video_id',
'video_filename',
'shape_id',
'shape_name',
'points',
'frames',
'annotation_id',
'created_at',
]);
foreach ($rows as $row) {
$csv->put([
$row->video_annotation_label_id,
$row->label_id,
$row->label_name,
$this->expandLabelName($row->label_id),
$row->user_id,
$row->firstname,
$row->lastname,
$row->video_id,
$row->video_filename,
$row->shape_id,
$row->shape_name,
$row->points,
$row->frames,
$row->annotation_id,
$row->created_at,
]);
}
$csv->close();
return $csv;
}
}