Skip to content

Commit

Permalink
add netfile v2 data to database during import
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenglimEar committed Nov 3, 2023
1 parent f7cf802 commit 2e0387d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 10 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ do-import-spreadsheets:

import-data: 496 497 A-Contributions B1-Loans B2-Loans C-Contributions \
D-Expenditure E-Expenditure F-Expenses F461P5-Expenditure F465P3-Expenditure \
F496P3-Contributions G-Expenditure H-Loans I-Contributions Summary
F496P3-Contributions G-Expenditure H-Loans I-Contributions Summary elections_v2 committees_v2 a_contributions_v2
echo 'CREATE TABLE IF NOT EXISTS "calculations" (id SERIAL PRIMARY KEY, subject_id integer, subject_type varchar(30), name varchar(40), value jsonb);' | psql $(DATABASE_NAME)
./bin/remove_duplicate_transactions
./bin/make_view
Expand All @@ -120,6 +120,9 @@ reindex:
496 497 A-Contributions B1-Loans B2-Loans C-Contributions D-Expenditure E-Expenditure F-Expenses F461P5-Expenditure F465P3-Expenditure F496P3-Contributions G-Expenditure H-Loans I-Contributions Summary:
DATABASE_NAME=$(DATABASE_NAME) ./bin/import-file $(CSV_PATH) $@

elections_v2 committees_v2 a_contributions_v2:
DATABASE_NAME=$(DATABASE_NAME) ./bin/import-file $(CSV_PATH) $@ 0

downloads/csv/candidates.csv:
mkdir -p downloads/csv downloads/raw
$(WGET) -O- \
Expand Down
2 changes: 2 additions & 0 deletions bin/clean
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ cat <<-QUERY | psql ${database_name}
DELETE FROM "$table_name"
WHERE "Tran_Date" is NULL;
QUERY
else
echo
fi
16 changes: 10 additions & 6 deletions bin/import-file
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# Contains logic to import files regardless of how many there are.
# If there's no file, don't do anything with the database.
# If the table already exists in the database, don't try to re-create it.
# The fix_pending parameter defaults to 1 if not set. Set it to 0
# to skip the section that fixes the pending Filer_ID
#
# Usage:
# bin/import-file [csv_path] [table]
# bin/import-file downloads/csv A-Contributions
# bin/import-file [csv_path] [table] [fix_pending]
# bin/import-file downloads/csv A-Contributions 1
set -euo pipefail

if [ -z "${DATABASE_NAME:-""}" ]; then
Expand All @@ -14,12 +16,13 @@ if [ -z "${DATABASE_NAME:-""}" ]; then
fi

if [ $# -eq 0 ]; then
echo "Usage: bin/import-file [csv_path] [table]"
echo "Usage: bin/import-file [csv_path] [table] [fix_pending]"
exit 1
fi

csv_path=$1
table_name=$2
fix_pending=${3:-1}
filename_glob=$csv_path'/*'${table_name}'.csv'
table_exists=
if psql disclosure-backend -c '\d "'${table_name}'"' >/dev/null 2>&1; then
Expand All @@ -45,9 +48,10 @@ if ls $filename_glob 2>/dev/null >/dev/null; then
csvsql --db postgresql:///$DATABASE_NAME --tables $table_name --insert --no-inference ${table_exists:+--no-create}
echo -n ' Removing empty Tran_Date... '
./bin/clean "$DATABASE_NAME" "$table_name"
echo
echo -n ' Fixing pending Filer_IDs... '
./bin/fix-pending "$DATABASE_NAME" "$table_name"
if [ "$fix_pending" = "1" ]; then
echo -n ' Fixing pending Filer_IDs... '
./bin/fix-pending "$DATABASE_NAME" "$table_name"
fi
else
echo 'Found no files to import'
fi
Empty file added dbschema/a_contributions_v2.sql
Empty file.
15 changes: 15 additions & 0 deletions dbschema/committees_v2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE committees_v2 (
filer_nid DECIMAL NOT NULL,
"Ballot_Measure_Election" VARCHAR,
"Filer_ID" VARCHAR,
"Filer_NamL" VARCHAR NOT NULL,
"_Status" VARCHAR NOT NULL,
"_Committee_Type" VARCHAR NOT NULL,
"Ballot_Measure" VARCHAR,
"Support_Or_Oppose" VARCHAR,
candidate_controlled_id BOOLEAN,
"Start_Date" DATE,
"End_Date" BOOLEAN,
data_warning BOOLEAN,
"Make_Active" BOOLEAN
);
6 changes: 6 additions & 0 deletions dbschema/elections_v2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE elections_v2 (
location VARCHAR NOT NULL,
date DATE NOT NULL,
name VARCHAR NOT NULL,
title VARCHAR NOT NULL
);
6 changes: 3 additions & 3 deletions download/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def main():
'XRef_Match',
]).sample(n=20))

elections.df.to_csv('.local/elections.csv', index=False)
committees.df.to_csv('.local/committees.csv', index=False)
a_contributions.df.to_csv('.local/a_contributions.csv', index=False)
elections.df.to_csv('downloads/csv/elections_v2.csv', index=False)
committees.df.to_csv('downloads/csv/committees_v2.csv', index=False)
a_contributions.df.to_csv('downloads/csv/a_contributions_v2.csv', index=False)

'''
with engine.connect() as conn:
Expand Down

0 comments on commit 2e0387d

Please sign in to comment.