From 920d1b368b9b3ecb55ac5271e4ae813e022278ea Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Wed, 8 Jul 2015 13:59:26 +0200 Subject: [PATCH] SONAR-6697 filter available metrics --- .../apps/custom-measures/custom-measures.js | 7 +++ .../main/js/apps/custom-measures/form-view.js | 15 +++++- .../templates/custom-measures-form.hbs | 27 ++++++---- .../custom-measures-spec/metrics-limited.json | 53 +++++++++++++++++++ .../test/medium/custom-measures.spec.js | 28 ++++++++++ .../resources/org/sonar/l10n/core.properties | 1 + 6 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 server/sonar-web/src/test/json/custom-measures-spec/metrics-limited.json diff --git a/server/sonar-web/src/main/js/apps/custom-measures/custom-measures.js b/server/sonar-web/src/main/js/apps/custom-measures/custom-measures.js index 4d2c6d29797..2febc1ea652 100644 --- a/server/sonar-web/src/main/js/apps/custom-measures/custom-measures.js +++ b/server/sonar-web/src/main/js/apps/custom-measures/custom-measures.js @@ -38,6 +38,13 @@ define([ hasMore: function () { return this.total > this.p * this.ps; + }, + + getTakenMetrics: function () { + var metrics = this.map(function (model) { + return model.get('metric').id; + }); + return _.uniq(metrics); } }); diff --git a/server/sonar-web/src/main/js/apps/custom-measures/form-view.js b/server/sonar-web/src/main/js/apps/custom-measures/form-view.js index 43a62a7de83..30ed30082b9 100644 --- a/server/sonar-web/src/main/js/apps/custom-measures/form-view.js +++ b/server/sonar-web/src/main/js/apps/custom-measures/form-view.js @@ -32,9 +32,20 @@ define([ this.sendRequest(); }, + getAvailableMetrics: function () { + var takenMetrics = this.collection.getTakenMetrics(); + return this.metrics.toJSON().filter(function (metric) { + return takenMetrics.indexOf(metric.id) === -1; + }); + }, + serializeData: function () { - // TODO show only not taken metrics - return _.extend(this._super(), { metrics: this.metrics.toJSON() }); + var metrics = this.getAvailableMetrics(), + isNew = !this.model; + return _.extend(this._super(), { + metrics: metrics, + canCreateMetric: !isNew || (isNew && metrics.length > 0) + }); } }); diff --git a/server/sonar-web/src/main/js/apps/custom-measures/templates/custom-measures-form.hbs b/server/sonar-web/src/main/js/apps/custom-measures/templates/custom-measures-form.hbs index 52e16a0e98b..71b01cc044c 100644 --- a/server/sonar-web/src/main/js/apps/custom-measures/templates/custom-measures-form.hbs +++ b/server/sonar-web/src/main/js/apps/custom-measures/templates/custom-measures-form.hbs @@ -4,27 +4,36 @@ diff --git a/server/sonar-web/src/test/json/custom-measures-spec/metrics-limited.json b/server/sonar-web/src/test/json/custom-measures-spec/metrics-limited.json new file mode 100644 index 00000000000..cc72eea9170 --- /dev/null +++ b/server/sonar-web/src/test/json/custom-measures-spec/metrics-limited.json @@ -0,0 +1,53 @@ +{ + "metrics": [ + { + "id": "155", + "key": "burned_budget", + "type": "FLOAT", + "name": "Burned budget", + "domain": "Management", + "direction": 0, + "qualitative": false, + "hidden": false, + "custom": true + }, + { + "id": "159", + "key": "dist", + "type": "DISTRIB", + "name": "Distribution", + "description": "", + "domain": "", + "direction": 0, + "qualitative": false, + "hidden": false, + "custom": true + }, + { + "id": "160", + "key": "rating", + "type": "RATING", + "name": "Rating", + "description": "", + "domain": "", + "direction": 0, + "qualitative": false, + "hidden": false, + "custom": true + }, + { + "id": "157", + "key": "team_size", + "type": "INT", + "name": "Team size", + "domain": "Management", + "direction": 0, + "qualitative": false, + "hidden": false, + "custom": true + } + ], + "total": 5, + "p": 1, + "ps": 100 +} diff --git a/server/sonar-web/test/medium/custom-measures.spec.js b/server/sonar-web/test/medium/custom-measures.spec.js index 6e67edc1529..26186842e26 100644 --- a/server/sonar-web/test/medium/custom-measures.spec.js +++ b/server/sonar-web/test/medium/custom-measures.spec.js @@ -68,6 +68,34 @@ define(function (require) { .checkElementInclude('[data-id="6"] .js-custom-measure-value', '17'); }); + bdd.it('should filter available metrics', function () { + return this.remote + .open() + .mockFromFile('/api/custom_measures/search', 'custom-measures-spec/search.json', + { data: { projectId: projectId } }) + .mockFromFile('/api/metrics/search', 'custom-measures-spec/metrics.json', { data: { isCustom: true } }) + .startApp('custom-measures', { projectId: projectId }) + .clickElement('#custom-measures-create') + .checkElementExist('#create-custom-measure-form') + .checkElementCount('#create-custom-measure-metric option', 1) + .checkElementExist('#create-custom-measure-metric option[value="156"]'); + }); + + bdd.it('should show warning when there are no available metrics', function () { + return this.remote + .open() + .mockFromFile('/api/custom_measures/search', 'custom-measures-spec/search.json', + { data: { projectId: projectId } }) + .mockFromFile('/api/metrics/search', 'custom-measures-spec/metrics-limited.json', + { data: { isCustom: true } }) + .startApp('custom-measures', { projectId: projectId }) + .clickElement('#custom-measures-create') + .checkElementExist('#create-custom-measure-form') + .checkElementNotExist('#create-custom-measure-metric') + .checkElementExist('#create-custom-measure-form .alert-warning') + .checkElementExist('#create-custom-measure-submit[disabled]'); + }); + bdd.it('should update a custom measure', function () { return this.remote .open() diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties index fd9b4b79784..f1f2ce12e86 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -1591,6 +1591,7 @@ manual_measures.to_define_new_manual_metric_il_require=You can define new manual custom_measures.pending=Pending custom_measures.pending_tooltip=The value will be integrated to project during next analysis. +custom_measures.all_metrics_taken=There are already measures on all available custom metrics. -- 2.39.5