Skip to content

Commit 15c0c89

Browse files
author
epriestley
committedMar 16, 2021
Make upstream callers respect "active bindings" when querying Almanac
Summary: Ref T13641. Make "active bindings" a real query and make callers that only care about active bindings only query for active bindings. Test Plan: - Queried for "bindings" and "activeBindings" via Conduit. - Disabled/enabled devices, saw binding status update in UI. - Loaded Diffusion cluster layout. - Grepped for `needBindings()`, `getActiveBindings()`, etc. Subscribers: yelirekim, PHID-OPKG-gm6ozazyms6q6i22gyam Maniphest Tasks: T13641 Differential Revision: https://secure.phabricator.com/D21628
1 parent 5d64fb1 commit 15c0c89

12 files changed

+188
-59
lines changed
 

‎src/applications/almanac/constants/AlmanacDeviceStatus.php

+21
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ public static function getStatusMap() {
6262
return $result;
6363
}
6464

65+
public static function getActiveStatusList() {
66+
$results = array();
67+
foreach (self::newDeviceStatusMap() as $status_value => $status) {
68+
if (empty($status['disabled'])) {
69+
$results[] = $status_value;
70+
}
71+
}
72+
return $results;
73+
}
74+
75+
public static function getDisabledStatusList() {
76+
$results = array();
77+
foreach (self::newDeviceStatusMap() as $status_value => $status) {
78+
if (!empty($status['disabled'])) {
79+
$results[] = $status_value;
80+
}
81+
}
82+
return $results;
83+
}
84+
6585
private function getDeviceStatusProperty($key, $default = null) {
6686
$map = self::newDeviceStatusMap();
6787
$properties = idx($map, $this->getValue(), array());
@@ -81,6 +101,7 @@ private static function newDeviceStatusMap() {
81101
'icon.color' => 'grey',
82102
'status-tag.icon' => 'fa-times',
83103
'status-tag.color' => 'indigo',
104+
'disabled' => true,
84105
),
85106
);
86107
}

