Skip to content

Commit

Permalink
Merge branch '50-dashboard-v2' into 51-dashboard-compress
Browse files Browse the repository at this point in the history
  • Loading branch information
GISRedeDev committed Jan 20, 2025
2 parents 418b1eb + 34e54e7 commit 883bd47
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
49 changes: 30 additions & 19 deletions scripts/reinsert_all_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import pycountry
import numpy as np
from contextlib import contextmanager

sys.path.append(str(Path(__file__).resolve().parent.parent / "api"))

Expand Down Expand Up @@ -30,6 +31,21 @@
engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)


@contextmanager
def session_scope():
"""Provide a transactional scope around a series of operations."""
session = Session()
try:
yield session
session.commit()
except Exception as e:
session.rollback()
raise
finally:
session.close()


def delete_all_rows(table_name):
session = Session()
try:
Expand All @@ -50,34 +66,29 @@ def get_country_name(row):
return row['gid_0']


def upload_csv_to_indicators(csv_path, table_model):
session = Session()
try:
df = pd.read_csv(csv_path)
df['date'] = pd.to_datetime(df['date'], format="%Y-%m").dt.strftime('%Y-%m')
df['country'] = df.apply(get_country_name, axis=1)
df = df.replace({np.nan: None, 'NaN': None, 'nan': None, 'null': None, 'NULL': None, 'None': None})
data = df.to_dict(orient='records')
session.bulk_insert_mappings(table_model, data)
session.commit()
except Exception as e:
print(f"Error uploading data to {table_model.__tablename__}: {e}")
session.rollback()
finally:
session.close()
def upload_csv_to_indicators(csv_path, table_model, session):
df = pd.read_csv(csv_path)
df['date'] = pd.to_datetime(df['date'], format="%Y-%m").dt.strftime('%Y-%m')
df['country'] = df.apply(get_country_name, axis=1)
df = df.replace({np.nan: None, 'NaN': None, 'nan': None, 'null': None, 'NULL': None, 'None': None})
data = df.to_dict(orient='records')
session.bulk_insert_mappings(table_model, data)


def main():
tables = {"subnational": "subnational_indicators", "national": "national_indicators"}
for level, table in tables.items():
try:
csv = next(x for x in BASE.joinpath(level).iterdir() if x.suffix == ".csv")
delete_all_rows(table)
model = SubNationalIndicators if level == "subnational" else NationalIndicators
upload_csv_to_indicators(csv, model)
print(f"Uploaded {level} data to {table} table.")
with session_scope() as session:
delete_all_rows(table)
model = SubNationalIndicators if level == "subnational" else NationalIndicators
upload_csv_to_indicators(csv, model, session)
print(f"Uploaded {level} data to {table} table.")
except StopIteration:
print(f"No csv found for {level} data.")
except Exception as e:
print(f"Error uploading {level} data: {e}")


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions www/public_html/js/plotxyLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ var min = (Math.min(...dataArray)),
data: dataArray,
pointRadius: pointRadius,
segment: {
borderColor: ctx => skipped(ctx, 'rgb(0, 0, 0)'),
borderDash: ctx => skipped(ctx, [4, 4]),
borderColor: ctx => skipped(ctx, 'rgb(255, 99, 132)'),
//borderDash: ctx => skipped(ctx, [4, 4]),
},
spanGaps: true
}]
Expand Down

0 comments on commit 883bd47

Please sign in to comment.