Skip to content

Commit

Permalink
move filter behavior to relevant subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
macfarlandian committed Jan 15, 2021
1 parent 671efbb commit 28a5b58
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 38 deletions.
17 changes: 15 additions & 2 deletions spotlight-client/src/contentModels/DemographicsByCategoryMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { DemographicsByCategoryRecord } from "../metricsApi";
import {
DemographicsByCategoryRecord,
recordIsTotalByDimension,
} from "../metricsApi";
import Metric from "./Metric";

export default class DemographicsByCategoryMetric extends Metric<
DemographicsByCategoryRecord
> {}
> {
get records(): DemographicsByCategoryRecord[] | undefined {
let recordsToReturn = this.getOrFetchRecords();
if (!recordsToReturn) return undefined;

recordsToReturn = recordsToReturn.filter(
recordIsTotalByDimension(this.demographicView)
);
return recordsToReturn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { HistoricalPopulationBreakdownRecord } from "../metricsApi";
import {
HistoricalPopulationBreakdownRecord,
recordIsTotalByDimension,
} from "../metricsApi";
import Metric from "./Metric";

export default class HistoricalPopulationBreakdownMetric extends Metric<
HistoricalPopulationBreakdownRecord
> {}
> {
get records(): HistoricalPopulationBreakdownRecord[] | undefined {
let recordsToReturn = this.getOrFetchRecords();
if (!recordsToReturn) return undefined;

recordsToReturn = recordsToReturn.filter(
recordIsTotalByDimension(this.demographicView)
);
return recordsToReturn;
}
}
26 changes: 6 additions & 20 deletions spotlight-client/src/contentModels/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import {
DemographicFields,
DemographicView,
LocalityFields,
recordIsTotalByDimension,
recordMatchesLocality,
} from "../metricsApi";
import { MetricRecord, CollectionMap } from "./types";

Expand Down Expand Up @@ -149,25 +147,13 @@ export default abstract class Metric<RecordFormat extends MetricRecord> {
* Returns fetched, transformed, and (optionally) filtered data for this metric.
* Will automatically initiate a fetch if necessary.
*/
get records(): RecordFormat[] | undefined {
let recordsToReturn = this.allRecords;
if (recordsToReturn) {
if (this.localityId) {
recordsToReturn = recordsToReturn.filter(
// TS can't seem to resolve this conditional type even after the conditional
recordMatchesLocality(this.localityId as string)
);
}

if (this.demographicView) {
recordsToReturn = recordsToReturn.filter(
// TS can't seem to resolve this conditional type even after the conditional
recordIsTotalByDimension(this.demographicView as DemographicView)
);
}
return recordsToReturn;
}
protected getOrFetchRecords(): RecordFormat[] | undefined {
if (this.allRecords) return this.allRecords;
if (!this.isLoading || !this.error) this.fetch();
return undefined;
}

get records(): RecordFormat[] | undefined {
return this.getOrFetchRecords();
}
}
16 changes: 12 additions & 4 deletions spotlight-client/src/contentModels/RecidivismRateMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

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

export default class RecidivismRateMetric extends Metric<
RecidivismRateRecord
> {}
export default class RecidivismRateMetric extends Metric<RecidivismRateRecord> {
get records(): RecidivismRateRecord[] | undefined {
let recordsToReturn = this.getOrFetchRecords();
if (!recordsToReturn) return undefined;

recordsToReturn = recordsToReturn.filter(
recordIsTotalByDimension(this.demographicView)
);
return recordsToReturn;
}
}
22 changes: 20 additions & 2 deletions spotlight-client/src/contentModels/SentenceTypeByLocationMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,27 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { SentenceTypeByLocationRecord } from "../metricsApi";
import {
recordIsTotalByDimension,
recordMatchesLocality,
SentenceTypeByLocationRecord,
} from "../metricsApi";
import Metric from "./Metric";

export default class SentenceTypeByLocationMetric extends Metric<
SentenceTypeByLocationRecord
> {}
> {
get records(): SentenceTypeByLocationRecord[] | undefined {
let recordsToReturn = this.getOrFetchRecords();
if (!recordsToReturn) return undefined;

recordsToReturn = recordsToReturn.filter(
recordMatchesLocality(this.localityId)
);

recordsToReturn = recordsToReturn.filter(
recordIsTotalByDimension(this.demographicView)
);
return recordsToReturn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,27 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { SupervisionSuccessRateDemographicsRecord } from "../metricsApi";
import {
recordIsTotalByDimension,
recordMatchesLocality,
SupervisionSuccessRateDemographicsRecord,
} from "../metricsApi";
import Metric from "./Metric";

export default class SupervisionSuccessRateDemographicsMetric extends Metric<
SupervisionSuccessRateDemographicsRecord
> {}
> {
get records(): SupervisionSuccessRateDemographicsRecord[] | undefined {
let recordsToReturn = this.getOrFetchRecords();
if (!recordsToReturn) return undefined;

recordsToReturn = recordsToReturn.filter(
recordMatchesLocality(this.localityId)
);

recordsToReturn = recordsToReturn.filter(
recordIsTotalByDimension(this.demographicView)
);
return recordsToReturn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,23 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { SupervisionSuccessRateMonthlyRecord } from "../metricsApi";
import {
recordMatchesLocality,
SupervisionSuccessRateMonthlyRecord,
} from "../metricsApi";
import Metric from "./Metric";

export default class SupervisionSuccessRateMonthlyMetric extends Metric<
SupervisionSuccessRateMonthlyRecord
> {}
> {
get records(): SupervisionSuccessRateMonthlyRecord[] | undefined {
let recordsToReturn = this.getOrFetchRecords();
if (!recordsToReturn) return undefined;

recordsToReturn = recordsToReturn.filter(
recordMatchesLocality(this.localityId)
);

return recordsToReturn;
}
}
6 changes: 2 additions & 4 deletions spotlight-client/src/metricsApi/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ export const NOFILTER_KEY: NoFilterIdentifier = "nofilter";
*/
export function recordIsTotalByDimension(
demographicView: DemographicView
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): (record: Record<string, any>) => boolean {
): (record: DemographicFields) => boolean {
if (demographicView === NOFILTER_KEY) return () => true;

const keysEnum = { ...DIMENSION_DATA_KEYS };
Expand Down Expand Up @@ -135,8 +134,7 @@ export function recordIsTotalByDimension(
*/
export function recordMatchesLocality(
locality: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): (record: Record<string, any>) => boolean {
): (record: LocalityFields) => boolean {
if (locality === NOFILTER_KEY) return () => true;
return (record) => record.locality === locality;
}

0 comments on commit 28a5b58

Please sign in to comment.