-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client function for new metrics endpoint (#268)
- Loading branch information
1 parent
df00860
commit 6b5b8b6
Showing
13 changed files
with
220 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
REACT_APP_AUTH_ENABLED={FLAG} | ||
REACT_APP_AUTH_ENV={ENV_NAME} | ||
REACT_APP_API_URL=http://localhost:3001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const { resolve } = require("path"); | ||
const { spawn } = require("child_process"); | ||
|
||
module.exports = async () => { | ||
// start the API test server. save reference so we can kill it in teardown | ||
global.TEST_SERVER = spawn("yarn", ["start-test-server"], { | ||
cwd: resolve(__dirname, "../spotlight-api"), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = async () => { | ||
// kill the test server we spawned during global setup | ||
global.TEST_SERVER.kill(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Recidiviz - a data platform for criminal justice reform | ||
// Copyright (C) 2020 Recidiviz, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// ============================================================================= | ||
|
||
import fetchMetrics from "./fetchMetrics"; | ||
import { waitForTestServer } from "./testUtils"; | ||
|
||
test("returns fetched metrics", async () => { | ||
await waitForTestServer(); | ||
// these are arbitrarily chosen to spot-check the API, nothing special about them | ||
const metricNames = [ | ||
"active_program_participation_by_region", | ||
"supervision_success_by_month", | ||
]; | ||
|
||
const tenantId = "US_ND"; | ||
|
||
const response = await fetchMetrics({ | ||
metricNames, | ||
tenantId, | ||
}); | ||
|
||
expect(Object.keys(response)).toEqual(metricNames); | ||
|
||
// these are records chosen at random from the spotlight-api data fixtures | ||
expect(response.active_program_participation_by_region).toEqual( | ||
expect.arrayContaining([ | ||
{ | ||
supervision_type: "PAROLE", | ||
participation_count: "21", | ||
state_code: "US_ND", | ||
region_id: "6", | ||
race_or_ethnicity: "ALL", | ||
}, | ||
]) | ||
); | ||
expect(response.supervision_success_by_month).toEqual( | ||
expect.arrayContaining([ | ||
{ | ||
state_code: "US_ND", | ||
projected_year: "2019", | ||
projected_month: "8", | ||
district: "SOUTH_CENTRAL", | ||
supervision_type: "PROBATION", | ||
successful_termination_count: "43", | ||
projected_completion_count: "95", | ||
success_rate: 0.45263157894736844, | ||
}, | ||
]) | ||
); | ||
}); | ||
|
||
test("handles error responses", async () => { | ||
expect.assertions(2); | ||
|
||
await waitForTestServer(); | ||
|
||
try { | ||
await fetchMetrics({ | ||
metricNames: ["this_file_does_not_exist"], | ||
tenantId: "US_ND", | ||
}); | ||
} catch (e) { | ||
expect(e.message).toMatch("500"); | ||
expect(e.message).toMatch("not registered"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Recidiviz - a data platform for criminal justice reform | ||
// Copyright (C) 2020 Recidiviz, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// ============================================================================= | ||
|
||
import { TenantId } from "./contentApi/types"; | ||
|
||
/** | ||
* All data comes back from the server as string values; | ||
* it will be up to us to cast those strings to other types as needed | ||
*/ | ||
type RawMetricData = Record<string, string>[]; | ||
|
||
type MetricsApiResponse = Record<string, RawMetricData | null>; | ||
type ErrorAPIResponse = { error: string }; | ||
|
||
type FetchMetricOptions = { | ||
metricNames: string[]; | ||
tenantId: TenantId; | ||
}; | ||
|
||
/** | ||
* Retrieves the metric data provided for this application in the `/spotlight-api` package. | ||
*/ | ||
export default async function fetchMetrics({ | ||
metricNames, | ||
tenantId, | ||
}: FetchMetricOptions): Promise<MetricsApiResponse> { | ||
const response = await fetch( | ||
`${process.env.REACT_APP_API_URL}/api/${tenantId}/public`, | ||
{ | ||
body: JSON.stringify({ | ||
metrics: metricNames, | ||
}), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
method: "POST", | ||
} | ||
); | ||
|
||
if (response.ok) { | ||
const responseData: MetricsApiResponse = await response.json(); | ||
return responseData; | ||
} | ||
|
||
const errorResponse: ErrorAPIResponse = await response.json(); | ||
throw new Error( | ||
`Metrics API responded with status ${response.status}. Error message: ${ | ||
errorResponse.error || "none" | ||
}` | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Recidiviz - a data platform for criminal justice reform | ||
// Copyright (C) 2020 Recidiviz, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// ============================================================================= | ||
|
||
import waitForLocalhost from "wait-for-localhost"; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export function waitForTestServer(): Promise<void> { | ||
return waitForLocalhost({ path: "/health", port: 3002 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters