-
Notifications
You must be signed in to change notification settings - Fork 2
i97 Insert observations grouped first by network #176
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
Open
QSparks
wants to merge
15
commits into
master
Choose a base branch
from
i97
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3adbbbc
Group observations by network before calling insert
QSparks 2836f1f
Pass black format test
QSparks bf14643
Add cached function to retrieve network from var_id
QSparks 1156669
Move groupby network into insert.py
QSparks c9b75eb
Add tests for insert from multiple networks
QSparks 6f3bbcf
Remove comments and print statements
QSparks 19510bd
Remove unused imports, fix capitalization
QSparks 0600e89
Simplify network tests and include cached_function decorator
QSparks b6c6884
move cached_function def up a level
QSparks 6c668a6
Move log helpers from __init__.py to seperate module
QSparks f6c0b4f
Move log helpers from __init__.py to seperate module
QSparks 97d8307
Rename log_helpers module to db_helpers
QSparks 5afa9a9
Rename log_helpers module to db_helpers
QSparks a4e9232
Log per-network db_metrics in insert.py
QSparks 15ddf05
Format insert for network log
QSparks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import logging | ||
| from types import SimpleNamespace | ||
|
|
||
|
|
||
| def sanitize_connection(sesh): | ||
| return sesh.bind.url.render_as_string(hide_password=True) | ||
|
|
||
|
|
||
| def cached_function(attrs): | ||
| """A decorator factory that can be used to cache database results | ||
| Neither database sessions (i.e. the sesh parameter of each wrapped | ||
| function) nor SQLAlchemy mapped objects (the results of queries) are | ||
| cachable or reusable. Therefore one cannot memoize database query | ||
| functions using builtin things like the lrucache. | ||
| This wrapper works, by a) assuming that the wrapped function's first | ||
| argument is a database session b) assuming that the result of the | ||
| query returns a single SQLAlchemy object (e.g. a History instance), | ||
| and c) accepting as a parameter a list of attributes to retrieve and | ||
| store in the cache result. | ||
| args (except sesh) and kwargs to the wrapped function are used as | ||
| the cache key, and results are the parametrized object attributes. | ||
| """ | ||
| log = logging.getLogger(__name__) | ||
|
|
||
| def wrapper(f): | ||
| cache = {} | ||
|
|
||
| def memoize(sesh, *args, **kwargs): | ||
| nonlocal cache | ||
| key = (args) + tuple(kwargs.items()) | ||
| if key not in cache: | ||
| obj = f(sesh, *args, **kwargs) | ||
| log.debug( | ||
| f"Cache miss: {f.__name__} {key} -> {repr(obj)}", | ||
| extra={"database": sanitize_connection(sesh)}, | ||
| ) | ||
| cache[key] = obj and SimpleNamespace( | ||
| **{attr: getattr(obj, attr) for attr in attrs} | ||
| ) | ||
| return cache[key] | ||
|
|
||
| return memoize | ||
|
|
||
| return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.