# Path: models.py
# class Trip(Base):
#     __tablename__ = 'trips'
#     __table_args__ = {'schema': 'mineturer'}
#     id = Column('tripid', Integer, primary_key=True)
#     title = Column('title', String)
#     description = Column('description', Text)
#     start = Column('start', DateTime)
#     stop = Column('stop', DateTime)
#     type = Column('triptype', String)
#     userid = Column(Integer, ForeignKey('mineturer.users.userid'))
#     user = relationship(User, primaryjoin=userid == User.id)
#     points = relationship(
#         'Point',
#         backref='mineturer.trips',
#         order_by='asc(Point.time)',
#         lazy='dynamic'
#     )
# 
#     stored_points_list = None
#     stats_dict = None
# 
#     def __init__(self, user=None, title=None, description=None, type=None,
#                  start=None, stop=None):
#         self.user = user
#         self.title = title
#         self.description = description
#         self.type = type
#         if start:
#             self.start = start
#         else:
#             self.start = datetime.now()
#         if stop:
#             self.stop = stop
#         else:
#             self.stop = datetime.now()
# 
#     def serialize(self):
#         return {
#             'id': self.id,
#             'type': self.type,
#             'date': self.start.isoformat(),
#             'title': self.title
#         }
# 
#     def serialize_with_point(self):
#         data = self.serialize()
#         start = to_shape(self.points.first().geom)
#         data['position'] = {'lon': start.x, 'lat': start.y}
#         return data
# 
#     @property
#     def stored_points(self):
#         if not self.stored_points_list:
#             self.stored_points_list = self.points.all()
#         return self.stored_points_list
# 
#     @property
#     def geom(self):
#         points = [to_shape(point.geom) for point in self.stored_points]
#         return LineString(points)
# 
#     @property
#     def stats(self):
# 
#         if not self.stats_dict:
#             points = self.stored_points
# 
#             start_time = points[0].time
#             end_time = points[-1].time
# 
#             stats = get_stats(points)
# 
#             total_time = end_time - start_time
# 
#             avg_speed = compute_speed(stats['distance_3d'], total_time)
#             avg_moving_speed = compute_speed(
#                 stats['distance_3d'],
#                 stats['active_time']
#             )
# 
#             asc_speed = compute_speed(stats['distance_asc'], stats['asc_time'])
#             desc_speed = compute_speed(
#                 stats['distance_desc'],
#                 stats['desc_time']
#             )
#             flat_speed = compute_speed(
#                 stats['distance_flat'],
#                 stats['flat_time']
#             )
# 
#             self.stats_dict = {
#                 'start': start_time.isoformat(),
#                 'stop': end_time.isoformat(),
#                 'total_time': total_time,
#                 'active_time': stats['active_time'],
#                 'distance_2d': stats['distance_2d'],
#                 'distance_3d': stats['distance_3d'],
#                 'distance_flat': stats['distance_flat'],
#                 'distance_asc': stats['distance_asc'],
#                 'distance_desc': stats['distance_desc'],
#                 'asc_speed': asc_speed,
#                 'desc_speed': desc_speed,
#                 'flat_speed': flat_speed,
#                 'avg_speed': avg_speed,
#                 'avg_moving_speed': avg_moving_speed,
#                 'total_descent': stats['total_descent'],
#                 'total_ascent': stats['total_ascent'],
#                 'max_height': stats['max_height'],
#                 'min_height': stats['min_height'],
#                 'flat_time': stats['flat_time'],
#                 'asc_time': stats['asc_time'],
#                 'desc_time': stats['desc_time'],
#                 'elev_diff': stats['max_height'] - stats['min_height'],
#             }
#         return self.stats_dict
# 
#     def __repr__(self):
#         return '<Trip %r>' % (self.title)
# 
# class Point(Base):
#     __tablename__ = 'points'
#     __table_args__ = {'schema': 'mineturer'}
#     pid = Column('pid', Integer, primary_key=True)
#     geom = Column(Geometry(geometry_type='POINT', srid=4326))
#     time = Column('time', DateTime)
#     ele = Column('ele', Numeric)
#     hr = Column('hr', Numeric)
#     tripid = Column(Integer, ForeignKey('mineturer.trips.tripid'))
#     trip = relationship(Trip, primaryjoin=tripid == Trip.id)
# 
#     def __init__(self, trip=None, geom=None, time=None, ele=None, hr=None):
#         self.trip = trip
#         self.geom = from_shape(geom, srid=4326)
#         self.time = time
#         self.ele = ele
#         self.hr = hr
# 
#     def __repr__(self):
#         return '<Point %r>' % (self.pid)


import unittest
import dateutil.parser
from shapely.geometry import Point
from models import Trip, Point as MTPoint
# -*- coding: utf-8 -*-




class TripStatTest(unittest.TestCase):

    def setUp(self):
        self.trip = Trip()
        p1 = MTPoint(
            self.trip,
            Point(10.434591, 63.399736),
            dateutil.parser.parse('2012-10-10T17:06:27.000Z'),
            ele=94.6,
        )
        self.trip.points.append(p1)
        p2 = MTPoint(
            self.trip,
            Point(10.434781, 63.40054),
            dateutil.parser.parse('2012-10-10T17:07:10.000Z'),
            ele=193.4,
        )
        self.trip.points.append(p2)

