Ejemplo n.º 1
0
    def test_garmin_total_summary_report_txt(self):
        """ test GarminReport.total_summary_report_txt """
        gsum = GarminSummary(FITFILE)
        gsum.read_file()
        gr_ = GarminReport()
        output = gr_.total_summary_report_txt(gsum, sport='running')

        mstr = hashlib.md5()
        mstr.update(output.encode())
        self.assertEqual(mstr.hexdigest(), '9f5aa437a6e8bbe6ecd25a088b634018')
Ejemplo n.º 2
0
    def test_garmin_file_report_txt(self):
        """ test GarminReport.file_report_txt """
        gfile = GarminParse(FITFILE)
        gfile.read_file()
        gr_ = GarminReport(gfile=gfile)
        output = gr_.file_report_txt()

        mstr = hashlib.md5()
        mstr.update(output.encode())
        self.assertEqual(mstr.hexdigest(), 'dc49eed73bf44c1b5d5c2444a59bec96')
Ejemplo n.º 3
0
 def test_garmin_month_average_report_txt(self):
     """ test GarminReport.month_average_report_txt """
     gsum = GarminSummary(FITFILE)
     gsum.read_file()
     gr_ = GarminReport()
     output = gr_.month_average_report_txt(gsum,
                                           sport='running',
                                           number_of_months=1)
     mstr = hashlib.md5()
     mstr.update(output.encode())
     self.assertEqual(mstr.hexdigest(), '08a90d3c2e16ba2aafb4403e3ed69824')
Ejemplo n.º 4
0
 def test_garmin_week_average_report_txt(self):
     """ test GarminReport.week_average_report_txt """
     gsum = GarminSummary(FITFILE)
     gsum.read_file()
     gr_ = GarminReport()
     output = gr_.week_average_report_txt(gsum,
                                          sport='running',
                                          number_of_weeks=1)
     mstr = hashlib.md5()
     mstr.update(output.encode())
     self.assertEqual(mstr.hexdigest(), '4b649d4c617128138a1540c9dc2e1a09')
Ejemplo n.º 5
0
    def test_garmin_day_summary_report_txt(self):
        """ test GarminReport.day_summary_report_txt """
        gsum = GarminSummary(FITFILE)
        gsum.read_file()
        gr_ = GarminReport()
        output = gr_.day_summary_report_txt(
            gsum, sport='running', cur_date=gsum.begin_datetime.date())

        mstr = hashlib.md5()
        mstr.update(output.encode())
        self.assertEqual(mstr.hexdigest(), 'b05ddcddc03a64b650a75ad397037d00')
Ejemplo n.º 6
0
 def test_garmin_year_summary_report_txt(self):
     """ test GarminReport.year_summary_report_txt """
     gsum = GarminSummary(FITFILE)
     gsum.read_file()
     gr_ = GarminReport()
     dt_ = gsum.begin_datetime
     output = gr_.year_summary_report_txt(gsum,
                                          sport='running',
                                          year=dt_.year,
                                          number_in_year=1)
     mstr = hashlib.md5()
     mstr.update(output.encode())
     self.assertEqual(mstr.hexdigest(), 'b7ab537ea3090fc44276b15bc61577b5')
Ejemplo n.º 7
0
 def test_garmin_month_summary_report_txt(self):
     """ test GarminReport.month_summary_report_txt """
     gsum = GarminSummary(FITFILE)
     gsum.read_file()
     gr_ = GarminReport()
     output = gr_.month_summary_report_txt(gsum,
                                           sport='running',
                                           year=gsum.begin_datetime.year,
                                           month=gsum.begin_datetime.month,
                                           number_in_month=1)
     mstr = hashlib.md5()
     mstr.update(output.encode())
     self.assertEqual(mstr.hexdigest(), 'd2a1204e0374c7e83c450a1ae4ce3981')
