Skip to content

Commit

Permalink
Merge pull request #12 from flash1293/agg-response-cleanup-types
Browse files Browse the repository at this point in the history
Type improvements
  • Loading branch information
maryia-lapata authored Apr 9, 2020
2 parents 5617d0c + 5f7b63e commit d7e5043
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { makeFakeXAspect } from './_fake_x_aspect';
import { Dimensions, Dimension, Aspects } from './point_series';
import { Dimensions, Aspects } from './point_series';
import { Table } from '../../types';

/**
Expand All @@ -29,12 +29,11 @@ import { Table } from '../../types';
* may be undefined or an array of aspects.
*/
export function getAspects(table: Table, dimensions: Dimensions) {
const aspects = {} as Aspects;
const aspects: Partial<Aspects> = {};
(Object.keys(dimensions) as Array<keyof Dimensions>).forEach(name => {
const dimension = Array.isArray(dimensions[name])
? (dimensions[name] as Dimension[])
: [dimensions[name] as Dimension];
dimension.forEach(d => {
const dimension = dimensions[name];
const dimensionList = Array.isArray(dimension) ? dimension : [dimension];
dimensionList.forEach(d => {
if (!d) {
return;
}
Expand All @@ -59,5 +58,5 @@ export function getAspects(table: Table, dimensions: Dimensions) {
aspects.x = [makeFakeXAspect()];
}

return aspects;
return aspects as Aspects;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import { getFormatService } from '../../../services';
import { Aspect } from './point_series';
import { Table, Row } from '../../types';

type RowValue = number | 'NaN';
type RowValue = number | string | object | 'NaN';
interface Raw {
table: Table;
column: number;
row: number;
value: RowValue;
column: number | undefined;
row: number | undefined;
value?: RowValue;
}
export interface Point {
x: RowValue | '_all';
Expand Down Expand Up @@ -61,7 +61,7 @@ export function getPoint(
const yRow = row[y.accessor];
const zRow = z && row[z.accessor];

const point = {
const point: Point = {
x: xRow,
y: yRow,
z: zRow,
Expand Down Expand Up @@ -98,7 +98,7 @@ export function getPoint(
title: table.$parent.name,
},
parent: series ? series[0] : null,
} as Point;
};

if (point.y === 'NaN') {
// filter out NaN from stats
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import _ from 'lodash';
import { partial } from 'lodash';
import { getPoint } from './_get_point';
import { addToSiri, Serie } from './_add_to_siri';
import { Chart } from './point_series';
Expand All @@ -30,57 +30,59 @@ export function getSeries(table: Table, chart: Chart) {
const zAspect = aspects.z && aspects.z[0];
const multiY = Array.isArray(aspects.y) && aspects.y.length > 1;

const partGetPoint = _.partial(getPoint, table, xAspect, aspects.series);
return _(table.rows)
.transform((seriesMap: any, row, rowIndex) => {
if (!multiY) {
const point = partGetPoint(row, rowIndex, yAspect, zAspect);
if (point) {
const id = `${point.series}-${yAspect.accessor}`;
point.seriesId = id;
addToSiri(
seriesMap as Map<string, Serie>,
point,
id,
point.series,
yAspect.format,
zAspect && zAspect.format,
zAspect && zAspect.title
);
}
return;
}

aspects.y.forEach(function(y) {
const point = partGetPoint(row, rowIndex, y, zAspect);
if (!point) {
return;
}

// use the point's y-axis as it's series by default,
// but augment that with series aspect if it's actually
// available
let seriesId = y.accessor;
let seriesLabel = y.title;
const partGetPoint = partial(getPoint, table, xAspect, aspects.series);

if (aspects.series) {
const prefix = point.series ? point.series + ': ' : '';
seriesId = prefix + seriesId;
seriesLabel = prefix + seriesLabel;
}
const seriesMap = new Map<string, Serie>();

point.seriesId = seriesId;
table.rows.forEach((row, rowIndex) => {
if (!multiY) {
const point = partGetPoint(row, rowIndex, yAspect, zAspect);
if (point) {
const id = `${point.series}-${yAspect.accessor}`;
point.seriesId = id;
addToSiri(
seriesMap,
point,
seriesId as string,
seriesLabel,
y.format,
id,
point.series,
yAspect.format,
zAspect && zAspect.format,
zAspect && zAspect.title
);
});
}, new Map<string, Serie>() as any)
.thru(s => [...s.values()])
.value() as Serie[];
}
return;
}

aspects.y.forEach(function(y) {
const point = partGetPoint(row, rowIndex, y, zAspect);
if (!point) {
return;
}

// use the point's y-axis as it's series by default,
// but augment that with series aspect if it's actually
// available
let seriesId = y.accessor;
let seriesLabel = y.title;

if (aspects.series) {
const prefix = point.series ? point.series + ': ' : '';
seriesId = prefix + seriesId;
seriesLabel = prefix + seriesLabel;
}

point.seriesId = seriesId;
addToSiri(
seriesMap,
point,
seriesId as string,
seriesLabel,
y.format,
zAspect && zAspect.format,
zAspect && zAspect.title
);
});
});

return [...seriesMap.values()];
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@

import { uniq } from 'lodash';
import moment from 'moment';
import { Chart, FakeParams, DateHistogramParams, HistogramParams } from './point_series';
import { Chart } from './point_series';
import { Table } from '../../types';

export function initXAxis(chart: Chart, table: Table) {
const { format, title, params, accessor } = chart.aspects.x[0];

chart.xAxisOrderedValues =
accessor === -1
? [(params as FakeParams).defaultValue]
accessor === -1 && 'defaultValue' in params
? [params.defaultValue]
: uniq(table.rows.map(r => r[accessor]));
chart.xAxisFormat = format;
chart.xAxisLabel = title;

const { interval, date } = params as DateHistogramParams;
if (interval) {
if (date) {
const { intervalESUnit, intervalESValue } = params as DateHistogramParams;
if ('interval' in params) {
const { interval } = params;
if ('date' in params) {
const { intervalESUnit, intervalESValue } = params;
chart.ordered = {
interval: moment.duration(interval),
intervalESUnit,
intervalESValue,
};
} else {
chart.ordered = {
interval: (params as HistogramParams).interval,
interval: params.interval,
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@
* under the License.
*/

import { DateHistogramParams, OrderedChart } from './point_series';
import { OrderedChart } from './point_series';

export function orderedDateAxis(chart: OrderedChart) {
const x = chart.aspects.x[0];
const { bounds } = x.params as DateHistogramParams;
const bounds = 'bounds' in x.params ? x.params.bounds : undefined;

chart.ordered.date = true;

if (bounds) {
chart.ordered.min = isNaN(bounds.min as number)
? Date.parse(bounds.min as string)
: (bounds.min as number);
chart.ordered.max = isNaN(bounds.max as number)
? Date.parse(bounds.max as string)
: (bounds.max as number);
chart.ordered.min = typeof bounds.min === 'string' ? Date.parse(bounds.min) : bounds.min;
chart.ordered.max = typeof bounds.max === 'string' ? Date.parse(bounds.max) : bounds.max;
} else {
chart.ordered.endzones = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export const buildPointSeriesData = (table: Table, dimensions: Dimensions) => {
initXAxis(chart, table);
initYAxis(chart);

if ((chart.aspects.x[0].params as DateHistogramParams).date) {
if ('date' in chart.aspects.x[0].params) {
// initXAxis will turn `chart` into an `OrderedChart if it is a date axis`
orderedDateAxis(chart as OrderedChart);
}

Expand Down

0 comments on commit d7e5043

Please sign in to comment.