forked from Kitware/CDash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicAnalysisController.php
More file actions
246 lines (213 loc) · 10.2 KB
/
DynamicAnalysisController.php
File metadata and controls
246 lines (213 loc) · 10.2 KB
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
<?php
namespace App\Http\Controllers;
use App\Utils\PageTimer;
use App\Utils\TestingDay;
use CDash\Model\DynamicAnalysis;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
final class DynamicAnalysisController extends AbstractBuildController
{
public function viewDynamicAnalysis(int $buildid): View
{
$this->setBuildById($buildid);
return $this->vue('view-dynamic-analysis', 'Dynamic Analysis', ['buildid' => $this->build->Id]);
}
public function viewDynamicAnalysisFile(int $buildid, int $fileid): View
{
$this->setBuildById($buildid);
$da = \App\Models\DynamicAnalysis::find($fileid);
return $this->vue(
'build-dynamic-analysis-id-page',
'Dynamic Analysis',
[
'build-id' => $buildid,
'dynamic-analysis-id' => $fileid,
'link' => url("queryTests.php?project={$this->project->Name}&date={$this->date}&filtercount=1&showfilters=1&field1=testname&compare1=61&value1={$da?->name}"),
],
);
}
public function apiViewDynamicAnalysis(): JsonResponse
{
$pageTimer = new PageTimer();
if (!isset($_GET['buildid']) || !is_numeric($_GET['buildid'])) {
abort(400, 'Invalid buildid!');
}
$this->setBuildById((int) $_GET['buildid']);
// lookup table to make the reported defect types more understandable.
// feel free to expand as necessary.
$defect_nice_names = [
'FIM' => 'Freeing Invalid Memory',
'IPR' => 'Invalid Pointer Read',
'IPW' => 'Invalid Pointer Write',
];
$date = TestingDay::get($this->project, $this->build->StartTime);
$response = begin_JSON_response();
get_dashboard_JSON($this->project->Name, $date, $response);
$response['title'] = "{$this->project->Name} - Dynamic Analysis";
$menu = [];
if ($this->build->GetParentId() > 0) {
$menu['back'] = 'index.php?project=' . urlencode($this->project->Name) . "&parentid={$this->build->GetParentId()}";
} else {
$menu['back'] = 'index.php?project=' . urlencode($this->project->Name) . "&date=$date";
}
$previousbuildid = self::get_previous_buildid_dynamicanalysis($this->build->ProjectId, $this->build->SiteId, $this->build->Type, $this->build->Name, $this->build->StartTime);
if ($previousbuildid > 0) {
$menu['previous'] = "/builds/$previousbuildid/dynamic_analysis";
} else {
$menu['previous'] = false;
}
$currentbuildid = self::get_last_buildid_dynamicanalysis($this->build->ProjectId, $this->build->SiteId, $this->build->Type, $this->build->Name);
$menu['current'] = "/builds/$currentbuildid/dynamic_analysis";
$nextbuildid = self::get_next_buildid_dynamicanalysis($this->build->ProjectId, $this->build->SiteId, $this->build->Type, $this->build->Name, $this->build->StartTime);
if ($nextbuildid > 0) {
$menu['next'] = "/builds/$nextbuildid/dynamic_analysis";
} else {
$menu['next'] = false;
}
$response['menu'] = $menu;
// Build
$build_response = [];
$build_response['site'] = $this->build->GetSite()->name;
$build_response['buildname'] = $this->build->Name;
$build_response['buildid'] = $this->build->Id;
$build_response['buildtime'] = $this->build->StartTime;
$response['build'] = $build_response;
// Dynamic Analysis
$defect_types = [];
$dynamic_analyses = [];
// Process 50 rows at a time so we don't run out of memory.
DB::table('dynamicanalysis')
->where('buildid', '=', $this->build->Id)
->orderBy('status', 'desc')
->chunk(50, function ($rows) use (&$dynamic_analyses, &$defect_types, $defect_nice_names): void {
foreach ($rows as $DA_row) {
$dynamic_analysis = [];
$dynamic_analysis['status'] = ucfirst($DA_row->status);
$dynamic_analysis['name'] = $DA_row->name;
$dynamic_analysis['id'] = $DA_row->id;
$dynid = $DA_row->id;
$defects_result = DB::select('SELECT * FROM dynamicanalysisdefect WHERE dynamicanalysisid = ?', [$dynid]);
// Initialize defects array as zero for each type.
$num_types = count($defect_types);
if ($num_types > 0) {
// Work around a bug in older versions of PHP where the 2nd argument to
// array_fill must be greater than zero.
$defects = array_fill(0, count($defect_types), 0);
} else {
$defects = [];
}
foreach ($defects_result as $defects_row) {
// Figure out how many defects of each type were found for this test.
$defect_type = $defects_row->type;
if (array_key_exists($defect_type, $defect_nice_names)) {
$defect_type = $defect_nice_names[$defect_type];
}
if (!in_array($defect_type, $defect_types, true)) {
$defect_types[] = $defect_type;
$defects[] = 0;
}
$column = array_search($defect_type, $defect_types, true);
$defects[$column] = $defects_row->value;
}
$dynamic_analysis['defects'] = $defects;
if ($this->project->DisplayLabels) {
get_labels_JSON_from_query_results('
SELECT text
FROM label, label2dynamicanalysis
WHERE
label.id = label2dynamicanalysis.labelid
AND label2dynamicanalysis.dynamicanalysisid = ?
ORDER BY text ASC
', [(int) $dynid], $dynamic_analysis);
if (array_key_exists('labels', $dynamic_analysis)) {
$dynamic_analysis['labels'] = implode(', ', $dynamic_analysis['labels']);
} else {
$dynamic_analysis['labels'] = '';
}
}
$dynamic_analyses[] = $dynamic_analysis;
}
});
// Insert zero entries for types of defects that were not detected by a given test.
$num_defect_types = count($defect_types);
foreach ($dynamic_analyses as &$dynamic_analysis) {
for ($i = 0; $i < $num_defect_types; $i++) {
if (!array_key_exists($i, $dynamic_analysis['defects'])) {
$dynamic_analysis['defects'][$i] = 0;
}
}
}
$response['dynamicanalyses'] = $dynamic_analyses;
// explicitly list the defect types encountered here
// so we can dynamically generate the header row
$types_response = [];
foreach ($defect_types as $defect_type) {
$type_response = [];
$type_response['type'] = $defect_type;
$types_response[] = $type_response;
}
$response['defecttypes'] = $types_response;
$pageTimer->end($response);
return response()->json(cast_data_for_JSON($response));
}
/**
* Get the previous build id dynamicanalysis
*/
private static function get_previous_buildid_dynamicanalysis(int $projectid, int $siteid, string $buildtype, string $buildname, string $starttime): int
{
$previousbuild = DB::select('
SELECT build.id
FROM build, dynamicanalysis
WHERE
build.siteid=?
AND build.type=?
AND build.name=?
AND build.projectid=?
AND build.starttime<?
AND dynamicanalysis.buildid=build.id
ORDER BY build.starttime DESC
LIMIT 1
', [$siteid, $buildtype, $buildname, $projectid, $starttime])[0] ?? [];
return $previousbuild->id ?? 0;
}
/**
* Get the next build id dynamicanalysis
*/
private static function get_next_buildid_dynamicanalysis(int $projectid, int $siteid, string $buildtype, string $buildname, string $starttime): int
{
$nextbuild = DB::select('
SELECT build.id
FROM build, dynamicanalysis
WHERE
build.siteid=?
AND build.type=?
AND build.name=?
AND build.projectid=?
AND build.starttime>?
AND dynamicanalysis.buildid=build.id
ORDER BY build.starttime ASC
LIMIT 1
', [$siteid, $buildtype, $buildname, $projectid, $starttime])[0] ?? [];
return $nextbuild->id ?? 0;
}
/**
* Get the last build id dynamicanalysis
*/
private static function get_last_buildid_dynamicanalysis(int $projectid, int $siteid, string $buildtype, string $buildname): int
{
$nextbuild = DB::select('
SELECT build.id
FROM build, dynamicanalysis
WHERE
build.siteid=?
AND build.type=?
AND build.name=?
AND build.projectid=?
AND dynamicanalysis.buildid=build.id
ORDER BY build.starttime DESC
LIMIT 1
', [$siteid, $buildtype, $buildname, $projectid])[0] ?? [];
return $nextbuild->id ?? 0;
}
}