Ejemplo n.º 8
0
    def test_garmin_day_average_report_txt(self):
        """ test GarminReport.day_average_report_txt """
        gsum = GarminSummary(FITFILE)
        gsum.read_file()
        gr_ = GarminReport()
        self.assertFalse(gr_.day_average_report_txt(gsum, number_days=0))
        output = gr_.day_average_report_txt(gsum,
                                            sport='running',
                                            number_days=1)

        mstr = hashlib.md5()
        mstr.update(output.encode())
        self.assertEqual(mstr.hexdigest(), '59c50d1eff34a78619e9f37e45445535')
Ejemplo n.º 9
0
def read_garmin_file(fname, msg_q=None, options=None, s3_files=None):
    """ read single garmin file """
    from garmin_app.garmin_cache import pool
    from garmin_app.garmin_cache import GarminCache
    from garmin_app.garmin_parse import GarminParse
    from garmin_app.garmin_report import GarminReport
    from garmin_app.garmin_corrections import list_of_corrected_laps
    if options is None:
        options = {'cache_dir': CACHEDIR, 'do_update': False}
    tcx_job = pool.submit(convert_fit_to_tcx, fname)
    gpx_job = pool.submit(convert_gmn_to_gpx, fname)
    cache_dir = options['cache_dir']

    corr_list_ = list_of_corrected_laps(json_path='%s/run' % cache_dir)

    pickle_file_ = '%s/run/garmin.pkl.gz' % cache_dir
    cache_dir_ = '%s/run/cache' % cache_dir

    avro_file = '%s/%s.avro' % (cache_dir, os.path.basename(fname))
    if not os.path.exists(avro_file):
        s3_cache_files = get_list_of_keys('garmin-scripts-cache-ddboline')
        s3_key = os.path.basename(avro_file)
        if s3_key in s3_cache_files:
            download_from_s3('garmin-scripts-cache-ddboline', s3_key,
                             avro_file)

    cache_ = GarminCache(cache_directory=cache_dir_, corr_list=corr_list_)

    _temp_file = None
    if not options['do_update']:
        _temp_file = cache_.read_cached_gfile(gfbname=os.path.basename(fname))
    if _temp_file:
        _gfile = _temp_file
    else:
        _gfile = GarminParse(fname, corr_list=corr_list_)
        _gfile.read_file()
    if not _gfile:
        return False
    if options['do_update'] or not os.path.exists(avro_file):
        cache_.write_cached_gfile(garminfile=_gfile)
    _report = GarminReport(cache_obj=cache_, msg_q=msg_q, gfile=_gfile)
    print(_report.file_report_txt())
    _report.file_report_html(options=options)
    for fn0, fn1 in ((tcx_job.result(), '/tmp/temp.tcx'), (gpx_job.result(),
                                                           '/tmp/temp.gpx')):
        if fn0 and os.path.exists(fn0):
            os.rename(fn0, fn1)
    return True
Ejemplo n.º 10
0
    def test_garmin_week_summary_report_txt(self):
        """ test GarminReport.week_summary_report_txt """
        gsum = GarminSummary(FITFILE)
        gsum.read_file()
        gr_ = GarminReport()
        ic_ = gsum.begin_datetime.isocalendar()
        output = gr_.week_summary_report_txt(gsum,
                                             sport='running',
                                             isoyear=ic_[0],
                                             isoweek=ic_[1],
                                             number_in_week=1,
                                             date=gsum.begin_datetime)

        mstr = hashlib.md5()
        mstr.update(output.encode())
        self.assertEqual(mstr.hexdigest(), 'e538e1b1abd954083c9cc19e14d04315')
