Spaces:
Paused
Paused
Commit
•
f7839d7
1
Parent(s):
59e569d
rollups working
Browse files- db/db_utils.py +3 -8
- rollups.py +28 -8
db/db_utils.py
CHANGED
@@ -54,13 +54,8 @@ def initialize_db(conn):
|
|
54 |
)
|
55 |
''')
|
56 |
cursor.execute('''
|
57 |
-
CREATE TABLE IF NOT EXISTS
|
58 |
-
|
59 |
-
run_row INTEGER,
|
60 |
-
year TEXT,
|
61 |
-
from TEXT,
|
62 |
-
to TEXT,
|
63 |
-
total_emissions_reduction REAL,
|
64 |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
65 |
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
66 |
)
|
@@ -110,7 +105,7 @@ def initialize_db(conn):
|
|
110 |
CREATE INDEX IF NOT EXISTS idx_dictionary_word ON mappings(dictionary_word);
|
111 |
CREATE INDEX IF NOT EXISTS idx_description ON dictionary(description);
|
112 |
CREATE UNIQUE INDEX IF NOT EXISTS run_row_run_key_uniq ON results(run_key text_ops,run_row int4_ops);
|
113 |
-
CREATE UNIQUE INDEX IF NOT EXISTS
|
114 |
''')
|
115 |
|
116 |
conn.commit()
|
|
|
54 |
)
|
55 |
''')
|
56 |
cursor.execute('''
|
57 |
+
CREATE TABLE IF NOT EXISTS donors (
|
58 |
+
name TEXT,
|
|
|
|
|
|
|
|
|
|
|
59 |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
60 |
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
61 |
)
|
|
|
105 |
CREATE INDEX IF NOT EXISTS idx_dictionary_word ON mappings(dictionary_word);
|
106 |
CREATE INDEX IF NOT EXISTS idx_description ON dictionary(description);
|
107 |
CREATE UNIQUE INDEX IF NOT EXISTS run_row_run_key_uniq ON results(run_key text_ops,run_row int4_ops);
|
108 |
+
CREATE UNIQUE INDEX IF NOT EXISTS rollups_run_key_key ON rollups(run_key text_ops);
|
109 |
''')
|
110 |
|
111 |
conn.commit()
|
rollups.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from db_utils import get_connection
|
2 |
import re
|
3 |
|
4 |
# Function to extract the year
|
@@ -15,6 +15,7 @@ db_cursor = db_conn.cursor()
|
|
15 |
db_cursor.execute("SELECT name from donors")
|
16 |
donor_results = db_cursor.fetchall()
|
17 |
list_of_valid_donors = [row[0] for row in donor_results]
|
|
|
18 |
|
19 |
|
20 |
db_cursor.execute("SELECT distinct(run_key) FROM results")
|
@@ -30,10 +31,12 @@ for row in result_rows:
|
|
30 |
year = extract_year(run_key)
|
31 |
run_keys_and_years.append((run_key, year))
|
32 |
|
|
|
33 |
|
34 |
for run_key, year in run_keys_and_years:
|
35 |
donations_to = 0
|
36 |
donations_from = 0
|
|
|
37 |
|
38 |
# find all run_keys for the same year
|
39 |
run_keys_in_year = [run_key for run_key, y in run_keys_and_years if y == year]
|
@@ -44,17 +47,19 @@ for run_key, year in run_keys_and_years:
|
|
44 |
if result:
|
45 |
donations_from = result[0]
|
46 |
|
47 |
-
# divide the donations_from by 2 because we attribute the emissions reductions to both the donor and the recipient
|
48 |
-
donations_from = donations_from / 2
|
49 |
-
|
50 |
# find all donations to the current donor
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
result = db_cursor.fetchone()
|
53 |
if result:
|
54 |
donations_to = result[0]
|
55 |
|
56 |
-
# divide the donations_to by 2 because we attribute the emissions reductions to both the donor and the recipient
|
57 |
-
donations_to = donations_to / 2
|
58 |
|
59 |
# calculate the total_donations
|
60 |
donations = donations_to + donations_from
|
@@ -67,7 +72,22 @@ for run_key, year in run_keys_and_years:
|
|
67 |
else:
|
68 |
total_emissions_reduction_pre = 0
|
69 |
|
|
|
|
|
70 |
total_emissions_reduction = total_emissions_reduction_pre - donations
|
71 |
|
72 |
# store this data in the rollups table
|
73 |
-
db_cursor.execute("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from db.db_utils import get_connection
|
2 |
import re
|
3 |
|
4 |
# Function to extract the year
|
|
|
15 |
db_cursor.execute("SELECT name from donors")
|
16 |
donor_results = db_cursor.fetchall()
|
17 |
list_of_valid_donors = [row[0] for row in donor_results]
|
18 |
+
list_of_valid_donors_lower = [donor.lower() for donor in list_of_valid_donors]
|
19 |
|
20 |
|
21 |
db_cursor.execute("SELECT distinct(run_key) FROM results")
|
|
|
31 |
year = extract_year(run_key)
|
32 |
run_keys_and_years.append((run_key, year))
|
33 |
|
34 |
+
print(run_keys_and_years)
|
35 |
|
36 |
for run_key, year in run_keys_and_years:
|
37 |
donations_to = 0
|
38 |
donations_from = 0
|
39 |
+
print(f"Processing run_key: {run_key} and year: {year}")
|
40 |
|
41 |
# find all run_keys for the same year
|
42 |
run_keys_in_year = [run_key for run_key, y in run_keys_and_years if y == year]
|
|
|
47 |
if result:
|
48 |
donations_from = result[0]
|
49 |
|
|
|
|
|
|
|
50 |
# find all donations to the current donor
|
51 |
+
query = """
|
52 |
+
SELECT sum(total_emissions_reduction)
|
53 |
+
FROM results
|
54 |
+
WHERE run_key = %s
|
55 |
+
AND LOWER(TRIM(donor)) = ANY(%s::text[])
|
56 |
+
"""
|
57 |
+
|
58 |
+
db_cursor.execute(query, (run_key, list_of_valid_donors_lower))
|
59 |
result = db_cursor.fetchone()
|
60 |
if result:
|
61 |
donations_to = result[0]
|
62 |
|
|
|
|
|
63 |
|
64 |
# calculate the total_donations
|
65 |
donations = donations_to + donations_from
|
|
|
72 |
else:
|
73 |
total_emissions_reduction_pre = 0
|
74 |
|
75 |
+
# divide the donations_from by 2 because we attribute the emissions reductions to both the donor and the recipient
|
76 |
+
donations = donations / 2
|
77 |
total_emissions_reduction = total_emissions_reduction_pre - donations
|
78 |
|
79 |
# store this data in the rollups table
|
80 |
+
db_cursor.execute("""
|
81 |
+
INSERT INTO rollups (run_key, year, donations_discount, total_emissions_reduction_pre, total_emissions_reduction)
|
82 |
+
VALUES (%s, %s, %s, %s, %s)
|
83 |
+
ON CONFLICT (run_key)
|
84 |
+
DO UPDATE SET
|
85 |
+
year = EXCLUDED.year,
|
86 |
+
donations_discount = EXCLUDED.donations_discount,
|
87 |
+
total_emissions_reduction_pre = EXCLUDED.total_emissions_reduction_pre,
|
88 |
+
total_emissions_reduction = EXCLUDED.total_emissions_reduction
|
89 |
+
""", (run_key, year, donations_discount, total_emissions_reduction_pre, total_emissions_reduction))
|
90 |
+
db_conn.commit()
|
91 |
+
|
92 |
+
db_cursor.close()
|
93 |
+
db_conn.close()
|