Merge lp:~makyo/juju-gui/endpoints-once into lp:juju-gui/experimental

Proposed by Madison Scott-Clary
Status: Merged
Merged at revision: 264
Proposed branch: lp:~makyo/juju-gui/endpoints-once
Merge into: lp:juju-gui/experimental
Diff against target: 114 lines (+59/-7)
2 files modified
app/app.js (+29/-5)
test/test_app.js (+30/-2)
To merge this branch: bzr merge lp:~makyo/juju-gui/endpoints-once
Reviewer Review Type Date Requested Status
Juju GUI Hackers Pending
Review via email: mp+137273@code.launchpad.net

Description of the change

Clean up get_endpoints

get_endpoints was being called on every instance of service_add and not being garbage collected. Moved it to on_database_changed and added gc around services being removed.

https://codereview.appspot.com/6858099/

To post a comment you must log in.
Revision history for this message
Madison Scott-Clary (makyo) wrote :

Reviewers: mp+137273_code.launchpad.net,

Message:
Please take a look.

Description:
Clean up get_endpoints

get_endpoints was being called on every instance of service_add and not
being garbage collected. Moved it to on_database_changed and added gc
around services being removed.

https://code.launchpad.net/~makyo/juju-gui/endpoints-once/+merge/137273

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/6858099/

Affected files:
   A [revision details]
   M app/app.js

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: <email address hidden>
+New revision: <email address hidden>

Index: app/app.js
=== modified file 'app/app.js'
--- app/app.js 2012-11-20 16:55:43 +0000
+++ app/app.js 2012-11-30 17:33:27 +0000
@@ -256,8 +256,6 @@
        // TODO - Bound views will automatically update this on individual
models
        this.db.on('update', this.on_database_changed, this);

- this.db.services.on('add', this.updateEndpoints, this);
-
        this.on('navigate', function(e) {
          console.log('app navigate', e);
        });
@@ -310,6 +308,35 @@
       */
      on_database_changed: function(evt) {
        Y.log(evt, 'debug', 'App: Database changed');
+
+ var updateNeeded = false,
+ self = this;
+ if (!Y.Lang.isValue(this.serviceEndpoints)) {
+ this.serviceEndpoints = {};
+ }
+
+ // Compare endpoints map against db to see if it needs to be changed.
+ this.db.services.some(function(service) {
+ if (self.serviceEndpoints[service.get('id')] === undefined) {
+ updateNeeded = true;
+ }
+ return updateNeeded;
+ });
+
+ // If there are new services in the DB, pull an updated endpoints
map.
+ if (updateNeeded) {
+ this.updateEndpoints();
+ } else {
+ // Check to see if any services have been removed (if there are,
and
+ // new ones also, updateEndpoints will replace the whole map, so
only
+ // do this if needed).
+ Y.Object.each(this.serviceEndpoints, function(key, value, obj) {
+ if (self.db.services.getById(key) === null) {
+ delete(self.serviceEndpoints[key]);
+ }
+ });
+ }
+
        // Redispatch to current view to update.
        this.dispatch();
      },