Ejemplo n.º 11
0
def do_summary(directory_, msg_q=None, options=None):
    """ produce summary report """
    from garmin_app.garmin_cache import (GarminCache,
                                         read_pickle_object_in_file,
                                         write_pickle_object_to_file)
    from garmin_app.garmin_corrections import list_of_corrected_laps
    from garmin_app.garmin_corrections_sql import write_corrections_table
    from garmin_app.garmin_report import GarminReport
    if options is None:
        options = {'cache_dir': CACHEDIR}
    cache_dir = options['cache_dir']
    corr_list_ = list_of_corrected_laps(json_path='%s/run' % cache_dir)
    pickle_file_ = '%s/run/garmin.pkl.gz' % cache_dir
    cache_dir_ = '%s/run/cache' % cache_dir
    cache_ = GarminCache(cache_directory=cache_dir_,
                         corr_list=corr_list_,
                         use_sql=True,
                         do_tunnel=options.get('do_tunnel', False),
                         check_md5=options.get('do_check', False))
    if 'build' in options and options['build']:
        summary_list_ = cache_.get_cache_summary_list(directory='%s/run' %
                                                      cache_dir,
                                                      options=options)
        cache_ = GarminCache(pickle_file=pickle_file_,
                             cache_directory=cache_dir_,
                             corr_list=corr_list_,
                             use_sql=False,
                             check_md5=True,
                             cache_read_fn=read_pickle_object_in_file,
                             cache_write_fn=write_pickle_object_to_file,
                             do_tunnel=options.get('do_tunnel', False))
        cache_.cache_write_fn(cache_.cache_summary_file_dict)
        write_corrections_table(corr_list_,
                                do_tunnel=options.get('do_tunnel', False))
        return summary_list_
    summary_list_ = cache_.get_cache_summary_list(directory=directory_,
                                                  options=options)
    if not summary_list_:
        return False
    _report = GarminReport(cache_obj=cache_, msg_q=msg_q)
    print(_report.summary_report(summary_list_.values(), options=options))
    return True
Ejemplo n.º 12
0
 def test_garmin_file_report_html(self):
     """ test GarminReport.file_report_html """
     gfile = GarminParse(FITFILE)
     gfile.read_file()
     gr_ = GarminReport(gfile=gfile)
     script_path = CURDIR
     options = {
         'script_path': '%s/garmin_app' % script_path,
         'cache_dir': script_path
     }
     html_path = gr_.file_report_html(copy_to_public_html=False,
                                      options=options)
     file_md5 = [[
         'index.html',
         [
             '1c1abe181f36a85949974a222cc874df',
             '548581a142811d412dbf955d2e5372aa',
             '73bb500bed38ef9f2881f11019b7c27c'
         ]
     ]]
     for fn_, fmd5 in file_md5:
         md5 = get_md5('%s/%s' % (html_path, fn_))
         if hasattr(md5, 'decode'):
             md5 = md5.decode()
