Source code for hyperstream.channels.sphere_channel

# The MIT License (MIT) # Copyright (c) 2014-2017 University of Bristol
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in all
#  copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
#  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
#  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
#  OR OTHER DEALINGS IN THE SOFTWARE.

# TODO: the sphere channel should come out of hyperstream

import os
from collections import Iterable
import logging

from sphere_connector_package.sphere_connector import SphereConnector, DataWindow

from memory_channel import MemoryChannel
from ..time_interval import TimeIntervals, TimeInterval
from ..utils import MIN_DATE, MAX_DATE

path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
sphere_connector = SphereConnector(
    config_filename=os.path.join(path, 'config_strauss.json'),
    include_mongo=True,
    include_redcap=False,
    sphere_logger=None)


[docs]class SphereDataWindow(DataWindow): """ Helper class to use the global sphere_connector object """ def __init__(self, time_interval): if isinstance(time_interval, TimeInterval): start, end = time_interval.to_tuple() elif isinstance(time_interval, Iterable): start, end = time_interval else: raise TypeError super(SphereDataWindow, self).__init__(sphere_connector, start, end)
[docs]class SphereChannel(MemoryChannel): """ SPHERE MongoDB storing the raw sensor data """ def __init__(self, channel_id, up_to_timestamp=None): super(SphereChannel, self).__init__(channel_id=channel_id) if up_to_timestamp is None: # TODO: maybe should be utcnow, but then we'd have to keep updating it? up_to_timestamp = MAX_DATE # utcnow() if up_to_timestamp > MIN_DATE: self.update_streams(up_to_timestamp)
[docs] def update_streams(self, up_to_timestamp): """ Call this function to report to the system that the SPHERE MongoDB is fully populated until up_to_timestamp """ for stream_id in self.streams: self.streams[stream_id].calculated_intervals = TimeIntervals([(MIN_DATE, up_to_timestamp)]) self.up_to_timestamp = up_to_timestamp