Skip to content

Commit 0831b3a

Browse files
committed
added MEDIAN
1 parent c93ebf1 commit 0831b3a

6 files changed

Lines changed: 27 additions & 3 deletions

File tree

herald-core/herald-grammar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
conjunctionAnd: ['AND'],
2525
conjunctionOr: ['OR'],
2626
field: ['LABEL', 'VALUE', 'UNIT', 'START', 'END', 'NUMERIC'],
27-
aggregation: ['AVERAGE', 'COUNT', 'SUM', 'MIN', 'MAX', 'MOST FREQUENT'],
27+
aggregation: ['AVERAGE', 'MEDIAN', 'COUNT', 'SUM', 'MIN', 'MAX', 'MOST FREQUENT'],
2828
selection: ['FIRST', 'LAST', 'ANY'],
2929
relationship: ['RATIO BETWEEN', 'DIFFERENCE BETWEEN', 'EQUALITY OF'],
3030
existence: ['EXISTS', 'NOT EXISTS'],

herald-core/herald-interpreter.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,28 @@ function parseFilter(observations, query) {
422422
}
423423
break;
424424
}
425+
case 'MEDIAN': {
426+
let numericObservations = observations.filter(obs => obs.isNumeric);
427+
if(numericObservations && numericObservations.length > 0){
428+
numericObservations.sort((a, b) => a.value - b.value);
429+
const count = numericObservations.length;
430+
const middleIndex = Math.floor(count / 2);
431+
if (count % 2 === 0) {
432+
// Return average of two middle values
433+
aggregatedValue = (parseFloat(numericObservations[middleIndex - 1].value) + parseFloat(numericObservations[middleIndex].value))/2;
434+
aggregatedUnit = numericObservations[middleIndex - 1].unit === numericObservations[middleIndex].unit ? numericObservations[middleIndex].unit : "Mixed";
435+
} else {
436+
// Return value of the middle index
437+
aggregatedValue = numericObservations[middleIndex].value;
438+
aggregatedUnit = validateUnit(numericObservations[middleIndex].unit);
439+
}
440+
} else {
441+
aggregatedValue = null;
442+
aggregatedUnit = validateUnit(null);
443+
}
444+
445+
break;
446+
}
425447
case 'COUNT': {
426448
aggregatedValue = observations.length;
427449
aggregatedUnit = validateUnit(null);

herald-core/herald-lexer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function getHeraldLexer(ignoreErrors = false) {
1818
conjunctionAnd: ['AND'],
1919
conjunctionOr: ['OR'],
2020
field: ['LABEL', 'VALUE', 'UNIT', 'START', 'END', 'NUMERIC'],
21-
aggregation: ['AVERAGE', 'COUNT', 'SUM', 'MIN', 'MAX', 'MOST FREQUENT'],
21+
aggregation: ['AVERAGE', 'MEDIAN', 'COUNT', 'SUM', 'MIN', 'MAX', 'MOST FREQUENT'],
2222
selection: ['FIRST', 'LAST', 'ANY'],
2323
relationship: ['RATIO BETWEEN', 'DIFFERENCE BETWEEN', 'EQUALITY OF'],
2424
existence: ['EXISTS', 'NOT EXISTS'],

herald-ui/herald-ui-binding.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ function abbreviateKeyword(keyword) {
362362
"MONTH": "M",
363363
"YEAR": "Y",
364364
"AVERAGE": "Avg",
365+
"MEDIAN": "Med",
365366
"AND": "And",
366367
"OR": "Or",
367368
"LABEL": "Lbl",

herald-ui/herald-ui-editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
const queryTypeKeywords = {
2727
selection: ['FIRST', 'LAST', 'ANY'],
28-
aggregation: ['AVERAGE', 'COUNT', 'SUM', 'MIN', 'MAX', 'MOST FREQUENT'],
28+
aggregation: ['AVERAGE', 'MEDIAN', 'COUNT', 'SUM', 'MIN', 'MAX', 'MOST FREQUENT'],
2929
existence: ['EXISTS', 'NOT EXISTS'],
3030
relationship: ['RATIO BETWEEN', 'DIFFERENCE BETWEEN', 'EQUALITY OF'],
3131
};

herald-ui/herald-ui-generic.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ function initHeraldQueryEntryForm() {
398398
<button class="dropdown-item herald-quick-add" data-action="EXISTS">EXISTS</button>
399399
<button class="dropdown-item herald-quick-add" data-action="NOT EXISTS">NOT EXISTS</button>
400400
<button class="dropdown-item herald-quick-add" data-action="AVERAGE">AVERAGE</button>
401+
<button class="dropdown-item herald-quick-add" data-action="MEDIAN">MEDIAN</button>
401402
<button class="dropdown-item herald-quick-add" data-action="COUNT">COUNT</button>
402403
<button class="dropdown-item herald-quick-add" data-action="SUM">SUM</button>
403404
<button class="dropdown-item herald-quick-add" data-action="MIN">MIN</button>

0 commit comments

Comments
 (0)