Ejemplo n.º 13
0
 def test_summary_report_file(self):
     """ test GarminCache.summary_report """
     gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR,
                       cache_read_fn=read_pickle_object_in_file,
                       cache_write_fn=write_pickle_object_to_file,
                       cache_directory='%s/run/cache' % CURDIR,
                       use_sql=False)
     sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR)
     rp_ = GarminReport(cache_obj=gc_)
     options = {
         'do_plot': False,
         'do_year': True,
         'do_month': True,
         'do_week': True,
         'do_day': True,
         'do_file': True,
         'do_sport': 'running',
         'do_update': False,
         'do_average': True
     }
     script_path = CURDIR
     options['script_path'] = '%s/garmin_app' % script_path
     options['cache_dir'] = script_path
     output = rp_.summary_report(sl_,
                                 copy_to_public_html=False,
                                 options=options)
     mstr = hashlib.md5()
     mstr.update(output.encode())
     self.assertEqual(mstr.hexdigest(), '79e43ff052e6a2238f54d7c2cd78b0ad')
     options['do_sport'] = None
     output = rp_.summary_report(sl_,
                                 copy_to_public_html=False,
                                 options=options)
     mstr = hashlib.md5()
     mstr.update(output.encode())
     self.assertEqual(mstr.hexdigest(), '927f7ac0f1eb04b20ffe78c23bc6936c')
     options['do_sport'] = 'running'
     options['do_week'] = False
     output = rp_.summary_report(sl_,
                                 copy_to_public_html=False,
                                 options=options)
     mstr = hashlib.md5()
     mstr.update(output.encode())
     self.assertEqual(mstr.hexdigest(), '88d026c8736bef031d84bede447471cc')
     options['do_sport'] = 'running'
     options['do_month'] = False
     output = rp_.summary_report(sl_,
                                 copy_to_public_html=False,
                                 options=options)
     mstr = hashlib.md5()
     mstr.update(output.encode())
     self.assertEqual(mstr.hexdigest(), '94954edf36e8625143735f8b3e263c6b')
     options = {
         'do_plot': False,
         'do_year': False,
         'do_month': False,
         'do_week': False,
         'do_day': False,
         'do_file': False,
         'do_sport': None,
         'do_update': False,
         'do_average': False,
         'occur': True
     }
     options['script_path'] = '%s/garmin_app' % script_path
     options['cache_dir'] = script_path
     output = rp_.summary_report(sl_,
                                 copy_to_public_html=False,
                                 options=options)
     mstr = hashlib.md5()
     mstr.update(output.encode())
     self.assertEqual(mstr.hexdigest(), '6256eb536104110794d9fc2123a8c104')
Ejemplo n.º 14
0
    def test_garmin_cache(self):
        """ test GarminCache.get_cache_summary_list """
        gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR,
                          cache_directory='%s/run/cache' % CURDIR,
                          cache_read_fn=read_pickle_object_in_file,
                          cache_write_fn=write_pickle_object_to_file,
                          use_sql=False)
        sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR)
        output = '\n'.join(
            '%s' % s for s in sorted(sl_.values(), key=lambda x: x.filename))
        test_output = open('tests/test_cache_summary.out', 'rt').read().strip()

        test_output0 = test_output.replace('10:43:08-05:00', '11:43:08-04:00')

        mstr = hashlib.md5()
        mstr.update(output.encode())
        print(output)
        #        print(test_output)
        self.assertIn(output, [test_output, test_output0])

        sqlite_str = 'sqlite:///%s/run/cache/test.db' % CURDIR
        gc_ = GarminCacheSQL(sql_string=sqlite_str)
        sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR)
        output = '\n'.join(
            '%s' % s for s in sorted(sl_.values(), key=lambda x: x.filename))
        mstr = hashlib.md5()
        mstr.update(output.encode())
        self.assertIn(mstr.hexdigest(), [
            'c06f13236f9abed0723e4af7537ca3d4',
            'a59c8ee120e789eda36e0cc8592ffce1',
            '35475bfdd07e72c9cd3988c83a07b083',
            'f1749a2ec48d1ca814b570d2bf36d587'
        ])

        gc0 = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR,
                          cache_directory='%s/run/cache' % CURDIR,
                          use_sql=False)
        sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR)
        sqlite_str = 'sqlite:///%s/run/cache/test.db' % CURDIR
        gc1 = GarminCacheSQL(sql_string=sqlite_str,
                             garmin_cache=gc0,
                             summary_list=sl_)
        output = '\n'.join('%s' % s for s in sorted(gc1.summary_list.values(),
                                                    key=lambda x: x.filename))
        mstr = hashlib.md5()
        mstr.update(output.encode())
        #        self.assertIn(mstr.hexdigest(), [
        #            '06465ba08d19d59c963e542bc19f12b7', 'a59c8ee120e789eda36e0cc8592ffce1',
        #            '34605a1d755eda499022946e46d46c1a', '9fbf84e57a513d875f471fbcabe20e22',
        #            '9e23c7a7bc3c436ef319a5a3d1003264'
        #        ])

        with OpenPostgreSQLsshTunnel(port=5435, do_tunnel=True) as pport:
            postgre_str = '%s:%d/test_garmin_summary' % (POSTGRESTRING, pport)
            gc_ = GarminCacheSQL(sql_string=postgre_str)
            sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR)
            output = '\n'.join(
                '%s' % s
                for s in sorted(sl_.values(), key=lambda x: x.filename))
            print(output)
            mstr = hashlib.md5()
            mstr.update(output.encode())
            #self.assertIn(mstr.hexdigest(), [
            #'c06f13236f9abed0723e4af7537ca3d4', 'a59c8ee120e789eda36e0cc8592ffce1',
            #'35475bfdd07e72c9cd3988c83a07b083', '34605a1d755eda499022946e46d46c1a',
            #'9fbf84e57a513d875f471fbcabe20e22', 'f1749a2ec48d1ca814b570d2bf36d587',
            #'9e23c7a7bc3c436ef319a5a3d1003264'
            #])

        with OpenPostgreSQLsshTunnel(port=5436, do_tunnel=True) as pport:
            postgre_str = '%s:%d/test_garmin_summary' % (POSTGRESTRING, pport)
            gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR,
                              cache_directory='%s/run/cache' % CURDIR,
                              cache_read_fn=read_pickle_object_in_file,
                              cache_write_fn=write_pickle_object_to_file,
                              use_sql=False)
            sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR)
            sl_ = _write_postgresql_table(sl_,
                                          get_summary_list=True,
                                          dbname='test_garmin_summary',
                                          port=pport)
            sl_ = _write_postgresql_table(sl_,
                                          dbname='test_garmin_summary',
                                          port=pport)
            print(len(sl_))
            output = '\n'.join(
                '%s' % s
                for s in sorted(sl_.values(), key=lambda x: x.filename))
            gc_ = GarminCacheSQL(sql_string=postgre_str)
            gc_.delete_table()
            mstr = hashlib.md5()
            mstr.update(output.encode())


