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

OpEx: recreate the geocoded-all-cases.csv file for QGIS #5576

Draft
wants to merge 11 commits into
base: staging
Choose a base branch
from
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@
"ts-node": "10.9.2",
"tsconfig-paths": "4.2.0",
"typescript": "5.6.3",
"us-census-geocoder": "1.1.0",
"us-state-converter": "1.0.8",
"utf8": "3.0.0"
}
}
91 changes: 91 additions & 0 deletions scripts/helpers/generate-csv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { generateCsv } from './generate-csv';
import fs from 'fs';

const exists = jest.spyOn(fs, 'existsSync').mockImplementation(jest.fn());
const unlink = jest.spyOn(fs, 'unlinkSync').mockImplementation(jest.fn());
const append = jest.spyOn(fs, 'appendFileSync').mockImplementation(jest.fn());

const MOCK_COLUMNS = [
{ header: 'Droid name', key: 'name' },
{ header: 'Droid type', key: 'type' },
{ header: 'Alliance', key: 'alliance' },
];
const MOCK_ROWS = [
{
alliance: 'Rebellion',
name: 'C-3PO',
restrained: true,
type: 'Protocol',
},
{
alliance: 'Rebellion',
name: 'R2-D2',
restrained: false,
type: 'Astromech',
},
{
alliance: 'Rebellion',
name: 'C1-10P',
restrained: false,
type: 'Astromech',
},
{
alliance: 'Empire',
name: 'IG-88',
restrained: false,
type: 'Assassin',
},
{
name: 'MSE-6',
restrained: true,
type: 'Mouse',
},
];
const MOCK_FILENAME = `${process.env.HOME}/tmp/jest.csv`;
const MOCK_CONTENTS =
'"Droid name","Droid type","Alliance"' +
'\n"C-3PO","Protocol","Rebellion"' +
'\n"R2-D2","Astromech","Rebellion"' +
'\n"C1-10P","Astromech","Rebellion"' +
'\n"IG-88","Assassin","Empire"' +
'\n"MSE-6","Mouse",""';

describe('generateCsv', () => {
beforeEach(() => {
exists.mockReturnValue(true);
});

it('deletes the specified output file if it already exists', () => {
generateCsv({
columns: MOCK_COLUMNS,
filename: MOCK_FILENAME,
rows: MOCK_ROWS,
});

expect(unlink).toHaveBeenCalled();
expect(append).toHaveBeenCalled();
});

it('does not attempt to delete the specified output file if it does not already exist', () => {
exists.mockReturnValueOnce(false);

generateCsv({
columns: MOCK_COLUMNS,
filename: MOCK_FILENAME,
rows: MOCK_ROWS,
});

expect(unlink).not.toHaveBeenCalled();
expect(append).toHaveBeenCalled();
});

it('compiles an array of objects into a CSV with the given columns', () => {
generateCsv({
columns: MOCK_COLUMNS,
filename: MOCK_FILENAME,
rows: MOCK_ROWS,
});

expect(append).toHaveBeenCalledWith(MOCK_FILENAME, MOCK_CONTENTS);
});
});
48 changes: 48 additions & 0 deletions scripts/helpers/generate-csv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { appendFileSync, existsSync, unlinkSync } from 'fs';

const compileOutput = ({
columns,
rows,
}: {
columns: { header: string; key: string }[];
rows: { [k: string]: any }[];
}): string => {
const headers = columns.map(c => c.header);
const keys = columns.map(c => c.key);
let output = `"${headers.join('","')}"`;
for (const row of rows) {
const values: string[] = [];
for (const key of keys) {
const value = row[key] || '';
values.push(`${value}`);
}
output += `\n"${values.join('","')}"`;
}
return output;
};

const writeFile = ({
contents,
filename,
}: {
contents: string;
filename: string;
}): void => {
if (existsSync(filename)) {
unlinkSync(filename);
}
appendFileSync(filename, contents);
};

export const generateCsv = ({
columns,
filename,
rows,
}: {
columns: { header: string; key: string }[];
filename: string;
rows: { [k: string]: any }[];
}): void => {
const contents = compileOutput({ columns, rows });
writeFile({ contents, filename });
};
1 change: 1 addition & 0 deletions scripts/helpers/geocoded-locations.json

Large diffs are not rendered by default.

44 changes: 20 additions & 24 deletions scripts/reports/closed-dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ServerApplicationContext,
createApplicationContext,
} from '@web-api/applicationContext';
import { appendFileSync } from 'fs';
import { generateCsv } from '../helpers/generate-csv';
import { searchAll } from '@web-api/persistence/elasticsearch/searchClient';
import { validateDateAndCreateISO } from '@shared/business/utilities/DateHandler';

Expand Down Expand Up @@ -58,33 +58,29 @@ const getAllCasesOpenedInYear = async ({
return results;
};

const outputCsv = ({
casesOpenedInYear,
filename,
}: {
casesOpenedInYear: RawCase[];
filename: string;
}): void => {
let output =
'"Docket Number","Date Created","Date Closed","Case Title",' +
'"Case Status","Case Type"';
for (const c of casesOpenedInYear) {
const rcvdAtHumanized = c.receivedAt.split('T')[0];
const closedHumanized = c.closedDate?.split('T')[0] || '';
output +=
`\n"${c.docketNumber}","${rcvdAtHumanized}","${closedHumanized}",` +
`"${c.caseCaption}","${c.status}","${c.caseType}"`;
}
appendFileSync(`${OUTPUT_DIR}/${filename}`, output);
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
const applicationContext = createApplicationContext({});
const casesOpenedInYear = await getAllCasesOpenedInYear({
applicationContext,
});
const filename = `closed-dates-of-cases-opened-in-${year}.csv`;
outputCsv({ casesOpenedInYear, filename });
console.log(`Generated ${OUTPUT_DIR}/${filename}`);
const filename = `${OUTPUT_DIR}/closed-dates-of-cases-opened-in-${year}.csv`;
const columns = [
{ header: 'Docket Number', key: 'docketNumber' },
{ header: 'Date Created', key: 'rcvdAtHumanized' },
{ header: 'Date Closed', key: 'closedHumanized' },
{ header: 'Case Title', key: 'caseCaption' },
{ header: 'Case Status', key: 'status' },
{ header: 'Case Type', key: 'caseType' },
];
const rows = casesOpenedInYear.map(c => ({
caseCaption: c.caseCaption,
caseType: c.caseType,
closedHumanized: c.closedDate?.split('T')[0] || '',
docketNumber: c.docketNumber,
rcvdAtHumanized: c.receivedAt.split('T')[0],
status: c.status,
}));
generateCsv({ columns, filename, rows });
console.log(`Generated ${filename}`);
})();
Loading
Loading