open-nomad/ui/stories/charts/stats-time-series.stories.js
Buck Doyle 576bcf554f
UI: Migrate to Storybook (#6507)
I originally planned to add component documentation, but as this dragged on and I found that JSDoc-to-Markdown sometimes needed hand-tuning, I decided to skip it and focus on replicating what was already present in Freestyle. Adding documentation is a finite task that can be revisited in the future.

My goal was to migrate everything from Freestyle with as few changes as possible. Some adaptations that I found necessary:
• the DelayedArray and DelayedTruth utilities that delay component rendering until slightly after initial render because without them:
  ◦ charts were rendering with zero width
  ◦ the JSON viewer was rendering with empty content
• Storybook in Ember renders components in a routerless/controllerless context by default, so some component stories needed changes:
  ◦ table pagination/sorting stories access to query params, which necessitates some reaching into Ember internals to start routing and dynamically generate a Storybook route/controller to render components into
  ◦ some stories have a faux controller as part of their Storybook context that hosts setInterval-linked dynamic computed properties
• some jiggery-pokery with anchor tags
  ◦ inert href='#' had to become href='javascript:;
  ◦ links that are actually meant to navigate need target='_parent' so they don’t navigate inside the Storybook iframe

Maybe some of these could be addressed by fixes in ember-cli-storybook but I’m wary of digging around in there any more than I already have, as I’ve lost a lot of time to Storybook confusion and frustrations already 😞

The STORYBOOK=true environment variable tweaks some environment settings to get things working as expected in the Storybook context.

I chose to:
• use angle bracket invocation within stories rather than have to migrate them soon after having moved to Storybook
• keep Freestyle around for now for its palette and typeface components
2020-01-21 15:46:32 -06:00

120 lines
3.7 KiB
JavaScript

import hbs from 'htmlbars-inline-precompile';
import EmberObject, { computed } from '@ember/object';
import { on } from '@ember/object/evented';
import moment from 'moment';
import DelayedArray from '../utils/delayed-array';
export default {
title: 'Charts|Stats Time Series',
};
let ts = offset =>
moment()
.subtract(offset, 'm')
.toDate();
export let Standard = () => {
return {
template: hbs`
<h5 class="title is-5">Stats Time Series</h5>
<div class="block" style="height:100px; width: 400px;">
{{#if staticMetrics}}
<StatsTimeSeries @data={{staticMetrics}} @chartClass="is-primary" />
{{/if}}
</div>
`,
context: {
staticMetrics: DelayedArray.create([
{ timestamp: ts(20), percent: 0.5 },
{ timestamp: ts(18), percent: 0.5 },
{ timestamp: ts(16), percent: 0.4 },
{ timestamp: ts(14), percent: 0.3 },
{ timestamp: ts(12), percent: 0.9 },
{ timestamp: ts(10), percent: 0.3 },
{ timestamp: ts(8), percent: 0.3 },
{ timestamp: ts(6), percent: 0.4 },
{ timestamp: ts(4), percent: 0.5 },
{ timestamp: ts(2), percent: 0.6 },
{ timestamp: ts(0), percent: 0.6 },
]),
},
};
};
export let HighLowComparison = () => {
return {
template: hbs`
<h5 class="title is-5">Stats Time Series high/low comparison</h5>
<div class="columns">
<div class="block column" style="height:200px; width:400px">
{{#if data.metricsHigh}}
<StatsTimeSeries @data={{data.metricsHigh}} @chartClass="is-info" />
{{/if}}
</div>
<div class="block column" style="height:200px; width:400px">
{{#if data.metricsLow}}
<StatsTimeSeries @data={{data.metricsLow}} @chartClass="is-info" />
{{/if}}
</div>
</div>
<p class="annotation">Line charts, and therefore stats time series charts, use a letant linear gradient with a height equal to the canvas. This makes the color intensity of the gradient at values consistent across charts as long as those charts have the same y-axis domain.</p>
<p class="annotation">This is used to great effect with stats charts since they all have a y-axis domain of 0-100%.</p>
`,
context: {
data: EmberObject.extend({
timerTicks: 0,
startTimer: on('init', function() {
this.set(
'timer',
setInterval(() => {
let metricsHigh = this.metricsHigh;
let prev = metricsHigh.length ? metricsHigh[metricsHigh.length - 1].percent : 0.9;
this.appendTSValue(
metricsHigh,
Math.min(Math.max(prev + Math.random() * 0.05 - 0.025, 0.5), 1)
);
let metricsLow = this.metricsLow;
let prev2 = metricsLow.length ? metricsLow[metricsLow.length - 1].percent : 0.1;
this.appendTSValue(
metricsLow,
Math.min(Math.max(prev2 + Math.random() * 0.05 - 0.025, 0), 0.5)
);
}, 1000)
);
}),
appendTSValue(array, percent, maxLength = 300) {
array.addObject({
timestamp: Date.now(),
percent,
});
if (array.length > maxLength) {
array.splice(0, array.length - maxLength);
}
},
willDestroy() {
clearInterval(this.timer);
},
metricsHigh: computed(() => {
return [];
}),
metricsLow: computed(() => {
return [];
}),
secondsFormat() {
return date => moment(date).format('HH:mm:ss');
},
}).create(),
},
};
};