-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UI: Clients filtering #5318
Merged
Merged
UI: Clients filtering #5318
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
645f4b5
Refactor qp helpers from the jobs faceted search
DingoEatingFuzz 683b952
Add nodeClass to the node model
DingoEatingFuzz 7246869
Implement faceted search on the clients page
DingoEatingFuzz 96bae7a
Test coverage for clients faceted search
DingoEatingFuzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { alias } from '@ember/object/computed'; | ||
import Controller, { inject as controller } from '@ember/controller'; | ||
import { computed } from '@ember/object'; | ||
import { scheduleOnce } from '@ember/runloop'; | ||
import intersection from 'lodash.intersection'; | ||
import Sortable from 'nomad-ui/mixins/sortable'; | ||
import Searchable from 'nomad-ui/mixins/searchable'; | ||
import { serialize, deserializedQueryParam as selection } from 'nomad-ui/utils/qp-serialize'; | ||
|
||
export default Controller.extend(Sortable, Searchable, { | ||
clientsController: controller('clients'), | ||
|
@@ -15,6 +18,10 @@ export default Controller.extend(Sortable, Searchable, { | |
searchTerm: 'search', | ||
sortProperty: 'sort', | ||
sortDescending: 'desc', | ||
qpClass: 'class', | ||
qpStatus: 'status', | ||
qpDatacenter: 'dc', | ||
qpFlags: 'flags', | ||
}, | ||
|
||
currentPage: 1, | ||
|
@@ -25,12 +32,97 @@ export default Controller.extend(Sortable, Searchable, { | |
|
||
searchProps: computed(() => ['id', 'name', 'datacenter']), | ||
|
||
listToSort: alias('nodes'), | ||
qpClass: '', | ||
qpStatus: '', | ||
qpDatacenter: '', | ||
qpFlags: '', | ||
|
||
selectionClass: selection('qpClass'), | ||
selectionStatus: selection('qpStatus'), | ||
selectionDatacenter: selection('qpDatacenter'), | ||
selectionFlags: selection('qpFlags'), | ||
|
||
optionsClass: computed('nodes.[]', function() { | ||
const classes = Array.from(new Set(this.get('nodes').mapBy('nodeClass'))).compact(); | ||
|
||
// Remove any invalid node classes from the query param/selection | ||
scheduleOnce('actions', () => { | ||
this.set('qpClass', serialize(intersection(classes, this.get('selectionClass')))); | ||
}); | ||
|
||
return classes.sort().map(dc => ({ key: dc, label: dc })); | ||
}), | ||
|
||
optionsStatus: computed(() => [ | ||
{ key: 'initializing', label: 'Initializing' }, | ||
{ key: 'ready', label: 'Ready' }, | ||
{ key: 'down', label: 'Down' }, | ||
]), | ||
|
||
optionsDatacenter: computed('nodes.[]', function() { | ||
const datacenters = Array.from(new Set(this.get('nodes').mapBy('datacenter'))).compact(); | ||
|
||
// Remove any invalid datacenters from the query param/selection | ||
scheduleOnce('actions', () => { | ||
this.set( | ||
'qpDatacenter', | ||
serialize(intersection(datacenters, this.get('selectionDatacenter'))) | ||
); | ||
}); | ||
|
||
return datacenters.sort().map(dc => ({ key: dc, label: dc })); | ||
}), | ||
|
||
optionsFlags: computed(() => [ | ||
{ key: 'ineligible', label: 'Ineligible' }, | ||
{ key: 'draining', label: 'Draining' }, | ||
]), | ||
|
||
filteredNodes: computed( | ||
'nodes.[]', | ||
'selectionClass', | ||
'selectionStatus', | ||
'selectionDatacenter', | ||
'selectionFlags', | ||
function() { | ||
const { | ||
selectionClass: classes, | ||
selectionStatus: statuses, | ||
selectionDatacenter: datacenters, | ||
selectionFlags: flags, | ||
} = this.getProperties( | ||
'selectionClass', | ||
'selectionStatus', | ||
'selectionDatacenter', | ||
'selectionFlags' | ||
); | ||
|
||
const onlyIneligible = flags.includes('ineligible'); | ||
const onlyDraining = flags.includes('draining'); | ||
|
||
return this.get('nodes').filter(node => { | ||
if (classes.length && !classes.includes(node.get('nodeClass'))) return false; | ||
if (statuses.length && !statuses.includes(node.get('status'))) return false; | ||
if (datacenters.length && !datacenters.includes(node.get('datacenter'))) return false; | ||
|
||
if (onlyIneligible && node.get('isEligible')) return false; | ||
if (onlyDraining && !node.get('isDraining')) return false; | ||
|
||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is clear and easy to read! |
||
}); | ||
} | ||
), | ||
|
||
listToSort: alias('filteredNodes'), | ||
listToSearch: alias('listSorted'), | ||
sortedNodes: alias('listSearched'), | ||
|
||
isForbidden: alias('clientsController.isForbidden'), | ||
|
||
setFacetQueryParam(queryParam, selection) { | ||
this.set(queryParam, serialize(selection)); | ||
}, | ||
|
||
actions: { | ||
gotoNode(node) { | ||
this.transitionToRoute('clients.client', node); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { computed } from '@ember/object'; | ||
|
||
// An unattractive but robust way to encode query params | ||
export const serialize = arr => (arr.length ? JSON.stringify(arr) : ''); | ||
|
||
export const deserialize = str => { | ||
try { | ||
return JSON.parse(str) | ||
.compact() | ||
.without(''); | ||
} catch (e) { | ||
return []; | ||
} | ||
}; | ||
|
||
// A computed property macro for deserializing a query param | ||
export const deserializedQueryParam = qpKey => | ||
computed(qpKey, function() { | ||
return deserialize(this.get(qpKey)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This all seems correct, but I'm a little wary of side effects in computed properties. What do you think about moving these QP clean-ups into a method which is called from the route's
setupController
hook, after the model is set?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely like that better, but it won't capture the case where via blocking queries the underlying data changes and invalidates query params (e.g., clients change node class for some reason).
Although... maybe I could add some new hook to routes that have Watchers... 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I did some light exploration on this idea and I think it has legs. I'd prefer to do it in a follow up PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I'd completely forgotten about blocking queries when I suggested it. Makes sense to explore post-merge.