# Path: models.py
# class Trip(Base):
#     __tablename__ = 'trips'
#     __table_args__ = {'schema': 'mineturer'}
#     id = Column('tripid', Integer, primary_key=True)
#     title = Column('title', String)
#     description = Column('description', Text)
#     start = Column('start', DateTime)
#     stop = Column('stop', DateTime)
#     type = Column('triptype', String)
#     userid = Column(Integer, ForeignKey('mineturer.users.userid'))
#     user = relationship(User, primaryjoin=userid == User.id)
#     points = relationship(
#         'Point',
#         backref='mineturer.trips',
#         order_by='asc(Point.time)',
#         lazy='dynamic'
#     )
# 
#     stored_points_list = None
#     stats_dict = None
# 
#     def __init__(self, user=None, title=None, description=None, type=None,
#                  start=None, stop=None):
#         self.user = user
#         self.title = title
#         self.description = description
#         self.type = type
#         if start:
#             self.start = start
#         else:
#             self.start = datetime.now()
#         if stop:
#             self.stop = stop
#         else:
#             self.stop = datetime.now()
# 
#     def serialize(self):
#         return {
#             'id': self.id,
#             'type': self.type,
#             'date': self.start.isoformat(),
#             'title': self.title
#         }
# 
#     def serialize_with_point(self):
#         data = self.serialize()
#         start = to_shape(self.points.first().geom)
#         data['position'] = {'lon': start.x, 'lat': start.y}
#         return data
# 
#     @property
#     def stored_points(self):
#         if not self.stored_points_list:
#             self.stored_points_list = self.points.all()
#         return self.stored_points_list
# 
#     @property
#     def geom(self):
#         points = [to_shape(point.geom) for point in self.stored_points]
#         return LineString(points)
# 
#     @property
#     def stats(self):
# 
#         if not self.stats_dict:
#             points = self.stored_points
# 
#             start_time = points[0].time
#             end_time = points[-1].time
# 
#             stats = get_stats(points)
# 
#             total_time = end_time - start_time
# 
#             avg_speed = compute_speed(stats['distance_3d'], total_time)
#             avg_moving_speed = compute_speed(
#                 stats['distance_3d'],
#                 stats['active_time']
#             )
# 
#             asc_speed = compute_speed(stats['distance_asc'], stats['asc_time'])
#             desc_speed = compute_speed(
#                 stats['distance_desc'],
#                 stats['desc_time']
#             )
#             flat_speed = compute_speed(
#                 stats['distance_flat'],
#                 stats['flat_time']
#             )
# 
#             self.stats_dict = {
#                 'start': start_time.isoformat(),
#                 'stop': end_time.isoformat(),
#                 'total_time': total_time,
#                 'active_time': stats['active_time'],
#                 'distance_2d': stats['distance_2d'],
#                 'distance_3d': stats['distance_3d'],
#                 'distance_flat': stats['distance_flat'],
#                 'distance_asc': stats['distance_asc'],
#                 'distance_desc': stats['distance_desc'],
#                 'asc_speed': asc_speed,
#                 'desc_speed': desc_speed,
#                 'flat_speed': flat_speed,
#                 'avg_speed': avg_speed,
#                 'avg_moving_speed': avg_moving_speed,
#                 'total_descent': stats['total_descent'],
#                 'total_ascent': stats['total_ascent'],
#                 'max_height': stats['max_height'],
#                 'min_height': stats['min_height'],
#                 'flat_time': stats['flat_time'],
#                 'asc_time': stats['asc_time'],
#                 'desc_time': stats['desc_time'],
#                 'elev_diff': stats['max_height'] - stats['min_height'],
#             }
#         return self.stats_dict
# 
#     def __repr__(self):
#         return '<Trip %r>' % (self.title)
# 
# class Point(Base):
#     __tablename__ = 'points'
#     __table_args__ = {'schema': 'mineturer'}
#     pid = Column('pid', Integer, primary_key=True)
#     geom = Column(Geometry(geometry_type='POINT', srid=4326))
#     time = Column('time', DateTime)
#     ele = Column('ele', Numeric)
#     hr = Column('hr', Numeric)
#     tripid = Column(Integer, ForeignKey('mineturer.trips.tripid'))
#     trip = relationship(Trip, primaryjoin=tripid == Trip.id)
# 
#     def __init__(self, trip=None, geom=None, time=None, ele=None, hr=None):
#         self.trip = trip
#         self.geom = from_shape(geom, srid=4326)
#         self.time = time
#         self.ele = ele
#         self.hr = hr
# 
#     def __repr__(self):
#         return '<Point %r>' % (self.pid)


# Path: tests/stats_test.py
import unittest
import dateutil.parser
from shapely.geometry import Point
from models import Trip, Point as MTPoint

# -*- coding: utf-8 -*-




class TripStatTest(unittest.TestCase):

    def setUp(self):
        self.trip = Trip()
        p1 = MTPoint(
            self.trip,
            Point(10.434591, 63.399736),
            dateutil.parser.parse('2012-10-10T17:06:27.000Z'),
            ele=94.6,
        )
        self.trip.points.append(p1)
        p2 = MTPoint(
            self.trip,
            Point(10.434781, 63.40054),
            dateutil.parser.parse('2012-10-10T17:07:10.000Z'),
            ele=193.4,
        )
        self.trip.points.append(p2)

        p3 = MTPoint(