Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/conduit/query/PhabricatorConduitLogSearchEngine.php
12256 views
1
<?php
2
3
final class PhabricatorConduitLogSearchEngine
4
extends PhabricatorApplicationSearchEngine {
5
6
public function getResultTypeDescription() {
7
return pht('Conduit Logs');
8
}
9
10
public function getApplicationClassName() {
11
return 'PhabricatorConduitApplication';
12
}
13
14
public function canUseInPanelContext() {
15
return false;
16
}
17
18
public function newQuery() {
19
return new PhabricatorConduitLogQuery();
20
}
21
22
protected function buildQueryFromParameters(array $map) {
23
$query = $this->newQuery();
24
25
if ($map['callerPHIDs']) {
26
$query->withCallerPHIDs($map['callerPHIDs']);
27
}
28
29
if ($map['methods']) {
30
$query->withMethods($map['methods']);
31
}
32
33
if ($map['statuses']) {
34
$query->withMethodStatuses($map['statuses']);
35
}
36
37
if ($map['epochMin'] || $map['epochMax']) {
38
$query->withEpochBetween(
39
$map['epochMin'],
40
$map['epochMax']);
41
}
42
43
return $query;
44
}
45
46
protected function buildCustomSearchFields() {
47
return array(
48
id(new PhabricatorUsersSearchField())
49
->setKey('callerPHIDs')
50
->setLabel(pht('Callers'))
51
->setAliases(array('caller', 'callers'))
52
->setDescription(pht('Find calls by specific users.')),
53
id(new PhabricatorSearchStringListField())
54
->setKey('methods')
55
->setLabel(pht('Methods'))
56
->setDescription(pht('Find calls to specific methods.')),
57
id(new PhabricatorSearchCheckboxesField())
58
->setKey('statuses')
59
->setLabel(pht('Method Status'))
60
->setAliases(array('status'))
61
->setDescription(
62
pht('Find calls to stable, unstable, or deprecated methods.'))
63
->setOptions(ConduitAPIMethod::getMethodStatusMap()),
64
id(new PhabricatorSearchDateField())
65
->setLabel(pht('Called After'))
66
->setKey('epochMin'),
67
id(new PhabricatorSearchDateField())
68
->setLabel(pht('Called Before'))
69
->setKey('epochMax'),
70
);
71
}
72
73
protected function getURI($path) {
74
return '/conduit/log/'.$path;
75
}
76
77
protected function getBuiltinQueryNames() {
78
$names = array();
79
80
$viewer = $this->requireViewer();
81
if ($viewer->isLoggedIn()) {
82
$names['viewer'] = pht('My Calls');
83
$names['viewerdeprecated'] = pht('My Deprecated Calls');
84
}
85
86
$names['all'] = pht('All Call Logs');
87
$names['deprecated'] = pht('Deprecated Call Logs');
88
89
return $names;
90
}
91
92
public function buildSavedQueryFromBuiltin($query_key) {
93
$query = $this->newSavedQuery();
94
$query->setQueryKey($query_key);
95
96
$viewer = $this->requireViewer();
97
$viewer_phid = $viewer->getPHID();
98
99
$deprecated = array(
100
ConduitAPIMethod::METHOD_STATUS_DEPRECATED,
101
);
102
103
switch ($query_key) {
104
case 'viewer':
105
return $query
106
->setParameter('callerPHIDs', array($viewer_phid));
107
case 'viewerdeprecated':
108
return $query
109
->setParameter('callerPHIDs', array($viewer_phid))
110
->setParameter('statuses', $deprecated);
111
case 'deprecated':
112
return $query
113
->setParameter('statuses', $deprecated);
114
case 'all':
115
return $query;
116
}
117
118
return parent::buildSavedQueryFromBuiltin($query_key);
119
}
120
121
protected function newExportFields() {
122
$viewer = $this->requireViewer();
123
124
return array(
125
id(new PhabricatorPHIDExportField())
126
->setKey('callerPHID')
127
->setLabel(pht('Caller PHID')),
128
id(new PhabricatorStringExportField())
129
->setKey('caller')
130
->setLabel(pht('Caller')),
131
id(new PhabricatorStringExportField())
132
->setKey('method')
133
->setLabel(pht('Method')),
134
id(new PhabricatorIntExportField())
135
->setKey('duration')
136
->setLabel(pht('Call Duration (us)')),
137
id(new PhabricatorStringExportField())
138
->setKey('error')
139
->setLabel(pht('Error')),
140
);
141
}
142
143
protected function newExportData(array $logs) {
144
$viewer = $this->requireViewer();
145
146
$phids = array();
147
foreach ($logs as $log) {
148
if ($log->getCallerPHID()) {
149
$phids[] = $log->getCallerPHID();
150
}
151
}
152
$handles = $viewer->loadHandles($phids);
153
154
$export = array();
155
foreach ($logs as $log) {
156
$caller_phid = $log->getCallerPHID();
157
if ($caller_phid) {
158
$caller_name = $handles[$caller_phid]->getName();
159
} else {
160
$caller_name = null;
161
}
162
163
$map = array(
164
'callerPHID' => $caller_phid,
165
'caller' => $caller_name,
166
'method' => $log->getMethod(),
167
'duration' => (int)$log->getDuration(),
168
'error' => $log->getError(),
169
);
170
171
$export[] = $map;
172
}
173
174
return $export;
175
}
176
177
protected function renderResultList(
178
array $logs,
179
PhabricatorSavedQuery $query,
180
array $handles) {
181
assert_instances_of($logs, 'PhabricatorConduitMethodCallLog');
182
$viewer = $this->requireViewer();
183
184
$methods = id(new PhabricatorConduitMethodQuery())
185
->setViewer($viewer)
186
->execute();
187
$methods = mpull($methods, null, 'getAPIMethodName');
188
189
Javelin::initBehavior('phabricator-tooltips');
190
191
$viewer = $this->requireViewer();
192
$rows = array();
193
foreach ($logs as $log) {
194
$caller_phid = $log->getCallerPHID();
195
196
if ($caller_phid) {
197
$caller = $viewer->renderHandle($caller_phid);
198
} else {
199
$caller = null;
200
}
201
202
$method = idx($methods, $log->getMethod());
203
if ($method) {
204
$method_status = $method->getMethodStatus();
205
} else {
206
$method_status = null;
207
}
208
209
switch ($method_status) {
210
case ConduitAPIMethod::METHOD_STATUS_STABLE:
211
$status = null;
212
break;
213
case ConduitAPIMethod::METHOD_STATUS_UNSTABLE:
214
$status = id(new PHUIIconView())
215
->setIcon('fa-exclamation-triangle yellow')
216
->addSigil('has-tooltip')
217
->setMetadata(
218
array(
219
'tip' => pht('Unstable'),
220
));
221
break;
222
case ConduitAPIMethod::METHOD_STATUS_DEPRECATED:
223
$status = id(new PHUIIconView())
224
->setIcon('fa-exclamation-triangle red')
225
->addSigil('has-tooltip')
226
->setMetadata(
227
array(
228
'tip' => pht('Deprecated'),
229
));
230
break;
231
default:
232
$status = id(new PHUIIconView())
233
->setIcon('fa-question-circle')
234
->addSigil('has-tooltip')
235
->setMetadata(
236
array(
237
'tip' => pht('Unknown ("%s")', $method_status),
238
));
239
break;
240
}
241
242
$rows[] = array(
243
$status,
244
$log->getMethod(),
245
$caller,
246
$log->getError(),
247
pht('%s us', new PhutilNumber($log->getDuration())),
248
phabricator_datetime($log->getDateCreated(), $viewer),
249
);
250
}
251
252
$table = id(new AphrontTableView($rows))
253
->setHeaders(
254
array(
255
null,
256
pht('Method'),
257
pht('Caller'),
258
pht('Error'),
259
pht('Duration'),
260
pht('Date'),
261
))
262
->setColumnClasses(
263
array(
264
null,
265
'pri',
266
null,
267
'wide right',
268
null,
269
null,
270
));
271
272
return id(new PhabricatorApplicationSearchResultView())
273
->setTable($table)
274
->setNoDataString(pht('No matching calls in log.'));
275
}
276
}
277
278