-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'staging' into 10525-story
- Loading branch information
Showing
8 changed files
with
579 additions
and
162 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
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); | ||
}); | ||
}); |
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,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 }); | ||
}; |
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,150 @@ | ||
#!/usr/bin/env npx ts-node --transpile-only | ||
|
||
// usage: npx ts-node --transpile-only scripts/reports/cases-closed-in-year.ts 2024 [-f] | ||
|
||
import { CASE_STATUS_TYPES } from '@shared/business/entities/EntityConstants'; | ||
import { DateTime } from 'luxon'; | ||
import { | ||
ServerApplicationContext, | ||
createApplicationContext, | ||
} from '@web-api/applicationContext'; | ||
import { generateCsv } from '../helpers/generate-csv'; | ||
import { pick } from 'lodash'; | ||
import { searchAll } from '@web-api/persistence/elasticsearch/searchClient'; | ||
import { validateDateAndCreateISO } from '@shared/business/utilities/DateHandler'; | ||
|
||
const year = process.argv[2] || `${DateTime.now().toObject().year}`; | ||
const fiscal = | ||
!!process.argv[3] && | ||
(process.argv[3] === '-f' || process.argv[3] === '--fiscal'); | ||
const OUTPUT_DIR = `${process.env.HOME}/Documents`; | ||
const CLOSED_STATUSES: string[] = [ | ||
CASE_STATUS_TYPES.closed, | ||
CASE_STATUS_TYPES.closedDismissed, | ||
]; | ||
const BEGIN = validateDateAndCreateISO({ | ||
day: '1', | ||
month: fiscal ? '10' : '1', | ||
year: fiscal ? `${Number(year) - 1}` : year, | ||
})!; | ||
const END = validateDateAndCreateISO({ | ||
day: '1', | ||
month: fiscal ? '10' : '1', | ||
year: fiscal ? year : `${Number(year) + 1}`, | ||
})!; | ||
|
||
const getAllCasesClosedInFiscalYear = async ({ | ||
applicationContext, | ||
}: { | ||
applicationContext: ServerApplicationContext; | ||
}): Promise<RawCase[]> => { | ||
const { results }: { results: RawCase[] } = await searchAll({ | ||
applicationContext, | ||
searchParameters: { | ||
body: { | ||
query: { | ||
bool: { | ||
must: [ | ||
{ | ||
term: { | ||
'entityName.S': 'Case', | ||
}, | ||
}, | ||
{ | ||
terms: { | ||
'caseStatusHistory.L.M.updatedCaseStatus.S': CLOSED_STATUSES, | ||
}, | ||
}, | ||
{ | ||
range: { | ||
'caseStatusHistory.L.M.date.S': { | ||
gte: BEGIN, | ||
lt: END, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
sort: [{ 'sortableDocketNumber.N': 'asc' }], | ||
}, | ||
index: 'efcms-case', | ||
}, | ||
}); | ||
console.log( | ||
`Found ${results.length} cases with a "closed" status history record and` + | ||
` a status history record generated in fiscal year ${year}`, | ||
); | ||
return results.filter(c => wasClosedThisFiscalYear(c)); | ||
}; | ||
|
||
const wasClosedThisFiscalYear = (c: RawCase): boolean => { | ||
let closedThisFiscalYear = false; | ||
for (const csh of c.caseStatusHistory) { | ||
if ( | ||
csh.date && | ||
csh.date >= BEGIN && | ||
csh.date < END && | ||
csh.updatedCaseStatus && | ||
CLOSED_STATUSES.includes(csh.updatedCaseStatus) | ||
) { | ||
closedThisFiscalYear = true; | ||
break; | ||
} | ||
} | ||
return closedThisFiscalYear; | ||
}; | ||
|
||
const outputCsv = ({ | ||
casesClosedInYear, | ||
}: { | ||
casesClosedInYear: RawCase[]; | ||
}): void => { | ||
const filename = `${OUTPUT_DIR}/cases-closed-in-${fiscal ? 'fy-' : ''}${year}.csv`; | ||
const columns = [ | ||
{ header: 'Docket Number', key: 'docketNumber' }, | ||
{ header: 'Case Title', key: 'caption' }, | ||
{ header: 'Judge', key: 'judge' }, | ||
{ header: 'Case Status', key: 'status' }, | ||
{ header: 'Closed Date', key: 'closedDateHumanized' }, | ||
]; | ||
const rows = casesClosedInYear.map(c => { | ||
const judge = | ||
c.associatedJudge | ||
?.replace('Chief Special Trial ', '') | ||
.replace('Special Trial ', '') | ||
.replace('Judge ', '') || ''; | ||
const closedDateHumanized = | ||
c.caseStatusHistory | ||
.reverse() | ||
.find( | ||
csh => | ||
CLOSED_STATUSES.includes(csh.updatedCaseStatus) && | ||
csh.date >= BEGIN && | ||
csh.date < END, | ||
) | ||
?.date.split('T')[0] || ''; | ||
const caption = c.caseCaption.replace(/\r\n|\r|\n/g, ' ').trim(); | ||
return { | ||
...pick(c, ['docketNumber', 'status']), | ||
caption, | ||
closedDateHumanized, | ||
judge, | ||
}; | ||
}); | ||
generateCsv({ columns, filename, rows }); | ||
console.log(`Generated ${filename}`); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
(async () => { | ||
const applicationContext = createApplicationContext({}); | ||
const casesClosedInYear = await getAllCasesClosedInFiscalYear({ | ||
applicationContext, | ||
}); | ||
console.log( | ||
`Filtered results to ${casesClosedInYear.length} cases having a "closed"` + | ||
` status history record that was generated in fiscal year ${year}`, | ||
); | ||
outputCsv({ casesClosedInYear }); | ||
})(); |
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
Oops, something went wrong.