@@ -322,9 +349,6 @@
      updateEndpoints: function(callback) {
        var self = this;

- if (!Y.Lang.isValue(this.serviceEndpoints)) {
- this.serviceEndpoints = {};
- }
        // Defensive code to aid tests. Other systems
        // don't have to mock enough to get_endpoints below.
        if (!this.env.get('connected')) {

Revision history for this message
Kapil Thangavelu (hazmat) wrote :

Missing a unit test.

Else looks good.

Merge with changes.

Thanks

https://codereview.appspot.com/6858099/diff/1/app/app.js
File app/app.js (right):

https://codereview.appspot.com/6858099/diff/1/app/app.js#newcode314
app/app.js:314: if (!Y.Lang.isValue(this.serviceEndpoints)) {
i'd suggest moving this to the initializer and dropping the conditional
instead of lazy value initialization.

https://codereview.appspot.com/6858099/diff/1/app/app.js#newcode323
app/app.js:323: return updateNeeded;
return is extraneous here

https://codereview.appspot.com/6858099/

lp:~makyo/juju-gui/endpoints-once updated
263. By Madison Scott-Clary

Clean up usage of get_endpoints in app so that it's only called when needed.

264. By Madison Scott-Clary

Clean up usage of get_endpoints in app so that it's only called when needed.

Revision history for this message
Madison Scott-Clary (makyo) wrote :
Revision history for this message
Brad Crittenden (bac) wrote :

Looks good but I've suggested a code simplification. No need to
re-review.

https://codereview.appspot.com/6858099/diff/5001/app/app.js
File app/app.js (right):

https://codereview.appspot.com/6858099/diff/5001/app/app.js#newcode325
app/app.js:325:
.some() returns true or false so I think you could just do:

var updateNeeded = this.db.services.some(function(service) {
          return (self.serviceEndpoints[service.get('id')] ===
undefined)});

https://codereview.appspot.com/6858099/

Revision history for this message
Madison Scott-Clary (makyo) wrote :

*** Submitted:

Clean up get_endpoints

get_endpoints was being called on every instance of service_add and not
being garbage collected. Moved it to on_database_changed and added gc
around services being removed.

R=
CC=
https://codereview.appspot.com/6858099

https://codereview.appspot.com/6858099/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'app/app.js'
--- app/app.js 2012-11-20 16:55:43 +0000
+++ app/app.js 2012-12-03 20:28:23 +0000
@@ -256,8 +256,6 @@
256 // TODO - Bound views will automatically update this on individual models256 // TODO - Bound views will automatically update this on individual models
257 this.db.on('update', this.on_database_changed, this);257 this.db.on('update', this.on_database_changed, this);
258258
259 this.db.services.on('add', this.updateEndpoints, this);
260
261 this.on('navigate', function(e) {259 this.on('navigate', function(e) {
262 console.log('app navigate', e);260 console.log('app navigate', e);
263 });261 });
@@ -310,6 +308,35 @@
310 */308 */
311 on_database_changed: function(evt) {309 on_database_changed: function(evt) {
312 Y.log(evt, 'debug', 'App: Database changed');310 Y.log(evt, 'debug', 'App: Database changed');
311
312 var updateNeeded = false,
313 self = this;
314 if (!Y.Lang.isValue(this.serviceEndpoints)) {
315 this.serviceEndpoints = {};
316 }
317
318 // Compare endpoints map against db to see if it needs to be changed.
319 this.db.services.some(function(service) {
320 if (self.serviceEndpoints[service.get('id')] === undefined) {
321 updateNeeded = true;
322 }
323 return updateNeeded;
324 });
325
326 // If there are new services in the DB, pull an updated endpoints map.
327 if (updateNeeded) {
328 this.updateEndpoints();
329 } else {
330 // Check to see if any services have been removed (if there are, and
331 // new ones also, updateEndpoints will replace the whole map, so only
332 // do this if needed).
333 Y.Object.each(this.serviceEndpoints, function(key, value, obj) {
334 if (self.db.services.getById(key) === null) {
335 delete(self.serviceEndpoints[key]);
336 }
337 });
338 }
339
313 // Redispatch to current view to update.340 // Redispatch to current view to update.
314 this.dispatch();341 this.dispatch();
315 },342 },
@@ -322,9 +349,6 @@
322 updateEndpoints: function(callback) {349 updateEndpoints: function(callback) {
323 var self = this;350 var self = this;
324351
325 if (!Y.Lang.isValue(this.serviceEndpoints)) {
326 this.serviceEndpoints = {};
327 }
328 // Defensive code to aid tests. Other systems352 // Defensive code to aid tests. Other systems
329 // don't have to mock enough to get_endpoints below.353 // don't have to mock enough to get_endpoints below.
330 if (!this.env.get('connected')) {354 if (!this.env.get('connected')) {
331355
=== modified file 'test/test_app.js'
--- test/test_app.js 2012-11-27 14:25:32 +0000
+++ test/test_app.js 2012-12-03 20:28:23 +0000
@@ -10,7 +10,7 @@
10 ['relation', 'add',10 ['relation', 'add',
11 {'interface': 'reversenginx', 'scope': 'global',11 {'interface': 'reversenginx', 'scope': 'global',
12 'endpoints': [['wordpress', {'role': 'peer', 'name': 'loadbalancer'}]],12 'endpoints': [['wordpress', {'role': 'peer', 'name': 'loadbalancer'}]],
13 'id': 'relation-0000000000'}],13 'id': 'relation-0000000002'}],
14 ['relation', 'add',14 ['relation', 'add',
15 {'interface': 'mysql',15 {'interface': 'mysql',
16 'scope': 'global', 'endpoints':16 'scope': 'global', 'endpoints':
@@ -186,7 +186,7 @@
186 env: env,186 env: env,
187 charm_store: {} });187 charm_store: {} });
188188
189 app.updateEndpoints = function() {};189 //app.updateEndpoints = function() {};
190 env.get_endpoints = function() {};190 env.get_endpoints = function() {};
191 });191 });
192192
@@ -208,5 +208,33 @@
208 // Tests of the actual load machinery are in the model and env tests, and208 // Tests of the actual load machinery are in the model and env tests, and
209 // so are not repeated here.209 // so are not repeated here.
210 });210 });
211
212 it('must request endpoints only when necessary', function() {
213 var get_endpoints_count = 0,
214 tmp_data = {
215 result: [
216 ['service', 'add', {
217 'charm': 'cs:precise/mysql-6',
218 'id': 'mysql2'
219 }],
220 ['unit', 'add', {
221 'machine': 0,
222 'agent-state': 'started',
223 'public-address': '192.168.122.222',
224 'id': 'mysql2/0'
225 }]
226 ],
227 op: 'delta'
228 };
229 env.get_endpoints = function(services, callback) {
230 get_endpoints_count += 1;
231 };
232 // Inject default data, should only get_endpoints once.
233 injectData(app);
234 get_endpoints_count.should.equal(1);
235 // Additional deltas should only call get_endpoints once.
236 app.db.on_delta({ data: tmp_data });
237 get_endpoints_count.should.equal(2);
238 });
211 });239 });
212});240});

Subscribers

People subscribed via source and target branches