Skip to content

Commit

Permalink
Merge pull request #25 from statgen/status
Browse files Browse the repository at this point in the history
Data collection counts at status endpoint
  • Loading branch information
grosscol committed Dec 7, 2023
2 parents 24f3917 + 81cc6f5 commit 0fceac2
Show file tree
Hide file tree
Showing 9 changed files with 2,171 additions and 560 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ venv/bin/flask load-genes \

venv/bin/flask load-snv 2 data/basis/vcfs/*.vcf.gz

venv/bin/flask load-qc-metrics
venv/bin/flask load-qc-metrics \
data/basis/qc_metrics/metrics.json.gz
```

Expand Down
5 changes: 3 additions & 2 deletions bravo_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from logging.config import dictConfig
from os import getenv
from os import getenv, getcwd
from flask import Flask
from flask_cors import CORS
from flask_pymongo import PyMongo
Expand Down Expand Up @@ -43,14 +43,15 @@ def version():


def create_app(test_config=None):
instance_path = getenv('BRAVO_API_INSTANCE_DIR', None)
instance_path = getenv('BRAVO_API_INSTANCE_DIR', getcwd())
app = Flask(__name__,
instance_relative_config=True,
instance_path=instance_path)

app.version = pkg_resources.read_text(__package__, 'VERSION').strip()

if test_config is None:
print(getcwd())
app.config.from_object('bravo_api.default_config')
app.config.from_envvar('BRAVO_API_CONFIG_FILE', silent=True)
else:
Expand Down
27 changes: 25 additions & 2 deletions bravo_api/blueprints/status/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
from flask import Blueprint, Response, current_app, jsonify, make_response


bp = Blueprint('status', __name__)
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -38,11 +39,33 @@ def usage() -> Response:
result = current_app.cache.get('usage')
if result is None:
result = usage_stats(current_app.mmongo.db)
current_app.cache.set('usage', result, timeout=3600)
logger.debug("Usage result updated")
current_app.cache.set('usage', result, timeout=0)
return make_response(jsonify(result))


@bp.route('/counts', methods=['GET'])
def counts() -> Response:
result = current_app.cache.get('counts')
if result is None:
snv_count = count_collection(current_app.mmongo.db.snv)
transcript_count = count_collection(current_app.mmongo.db.transcripts)
gene_count = count_collection(current_app.mmongo.db.genes)

result = {'snvs': snv_count, 'transcripts': transcript_count, 'genes': gene_count}

current_app.cache.set('counts', result, timeout=3600)
logger.debug('variant counts updated')
return make_response(jsonify(result))


def count_collection(collection: pymongo.collection.Collection) -> int:
"""
Count (estimate) the number of snv in backing database
"""
result = collection.estimated_document_count()
return result


def usage_stats(db: pymongo.database.Database) -> dict:
"""
Given a mongo database, run queries to compile statistics about user usage of API.
Expand Down
3 changes: 2 additions & 1 deletion bravo_api/blueprints/structvar/structvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def sv_region(structvars: pymongo.collection.Collection,
]}
]}
]}
}
},
{'$project': {'_id': 0}}
]

cursor = structvars.aggregate(pipeline)
Expand Down

0 comments on commit 0fceac2

Please sign in to comment.