Spaces:
Paused
Paused
beweinreich
commited on
Commit
•
1ea315f
1
Parent(s):
47d890f
total weight in metric tonnes
Browse files- db/db_utils.py +1 -0
- rollups.py +13 -4
- run.py +3 -2
db/db_utils.py
CHANGED
@@ -78,6 +78,7 @@ def initialize_db(conn):
|
|
78 |
donations_discount REAL,
|
79 |
total_emissions_reduction_pre REAL,
|
80 |
total_emissions_reduction REAL,
|
|
|
81 |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
82 |
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
83 |
)
|
|
|
78 |
donations_discount REAL,
|
79 |
total_emissions_reduction_pre REAL,
|
80 |
total_emissions_reduction REAL,
|
81 |
+
total_weight_metric_tonnes REAL,
|
82 |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
83 |
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
84 |
)
|
rollups.py
CHANGED
@@ -74,21 +74,30 @@ for run_meta_row in run_meta_rows:
|
|
74 |
else:
|
75 |
total_emissions_reduction_pre = 0
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
# divide the donations_from by 2 because we attribute the emissions reductions to both the donor and the recipient
|
78 |
donations_discount = donations / 2
|
79 |
total_emissions_reduction = total_emissions_reduction_pre - donations_discount
|
80 |
|
81 |
# store this data in the rollups table
|
82 |
db_cursor.execute("""
|
83 |
-
INSERT INTO rollups (run_key, year, donations_discount, total_emissions_reduction_pre, total_emissions_reduction)
|
84 |
-
VALUES (%s, %s, %s, %s, %s)
|
85 |
ON CONFLICT (run_key)
|
86 |
DO UPDATE SET
|
87 |
year = EXCLUDED.year,
|
88 |
donations_discount = EXCLUDED.donations_discount,
|
89 |
total_emissions_reduction_pre = EXCLUDED.total_emissions_reduction_pre,
|
90 |
-
total_emissions_reduction = EXCLUDED.total_emissions_reduction
|
91 |
-
|
|
|
92 |
db_conn.commit()
|
93 |
|
94 |
db_cursor.close()
|
|
|
74 |
else:
|
75 |
total_emissions_reduction_pre = 0
|
76 |
|
77 |
+
# calculate total_weight
|
78 |
+
db_cursor.execute("SELECT sum(weight_metric_tonnes) FROM results WHERE run_key = %s", (run_key,))
|
79 |
+
weight_result = db_cursor.fetchone()
|
80 |
+
if weight_result and weight_result[0]:
|
81 |
+
total_weight_metric_tonnes = weight_result[0]
|
82 |
+
else:
|
83 |
+
total_weight_metric_tonnes = 0
|
84 |
+
|
85 |
# divide the donations_from by 2 because we attribute the emissions reductions to both the donor and the recipient
|
86 |
donations_discount = donations / 2
|
87 |
total_emissions_reduction = total_emissions_reduction_pre - donations_discount
|
88 |
|
89 |
# store this data in the rollups table
|
90 |
db_cursor.execute("""
|
91 |
+
INSERT INTO rollups (run_key, year, donations_discount, total_emissions_reduction_pre, total_emissions_reduction, total_weight_metric_tonnes)
|
92 |
+
VALUES (%s, %s, %s, %s, %s, %s)
|
93 |
ON CONFLICT (run_key)
|
94 |
DO UPDATE SET
|
95 |
year = EXCLUDED.year,
|
96 |
donations_discount = EXCLUDED.donations_discount,
|
97 |
total_emissions_reduction_pre = EXCLUDED.total_emissions_reduction_pre,
|
98 |
+
total_emissions_reduction = EXCLUDED.total_emissions_reduction,
|
99 |
+
total_weight_metric_tonnes = EXCLUDED.total_weight_metric_tonnes
|
100 |
+
""", (run_key, year, donations_discount, total_emissions_reduction_pre, total_emissions_reduction, total_weight_metric_tonnes, ))
|
101 |
db_conn.commit()
|
102 |
|
103 |
db_cursor.close()
|
run.py
CHANGED
@@ -17,8 +17,9 @@ if __name__ == "__main__":
|
|
17 |
# remove test.csv from raw_files
|
18 |
raw_files = [f for f in raw_files if f != 'test.csv']
|
19 |
|
20 |
-
# for raw_file_name in ['
|
21 |
-
for raw_file_name in
|
|
|
22 |
if not raw_file_name.endswith('.csv'):
|
23 |
continue
|
24 |
|
|
|
17 |
# remove test.csv from raw_files
|
18 |
raw_files = [f for f in raw_files if f != 'test.csv']
|
19 |
|
20 |
+
# for raw_file_name in ['sharing-excess-2020-raw-data.csv', 'sharing-excess-2021-raw-data.csv', 'sharing-excess-2022-raw-data.csv', 'sharing-excess-2023-raw-data.csv']:
|
21 |
+
for raw_file_name in ['spoonfuls-2023-Raw-Data.csv']:
|
22 |
+
# for raw_file_name in raw_files:
|
23 |
if not raw_file_name.endswith('.csv'):
|
24 |
continue
|
25 |
|