Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/schema #417

Merged
merged 9 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/nodemon/analyze.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"config",
"package.json",
"postcss.config.js",
"schemas",
"src",
"stylelint.config.js",
"tsconfig.json",
Expand Down
1 change: 1 addition & 0 deletions config/nodemon/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"config",
"package.json",
"postcss.config.js",
"schemas",
"stylelint.config.js",
"tsconfig.json",
"tslint.json",
Expand Down
3 changes: 2 additions & 1 deletion config/nodemon/prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
".prettierrc",
"babel.config.js",
"config",
"infra/lambda-at-edge/modifyOutgoingHeaders.lambda.js",
"package.json",
"postcss.config.js",
"infra/lambda-at-edge/modifyOutgoingHeaders.lambda.js",
"schemas",
"src",
"static",
"stylelint.config.js",
Expand Down
5 changes: 5 additions & 0 deletions config/webpack/lib/webpackLoadJavascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,10 @@ export default function webpackLoadJavaScript({
},
],
},
{
test: /\.ya?ml$/,
type: 'json', // Required by Webpack v4
use: 'yaml-loader',
},
].filter(Boolean)
}
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
"license": "MIT",
"scripts": {
"clear": "node lib/clear.js",
"schema:totypes": "run-s -c schema:mkdir:typescript schema:totypes:typescript schema:mkdir:python schema:totypes:python",
"schema:totypes": "run-s -c schema:mkdir:typescript schema:clear:typescript schema:totypes:typescript schema:mkdir:python schema:clear:python schema:totypes:python",
"schema:mkdir:typescript": "mkdir -p 'src/.generated/types'",
"schema:mkdir:python": "mkdir -p 'data/generated_types'",
"schema:totypes:typescript": "quicktype --src-lang schema --lang typescript --converters all-objects --out src/.generated/types/types.ts schemas/*.yml",
"schema:totypes:python": "quicktype --src-lang schema --lang python --out data/generated_types/types.py schemas/*.yml",
"schema:clear:typescript": "rimraf 'src/.generated/types/{*,.*}'",
"schema:clear:python": "rimraf 'data/generated_types/{*,.*}'",
"dev": "nodemon --config config/nodemon/dev.json",
"dev:start": "yarn install && yarn schema:totypes && yarn dev:nowatch || cd .",
"dev:nowatch": "cross-env NODE_ENV=development BABEL_ENV=development webpack-dev-server --config config/webpack/webpack.client.babel.ts",
"dev:clean": "rimraf '.build/development/{*,.*}' '.generated/types/{*,.*}' 'node_modules/.cache'",
"dev:clean": "rimraf '.build/development/{*,.*}' 'src/.generated/types/{*,.*}' 'node_modules/.cache'",
"prod": "cross-env NODE_ENV=production BABEL_ENV=production webpack --config config/webpack/webpack.client.babel.ts",
"prod:clean": "rimraf '.build/production/{*,.*}' '.generated/types/{*,.*}' 'node_modules/.cache'",
"prod:clean": "rimraf '.build/production/{*,.*}' 'src/.generated/types/{*,.*}' 'node_modules/.cache'",
"prod:serve:nowatch": "babel-node --extensions \".ts\" ./src/server/server.ts",
"prod:serve": "nodemon --config config/nodemon/serve.json",
"prod:watch": "nodemon --config config/nodemon/prod.json",
Expand All @@ -40,7 +42,7 @@
"stylelint:watch": "nodemon --config config/nodemon/stylelint.json",
"stylelint:fix:watch": "nodemon --config config/nodemon/stylelint.fix.json",
"analyze": "cross-env ANALYZE=1 yarn prod",
"analyze:clean": "rimraf '.build/analyze/*' '.generated/types/{*,.*}' 'node_modules/.cache'",
"analyze:clean": "rimraf '.build/analyze/*' 'src/.generated/types/{*,.*}' 'node_modules/.cache'",
"analyze:watch": "nodemon --config config/nodemon/analyze.json",
"format": "prettier --check \"**/*.{js,jsx,ts,tsx,json,html,css,less,scss,yml,yaml}\"",
"format:fix": "yarn format --write",
Expand Down Expand Up @@ -279,7 +281,7 @@
"prettier": "2.0.1",
"promise-timeout": "1.3.0",
"purgecss-webpack-plugin": "2.1.0",
"quicktype-core": "6.0.56",
"quicktype": "15.0.223",
"react-dropzone": "10.2.1",
"react-refresh": "0.8.0",
"redux-devtools-extension": "2.13.8",
Expand Down Expand Up @@ -334,7 +336,8 @@
"webpack-cli": "3.3.11",
"webpack-dev-server": "3.10.3",
"webpack-hot-middleware": "2.25.0",
"webpackbar": "4.0.0"
"webpackbar": "4.0.0",
"yaml-loader": "0.6.0"
},
"resolutions": {
"**/babel-polyfill": "link:3rdparty/__empty-module",
Expand Down
40 changes: 22 additions & 18 deletions schemas/CaseCounts.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$schema: https://json-schema.org/draft-07/schema
$schema: http://json-schema.org/draft-07/schema

title: CaseCounts

Expand Down Expand Up @@ -31,37 +31,41 @@ definitions:
required:
- time
- cases
- deaths
- hospitalized
- icu
- recovered

properties:
time:
type: string
format: date-time
format: date

cases:
type: integer
multipleOf: 1
min: 0

deaths:
type: integer
multipleOf: 1
min: 0
anyOf:
- type: integer
multipleOf: 1
min: 0
- type: 'null'

hospitalized:
type: integer
multipleOf: 1
min: 0
anyOf:
- type: integer
multipleOf: 1
min: 0
- type: 'null'

icu:
type: integer
multipleOf: 1
min: 0
anyOf:
- type: integer
multipleOf: 1
min: 0
- type: 'null'

recovered:
type: integer
multipleOf: 1
min: 0
anyOf:
- type: integer
multipleOf: 1
min: 0
- type: 'null'
2 changes: 1 addition & 1 deletion schemas/CountryAgeDistribution.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$schema: https://json-schema.org/draft-07/schema
$schema: http://json-schema.org/draft-07/schema
$id: 'schemas/CountryAgeDistribution.yml'

title: CountryAgeDistribution
Expand Down
2 changes: 1 addition & 1 deletion schemas/Integer.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$schema: https://json-schema.org/draft-07/schema
$schema: http://json-schema.org/draft-07/schema

# This is necessary to avoid discrepancies in current implementations
# https://json-schema.org/understanding-json-schema/reference/numeric.html#id4
Expand Down
2 changes: 1 addition & 1 deletion schemas/IntegerPositive.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$schema: https://json-schema.org/draft-07/schema
$schema: http://json-schema.org/draft-07/schema

# TODO: replace with a ref to Integer
type: integer
Expand Down
10 changes: 7 additions & 3 deletions schemas/Scenarios.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
$schema: https://json-schema.org/draft-07/schema

$schema: http://json-schema.org/draft-07/schema
title: Scenario

type: array
Expand Down Expand Up @@ -48,6 +47,7 @@ definitions:
required:
- populationServed
- country
- cases
- suspectedCasesToday
- importsPerDay
- hospitalBeds
Expand All @@ -63,6 +63,10 @@ definitions:
type: string
minLength: 1

cases:
type: string
minLength: 1

suspectedCasesToday:
type: integer
multipleOf: 1
Expand Down Expand Up @@ -198,7 +202,7 @@ definitions:

color:
type: string
format: rgbcolor
pattern: ^#(?:[0-9a-fA-F]{3}){1,2}$

timeRange:
$ref: '#/definitions/DateRange'
Expand Down
2 changes: 1 addition & 1 deletion schemas/Severity.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$schema: https://json-schema.org/draft-07/schema
$schema: http://json-schema.org/draft-07/schema

title: Severity

Expand Down
68 changes: 34 additions & 34 deletions src/assets/data/case_counts.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@
{
"country": "CHE-Basel-Stadt",
"empiricalData": [
{ "time": "2020-02-27", "cases": 0, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-02-28", "cases": 0, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-02-29", "cases": 0, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-01", "cases": 1, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-02", "cases": 3, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-03", "cases": 3, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-04", "cases": 3, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-05", "cases": 8, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-06", "cases": 15, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-07", "cases": 21, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-08", "cases": 24, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-09", "cases": 28, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-10", "cases": 33, "deaths": null, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-11", "cases": 49, "deaths": null, "hospitalized": 13, "ICU": 2, "recovered": 4 },
{ "time": "2020-03-12", "cases": 73, "deaths": 1, "hospitalized": null, "ICU": null, "recovered": 4 },
{ "time": "2020-03-13", "cases": 92, "deaths": 1, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-14", "cases": 100, "deaths": 1, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-16", "cases": 144, "deaths": 4, "hospitalized": null, "ICU": null, "recovered": null },
{ "time": "2020-03-17", "cases": 165, "deaths": 4, "hospitalized": 30, "ICU": null, "recovered": 25 },
{ "time": "2020-03-18", "cases": 182, "deaths": 4, "hospitalized": 40, "ICU": null, "recovered": 36 },
{ "time": "2020-03-19", "cases": 222, "deaths": 4, "hospitalized": 40, "ICU": null, "recovered": 44 },
{ "time": "2020-03-20", "cases": 272, "deaths": 4, "hospitalized": 45, "ICU": null, "recovered": 46 },
{ "time": "2020-03-21", "cases": 299, "deaths": 5, "hospitalized": 46, "ICU": null, "recovered": 57 },
{ "time": "2020-03-22", "cases": 358, "deaths": 5, "hospitalized": 50, "ICU": null, "recovered": 73 },
{ "time": "2020-03-23", "cases": 376, "deaths": 5, "hospitalized": 56, "ICU": null, "recovered": 78 },
{ "time": "2020-03-24", "cases": 414, "deaths": 5, "hospitalized": 58, "ICU": null, "recovered": 105 },
{ "time": "2020-03-25", "cases": 466, "deaths": 8, "hospitalized": 58, "ICU": null, "recovered": 128 },
{ "time": "2020-03-26", "cases": 505, "deaths": 12, "hospitalized": 74, "ICU": 8, "recovered": 155 },
{ "time": "2020-03-27", "cases": 534, "deaths": 13, "hospitalized": 76, "ICU": 8, "recovered": 191 },
{ "time": "2020-03-28", "cases": 573, "deaths": 13, "hospitalized": 79, "ICU": 11, "recovered": 211 },
{ "time": "2020-03-29", "cases": 609, "deaths": 15, "hospitalized": 87, "ICU": 12, "recovered": 228 },
{ "time": "2020-03-30", "cases": 621, "deaths": 15, "hospitalized": 90, "ICU": 12, "recovered": 263 },
{ "time": "2020-03-31", "cases": 657, "deaths": 16, "hospitalized": 105, "ICU": 15, "recovered": 292 },
{ "time": "2020-04-01", "cases": 691, "deaths": 18, "hospitalized": 108, "ICU": 16, "recovered": 323 }
{ "time": "2020-02-27", "cases": 0, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-02-28", "cases": 0, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-02-29", "cases": 0, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-01", "cases": 1, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-02", "cases": 3, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-03", "cases": 3, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-04", "cases": 3, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-05", "cases": 8, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-06", "cases": 15, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-07", "cases": 21, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-08", "cases": 24, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-09", "cases": 28, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-10", "cases": 33, "deaths": null, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-11", "cases": 49, "deaths": null, "hospitalized": 13, "icu": 2, "recovered": 4 },
{ "time": "2020-03-12", "cases": 73, "deaths": 1, "hospitalized": null, "icu": null, "recovered": 4 },
{ "time": "2020-03-13", "cases": 92, "deaths": 1, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-14", "cases": 100, "deaths": 1, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-16", "cases": 144, "deaths": 4, "hospitalized": null, "icu": null, "recovered": null },
{ "time": "2020-03-17", "cases": 165, "deaths": 4, "hospitalized": 30, "icu": null, "recovered": 25 },
{ "time": "2020-03-18", "cases": 182, "deaths": 4, "hospitalized": 40, "icu": null, "recovered": 36 },
{ "time": "2020-03-19", "cases": 222, "deaths": 4, "hospitalized": 40, "icu": null, "recovered": 44 },
{ "time": "2020-03-20", "cases": 272, "deaths": 4, "hospitalized": 45, "icu": null, "recovered": 46 },
{ "time": "2020-03-21", "cases": 299, "deaths": 5, "hospitalized": 46, "icu": null, "recovered": 57 },
{ "time": "2020-03-22", "cases": 358, "deaths": 5, "hospitalized": 50, "icu": null, "recovered": 73 },
{ "time": "2020-03-23", "cases": 376, "deaths": 5, "hospitalized": 56, "icu": null, "recovered": 78 },
{ "time": "2020-03-24", "cases": 414, "deaths": 5, "hospitalized": 58, "icu": null, "recovered": 105 },
{ "time": "2020-03-25", "cases": 466, "deaths": 8, "hospitalized": 58, "icu": null, "recovered": 128 },
{ "time": "2020-03-26", "cases": 505, "deaths": 12, "hospitalized": 74, "icu": 8, "recovered": 155 },
{ "time": "2020-03-27", "cases": 534, "deaths": 13, "hospitalized": 76, "icu": 8, "recovered": 191 },
{ "time": "2020-03-28", "cases": 573, "deaths": 13, "hospitalized": 79, "icu": 11, "recovered": 211 },
{ "time": "2020-03-29", "cases": 609, "deaths": 15, "hospitalized": 87, "icu": 12, "recovered": 228 },
{ "time": "2020-03-30", "cases": 621, "deaths": 15, "hospitalized": 90, "icu": 12, "recovered": 263 },
{ "time": "2020-03-31", "cases": 657, "deaths": 16, "hospitalized": 105, "icu": 15, "recovered": 292 },
{ "time": "2020-04-01", "cases": 691, "deaths": 18, "hospitalized": 108, "icu": 16, "recovered": 323 }
]
}
]
14 changes: 2 additions & 12 deletions src/components/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { run, intervalsToTimeSeries } from '../../algorithms/run'
import LocalStorage, { LOCAL_STORAGE_KEYS } from '../../helpers/localStorage'

import severityData from '../../assets/data/severityData.json'

import countryCaseCountData from '../../assets/data/case_counts.json'
import { getCaseCountsData } from './state/caseCountsData'

import { schema } from './validation/schema'

Expand Down Expand Up @@ -57,12 +56,7 @@ async function runSimulation(
...params.containment,
}

if (params.population.cases !== 'none' && !isRegion(params.population.cases)) {
console.error(`The given confirmed cases region is invalid: ${params.population.cases}`)
return
}

const caseCounts: EmpiricalData = countryCaseCountData[params.population.cases] || []
const caseCounts = getCaseCountsData(params.population.cases)
const containment: TimeSeries = intervalsToTimeSeries(params.containment.mitigationIntervals)

intervalsToTimeSeries(params.containment.mitigationIntervals)
Expand All @@ -74,10 +68,6 @@ async function runSimulation(

const severityDefaults: SeverityTableRow[] = updateSeverityTable(severityData)

const isRegion = (region: string): region is keyof typeof countryCaseCountData => {
return Object.prototype.hasOwnProperty.call(countryCaseCountData, region)
}

function Main() {
const [result, setResult] = useState<AlgorithmResult | undefined>()
const [autorunSimulation, setAutorunSimulation] = useState(false)
Expand Down
6 changes: 4 additions & 2 deletions src/components/Main/Scenario/ScenarioCardPopulation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { AnyAction } from 'typescript-fsa'
import { useTranslation } from 'react-i18next'

import countryAgeDistribution from '../../../assets/data/country_age_distribution.json'
import countryCaseCounts from '../../../assets/data/case_counts.json'
import { caseCountsNames } from '../state/caseCountsData'

import { State, CUSTOM_COUNTRY_NAME } from '../state/state'

import { CardWithoutDropdown } from '../../Form/CardWithoutDropdown'
Expand All @@ -18,7 +19,8 @@ import { FormSpinBox } from '../../Form/FormSpinBox'
const countries = Object.keys(countryAgeDistribution)
const countryOptions = countries.map((country) => ({ value: country, label: country }))
countryOptions.push({ value: CUSTOM_COUNTRY_NAME, label: i18next.t(CUSTOM_COUNTRY_NAME) })
const caseCountOptions = Object.keys(countryCaseCounts).map((country) => ({ value: country, label: country }))

const caseCountOptions = caseCountsNames.map((country) => ({ value: country, label: country }))
caseCountOptions.push({ value: 'none', label: 'None' })

export interface ScenarioCardPopulationProps {
Expand Down
29 changes: 29 additions & 0 deletions src/components/Main/state/caseCountsData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import _ from 'lodash'
import Ajv from 'ajv'

import { CaseCounts, Convert } from '../../../.generated/types/types'

import allCaseCountsRaw from '../../../assets/data/case_counts.json'
import schema from '../../../../schemas/CaseCounts.yml'

function validate() {
const ajv = new Ajv()
const valid = ajv.validate(schema, allCaseCountsRaw)
if (!valid) {
console.error(ajv.errors)
throw new Error('caseCountData validation error')
}
return (allCaseCountsRaw as unknown) as CaseCounts[]
}

const allCaseCountsRawValidated = validate()

export const caseCountsNames = allCaseCountsRawValidated.map((cc) => cc.country)

export function getCaseCountsData(key: string) {
const caseCountsRaw = allCaseCountsRawValidated.find((cc) => cc.country === key)
if (!caseCountsRaw) {
throw new Error(`Error: case counts "${key}" not found in JSON`)
}
return Convert.toCaseCounts(JSON.stringify(caseCountsRaw)).empiricalData
}
Loading