2019-01-17 16:27:43 +08:00
|
|
|
import React, { FC } from 'react';
|
2018-12-20 20:36:10 +08:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
2020-01-17 19:25:47 +08:00
|
|
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
2020-09-03 14:54:06 +08:00
|
|
|
import { SelectableValue, rangeUtil } from '@grafana/data';
|
2020-01-17 19:25:47 +08:00
|
|
|
import { Segment } from '@grafana/ui';
|
2019-01-11 22:14:35 +08:00
|
|
|
import { alignmentPeriods, alignOptions } from '../constants';
|
2018-12-20 20:36:10 +08:00
|
|
|
|
|
|
|
|
export interface Props {
|
2020-03-02 22:31:09 +08:00
|
|
|
onChange: (alignmentPeriod: string) => void;
|
2019-01-15 06:43:31 +08:00
|
|
|
templateSrv: TemplateSrv;
|
2020-01-17 19:25:47 +08:00
|
|
|
templateVariableOptions: Array<SelectableValue<string>>;
|
2018-12-20 20:36:10 +08:00
|
|
|
alignmentPeriod: string;
|
2019-01-11 22:14:35 +08:00
|
|
|
perSeriesAligner: string;
|
2020-08-13 15:35:32 +08:00
|
|
|
usedAlignmentPeriod?: number;
|
2018-12-20 20:36:10 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-17 16:27:43 +08:00
|
|
|
export const AlignmentPeriods: FC<Props> = ({
|
2019-01-11 22:14:35 +08:00
|
|
|
alignmentPeriod,
|
|
|
|
|
templateSrv,
|
2020-01-17 19:25:47 +08:00
|
|
|
templateVariableOptions,
|
2019-01-11 22:14:35 +08:00
|
|
|
onChange,
|
|
|
|
|
perSeriesAligner,
|
|
|
|
|
usedAlignmentPeriod,
|
|
|
|
|
}) => {
|
|
|
|
|
const alignment = alignOptions.find(ap => ap.value === templateSrv.replace(perSeriesAligner));
|
2020-08-13 15:35:32 +08:00
|
|
|
const formatAlignmentText = usedAlignmentPeriod
|
2020-09-03 14:54:06 +08:00
|
|
|
? `${rangeUtil.secondsToHms(usedAlignmentPeriod)} interval (${alignment ? alignment.text : ''})`
|
2020-08-13 15:35:32 +08:00
|
|
|
: '';
|
2020-01-17 19:25:47 +08:00
|
|
|
const options = alignmentPeriods.map(ap => ({
|
|
|
|
|
...ap,
|
|
|
|
|
label: ap.text,
|
|
|
|
|
}));
|
2020-06-30 23:47:13 +08:00
|
|
|
const visibleOptions = options.filter(ap => !ap.hidden);
|
2019-01-11 22:14:35 +08:00
|
|
|
|
2018-12-20 20:36:10 +08:00
|
|
|
return (
|
2019-01-08 20:00:31 +08:00
|
|
|
<>
|
2018-12-20 20:36:10 +08:00
|
|
|
<div className="gf-form-inline">
|
2020-01-17 19:25:47 +08:00
|
|
|
<label className="gf-form-label query-keyword width-9">Alignment Period</label>
|
|
|
|
|
<Segment
|
2020-03-27 19:01:16 +08:00
|
|
|
onChange={({ value }) => onChange(value!)}
|
2020-01-17 19:25:47 +08:00
|
|
|
value={[...options, ...templateVariableOptions].find(s => s.value === alignmentPeriod)}
|
|
|
|
|
options={[
|
|
|
|
|
{
|
|
|
|
|
label: 'Template Variables',
|
|
|
|
|
options: templateVariableOptions,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'Aggregations',
|
|
|
|
|
expanded: true,
|
2020-06-30 23:47:13 +08:00
|
|
|
options: visibleOptions,
|
2020-01-17 19:25:47 +08:00
|
|
|
},
|
|
|
|
|
]}
|
|
|
|
|
placeholder="Select Alignment"
|
|
|
|
|
></Segment>
|
2019-01-08 23:08:09 +08:00
|
|
|
<div className="gf-form gf-form--grow">
|
2019-01-11 22:14:35 +08:00
|
|
|
{usedAlignmentPeriod && <label className="gf-form-label gf-form-label--grow">{formatAlignmentText}</label>}
|
2019-01-08 23:08:09 +08:00
|
|
|
</div>
|
2018-12-20 20:36:10 +08:00
|
|
|
</div>
|
2019-01-08 20:00:31 +08:00
|
|
|
</>
|
2018-12-20 20:36:10 +08:00
|
|
|
);
|
|
|
|
|
};
|