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

Full data dump export #312

Closed
wants to merge 2 commits into from
Closed
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 src/backend/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function createAppConstants() {
cityLevelPastWeekGeneralResultsKey: 'city_level_past_week_general_results.json',
postalCodeLevelGeneralResultsKey: 'postalcode_level_general_results.json',
dailyTotalsKey: 'daily_totals.json',
responsesFullKey: 'responses_full.json',
};
}

Expand Down
1 change: 1 addition & 0 deletions src/backend/appMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function createMockApp(overrides: Partial<App> = {}): App {
cityLevelPastWeekGeneralResultsKey: 'city_level_past_week_general_results.json',
postalCodeLevelGeneralResultsKey: 'postalcode_level_general_results.json',
dailyTotalsKey: 'daily_totals.json',
responsesFullKey: 'responses_full.json',
};

const mockApp: App = {
Expand Down
12 changes: 11 additions & 1 deletion src/backend/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
fetchPostalCodeLevelGeneralResults,
pushPostalCodeLevelGeneralResults,
} from './dataExports/postalCodeLevelGeneralResults';
import { fetchResponses, pushResponses } from './dataExports/responses';

const writeFile = promisify(fs.writeFile);

Expand Down Expand Up @@ -46,12 +47,17 @@ const dataExportHandlers: { [K in keyof AppConstants]?: DataExportHandler } = {
push: pushPostalCodeLevelGeneralResults,
},
[app.constants.dailyTotalsKey]: { fetch: fetchDailyTotals, push: pushDailyTotals },
[app.constants.responsesFullKey]: {
fetch: fetchResponses,
push: pushResponses,
},
};

interface CommonArgs {
env: 'dev' | 'prod';
}

// prettier-ignore
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
yargs
.command(
Expand Down Expand Up @@ -95,7 +101,11 @@ yargs
process.exit(1);
}
},
).argv;
)
.strict()
.demandCommand()
.help()
.argv;

interface DumpArgs extends CommonArgs {
filename: string;
Expand Down
1 change: 1 addition & 0 deletions src/backend/dataExports/openDataIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const openDataConstantKeys: Array<keyof AppConstants> = [
'cityLevelPastWeekGeneralResultsKey',
'postalCodeLevelGeneralResultsKey',
'dailyTotalsKey',
'responsesFullKey',
'lowPopulationPostalCodesKey',
'populationPerCityKey',
'postalCodeAreasKey',
Expand Down
117 changes: 117 additions & 0 deletions src/backend/dataExports/responses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { App, s3PutJsonHelper } from '../app';
import { LowPopulationPostalCodes, PostalCodeCityMappings } from '../../common/model';

export async function exportResponses(app: App) {
const responses = await fetchResponses(app);

await pushResponses(app, responses);
}

export async function fetchResponses(app: App) {
const responsesResult = await queryResponses(app);

const postalCodeCityMappings = await app.s3Sources.fetchPostalCodeCityMappings();
const lowPopulationPostalCodes = await app.s3Sources.fetchLowPopulationPostalCodes();

const responses = mapResponses(responsesResult.Items, postalCodeCityMappings, lowPopulationPostalCodes);

return responses;
}

interface ResponsesQuery {
response_id: string;
timestamp: string;
participant_id: string;
app_version: string;
country_code: string;
fever: string;
cough: string;
breathing_difficulties: string;
muscle_pain: string;
headache: string;
sore_throat: string;
rhinitis: string;
stomach_issues: string;
sensory_issues: string;
healthcare_contact: string;
general_wellbeing: string;
longterm_medication: string;
smoking: string;
corona_suspicion: string;
age_group: string;
gender: string;
postal_code: string;
duration: string;
abuse_score: string;
}

export const responsesQuery = `
SELECT *
FROM
responses
WHERE
country_code = 'FI'
OR
country_code = ''
ORDER BY timestamp ASC
`;

export async function queryResponses(app: App) {
return app.athenaClient.query<ResponsesQuery>({
sql: responsesQuery,
db: app.constants.athenaDb,
});
}

export function mapResponses(
responses: ResponsesQuery[],
postalCodeCityMappings: PostalCodeCityMappings,
lowPopulationPostalCodes: LowPopulationPostalCodes,
) {
// TODO: Map and filter the responses
const outputs = [];

for (const response of responses) {
// Normalize postal code
const postal_code = lowPopulationPostalCodes.data[response.postal_code] || response.postal_code;

// Filter by valid postal areas
// TODO: Use the new `postalcode_areas.json
if (postal_code in postalCodeCityMappings.data) {
// Filter keys
const { response_id, app_version, abuse_score, ...output } = response;

outputs.push({
...output,
postal_code,
// Timestamps to be shown to the hour
timestamp: `${response.timestamp.slice(0, 13)}:00:00.000Z`,
// Two age groups, _under 50_ and _over 50_
age_group: Number(response.age_group) < 50 ? 'under50' : 'over50',
// Two genders, male and female. Other genders coalesce to male
gender: response.gender === 'female' ? 'female' : 'male',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there is already a discussion for this decision that I was not aware of, but out of curiosity, why not keep other gender as other?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of any discussion on this (which doesn't mean that such doesn't exist), but I'll just refer you the issue this PR is implementing: #231

Sukupuoli kahtena luokkana (mies, nainen. Muu yhdistetään mies-luokkaan)

Translated:

Gender in two categories (male, female. Other is combined with male-category)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh :( Looks like that is something defined by tietosuoja requirements.

duration: Number(response.duration) || 0,
});
}
}

return outputs;
}

type Responses = ReturnType<typeof mapResponses>;

export async function pushResponses(app: App, responses: Responses) {
await s3PutJsonHelper(app.s3Client, {
Bucket: app.constants.openDataBucket,
// TODO:
Key: app.constants.responsesFullKey,
Body: {
meta: {
description: 'TODO',
generated: new Date().toISOString(),
link: `https://${app.constants.domainName}/${app.constants.responsesFullKey}`,
},
data: responses,
},
});
}