Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Store stats in mongo atlas #1

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import os
import requests
import datetime
from urllib.parse import quote_plus
from pymongo import MongoClient

ISSUES = {
Expand All @@ -37,7 +38,7 @@
epoch = datetime.datetime.utcfromtimestamp(0)


def get_events(event_name, token=None, limit=None):
def get_events(event_name, token=None, limit=None, mongo_user=None, mongo_password=None):
"""Retrieve events."""

token = token or os.getenv("SENTRY_TOKEN", None)
Expand All @@ -48,7 +49,13 @@ def get_events(event_name, token=None, limit=None):
issue_id = ISSUES[event_name]

# Initiate session
db_client = MongoClient()
db_client = (
MongoClient() if mongo_password is None
else MongoClient(
f"mongodb+srv://{quote_plus(mongo_user)}:{quote_plus(mongo_password)}"
"@sentrybackup.sywajav.mongodb.net/?retryWrites=true&w=majority"
)
)
Comment on lines +52 to +58
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than calling MongoClient twice, what about having host default to "localhost" and then updating it if user credentials are present?

db = db_client.fmriprep_stats
url = f"https://sentry.io/api/0/issues/{issue_id}/events/?query="
counter = 0
Expand Down
10 changes: 8 additions & 2 deletions src/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ def cli():

@cli.command()
@click.option("-l", "--limit", type=click.IntRange(min=1), default=None)
def get(limit):
@click.option("--mongo-user", envvar="MONGO_USERNAME", type=str, default=None)
@click.password_option(
"--mongo-password",
envvar="MONGO_PASSWORD",
default=None,
)
def get(limit, mongo_password, mongo_user):
from api import get_events

# Get events
for event in ("started", "success", "failed"):
get_events(event, limit=limit)
get_events(event, limit=limit, mongo_password=mongo_password, mongo_user=mongo_user)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this function is called multiple times while initializing the database connection - I think it would be cleaner to isolate the client initialization within its own function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I hadn't thought of this. This is almost certainly a problem also in current main.



if __name__ == "__main__":
Expand Down