Skip to content

Commit

Permalink
Stabilizing backup of tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
Didosa committed Sep 1, 2024
1 parent 2d64621 commit 4322fff
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
7 changes: 6 additions & 1 deletion PiLotBackupAPI/Helpers/BackupHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ public static void BackupGpsData(List<TrackPoint> pRecords, System.Date pDay, St
/// <param name="pBackupTime"></param>
public static void BackupTrack(Track pTrack, String pClientName, DateTime pBackupTime) {
DirectoryInfo backupDirectory = BackupHelper.GetTempDirectory(pClientName, pBackupTime);
TrackDataConnector2.GetInstance(backupDirectory.FullName).SaveTrack(pTrack);
TrackDataConnector2 dataConnector = TrackDataConnector2.GetInstance(backupDirectory.FullName);
if (pTrack.HasTrackPoints) {
dataConnector.SaveTrack(pTrack);
} else if (pTrack.ID != null) {
dataConnector.DeleteTrack(pTrack.ID.Value);
}
Logger.Log($"Recieved Track with id {pTrack.ID} to backup", LogLevels.DEBUG);
}

Expand Down
2 changes: 1 addition & 1 deletion PiLotBackupClient/Data/TrackDataConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class TrackDataConnector : PiLot.Data.Postgres.Nav.TrackDataConnector {
internal TrackDataConnector(): base(){}

/// <summary>
/// Reads the total number of non.empty tracks and the ids of the tracks that have been changed since the last backup
/// Reads the total number of non-empty tracks and the ids of the tracks that have been changed since the last backup
/// </summary>
/// <param name="pChangedAfter">The date of the last backup</param>
/// <returns>The ids of the changed tracks, the number of non-empty tracks</returns>
Expand Down
4 changes: 2 additions & 2 deletions PiLotBackupClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ private static async Task BackupTargetAsync(BackupTarget pTarget, Boolean pFullB
Out.WriteError("Committing backup failed");
}
} else {
/*Boolean rollbackSuccess = await proxy.RollbackAsync(backupDate);
Boolean rollbackSuccess = await proxy.RollbackAsync(backupDate);
if (rollbackSuccess) {
Out.WriteInfo("Rolled back successfully");
} else {
Out.WriteError("Rollback failed");
}*/
}
}
if (success) {
pTarget.BackupTasks.ForEach(t => t.LastSuccess = backupDate);
Expand Down
32 changes: 16 additions & 16 deletions PiLotDataFiles/Nav/TrackDataConnector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,23 @@ public List<Boolean> ReadTracksMonthInfo(Int32 pYear, Int32 pMonth) {

/// <summary>
/// Saves a track to disk, either by creating a new one, or by updating an existing one. As a special
/// goodie, this allows inserting tracks with a given ID.
/// goodie, this allows inserting tracks with a given ID. Tracks with no TrackPoints will be deleted.
/// </summary>
/// <param name="pTrack">The track to save</param>
public void SaveTrack(Track pTrack) {
lock (this.lockObject) {
if(
(pTrack.StartUTC != null)
&& (pTrack.EndUTC != null)
&& this.ReadTracksMetadata(pTrack.StartUTC.Value, pTrack.EndUTC.Value, false).Exists(t => t.TrackID != pTrack.ID)
){
if (
(pTrack.StartUTC != null)
&& (pTrack.EndUTC != null)
&& this.ReadTracksMetadata(pTrack.StartUTC.Value, pTrack.EndUTC.Value, false).Exists(t => t.TrackID != pTrack.ID)
) {
String msg = "TrackDataConnector.SaveTrack: Could not save Track as there is an overlapping Track";
Logger.Log(msg, LogLevels.ERROR);
throw new Exception(msg);
}
this.SaveTrackMetadata(pTrack);
this.SaveTrackPoints(pTrack.TrackPoints, pTrack.ID.Value, true, true);
}
}
}

/// <summary>
Expand Down Expand Up @@ -509,14 +509,16 @@ private void SaveTrackMetadata(TrackMetadata pMetadata){
}

/// <summary>
/// Deletes the metadata for a track from the index
/// Deletes the metadata for a track from the index, if the track exists in the index.
/// </summary>
/// <param name="pTrackId">The track id</param>
private void DeleteTrackMetadata(Int32 pTrackId){
String trackIndexPath = this.GetTrackIndexPath(this.GetTrackDirectoryPath(pTrackId, false));
List<TrackMetadata> index = this.ReadTrackIndex(trackIndexPath);
index.RemoveAll(m => m.TrackID == pTrackId);
this.PersistTrackIndex(index, trackIndexPath);
if (File.Exists(trackIndexPath)) {
List<TrackMetadata> index = this.ReadTrackIndex(trackIndexPath);
index.RemoveAll(m => m.TrackID == pTrackId);
this.PersistTrackIndex(index, trackIndexPath);
}
}

/// <summary>
Expand Down Expand Up @@ -586,7 +588,7 @@ protected List<TrackPoint> ReadTrackPoints(Int32 pTrackId, Int64? pStartTime = n

/// <summary>
/// Saves trackpoints to file, either by adding them or by replacing the entire track. If there are no
/// track points, the file will be deleted.
/// track points, the file for the track points will be deleted.
/// </summary>
/// <param name="pTrack">The track for which we save the data</param>
/// <param name="pReplaceExisting">Set true, if the existing trackPoints should be replaced</param>
Expand All @@ -600,14 +602,12 @@ private void SaveTrackPoints(List<TrackPoint> pTrackPoints, Int32 pTrackId, Bool
} else {
trackPoints = pTrackPoints;
}
trackPoints.Sort();
if (trackPoints.Count > 0) {
trackPoints.Sort();
String[] lines = trackPoints.Select(r => r.ToString()).ToArray();
File.WriteAllLines(trackFilePath, lines);
} else {
if (File.Exists(trackFilePath)) {
File.Delete(trackFilePath);
}
this.DeleteTrackPoints(pTrackId);
}
if(pDoUpdateMetadata){
TrackMetadata metaData = this.ReadTrackMetadata(pTrackId);
Expand Down
2 changes: 1 addition & 1 deletion installScripts/sql/tracks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ AS $$ BEGIN
WHERE id = p_id;
ELSE
UPDATE tracks
SET start_utc = NULL, end_utc = NULL, start_boattime = NULL, end_boattime = NULL
SET start_utc = NULL, end_utc = NULL, start_boattime = NULL, end_boattime = NULL, date_changed = NOW()
WHERE id = p_id;
DELETE FROM track_points WHERE track_id = p_id;
DELETE FROM track_segments WHERE track_id = p_id;
Expand Down

0 comments on commit 4322fff

Please sign in to comment.