Skip to content
Open
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
92 changes: 42 additions & 50 deletions util/query.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,89 @@
const where = {
import { Query, CreateQueryParams } from "./types";

const WHERE = {
confirmed: `(Confirmed > 0)`,
deaths: `(Deaths > 0)`,
recovered: `(Recovered <> 0)`,
all: `1=1`
};

export const createQuery = ({ where }) => ({
export const createQuery = (queryParams: CreateQueryParams): Query => ({
f: "json",
outFields: "*",
returnGeometry: false,
where
...queryParams,
...(queryParams.field != undefined && {
outStatistics: `[{"statisticType":"sum","onStatisticField":"${queryParams.field}","outStatisticFieldName":"value"}]`
})
});

export const withCountryRegion = (where: string, countryRegion?: string) =>
export const withCountryRegion = (where: string, countryRegion?: string): string =>
countryRegion ? `${where} AND (Country_Region='${countryRegion}')` : where;

export const createArrayQuery = ({ where, orderByFields }) => ({
...createQuery({ where }),
orderByFields
});

export const queryConfirmed = (
countryRegion?: string,
shouldUseProvinceState?: boolean
) =>
createArrayQuery({
): Query =>
createQuery({
where: countryRegion
? withCountryRegion(where.confirmed, countryRegion)
: where.confirmed,
orderByFields: `Confirmed desc, Country_Region asc${
shouldUseProvinceState ? ",Province_State asc" : ""
}`
? withCountryRegion(WHERE.confirmed, countryRegion)
: WHERE.confirmed,
orderByFields: `Confirmed desc, Country_Region asc${shouldUseProvinceState ? ",Province_State asc" : ""
}`
});

export const queryDeaths = (
countryRegion?: string,
shouldUseProvinceState?: boolean
) =>
createArrayQuery({
): Query =>
createQuery({
where: countryRegion
? withCountryRegion(where.deaths, countryRegion)
: where.deaths,
orderByFields: `Deaths desc, Country_Region asc${
shouldUseProvinceState ? ",Province_State asc" : ""
}`
? withCountryRegion(WHERE.deaths, countryRegion)
: WHERE.deaths,
orderByFields: `Deaths desc, Country_Region asc${shouldUseProvinceState ? ",Province_State asc" : ""
}`
});

export const queryRecovered = (
countryRegion?: string,
shouldUseProvinceState?: boolean
) =>
createArrayQuery({
): Query =>
createQuery({
where: countryRegion
? withCountryRegion(where.recovered, countryRegion)
: where.recovered,
orderByFields: `Recovered desc, Country_Region asc${
shouldUseProvinceState ? ",Province_State asc" : ""
}`
? withCountryRegion(WHERE.recovered, countryRegion)
: WHERE.recovered,
orderByFields: `Recovered desc, Country_Region asc${shouldUseProvinceState ? ",Province_State asc" : ""
}`
});

export const queryLastUpdate = (countryRegion?: string) => ({
...createArrayQuery({
where: withCountryRegion(where.confirmed, countryRegion),
...createQuery({
where: withCountryRegion(WHERE.confirmed, countryRegion),
orderByFields: "Last_Update desc"
}),
resultRecordCount: 1
});

export const createTotalQuery = ({ where, field }) => ({
...createQuery({ where }),
outStatistics: `[{"statisticType":"sum","onStatisticField":"${field}","outStatisticFieldName":"value"}]`
});

export const queryTotalConfirmed = (countryRegion?: string) =>
createTotalQuery({
where: withCountryRegion(where.confirmed, countryRegion),
export const queryTotalConfirmed = (countryRegion?: string): Query =>
createQuery({
where: withCountryRegion(WHERE.confirmed, countryRegion),
field: "Confirmed"
});

export const queryTotalDeaths = (countryRegion?: string) =>
createTotalQuery({
where: withCountryRegion(where.deaths, countryRegion),
export const queryTotalDeaths = (countryRegion?: string): Query =>
createQuery({
where: withCountryRegion(WHERE.deaths, countryRegion),
field: "Deaths"
});

export const queryTotalRecovered = (countryRegion?: string) =>
createTotalQuery({
where: withCountryRegion(where.recovered, countryRegion),
field: "Recovered"
export const queryTotalRecovered = (countryRegion?: string): Query =>
createQuery({
where: withCountryRegion(WHERE.recovered, countryRegion),
field: "Recovered",
});

export const queryCasesTimeSeries = () =>
createArrayQuery({
where: where.all,
export const queryCasesTimeSeries = (): Query =>
createQuery({
where: WHERE.all,
orderByFields: "Report_Date_String asc"
});
14 changes: 14 additions & 0 deletions util/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface Query {
f: string
outFields: string
returnGeometry: boolean
where: string
orderByFields?: string
outStatistics?: string
}

export interface CreateQueryParams {
where: string,
orderByFields?: string
field?: string
}