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

Change the webserver to initalize the database lazily #675

Open
wants to merge 1 commit into
base: random-forest-mode-detection
Choose a base branch
from
Open
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
6 changes: 1 addition & 5 deletions emission/analysis/section_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from sklearn.cluster import DBSCAN

# Our imports
from emission.core.get_database import get_section_db, get_mode_db, get_routeCluster_db,get_transit_db
import emission.core.get_database as edb
from emission.core.common import calDistance, Include_place_2
from emission.analysis.modelling.tour_model.trajectory_matching.route_matching import getRoute,fullMatchDistance,matchTransitRoutes,matchTransitStops
import emission.storage.timeseries.abstract_timeseries as esta
Expand All @@ -25,10 +25,6 @@

from uuid import UUID

Sections = get_section_db()
Modes = get_mode_db()


# The speed is in m/s
def calOverallSectionSpeed(section):
distanceDelta = section.distance
Expand Down
10 changes: 6 additions & 4 deletions emission/core/get_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

config_data = json.load(config_file)
url = config_data["timeseries"]["url"]

print("Connecting to database URL "+url)
_current_db = MongoClient(url).Stage_database
#config_file.close()
_current_db = None

def _get_current_db():
global _current_db
if (_current_db is None):
print("Connecting to database URL "+url)
_current_db = MongoClient(url).Stage_database
#config_file.close()
return _current_db

def get_mode_db():
Expand Down
20 changes: 13 additions & 7 deletions emission/storage/timeseries/builtin_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@

import emission.core.wrapper.entry as ecwe

ts_enum_map = {
esta.EntryType.DATA_TYPE: edb.get_timeseries_db(),
esta.EntryType.ANALYSIS_TYPE: edb.get_analysis_timeseries_db()
}
ts_enum_map = None

def get_ts_enum_map():
global ts_enum_map
if (ts_enum_map is None):
ts_enum_map = {
esta.EntryType.DATA_TYPE: edb.get_timeseries_db(),
esta.EntryType.ANALYSIS_TYPE: edb.get_analysis_timeseries_db()
}
return ts_enum_map

INVALID_QUERY = {'metadata.key': 'invalid'}

Expand All @@ -28,8 +34,8 @@ def __init__(self, user_id):
self.key_query = lambda key: {"metadata.key": key}
self.type_query = lambda entry_type: {"metadata.type": entry_type}
self.user_query = {"user_id": self.user_id} # UUID is mandatory for this version
self.timeseries_db = ts_enum_map[esta.EntryType.DATA_TYPE]
self.analysis_timeseries_db = ts_enum_map[esta.EntryType.ANALYSIS_TYPE]
self.timeseries_db = get_ts_enum_map()[esta.EntryType.DATA_TYPE]
self.analysis_timeseries_db = get_ts_enum_map()[esta.EntryType.ANALYSIS_TYPE]
# Design question: Should the stats be a separate database, or should it be part
# of the timeseries database? Technically, it should be part of the timeseries
# database. However, I am concerned about the performance of the database
Expand Down Expand Up @@ -322,7 +328,7 @@ def bulk_insert(self, entries, data_type = None):
else:
multi_result = None
try:
multi_result = ts_enum_map[data_type].insert_many(entries, ordered=False)
multi_result = get_ts_enum_map()[data_type].insert_many(entries, ordered=False)
logging.debug("Returning multi_result.inserted_ids = %s... of length %d" %
(multi_result.inserted_ids[:10], len(multi_result.inserted_ids)))
return multi_result
Expand Down