#            self.assertIn(mstr.hexdigest(), [
#                '06465ba08d19d59c963e542bc19f12b7', '34605a1d755eda499022946e46d46c1a',
#                '9fbf84e57a513d875f471fbcabe20e22', '9e23c7a7bc3c436ef319a5a3d1003264'
#            ])

        gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR,
                          cache_directory='%s/run/cache' % CURDIR,
                          use_sql=False)
        gsum = GarminSummary(FITFILE)
        gfile = gsum.read_file()
        gc_.write_cached_gfile(gfile)
        gfile_new = gc_.read_cached_gfile(gfbname=gfile.filename)
        self.assertEqual(gfile, gfile_new)

        gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR, use_sql=False)
        gfile_new = gc_.read_cached_gfile(gfbname=gfile.filename)
        self.assertEqual(gfile_new, False)

        gc_ = GarminCache(pickle_file='%s/temp.pkl.gz' % CURDIR,
                          cache_read_fn=read_pickle_object_in_file,
                          cache_write_fn=write_pickle_object_to_file,
                          cache_directory='%s/run/cache' % CURDIR,
                          use_sql=False)
        sl_ = gc_.get_cache_summary_list(directory='%s/tests' % CURDIR)
        rp_ = GarminReport(cache_obj=gc_)
        options = {
            'do_plot': False,
            'do_year': False,
            'do_month': False,
            'do_week': False,
            'do_day': False,
            'do_file': False,
            'do_sport': None,
            'do_update': False,
            'do_average': False
        }
        script_path = CURDIR
        options['script_path'] = '%s/garmin_app' % script_path
        options['cache_dir'] = script_path
        output = rp_.summary_report(sl_,
                                    copy_to_public_html=False,
                                    options=options)
        mstr = hashlib.md5()
        mstr.update(output.encode())
        self.assertEqual(mstr.hexdigest(), 'dd7cc23be0f6f21a6d05782e506cb647')