1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
'use strict';
const api = require('../api.js');
const router = require('../router.js');
const views = require('../util/views.js');
const topNavigation = require('../models/top_navigation.js');
const Post = require('../models/post.js');
const PostMetric = require('../models/post_metric.js');
const PostMetricRange = require('../models/post_metric_range.js');
const PostList = require('../models/post_list.js');
const MetricSorterView = require('../views/metric_sorter_view.js');
const EmptyView = require('../views/empty_view.js');
const LEFT = 'left';
const RIGHT = 'right';
class MetricSorterController {
constructor(ctx) {
if (!api.hasPrivilege('posts:view') ||
!api.hasPrivilege('metrics:edit:posts')) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to edit post metric values.');
return;
}
topNavigation.activate('posts');
topNavigation.setTitle('Sorting metrics');
this._ctx = ctx;
this._metricNames = (ctx.parameters.metrics || '')
.split(' ')
.filter(m => m);
if (!this._metricNames.length) {
this._view = new EmptyView();
this._view.showError('No metrics selected');
return;
}
this._primaryMetricName = this._metricNames[0];
this._view = new MetricSorterView({
primaryMetric: this._primaryMetricName,
greaterPost: RIGHT,
});
this._view.addEventListener('submit', e => this._evtSubmit(e));
this._view.addEventListener('skip', e => this._evtSkip(e));
this._view.addEventListener('changeMetric', e => this._evtChangeMetric(e));
if (ctx.parameters.id === 'random') {
this.startSortingRandomPost();
} else {
this.startSortingPost(ctx.parameters.id);
}
}
startSortingPost(id) {
this._view.clearMessages();
this._foundExactValue = false;
Post.get(id).then(post => {
this._unsortedPost = post;
this._view.installLeftPost(post);
this.reloadMedianPost();
}).catch(error => {
this._view.showError(error.message)
});
}
startSortingRandomPost() {
this._view.clearMessages();
this._getRandomUnsortedPostId().then(id => {
this._ctx.parameters.id = id;
router.replace(views.getMetricSorterUrl(id, this._ctx.parameters));
this.startSortingPost(id);
}).catch(error => {
this._view.showError(error.message)
});
}
reloadMedianPost() {
const metricName = this._primaryMetricName;
let range = this._getOrCreateRange(this._unsortedPost, metricName);
this._tryGetMedianPost(metricName, range).then(medianResponse => {
if (medianResponse.post) {
this._sortedPost = medianResponse.post;
this._view.installRightPost(this._sortedPost);
} else {
// No existing metrics, apply the median value
this._foundExactValue = true;
let exactValue = (medianResponse.range.low + medianResponse.range.high) / 2;
this._view.showSuccess(`Found exact value: ${exactValue}`);
this._setExactMetric(this._unsortedPost, metricName, exactValue);
//TODO: maybe allow to set exact value?
}
}).catch(error => {
this._view.showError(error.message)
});
}
_getRandomUnsortedPostId() {
let unsetMetricsQuery = this._metricNames
.map(m => `${m} -metric:${m}`)
.join(' ');
let filterQuery = this._ctx.parameters.query || '';
let unsetFullQuery = `${filterQuery} ${unsetMetricsQuery} sort:random`;
return PostList.search(unsetFullQuery,
this._ctx.parameters.skips || 0, 1, ['id']).then(response => {
if (!response.results.length) {
return Promise.reject(new Error('No posts found'));
} else {
return Promise.resolve(response.results.at(0).id);
}
});
}
_tryGetMedianPost(metric, range) {
let low = range.low + 0.000000001;
let high = range.high - 0.000000001;
let median_query = `metric-${metric}:${low}..${high} sort:metric-${metric}`;
return PostList.getMedian(median_query, []).then(response => {
return Promise.resolve({
range: range,
post: response.results.at(0)
});
});
}
_getOrCreateRange(post, metricName) {
let range = post.metricRanges.findByTagName(metricName);
if (!range) {
let tag = post.tags.findByName(metricName);
range = PostMetricRange.create(post.id, tag);
post.metricRanges.add(range);
}
return range;
}
_setExactMetric(post, metricName, value) {
let range = post.metricRanges.findByTagName(metricName);
if (!range) {
post.metricRanges.remove(range);
}
let tag = post.tags.findByName(metricName);
let exactMetric = PostMetric.create(post.id, tag);
exactMetric.value = value;
post.metrics.add(exactMetric);
}
_evtSubmit(e) {
let range = this._getOrCreateRange(this._unsortedPost, this._primaryMetricName);
if (this._foundExactValue) {
this._unsortedPost.metricRanges.remove(range);
} else {
let medianValue = this._sortedPost.metrics.findByTagName(this._primaryMetricName).value;
if (e.detail.greaterPost === LEFT) {
range.low = medianValue;
} else {
range.high = medianValue;
}
}
this._unsortedPost.save().then(() => {
if (this._foundExactValue) {
this.startSortingRandomPost();
} else {
this.reloadMedianPost();
}
}, error => {
this._view.showError(error.message)
});
}
_evtSkip(e) {
this._ctx.parameters.skips = (this._ctx.parameters.skips || 0) + 1;
this.startSortingRandomPost();
}
_evtChangeMetric(e) {
// this._primaryMetricName = e.detail.metricName;
}
}
module.exports = router => {
router.enter(
['post', ':id', 'metric-sorter'],
(ctx, next) => {
ctx.controller = new MetricSorterController(ctx);
});
};
|