From 31f2f3c9fba12b458e9c7c792d818ab718dc9177 Mon Sep 17 00:00:00 2001
From: Mathis Neumann <mathis@simpletechs.net>
Date: Sat, 9 Jul 2016 18:13:35 +0200
Subject: [PATCH] update the graph whenever the changelog changes

---
 app/controllers/deployments/single.js | 34 +++++++++++++++++++++++++
 app/routes/deployments/single.js      | 36 ++++++++++++++++-----------
 app/templates/deployments/single.hbs  |  2 +-
 3 files changed, 57 insertions(+), 15 deletions(-)
 create mode 100644 app/controllers/deployments/single.js

diff --git a/app/controllers/deployments/single.js b/app/controllers/deployments/single.js
new file mode 100644
index 0000000..cd0b8dc
--- /dev/null
+++ b/app/controllers/deployments/single.js
@@ -0,0 +1,34 @@
+import Ember from 'ember';
+
+const observables = [
+    'nodes',
+    'nodeGroups',
+    'serviceInstances',
+    'communicationInstances'
+];
+
+export default Ember.Controller.extend({
+    graphingService: Ember.inject.service(),
+
+    init() {
+        this.debug('initializing controller');
+    },
+    // gets automatically updated when any of the instances changes (updated from changelog stream)
+    graphModel: Ember.computed(`model.instances.{${observables.join(',')}}.[]`, function() {
+        const systemId = this.get('model.systemId');
+        const instances = this.get('model.instances');
+        /*
+         * Since we use findAll to not load ALL instances but only for a specific system,
+         * Ember would cache any instances by any system in the store,
+         * this might lead to unexpected behavior after navigating to multiple systems/deployments.
+         * Solution: filter out invalid
+         */
+        const filteredInstances = {};
+        Object.keys(instances).map((key) => {
+            filteredInstances[key] = instances[key].filterBy('systemId', systemId);
+        });
+
+        this.debug('creating graph', filteredInstances);
+        return this.get('graphingService').createGraph(filteredInstances); // TODO: update instead of complete recalculation?
+    }),
+});
\ No newline at end of file
diff --git a/app/routes/deployments/single.js b/app/routes/deployments/single.js
index 344ef5b..341da11 100644
--- a/app/routes/deployments/single.js
+++ b/app/routes/deployments/single.js
@@ -2,7 +2,6 @@ import Ember from 'ember';
 
 export default Ember.Route.extend({
   session: Ember.inject.service(), // loads services/session.js
-  graphingService: Ember.inject.service(),
   changelogStream: Ember.inject.service(),
   model(params) {
     const systemId = params.systemId;
@@ -10,22 +9,31 @@ export default Ember.Route.extend({
     const changelogStream = this.get('changelogStream'); // lazy loaded, requires session id
     changelogStream.connect(systemId);
 
-    const graphingService = this.get('graphingService');
-    const createGraph = graphingService.createGraph.bind(graphingService);
+    /*
+     * note that findAll returns an Observable Array which automatically
+     * update whenever new records are pushed into the store.
+     * The controller can observe this.
+     * Also note that since we changed the behavior of findAll() to use the systemId
+     * Ember will probably also update for other systems. These are filtered in the controller
+     */
+    const load = (type) => this.store.findAll(type);
 
-    this.set('loading', true);
     return Ember.RSVP.hash({
-        nodes: this.store.findAll('node'),
-        nodeGroups: this.store.findAll('nodegroup'),
-        services: this.store.findAll('service'),
-        serviceInstances: this.store.findAll('serviceinstance'),
-        communications: this.store.findAll('communication'),
-        communicationInstances: this.store.findAll('communicationinstance')
-    }).then(createGraph).then((graph) => {
-      this.set('loading', false);
-      return graph;
+        nodes: load('node'),
+        nodeGroups: load('nodegroup'),
+        services: load('service'),
+        serviceInstances: load('serviceinstance'),
+        communications: load('communication'),
+        communicationInstances: load('communicationinstance')
+    }).then((models) => {
+        this.debug('loaded models', models);
+        return {
+            systemId: systemId,
+            instances: models
+        };
     });
   },
+
   actions: {
     loadDetails(rawEntity) {
         this.debug('loadDetails action', rawEntity);
@@ -42,7 +50,7 @@ export default Ember.Route.extend({
         });
         this.transitionTo(url);
     },
-    willTransition() {
+    willTransition() { // FIXME: do not disconnect for subpages!
         this.get('changelogStream').disconnect();
     }
   }
diff --git a/app/templates/deployments/single.hbs b/app/templates/deployments/single.hbs
index 46f98c4..369bd73 100644
--- a/app/templates/deployments/single.hbs
+++ b/app/templates/deployments/single.hbs
@@ -1,4 +1,4 @@
-{{#architecture-viewer graph=model}}
+{{#architecture-viewer graph=graphModel}}
     {{!-- show entity details in sidebar if subroute (details) is used --}}
     {{outlet}}
 {{/architecture-viewer}}
\ No newline at end of file
-- 
GitLab