Skip to content

Commit

Permalink
Add db seed restore status check (#2917)
Browse files Browse the repository at this point in the history
* git push originAdd db seed restore status to health check

Add route to expose db seed status

* Lint
  • Loading branch information
dmanjunath committed Apr 14, 2022
1 parent 210120b commit fded9cf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
33 changes: 33 additions & 0 deletions discovery-provider/src/queries/get_db_seed_restore_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sqlalchemy
from src.utils.config import shared_config
from src.utils.db_session import get_db_read_replica

env = shared_config["discprov"]["env"]

# environment to db file md5 hash lookup, these are constants and will not change
env_to_hash_lookup = {"prod": "c785fc03cfc9cca3e8a48226b9cd424b"}


def get_db_seed_restore_status():
db = get_db_read_replica()
with db.scoped_session() as session:
db_seed_restore_status_query = sqlalchemy.text(
"""
SELECT md5(cast(array_agg(sorted_hashes."str" ORDER BY sorted_hashes."str" ASC) as text)) FROM (
SELECT (CAST("user_id" AS VARCHAR) || CAST("blocknumber" AS VARCHAR) || "txhash") as "str"
FROM "users"
WHERE "updated_at" BETWEEN '09-01-2021' AND '02-01-2022'
ORDER BY "user_id" ASC, "blocknumber" ASC, "txhash" ASC
) as sorted_hashes;
"""
)
db_hash = session.execute(db_seed_restore_status_query).fetchone()[0]

env_hash = env_to_hash_lookup.get(env, None)
has_restored = False
seed_hash = db_hash

if env_hash and env_hash == db_hash:
has_restored = True

return has_restored, seed_hash
8 changes: 8 additions & 0 deletions discovery-provider/src/queries/health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from flask import Blueprint, request
from src.api_helpers import success_response
from src.queries.get_alembic_version import get_alembic_version
from src.queries.get_db_seed_restore_status import get_db_seed_restore_status
from src.queries.get_health import get_health, get_latest_ipld_indexed_block
from src.queries.get_latest_play import get_latest_play
from src.queries.get_sol_plays import get_latest_sol_play_check_info
Expand Down Expand Up @@ -131,3 +132,10 @@ def ipld_block_check():
def ip_check():
ip = helpers.get_ip(request)
return success_response(ip, sign_response=False)


@bp.route("/db_seed_restore_check", methods=["GET"])
def db_seed_restore_check():
has_restored, seed_hash = get_db_seed_restore_status()
response = {"has_restored": has_restored, "seed_hash": seed_hash}
return success_response(response, sign_response=False)

0 comments on commit fded9cf

Please sign in to comment.