‎src/applications/almanac/engineextension/AlmanacBindingsSearchEngineAttachment.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
final class AlmanacBindingsSearchEngineAttachment
44
extends AlmanacSearchEngineAttachment {
55

6+
private $isActive;
7+
8+
public function setIsActive($is_active) {
9+
$this->isActive = $is_active;
10+
return $this;
11+
}
12+
13+
public function getIsActive() {
14+
return $this->isActive;
15+
}
16+
617
public function getAttachmentName() {
718
return pht('Almanac Bindings');
819
}
@@ -13,12 +24,24 @@ public function getAttachmentDescription() {
1324

1425
public function willLoadAttachmentData($query, $spec) {
1526
$query->needProperties(true);
16-
$query->needBindings(true);
27+
28+
if ($this->getIsActive()) {
29+
$query->needBindings(true);
30+
} else {
31+
$query->needActiveBindings(true);
32+
}
1733
}
1834

1935
public function getAttachmentForObject($object, $data, $spec) {
2036
$bindings = array();
21-
foreach ($object->getBindings() as $binding) {
37+
38+
if ($this->getIsActive()) {
39+
$service_bindings = $object->getActiveBindings();
40+
} else {
41+
$service_bindings = $object->getBindings();
42+
}
43+
44+
foreach ($service_bindings as $binding) {
2245
$bindings[] = $this->getAlmanacBindingDictionary($binding);
2346
}
2447

‎src/applications/almanac/engineextension/AlmanacSearchEngineAttachment.php

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ protected function getAlmanacDeviceDictionary(AlmanacDevice $device) {
5151
'phid' => $device->getPHID(),
5252
'name' => $device->getName(),
5353
'properties' => $this->getAlmanacPropertyList($device),
54+
'status' => $device->getStatus(),
55+
'disabled' => $device->isDisabled(),
5456
);
5557
}
5658

‎src/applications/almanac/query/AlmanacBindingQuery.php

+51-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ final class AlmanacBindingQuery
88
private $servicePHIDs;
99
private $devicePHIDs;
1010
private $interfacePHIDs;
11+
private $isActive;
1112

1213
public function withIDs(array $ids) {
1314
$this->ids = $ids;
@@ -34,6 +35,11 @@ public function withInterfacePHIDs(array $phids) {
3435
return $this;
3536
}
3637

38+
public function withIsActive($active) {
39+
$this->isActive = $active;
40+
return $this;
41+
}
42+
3743
public function newResultObject() {
3844
return new AlmanacBinding();
3945
}
@@ -95,39 +101,79 @@ protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
95101
if ($this->ids !== null) {
96102
$where[] = qsprintf(
97103
$conn,
98-
'id IN (%Ld)',
104+
'binding.id IN (%Ld)',
99105
$this->ids);
100106
}
101107

102108
if ($this->phids !== null) {
103109
$where[] = qsprintf(
104110
$conn,
105-
'phid IN (%Ls)',
111+
'binding.phid IN (%Ls)',
106112
$this->phids);
107113
}
108114

109115
if ($this->servicePHIDs !== null) {
110116
$where[] = qsprintf(
111117
$conn,
112-
'servicePHID IN (%Ls)',
118+
'binding.servicePHID IN (%Ls)',
113119
$this->servicePHIDs);
114120
}
115121

116122
if ($this->devicePHIDs !== null) {
117123
$where[] = qsprintf(
118124
$conn,
119-
'devicePHID IN (%Ls)',
125+
'binding.devicePHID IN (%Ls)',
120126
$this->devicePHIDs);
121127
}
122128

123129
if ($this->interfacePHIDs !== null) {
124130
$where[] = qsprintf(
125131
$conn,
126-
'interfacePHID IN (%Ls)',
132+
'binding.interfacePHID IN (%Ls)',
127133
$this->interfacePHIDs);
128134
}
129135

136+
if ($this->isActive !== null) {
137+
if ($this->isActive) {
138+
$where[] = qsprintf(
139+
$conn,
140+
'(binding.isDisabled = 0) AND (device.status IN (%Ls))',
141+
AlmanacDeviceStatus::getActiveStatusList());
142+
} else {
143+
$where[] = qsprintf(
144+
$conn,
145+
'(binding.isDisabled = 1) OR (device.status IN (%Ls))',
146+
AlmanacDeviceStatus::getDisabledStatusList());
147+
}
148+
}
149+
130150
return $where;
131151
}
132152

153+
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
154+
$joins = parent::buildJoinClauseParts($conn);
155+
156+
if ($this->shouldJoinDeviceTable()) {
157+
$device_table = new AlmanacDevice();
158+
$joins[] = qsprintf(
159+
$conn,
160+
'JOIN %R device ON binding.devicePHID = device.phid',
161+
$device_table);
162+
}
163+
164+
return $joins;
165+
}
166+
167+
private function shouldJoinDeviceTable() {
168+
if ($this->isActive !== null) {
169+
return true;
170+
}
171+
172+
return false;
173+
}
174+
175+
protected function getPrimaryTableAlias() {
176+
return 'binding';
177+
}
178+
133179
}

‎src/applications/almanac/query/AlmanacServiceQuery.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class AlmanacServiceQuery
1212
private $nameSuffix;
1313

1414
private $needBindings;
15+
private $needActiveBindings;
1516

1617
public function withIDs(array $ids) {
1718
$this->ids = $ids;
@@ -59,6 +60,11 @@ public function needBindings($need_bindings) {
5960
return $this;
6061
}
6162

63+
public function needActiveBindings($need_active) {
64+
$this->needActiveBindings = $need_active;
65+
return $this;
66+
}
67+
6268
public function newResultObject() {
6369
return new AlmanacService();
6470
}
@@ -160,18 +166,35 @@ protected function willFilterPage(array $services) {
160166
}
161167

162168
protected function didFilterPage(array $services) {
163-
if ($this->needBindings) {
169+
$need_all = $this->needBindings;
170+
$need_active = $this->needActiveBindings;
171+
172+
$need_any = ($need_all || $need_active);
173+
$only_active = ($need_active && !$need_all);
174+
175+
if ($need_any) {
164176
$service_phids = mpull($services, 'getPHID');
165-
$bindings = id(new AlmanacBindingQuery())
177+
178+
$bindings_query = id(new AlmanacBindingQuery())
166179
->setViewer($this->getViewer())
167180
->withServicePHIDs($service_phids)
168-
->needProperties($this->getNeedProperties())
169-
->execute();
181+
->needProperties($this->getNeedProperties());
182+
183+
if ($only_active) {
184+
$bindings_query->withIsActive(true);
185+
}
186+
187+
$bindings = $bindings_query->execute();
170188
$bindings = mgroup($bindings, 'getServicePHID');
171189

172190
foreach ($services as $service) {
173191
$service_bindings = idx($bindings, $service->getPHID(), array());
174-
$service->attachBindings($service_bindings);
192+
193+
if ($only_active) {
194+
$service->attachActiveBindings($service_bindings);
195+
} else {
196+
$service->attachBindings($service_bindings);
197+
}
175198
}
176199
}
177200

‎src/applications/almanac/storage/AlmanacDevice.php

+5
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ public function getFieldSpecificationsForConduit() {
282282
->setKey('status')
283283
->setType('map<string, wild>')
284284
->setDescription(pht('Device status information.')),
285+
id(new PhabricatorConduitSearchFieldSpecification())
286+
->setKey('disabled')
287+
->setType('bool')
288+
->setDescription(pht('True if device is disabled.')),
285289
);
286290
}
287291

@@ -294,6 +298,7 @@ public function getFieldValuesForConduit() {
294298
'value' => $status->getValue(),
295299
'name' => $status->getName(),
296300
),
301+
'disabled' => $this->isDisabled(),
297302
);
298303
}
299304

‎src/applications/almanac/storage/AlmanacService.php

+23-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ final class AlmanacService
2121

2222
private $almanacProperties = self::ATTACHABLE;
2323
private $bindings = self::ATTACHABLE;
24+
private $activeBindings = self::ATTACHABLE;
2425
private $serviceImplementation = self::ATTACHABLE;
2526

2627
public static function initializeNewService($type) {
@@ -91,23 +92,36 @@ public function getBindings() {
9192
}
9293

9394
public function getActiveBindings() {
94-
$bindings = $this->getBindings();
95+
return $this->assertAttached($this->activeBindings);
96+
}
9597

96-
// Filter out disabled bindings.
98+
public function attachBindings(array $bindings) {
99+
$active_bindings = array();
97100
foreach ($bindings as $key => $binding) {
101+
// Filter out disabled bindings.
98102
if ($binding->getIsDisabled()) {
99-
unset($bindings[$key]);
103+
continue;
104+
}
105+
106+
// Filter out bindings to disabled devices.
107+
if ($binding->getDevice()->isDisabled()) {
108+
continue;
100109
}
110+
111+
$active_bindings[$key] = $binding;
101112
}
102113

103-
return $bindings;
104-
}
114+
$this->attachActiveBindings($active_bindings);
105115

106-
public function attachBindings(array $bindings) {
107116
$this->bindings = $bindings;
108117
return $this;
109118
}
110119

120+
public function attachActiveBindings(array $bindings) {
121+
$this->activeBindings = $bindings;
122+
return $this;
123+
}
124+
111125
public function getServiceImplementation() {
112126
return $this->assertAttached($this->serviceImplementation);
113127
}
@@ -289,6 +303,9 @@ public function getConduitSearchAttachments() {
289303
->setAttachmentKey('properties'),
290304
id(new AlmanacBindingsSearchEngineAttachment())
291305
->setAttachmentKey('bindings'),
306+
id(new AlmanacBindingsSearchEngineAttachment())
307+
->setIsActive(true)
308+
->setAttachmentKey('activeBindings'),
292309
);
293310
}
294311

‎src/applications/almanac/view/AlmanacBindingTableView.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,41 @@ public function render() {
5656

5757
$icon_active = id(new PHUIIconView())
5858
->setIcon('fa-check')
59+
->setColor('green')
5960
->addSigil('has-tooltip')
6061
->setMetadata(
6162
array(
6263
'tip' => pht('Active'),
6364
));
6465

66+
$icon_device_disabled = id(new PHUIIconView())
67+
->setIcon('fa-times')
68+
->setColor('grey')
69+
->addSigil('has-tooltip')
70+
->setMetadata(
71+
array(
72+
'tip' => pht('Device Disabled'),
73+
));
74+
6575
$rows = array();
6676
foreach ($bindings as $binding) {
6777
$addr = $binding->getInterface()->getAddress();
6878
$port = $binding->getInterface()->getPort();
6979

80+
$device = $binding->getDevice();
81+
if ($device->isDisabled()) {
82+
$binding_icon = $icon_device_disabled;
83+
} else if ($binding->getIsDisabled()) {
84+
$binding_icon = $icon_disabled;
85+
} else {
86+
$binding_icon = $icon_active;
87+
}
88+
7089
$rows[] = array(
7190
$binding->getID(),
72-
($binding->getIsDisabled() ? $icon_disabled : $icon_active),
91+
$binding_icon,
7392
$handles->renderHandle($binding->getServicePHID()),
93+
7494
$handles->renderHandle($binding->getDevicePHID()),
7595
$handles->renderHandle($binding->getInterface()->getNetworkPHID()),
7696
$binding->getInterface()->renderDisplayAddress(),

0 commit comments

Comments
 (0)
Failed to load comments.