diff --git a/beat/web/reports/static/reports/app/controllers/reportController.js b/beat/web/reports/static/reports/app/controllers/reportController.js index abe6f20fa8d832ac8cf924ee4a85357f5209d0a9..092c25aef75df838d5d6fa64ac2b2df87d962d10 100644 --- a/beat/web/reports/static/reports/app/controllers/reportController.js +++ b/beat/web/reports/static/reports/app/controllers/reportController.js @@ -21,7 +21,7 @@ */ //This controller retrieves data from the reportFactory/experimentFactory through the REST API and associates it with the $scope //The $scope is ultimately bound to the report view -angular.module('reportApp').controller('reportController',['$scope', 'reportFactory', 'experimentFactory', 'plotterFactory', 'dataFactory', '$q', function ($scope, reportFactory, experimentFactory, plotterFactory, dataFactory, $q){ +angular.module('reportApp').controller('reportController',['$scope', 'reportFactory', 'experimentFactory', 'plotterFactory', 'dataFactory', '$q', 'ReportService', function ($scope, reportFactory, experimentFactory, plotterFactory, dataFactory, $q, ReportService){ $scope.q = $q; $scope.user; $scope.report_id; diff --git a/beat/web/reports/static/reports/app/services/reportService.js b/beat/web/reports/static/reports/app/services/reportService.js new file mode 100644 index 0000000000000000000000000000000000000000..cf60718d83d917033f9f88589e26e3b542cab328 --- /dev/null +++ b/beat/web/reports/static/reports/app/services/reportService.js @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/ + * Contact: beat.support@idiap.ch + * + * This file is part of the beat.web module of the BEAT platform. + * + * Commercial License Usage + * Licensees holding valid commercial BEAT licenses may use this file in + * accordance with the terms contained in a written agreement between you + * and Idiap. For further information contact tto@idiap.ch + * + * Alternatively, this file may be used under the terms of the GNU Affero + * Public License version 3 as published by the Free Software and appearing + * in the file LICENSE.AGPL included in the packaging of this file. + * The BEAT platform is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero Public License along + * with the BEAT platform. If not, see http://www.gnu.org/licenses/. + */ + +/* + * ReportService + * Desc: + * The datastore for the reports app, including data fetched from the + * server and cross-component app state + */ +angular.module('reportApp').factory('ReportService', ['$http', function($http){ + let reportServiceInstance = {}; + // experiments of reports are in arbitrary groups, + // in a many-to-many relationship + let groupData = []; + + // represents a Group in the report + // has a name and a list of experiments that belong to it + class Group { + constructor (name) { + this.name = name; + this.experimentNames = new Set(); + } + + // get the experiment names in this group + get experiments () { + return Array.from(this.experimentNames); + } + + // get the group name + get name () { + return this.name; + } + + // check if an exp is in this group + hasExperiment (expName) { + return this.experimentNames.has(expName); + } + + // add an exp to this group + addExperiment (expName) { + return this.experimentNames.add(expName); + } + + // rm an exp from this group + removeExperiment (expName) { + return this.experimentNames.delete(expName); + } + }; + + // gets groups + reportServiceInstance.groups = () => groupData.map(g => g.name); + + // create a new group for the report + // returns false if it already exists + // returns true if it adds a new group + reportServiceInstance.createGroup = (name) => { + if(typeof name !== 'string'){ + throw new Error(`new group name is not a string: ${JSON.stringify(name)}`); + } + + if(groupData.find(g => g.name === name)){ + return false; + } + + groupData.push(new Group(name)); + return true; + }; + + // delete a group + reportServiceInstance.deleteGroup = (name) => { + groupData = groupData.filter(g => g.name !== name); + }; + + // fetch group info from the api + reportServiceInstance.fetchGroups = (reportName) => { + console.warn(`not implemented: reportServiceInstance.fetchGroups(${JSON.stringify(reportName)})`); + }; + + // save group info via sending to server + reportServiceInstance.saveGroups = () => { + console.warn(`not implemented: reportServiceInstance.saveGroups`); + }; + + // add experiment to group + reportServiceInstance.addExperimentToGroup = (expName, groupName) => { + checkForGroup(groupName); + + let group = groupData.find(g => g.name === groupName); + return group.addExperiment(expName); + }; + + // rm experiment from group + reportServiceInstance.removeExperimentFromGroup = (expName, groupName) => { + checkForGroup(groupName); + let group = groupData.find(g => g.name === groupName); + return group.removeExperiment(expName); + }; + + // gets experiments from a group + reportServiceInstance.getGroupExperiments = (groupName) => { + checkForGroup(groupName); + let group = groupData.find(g => g.name === groupName); + return group.experiments; + }; + + // gets groups for an experiment + reportServiceInstance.getExperimentGroups = (expName) => { + return groupData.filter(g => g.hasExperiment(expName)).map(g => g.name); + }; + + // helper to assert that a group exists + function checkForGroup (groupName) { + if(!reportServiceInstance.groups().includes(groupName)){ + throw new Error(`Could not find group "${JSON.stringify(groupName)}"`); + } + } + + return reportServiceInstance; +}]); diff --git a/beat/web/reports/templates/reports/report.html b/beat/web/reports/templates/reports/report.html index a4132f70aa1806fa81f251909037f445e5b8c970..5e816dff5be3ef894972edd670559abe70061e34 100644 --- a/beat/web/reports/templates/reports/report.html +++ b/beat/web/reports/templates/reports/report.html @@ -75,6 +75,7 @@ <script src="{% fingerprint "reports/app/factories/experimentFactory.js" %}" type="text/javascript" charset="utf-8"></script> <script src="{% fingerprint "reports/app/factories/plotterFactory.js" %}" type="text/javascript" charset="utf-8"></script> <script src="{% fingerprint "reports/app/factories/dataFactory.js" %}" type="text/javascript" charset="utf-8"></script> + <script src="{% fingerprint "reports/app/services/reportService.js" %}" type="text/javascript" charset="utf-8"></script> <!-- directives -->