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
1=== modified file 'app/app.js'
2--- app/app.js 2012-11-20 16:55:43 +0000
3+++ app/app.js 2012-12-03 20:28:23 +0000
4@@ -256,8 +256,6 @@
5 // TODO - Bound views will automatically update this on individual models
6 this.db.on('update', this.on_database_changed, this);
7
8- this.db.services.on('add', this.updateEndpoints, this);
9-
10 this.on('navigate', function(e) {
11 console.log('app navigate', e);
12 });
13@@ -310,6 +308,35 @@
14 */
15 on_database_changed: function(evt) {
16 Y.log(evt, 'debug', 'App: Database changed');
17+
18+ var updateNeeded = false,
19+ self = this;
20+ if (!Y.Lang.isValue(this.serviceEndpoints)) {
21+ this.serviceEndpoints = {};
22+ }
23+
24+ // Compare endpoints map against db to see if it needs to be changed.
25+ this.db.services.some(function(service) {
26+ if (self.serviceEndpoints[service.get('id')] === undefined) {
27+ updateNeeded = true;
28+ }
29+ return updateNeeded;
30+ });
31+
32+ // If there are new services in the DB, pull an updated endpoints map.
33+ if (updateNeeded) {
34+ this.updateEndpoints();
35+ } else {
36+ // Check to see if any services have been removed (if there are, and
37+ // new ones also, updateEndpoints will replace the whole map, so only
38+ // do this if needed).
39+ Y.Object.each(this.serviceEndpoints, function(key, value, obj) {
40+ if (self.db.services.getById(key) === null) {
41+ delete(self.serviceEndpoints[key]);
42+ }
43+ });
44+ }
45+
46 // Redispatch to current view to update.
47 this.dispatch();
48 },
49@@ -322,9 +349,6 @@
50 updateEndpoints: function(callback) {
51 var self = this;
52
53- if (!Y.Lang.isValue(this.serviceEndpoints)) {
54- this.serviceEndpoints = {};
55- }
56 // Defensive code to aid tests. Other systems
57 // don't have to mock enough to get_endpoints below.
58 if (!this.env.get('connected')) {
59
60=== modified file 'test/test_app.js'
61--- test/test_app.js 2012-11-27 14:25:32 +0000
62+++ test/test_app.js 2012-12-03 20:28:23 +0000
63@@ -10,7 +10,7 @@
64 ['relation', 'add',
65 {'interface': 'reversenginx', 'scope': 'global',
66 'endpoints': [['wordpress', {'role': 'peer', 'name': 'loadbalancer'}]],
67- 'id': 'relation-0000000000'}],
68+ 'id': 'relation-0000000002'}],
69 ['relation', 'add',
70 {'interface': 'mysql',
71 'scope': 'global', 'endpoints':
72@@ -186,7 +186,7 @@
73 env: env,
74 charm_store: {} });
75
76- app.updateEndpoints = function() {};
77+ //app.updateEndpoints = function() {};
78 env.get_endpoints = function() {};
79 });
80
81@@ -208,5 +208,33 @@
82 // Tests of the actual load machinery are in the model and env tests, and
83 // so are not repeated here.
84 });
85+
86+ it('must request endpoints only when necessary', function() {
87+ var get_endpoints_count = 0,
88+ tmp_data = {
89+ result: [
90+ ['service', 'add', {
91+ 'charm': 'cs:precise/mysql-6',
92+ 'id': 'mysql2'
93+ }],
94+ ['unit', 'add', {
95+ 'machine': 0,
96+ 'agent-state': 'started',
97+ 'public-address': '192.168.122.222',
98+ 'id': 'mysql2/0'
99+ }]
100+ ],
101+ op: 'delta'
102+ };
103+ env.get_endpoints = function(services, callback) {
104+ get_endpoints_count += 1;
105+ };
106+ // Inject default data, should only get_endpoints once.
107+ injectData(app);
108+ get_endpoints_count.should.equal(1);
109+ // Additional deltas should only call get_endpoints once.
110+ app.db.on_delta({ data: tmp_data });
111+ get_endpoints_count.should.equal(2);
112+ });
113 });
114 });

Subscribers

People subscribed via source and target branches