Skip to content

Commit

Permalink
Merge 2ac76d6 into backport/ui/enhancement/updateNodeList/mentally-mo…
Browse files Browse the repository at this point in the history
…dern-feline
  • Loading branch information
hc-github-team-consul-core committed Jul 12, 2023
2 parents e9d45d4 + 2ac76d6 commit 94884c4
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions ui/packages/consul-ui/app/filter/predicates/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export default {
warning: (item, value) => item.Status === value,
critical: (item, value) => item.Status === value,
},
version: (item, value) => item.Version.includes(value + '.'),
};
5 changes: 5 additions & 0 deletions ui/packages/consul-ui/app/models/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ export default class Node extends Model {
get ChecksWarning() {
return this.NodeChecks.filter((item) => item.Status === 'warning').length;
}

@computed('Meta')
get Version() {
return this.Meta?.['consul-version'] ?? '';
}
}
45 changes: 45 additions & 0 deletions ui/packages/consul-ui/app/serializers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ export default class ApplicationSerializer extends Serializer {
// ember-data methods so we have the opportunity to do this on a per-model
// level
const meta = this.normalizeMeta(store, modelClass, normalizedPayload, id, requestType);
// get distinct consul versions from list and add it as meta
if (modelClass.modelName === 'node' && requestType === 'query') {
meta.versions = this.getDistinctConsulVersions(normalizedPayload);
}
if (requestType !== 'query') {
normalizedPayload.meta = meta;
}
Expand Down Expand Up @@ -215,4 +219,45 @@ export default class ApplicationSerializer extends Serializer {
normalizePayload(payload, id, requestType) {
return payload;
}

// getDistinctConsulVersions will be called only for nodes and query request type
// the list of versions is to be added as meta to resp, without changing original response structure
// hence this function is added in application.js
getDistinctConsulVersions(payload) {
// create a Set and add version with only major.minor : ex-1.24.6 as 1.24
let versionSet = new Set();
payload.forEach(function (item) {
if (item.Meta && item.Meta['consul-version'] !== '') {
const split = item.Meta['consul-version'].split('.');
versionSet.add(split[0] + '.' + split[1]);
}
});

const versionArray = Array.from(versionSet);

// Sort the array in descending order using a custom comparison function
versionArray.sort((a, b) => {
// Split the versions into arrays of numbers
const versionA = a.split('.').map((part) => {
const number = Number(part);
return isNaN(number) ? 0 : number;
});
const versionB = b.split('.').map((part) => {
const number = Number(part);
return isNaN(number) ? 0 : number;
});

const minLength = Math.min(versionA.length, versionB.length);

// start with comparing major version num, if equal then compare minor
for (let i = 0; i < minLength; i++) {
if (versionA[i] !== versionB[i]) {
return versionB[i] - versionA[i];
}
}
return versionB.length - versionA.length;
});

return versionArray; //sorted array
}
}
37 changes: 37 additions & 0 deletions ui/packages/consul-ui/app/sort/comparators/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ export default ({ properties }) =>
return 0;
}
};
} else if (key.startsWith('Version:')) {
return function (itemA, itemB) {
const [, dir] = key.split(':');
let a, b;
if (dir === 'asc') {
a = itemA;
b = itemB;
} else {
b = itemA;
a = itemB;
}

// Split the versions into arrays of numbers
const versionA = a.Version.split('.').map((part) => {
const number = Number(part);
return isNaN(number) ? 0 : number;
});
const versionB = b.Version.split('.').map((part) => {
const number = Number(part);
return isNaN(number) ? 0 : number;
});

const minLength = Math.min(versionA.length, versionB.length);

for (let i = 0; i < minLength; i++) {
const diff = versionA[i] - versionB[i];
switch (true) {
case diff > 0:
return 1;
case diff < 0:
return -1;
}
}

return versionA.length - versionB.length;
};
}

return properties(['Node'])(key);
};
45 changes: 45 additions & 0 deletions ui/packages/consul-ui/tests/unit/sort/comparators/node-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

import comparators from 'consul-ui/sort/comparators/node';
import { properties } from 'consul-ui/services/sort';
import { module, test } from 'qunit';

module('Unit | Sort | Comparator | node', function () {
const comparator = comparators({ properties });
test('items are sorted by a fake Version', function (assert) {
const items = [
{
Version: '2.24.1',
},
{
Version: '1.12.6',
},
{
Version: '2.09.3',
},
];
const comp = comparator('Version:asc');
assert.equal(typeof comp, 'function');

const expected = [
{
Version: '1.12.6',
},
{
Version: '2.09.3',
},
{
Version: '2.24.1',
},
];
let actual = items.sort(comp);
assert.deepEqual(actual, expected);

expected.reverse();
actual = items.sort(comparator('Version:desc'));
assert.deepEqual(actual, expected);
});
});

0 comments on commit 94884c4

Please sign in to comment.