aboutsummaryrefslogtreecommitdiff
path: root/client/js/models/metric.js
diff options
context:
space:
mode:
authorHunternif2019-04-16 22:09:04 +0700
committerHunternif2019-04-16 22:09:04 +0700
commit9f673a39bfad299c101469f38d8838d7d266279e (patch)
tree5741a2725cb456a848f3209b1079f9d01c91ba33 /client/js/models/metric.js
parent2307abc8aa27781a3e44930599141a778f09f800 (diff)
POC create metric in tag edit view.
Diffstat (limited to 'client/js/models/metric.js')
-rw-r--r--client/js/models/metric.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/client/js/models/metric.js b/client/js/models/metric.js
new file mode 100644
index 0000000..2095999
--- /dev/null
+++ b/client/js/models/metric.js
@@ -0,0 +1,70 @@
+'use strict';
+
+const api = require('../api.js');
+const uri = require('../util/uri.js');
+const events = require('../events.js');
+const misc = require('../util/misc.js');
+
+class Metric extends events.EventTarget {
+ constructor() {
+ super();
+ this._orig = {};
+
+ this._updateFromResponse({});
+ }
+
+ get min() { return this._min; }
+ get max() { return this._max; }
+
+ set min(value) { this._min = value; }
+ set max(value) { this._max = value; }
+
+ static fromResponse(response) {
+ const ret = new Metric();
+ ret._updateFromResponse(response);
+ return ret;
+ }
+
+ static get(name) {
+ //TODO get metric. Or only via tag?
+ return api.get(uri.formatApiLink('metric', name))
+ .then(response => {
+ return Promise.resolve(Metric.fromResponse(response));
+ });
+ }
+
+ save() {
+ const detail = {version: this._version};
+
+ if (this._min !== this._orig._min) {
+ detail.min = this._min;
+ }
+ if (this._max !== this._orig._max) {
+ detail.max = this._max;
+ }
+
+ return api.post(uri.formatApiLink('metrics'), detail)
+ .then(response => {
+ this._updateFromResponse(response);
+ this.dispatchEvent(new CustomEvent('change', {
+ detail: {
+ metric: this,
+ },
+ }));
+ return Promise.resolve();
+ });
+ }
+
+ _updateFromResponse(response) {
+ const map = {
+ _version: response.version,
+ _min: response.min,
+ _max: response.max,
+ };
+
+ Object.assign(this, map);
+ Object.assign(this._orig, map);
+ }
+}
+
+module.exports = Metric;

© 2015 - 2026 Jakob L. Kreuze