Files
weave-scope/client/app/scripts/components/node-details/node-details-table.js
David Kaltschmidt d39fd847b7 Details Panel UI Redesign
Refactored nodedetails to support multiple data sets, probably broke some tests
  Allow api requests to out-of-view topologies
  Fix ESC behavior with details panel
  Stack details panel like cards
  Details pain side-by-side
  Details panel piles
  Fix node details table header styles
  Render load and not-found captions like relatives
  Fix topology click action
  Make node detail tables sortable
  Grouped metrics for details health
  Group metrics in same style
  Link node details children
  Fix scroll issues on double-details
  Fix DESC sort order for node details table
  Save selected node labels in state - allows rendering of node labels from other topologies before details are loaded
  Change detail card UX, newest one at top, pile at bottom
  Details panel one pile w/ animation
  Sort details table nodes by metadata too
  Animate sidepanel from children too
  Fix radial layout
  Dont set origin if a node was already selected, suppresses animation
  stack effect: shift top cards to the left, shrink lower cards vertically
  Clear details card stack if sibling is selected
  Check if node is still selected on API response
  Make detail table sorters robust against non-uniform metadata
  Dont show scrollbar all the time, fix sort icon issue
  Button to show topology for relative
  Overflow metrics for details panel health
  Fix JS error when no metrics are available for container image details
  Column-based rendering of node details table
  Fix JS tests
  Review feedback (UI)
2016-01-19 16:47:05 +01:00

158 lines
5.1 KiB
JavaScript

import _ from 'lodash';
import React from 'react';
import NodeDetailsTableNodeLink from './node-details-table-node-link';
import { formatMetric } from '../../utils/string-utils';
export default class NodeDetailsTable extends React.Component {
constructor(props, context) {
super(props, context);
this.DEFAULT_LIMIT = 5;
this.state = {
limit: this.DEFAULT_LIMIT,
sortedDesc: true,
sortBy: null
};
this.handleLimitClick = this.handleLimitClick.bind(this);
this.getValueForSortBy = this.getValueForSortBy.bind(this);
}
handleHeaderClick(ev, headerId) {
ev.preventDefault();
const sortedDesc = headerId === this.state.sortBy ? !this.state.sortedDesc : this.state.sortedDesc;
const sortBy = headerId;
this.setState({sortBy, sortedDesc});
}
handleLimitClick(ev) {
ev.preventDefault();
const limit = this.state.limit ? 0 : this.DEFAULT_LIMIT;
this.setState({limit});
}
getDefaultSortBy() {
// first metric
return _.get(this.props.nodes, [0, 'metrics', 0, 'id']);
}
getMetaDataSorters() {
// returns an array of sorters that will take a node
return _.get(this.props.nodes, [0, 'metadata'], []).map((field, index) => {
return node => node.metadata[index] ? node.metadata[index].value : null;
});
}
getValueForSortBy(node) {
// return the node's value based on the sortBy field
const sortBy = this.state.sortBy || this.getDefaultSortBy();
if (sortBy !== null) {
const field = _.union(node.metrics, node.metadata).find(f => f.id === sortBy);
if (field) {
return field.value;
}
}
return 0;
}
getValuesForNode(node) {
const values = {};
['metrics', 'metadata'].forEach(collection => {
if (node[collection]) {
node[collection].forEach(field => {
values[field.id] = field;
});
}
});
return values;
}
renderHeaders() {
if (this.props.nodes && this.props.nodes.length > 0) {
let headers = [{id: 'label', label: this.props.label}];
// gather header labels from metrics and metadata
const firstValues = this.getValuesForNode(this.props.nodes[0]);
headers = headers.concat(this.props.columns.map(column => ({id: column, label: firstValues[column].label})));
const defaultSortBy = this.getDefaultSortBy();
return (
<tr>
{headers.map(header => {
const headerClasses = ['node-details-table-header', 'truncate'];
const onHeaderClick = ev => {
this.handleHeaderClick(ev, header.id);
};
// sort by first metric by default
const isSorted = this.state.sortBy !== null ? header.id === this.state.sortBy : header.id === defaultSortBy;
const isSortedDesc = isSorted && this.state.sortedDesc;
const isSortedAsc = isSorted && !isSortedDesc;
if (isSorted) {
headerClasses.push('node-details-table-header-sorted');
}
return (
<td className={headerClasses.join(' ')} onClick={onHeaderClick} key={header.id}>
{isSortedAsc && <span className="node-details-table-header-sorter fa fa-caret-up" />}
{isSortedDesc && <span className="node-details-table-header-sorter fa fa-caret-down" />}
{header.label}
</td>
);
})}
</tr>
);
}
return '';
}
renderValues(node) {
const fields = this.getValuesForNode(node);
return this.props.columns.map(col => {
const field = fields[col];
if (field) {
return (
<td className="node-details-table-node-value" key={field.id}>
{formatMetric(field.value, field)}
</td>
);
}
});
}
render() {
const headers = this.renderHeaders();
let nodes = _.sortByAll(this.props.nodes, this.getValueForSortBy, 'label', this.getMetaDataSorters());
const limited = nodes && this.state.limit > 0 && nodes.length > this.state.limit;
const showLimitAction = nodes && (limited || (this.state.limit === 0 && nodes.length > this.DEFAULT_LIMIT));
const limitActionText = limited ? 'Show more' : 'Show less';
if (this.state.sortedDesc) {
nodes.reverse();
}
if (nodes && limited) {
nodes = nodes.slice(0, this.state.limit);
}
return (
<div className="node-details-table-wrapper">
<table className="node-details-table">
<thead>
{headers}
</thead>
<tbody>
{nodes && nodes.map(node => {
const values = this.renderValues(node);
return (
<tr className="node-details-table-node" key={node.id}>
<td className="node-details-table-node-label truncate">
<NodeDetailsTableNodeLink topologyId={this.props.topologyId} {...node} />
</td>
{values}
</tr>
);
})}
</tbody>
</table>
{showLimitAction && <div className="node-details-table-more" onClick={this.handleLimitClick}>{limitActionText}</div>}
</div>
);
}
}