Skip to content

Commit

Permalink
move data series production to models
Browse files Browse the repository at this point in the history
  • Loading branch information
macfarlandian committed Jan 22, 2021
1 parent 03aaab0 commit 2b6a4e5
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,29 @@
// =============================================================================

import { startOfMonth } from "date-fns";
import { observer } from "mobx-react-lite";
import React from "react";
import { WindowedTimeSeries } from "../charts";
import { DataSeries } from "../charts/types";
import { HistoricalPopulationBreakdownRecord } from "../metricsApi";
import type HistoricalPopulationBreakdownMetric from "../contentModels/HistoricalPopulationBreakdownMetric";

type VizHistoricalPopulationBreakdownProps = {
data: DataSeries<HistoricalPopulationBreakdownRecord>[] | null;
error?: Error;
};

const VizHistoricalPopulationBreakdown: React.FC<VizHistoricalPopulationBreakdownProps> = ({
data,
error,
}) => {
const VizHistoricalPopulationBreakdown: React.FC<{
metric: HistoricalPopulationBreakdownMetric;
}> = ({ metric }) => {
// TODO(#278): implement filter UI to change this
const defaultRangeEnd = startOfMonth(new Date());

if (data)
if (metric.dataSeries)
return (
<WindowedTimeSeries
data={data}
data={metric.dataSeries}
setTimeRangeId={() => undefined}
defaultRangeEnd={defaultRangeEnd}
/>
);

if (error) throw error;
if (metric.error) throw metric.error;

return <div>loading...</div>;
};

export default VizHistoricalPopulationBreakdown;
export default observer(VizHistoricalPopulationBreakdown);

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

export { default } from "./VizHistoricalPopulationBreakdownContainer";
export { default } from "./VizHistoricalPopulationBreakdown";
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { DataSeries } from "../charts/types";
import {
DemographicsByCategoryRecord,
recordIsTotalByDimension,
Expand All @@ -33,4 +34,9 @@ export default class DemographicsByCategoryMetric extends Metric<
);
return recordsToReturn;
}

// eslint-disable-next-line class-methods-use-this
get dataSeries(): DataSeries<DemographicsByCategoryRecord>[] | null {
throw new Error("Method not implemented.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@
// =============================================================================

import { ascending } from "d3-array";
import { DataSeries } from "../charts/types";
import {
DEMOGRAPHIC_UNKNOWN,
DIMENSION_DATA_KEYS,
DIMENSION_MAPPINGS,
} from "../demographics";
import {
HistoricalPopulationBreakdownRecord,
recordIsTotalByDimension,
} from "../metricsApi";
import { colors } from "../UiLibrary";
import Metric from "./Metric";

export default class HistoricalPopulationBreakdownMetric extends Metric<
Expand Down Expand Up @@ -49,4 +56,33 @@ export default class HistoricalPopulationBreakdownMetric extends Metric<
);
return recordsToReturn;
}

get dataSeries(): DataSeries<HistoricalPopulationBreakdownRecord>[] | null {
const { records, demographicView } = this;
if (!records || demographicView === "nofilter") return null;

const labelsForDimension = DIMENSION_MAPPINGS.get(demographicView);
// this should never happen, it's really just a type safety measure.
// if it does, something has gone catastrophically wrong
if (!labelsForDimension)
throw new Error("Unsupported demographic view. Unable to provide data.");

return (
Array.from(labelsForDimension)
// don't need to include unknown in this data;
// they are minimal to nonexistent in historical data and make the legend confusing
.filter(([value]) => value !== DEMOGRAPHIC_UNKNOWN)
.map(([value, label], index) => ({
label,
color: colors.dataViz[index],
coordinates:
demographicView === "total"
? records
: records.filter(
(record) =>
record[DIMENSION_DATA_KEYS[demographicView]] === value
),
}))
);
}
}
4 changes: 4 additions & 0 deletions spotlight-client/src/contentModels/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
observable,
runInAction,
} from "mobx";
import { DataSeries } from "../charts/types";
import { ERROR_MESSAGES } from "../constants";
import { TenantId } from "../contentApi/types";
import {
Expand Down Expand Up @@ -81,6 +82,8 @@ export default abstract class Metric<RecordFormat extends MetricRecord> {

error?: Error;

abstract get dataSeries(): DataSeries<RecordFormat>[] | null;

// filter properties
localityId: RecordFormat extends LocalityFields ? string : undefined;

Expand All @@ -105,6 +108,7 @@ export default abstract class Metric<RecordFormat extends MetricRecord> {
populateAllRecords: action,
isLoading: observable,
records: computed,
dataSeries: computed,
});

// initialize metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { DataSeries } from "../charts/types";
import { PopulationBreakdownByLocationRecord } from "../metricsApi";
import Metric from "./Metric";

export default class PopulationBreakdownByLocationMetric extends Metric<
PopulationBreakdownByLocationRecord
> {}
> {
// eslint-disable-next-line class-methods-use-this
get dataSeries(): DataSeries<PopulationBreakdownByLocationRecord>[] | null {
throw new Error("Method not implemented.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { DataSeries } from "../charts/types";
import { ProgramParticipationCurrentRecord } from "../metricsApi";
import Metric from "./Metric";

export default class ProgramParticipationCurrentMetric extends Metric<
ProgramParticipationCurrentRecord
> {}
> {
// eslint-disable-next-line class-methods-use-this
get dataSeries(): DataSeries<ProgramParticipationCurrentRecord>[] | null {
throw new Error("Method not implemented.");
}
}
6 changes: 6 additions & 0 deletions spotlight-client/src/contentModels/RecidivismRateMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { DataSeries } from "../charts/types";
import { RecidivismRateRecord, recordIsTotalByDimension } from "../metricsApi";
import Metric from "./Metric";

Expand All @@ -28,4 +29,9 @@ export default class RecidivismRateMetric extends Metric<RecidivismRateRecord> {
);
return recordsToReturn;
}

// eslint-disable-next-line class-methods-use-this
get dataSeries(): DataSeries<RecidivismRateRecord>[] | null {
throw new Error("Method not implemented.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { DataSeries } from "../charts/types";
import {
recordIsTotalByDimension,
recordMatchesLocality,
Expand All @@ -38,4 +39,9 @@ export default class SentenceTypeByLocationMetric extends Metric<
);
return recordsToReturn;
}

// eslint-disable-next-line class-methods-use-this
get dataSeries(): DataSeries<SentenceTypeByLocationRecord>[] | null {
throw new Error("Method not implemented.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { DataSeries } from "../charts/types";
import {
recordIsTotalByDimension,
recordMatchesLocality,
Expand All @@ -38,4 +39,11 @@ export default class SupervisionSuccessRateDemographicsMetric extends Metric<
);
return recordsToReturn;
}

// eslint-disable-next-line class-methods-use-this
get dataSeries():
| DataSeries<SupervisionSuccessRateDemographicsRecord>[]
| null {
throw new Error("Method not implemented.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { DataSeries } from "../charts/types";
import {
recordMatchesLocality,
SupervisionSuccessRateMonthlyRecord,
Expand All @@ -34,4 +35,9 @@ export default class SupervisionSuccessRateMonthlyMetric extends Metric<

return recordsToReturn;
}

// eslint-disable-next-line class-methods-use-this
get dataSeries(): DataSeries<SupervisionSuccessRateMonthlyRecord>[] | null {
throw new Error("Method not implemented.");
}
}

0 comments on commit 2b6a4e5

Please sign in to comment.