From 66024bac58dfc9af8ce88e64e8027d37ee440343 Mon Sep 17 00:00:00 2001
From: Jaden Diefenbaugh <blakcap@users.noreply.github.com>
Date: Thu, 2 Mar 2017 11:27:53 +0100
Subject: [PATCH] add aliases to groups, save report item objs instead of just
 ids

---
 .../reports/app/services/groupsService.js     | 112 ++++++++++++------
 1 file changed, 79 insertions(+), 33 deletions(-)

diff --git a/beat/web/reports/static/reports/app/services/groupsService.js b/beat/web/reports/static/reports/app/services/groupsService.js
index c10caa276..7e14e6852 100644
--- a/beat/web/reports/static/reports/app/services/groupsService.js
+++ b/beat/web/reports/static/reports/app/services/groupsService.js
@@ -39,7 +39,8 @@ angular.module('reportApp').factory('GroupsService', ['$http', function($http){
 			this._name = name;
 			this._analyzer = '';
 			this._experimentNames = new Set();
-			this._reportItemIds = new Set();
+			this._reportItems = [];
+			this._aliases = {};
 		}
 
 		// get the experiment names in this group
@@ -47,9 +48,9 @@ angular.module('reportApp').factory('GroupsService', ['$http', function($http){
 			return Array.from(this._experimentNames);
 		}
 
-		// get the ids of report items that use this group
-		get reportItemIds () {
-			return Array.from(this._reportItemIds);
+		// get the report items in this group
+		get reportItems () {
+			return Array.from(this._reportItems);
 		}
 
 		// get the group name
@@ -62,40 +63,87 @@ angular.module('reportApp').factory('GroupsService', ['$http', function($http){
 			return this._analyzer;
 		}
 
+		// gets the aliases for the experiments in the group
+		get aliases () {
+			return this._aliases;
+		}
+
 		set analyzer (analyzer) {
-			console.log(`setting anayzler = ${analyzer}`);
 			this._analyzer = analyzer;
 		}
 
 		// add an exp to this group
-		addExperiment (expName) {
-			return this._experimentNames.add(expName);
+		addExperiment (expName, analyzer) {
+			let res = this._experimentNames.add(expName);
+			if(this._experimentNames.size === 0 && analyzer){
+				this.analyzer = analyzer;
+			}
+			if(!this.aliases[expName]){
+				let autoAlias = expName.split('/').pop();
+				this.setAliasToExperiment(autoAlias, expName);
+			}
+
+			return res;
 		}
 
 		// rm an exp from this group
 		removeExperiment (expName) {
-			return this._experimentNames.delete(expName);
+			let res = this._experimentNames.delete(expName);
+			if(this._experimentNames.size === 0){
+				this.analyzer = '';
+			}
+			this.unsetExperimentAlias(expName);
+
+			return res;
 		}
 
-		// add an exp to this group
-		addReportItem (id) {
-			return this._reportItemIds.add(id);
+		// add an item (table or plot) to this group
+		// if the id already exists, it just replaces the old
+		// element with the new one
+		addReportItem (id, content) {
+			let newEl = {
+				id,
+				content
+			};
+
+			let alreadyAddedEl = this._reportItems.find(i => i.id === id);
+			if(alreadyAddedEl){
+				let idx = this._reportItems.indexOf(alreadyAddedEl);
+				return this._reportItems.splice(idx, 1, newEl);
+			} else {
+				return this._reportItems.push(newEl);
+			}
 		}
 
 		// rm an exp from this group
 		removeReportItem (id) {
-			return this._reportItemIds.delete(id);
+			let idx = this._reportItems
+			.indexOf(this._reportItems.find(o => o.id === id));
+
+			return this._reportItems.splice(idx, 1);
+		}
+
+		// (re)sets an alias to an experiment in the group
+		setAliasToExperiment (alias, expName) {
+			if(!this.experiments.includes(expName)){
+				return false;
+			}
+
+			this._aliases[expName] = alias;
+		}
+
+		// unsets an alias for an experiment
+		unsetExperimentAlias (expName) {
+			return delete this._aliases[expName];
 		}
 	};
 
 	// gets groups
 	groupsServiceInstance.groups = groupData;
-	// TODO: move this hacky state to the addReportItem miniwindows
-	groupsServiceInstance.activeGroup = '';
 
 	// serializes groups as an object with form:
 	// {
-	// 	<group name 1>: { experiments: [], reportItemIds: [], analyzer: '' },
+	// 	<group name 1>: { experiments: [], reportItems: [], analyzer: '' },
 	// 	...
 	// }
 	groupsServiceInstance.serializeGroups = () => {
@@ -103,8 +151,9 @@ angular.module('reportApp').factory('GroupsService', ['$http', function($http){
 		.map(g => { return {
 			[g.name]: {
 				experiments: g.experiments,
-				reportItemIds: g.reportItemIds,
-				analyzer: g.analyzer
+				reportItems: g.reportItems,
+				analyzer: g.analyzer,
+				aliases: g.aliases
 			}
 		};
 		})
@@ -126,8 +175,6 @@ angular.module('reportApp').factory('GroupsService', ['$http', function($http){
 		let g = new Group(name);
 
 		groupData.push(g);
-		// change active group to newest group by default
-		groupsServiceInstance.activeGroup = g.name;
 		return g;
 	};
 
@@ -141,27 +188,26 @@ angular.module('reportApp').factory('GroupsService', ['$http', function($http){
 
 	// load group info from the serialized format:
 	// {
-	// 	<group name 1>: { experiments: [], reportItemIds: [], analyzer: '' },
+	// 	<group name 1>: { experiments: [], reportItems: [], analyzer: '' },
 	// 	...
 	// }
 	groupsServiceInstance.loadGroups = (data) => {
 		// wipe data and load groups
 		groupData.splice(0, groupData.length);
 
-		// if there's no group data, create a default one
-		if(!data || Object.keys(data).length === 0){
-			groupsServiceInstance.createGroup('default');
-			return;
-		}
-
-		Object.entries(data)
+		Object.entries(data || {})
 		// sometimes we get an empty string for name for some reason
 		.filter(([groupName, gData]) => groupName.length > 0)
 		.forEach(([groupName, gData]) => {
 			let g = groupsServiceInstance.createGroup(groupName);
-			g.analyzer = gData.analyzer;
-			gData.experiments.forEach(n => g.addExperiment(n));
-			gData.reportItemIds.forEach(id => g.addReportItem(id));
+			let analyzer = gData.analyzer || '';
+			let experiments = gData.experiments || [];
+			let reportItems = gData.reportItems || [];
+			let aliases = gData.aliases || {};
+			g.analyzer = analyzer;
+			experiments.forEach(n => g.addExperiment(n));
+			reportItems.forEach(i => g.addReportItem(i.id, i.content));
+			Object.entries(aliases).forEach(([e, a]) => g.setAliasToExperiment(a, e));
 		});
 	};
 
@@ -198,11 +244,11 @@ angular.module('reportApp').factory('GroupsService', ['$http', function($http){
 	};
 
 	// add report item to group
-	groupsServiceInstance.addReportItemToGroup = (id, groupName) => {
+	groupsServiceInstance.addReportItemToGroup = (id, content, groupName) => {
 		checkForGroup(groupName);
 
 		let group = groupData.find(g => g.name === groupName);
-		group.addReportItem(id);
+		group.addReportItem(id, content);
 	};
 
 	// rm report item id from group
@@ -214,7 +260,7 @@ angular.module('reportApp').factory('GroupsService', ['$http', function($http){
 
 	// gets group for a report item's id
 	groupsServiceInstance.getReportItemGroup = (id) => {
-		return groupData.find(g => g.reportItemIds.includes(id));
+		return groupData.find(g => g.reportItems.find(i => i.id === id));
 	};
 
 	// helper to assert that a group exists
-- 
GitLab