public DbClient(Configuration configuration, IFileHandling fileHandler) { _fileHandler = fileHandler; using var metrics = DbActionDuration .WithLabels("using", "syncHistoryTable") .NewTimer(); using var tracing = Tracing.Trace("LoadTable", TagValue.Db).WithWorkoutId("SyncHistoryItem"); if (!File.Exists(configuration.App.SyncHistoryDbPath)) { Log.Debug("Creating syncHistory db: {@Path}", configuration.App.SyncHistoryDbPath); try { var dir = Path.GetDirectoryName(configuration.App.SyncHistoryDbPath); _fileHandler.MkDirIfNotExists(dir); File.WriteAllText(configuration.App.SyncHistoryDbPath, "{}"); } catch (Exception e) { Log.Error(e, "Failed to create syncHistory db file: {@Path}", configuration.App.SyncHistoryDbPath); throw; } } _database = new DataStore(configuration.App.SyncHistoryDbPath); _syncHistoryTable = new Lazy <IDocumentCollection <SyncHistoryItem> >(() => _database.GetCollection <SyncHistoryItem>()); }
private void SaveRawData(dynamic data, string workoutTitle) { using var tracing = Tracing.Trace($"{nameof(PelotonService)}.{nameof(SaveRawData)}") .WithTag("workout.title", workoutTitle); var outputDir = _config.App.JsonDirectory; _fileHandler.MkDirIfNotExists(outputDir); _logger.Debug("Write peloton json to file for {@WorkoutId}", data.Workout.Id); _fileHandler.WriteToFile(Path.Join(outputDir, $"{workoutTitle}.json"), data.ToString()); }
protected void CreateDbIfNotExist(string path, string dbName) { if (!File.Exists(path)) { _logger.Debug("Creating {@DbName} db: {@Path}", dbName, path); try { var dir = Path.GetDirectoryName(path); _fileHandler.MkDirIfNotExists(dir); File.WriteAllText(path, "{}"); } catch (Exception e) { _logger.Error(e, "Failed to create {@DbName} db file: {@Path}", dbName, path); throw; } } }
public async Task DownloadLatestWorkoutDataAsync() { if (_config.Peloton.NumWorkoutsToDownload <= 0) { return; } using var tracing = Tracing.Trace(nameof(DownloadLatestWorkoutDataAsync)); await _pelotonApi.InitAuthAsync(); var recentWorkouts = await _pelotonApi.GetWorkoutsAsync(_config.Peloton.NumWorkoutsToDownload); var completedWorkouts = recentWorkouts.data.Where(w => { if (w.Status == "COMPLETE") { return(true); } Log.Debug("Skipping in progress workout. {@WorkoutId} {@WorkoutStatus} {@WorkoutType}", w.Id, w.Status, w.Fitness_Discipline); return(false); }); var filteredWorkouts = completedWorkouts.Where(w => { if (!_config.Peloton.ExcludeWorkoutTypes?.Contains(w.Fitness_Discipline) ?? true) { return(true); } Log.Debug("Skipping excluded workout type. {@WorkoutId} {@WorkoutStatus} {@WorkoutType}", w.Id, w.Status, w.Fitness_Discipline); return(false); }); var workingDir = _config.App.DownloadDirectory; _fileHandler.MkDirIfNotExists(workingDir); foreach (var recentWorkout in filteredWorkouts) { var workoutId = recentWorkout.Id; SyncHistoryItem syncRecord = _dbClient.Get(recentWorkout.Id); if ((syncRecord?.DownloadDate is object)) { Log.Debug("Workout {@WorkoutId} already downloaded, skipping.", recentWorkout.Id); continue; } using var workoutTimer = WorkoutDownloadDuration.NewTimer(); var workoutTask = _pelotonApi.GetWorkoutByIdAsync(recentWorkout.Id); var workoutSamplesTask = _pelotonApi.GetWorkoutSamplesByIdAsync(recentWorkout.Id); await Task.WhenAll(workoutTask, workoutSamplesTask); var workout = workoutTask.GetAwaiter().GetResult(); var workoutSamples = workoutSamplesTask.GetAwaiter().GetResult(); dynamic data = new JObject(); data.Workout = workout; data.WorkoutSamples = workoutSamples; var workoutTitle = string.Empty; P2GWorkout deSerializedData = null; try { deSerializedData = JsonSerializer.Deserialize <P2GWorkout>(data.ToString(), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); workoutTitle = WorkoutHelper.GetUniqueTitle(deSerializedData.Workout); if (_config.Format.Json && _config.Format.SaveLocalCopy) { SaveRawData(data, workoutTitle); } } catch (Exception e) { _failedCount++; var title = "workout_failed_to_deserialize_" + _failedCount; Log.Error("Failed to deserialize workout from Peloton. You can find the raw data from the workout here: @FileName", title, e); SaveRawData(data, title); return; } Log.Debug("Write peloton workout details to file for {@WorkoutId}.", workoutId); File.WriteAllText(Path.Join(workingDir, $"{workoutTitle}.json"), data.ToString()); var syncHistoryItem = new SyncHistoryItem(deSerializedData.Workout) { DownloadDate = DateTime.Now, }; _dbClient.Upsert(syncHistoryItem); } }
protected void Convert(string format) { if (!_fileHandler.DirExists(_config.App.DownloadDirectory)) { Log.Information("No download directory found. Nothing to do. {@File}", _config.App.DownloadDirectory); return; } var files = _fileHandler.GetFiles(_config.App.DownloadDirectory); if (files.Length == 0) { Log.Information("No files to convert in download directory. Nothing to do."); return; } if (_config.Garmin.Upload) { _fileHandler.MkDirIfNotExists(_config.App.UploadDirectory); } // Foreach file in directory foreach (var file in files) { using var workoutTimer = WorkoutsConverted.WithLabels(format).NewTimer(); // load file and deserialize P2GWorkout workoutData = null; try { workoutData = _fileHandler.DeserializeJson <P2GWorkout>(file); } catch (Exception e) { Log.Error(e, "Failed to load and parse workout data {@File}", file); _fileHandler.MoveFailedFile(file, _config.App.FailedDirectory); continue; } using var tracing = Tracing.Trace("Convert") .WithWorkoutId(workoutData.Workout.Id) .WithTag(TagKey.Format, format); // call internal convert method T converted = default; var workoutTitle = WorkoutHelper.GetUniqueTitle(workoutData.Workout); try { converted = Convert(workoutData.Workout, workoutData.WorkoutSamples); } catch (Exception e) { Log.Error(e, "Failed to convert workout data {@Workout} {@File}", workoutTitle, file); } if (converted is null) { _fileHandler.MoveFailedFile(file, _config.App.FailedDirectory); continue; } // write to output dir var path = Path.Join(_config.App.WorkingDirectory, $"{workoutTitle}.{format}"); try { Save(converted, path); } catch (Exception e) { Log.Error(e, "Failed to write {@Format} file for {@Workout}", format, workoutTitle); continue; } // copy to local save if (_config.Format.SaveLocalCopy) { try { _fileHandler.MkDirIfNotExists(_config.App.TcxDirectory); _fileHandler.MkDirIfNotExists(_config.App.FitDirectory); var dir = format == "fit" ? _config.App.FitDirectory : _config.App.TcxDirectory; var backupDest = Path.Join(dir, $"{workoutTitle}.{format}"); _fileHandler.Copy(path, backupDest, overwrite: true); Log.Information("Backed up file {@File}", backupDest); } catch (Exception e) { Log.Error(e, "Failed to backup {@Format} file for {@Workout}", format, workoutTitle); continue; } } // copy to upload dir if (_config.Garmin.Upload && _config.Garmin.FormatToUpload == format) { try { var uploadDest = Path.Join(_config.App.UploadDirectory, $"{workoutTitle}.{format}"); _fileHandler.Copy(path, uploadDest, overwrite: true); Log.Debug("Prepped {@Format} file {@Path} for upload.", format, uploadDest); } catch (Exception e) { Log.Error(e, "Failed to copy {@Format} file for {@Workout}", format, workoutTitle); continue; } } // update db item with conversion date SyncHistoryItem syncRecord = _dbClient.Get(workoutData.Workout.Id); if (syncRecord is null) { syncRecord = new SyncHistoryItem(workoutData.Workout) { DownloadDate = System.DateTime.Now, }; } syncRecord.ConvertedToFit = syncRecord.ConvertedToFit || format == "fit"; syncRecord.ConvertedToTcx = syncRecord.ConvertedToTcx || format == "tcx"; _dbClient.Upsert(syncRecord); } }
protected ConvertStatus Convert(FileFormat format, P2GWorkout workoutData) { using var tracing = Tracing.Trace($"{nameof(IConverter)}.{nameof(Convert)}.Workout")? .WithWorkoutId(workoutData.Workout.Id) .WithTag(TagKey.Format, format.ToString()); var status = new ConvertStatus(); // call internal convert method T converted = default; var workoutTitle = WorkoutHelper.GetUniqueTitle(workoutData.Workout); try { converted = Convert(workoutData.Workout, workoutData.WorkoutSamples); } catch (Exception e) { _logger.Error(e, "Failed to convert workout data to format {@Format} {@Workout}", format, workoutTitle); status.Success = false; status.ErrorMessage = "Failed to convert workout data."; tracing?.AddTag("excetpion.message", e.Message); tracing?.AddTag("exception.stacktrace", e.StackTrace); tracing?.AddTag("convert.success", false); tracing?.AddTag("convert.errormessage", status.ErrorMessage); return(status); } // write to output dir var path = Path.Join(_config.App.WorkingDirectory, $"{workoutTitle}.{format}"); try { _fileHandler.MkDirIfNotExists(_config.App.WorkingDirectory); Save(converted, path); status.Success = true; } catch (Exception e) { status.Success = false; status.ErrorMessage = "Failed to save converted workout for upload."; _logger.Error(e, "Failed to write {@Format} file for {@Workout}", format, workoutTitle); tracing?.AddTag("excetpion.message", e.Message); tracing?.AddTag("exception.stacktrace", e.StackTrace); tracing?.AddTag("convert.success", false); tracing?.AddTag("convert.errormessage", status.ErrorMessage); return(status); } // copy to local save try { SaveLocalCopy(path, workoutTitle); } catch (Exception e) { _logger.Error(e, "Failed to backup {@Format} file for {@Workout}", format, workoutTitle); } // copy to upload dir if (_config.Garmin.Upload && _config.Garmin.FormatToUpload == format) { try { var uploadDest = Path.Join(_config.App.UploadDirectory, $"{workoutTitle}.{format}"); _fileHandler.MkDirIfNotExists(_config.App.UploadDirectory); _fileHandler.Copy(path, uploadDest, overwrite: true); _logger.Debug("Prepped {@Format} for upload: {@Path}", format, uploadDest); } catch (Exception e) { _logger.Error(e, "Failed to copy {@Format} file for {@Workout}", format, workoutTitle); status.Success = false; status.ErrorMessage = $"Failed to save file for {@format} and workout {workoutTitle} to Upload directory"; tracing?.AddTag("excetpion.message", e.Message); tracing?.AddTag("exception.stacktrace", e.StackTrace); tracing?.AddTag("convert.success", false); tracing?.AddTag("convert.errormessage", status.ErrorMessage); return(status); } } return(status); }