-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Backup for Tracks, use new file structure for track backup.…
… Not much tested yet.
- Loading branch information
Showing
19 changed files
with
341 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
using PiLot.Backup.API.ActionFilters; | ||
using PiLot.Backup.API.Helpers; | ||
using PiLot.Model.Nav; | ||
using PiLot.Model.Users; | ||
using PiLot.Utils.DateAndTime; | ||
using PiLot.Utils.Logger; | ||
|
||
namespace PiLot.Backup.API.Controllers { | ||
|
||
[ApiController] | ||
public class TracksController : ControllerBase { | ||
|
||
/// <summary> | ||
/// Accepts puts with a track and saves the data to the backup folder. If the folder does | ||
/// not exist yet, it will be created and be pre-populated with the latest backup data. | ||
/// </summary> | ||
/// <param name="track">A Track</param> | ||
/// <param name="backupTime">The unix timestamp for the backup set to create/use</param> | ||
[Route(Program.APIROOT + "[controller]")] | ||
[HttpPut] | ||
[ServiceFilter(typeof(BackupAuthorizationFilter))] | ||
public ActionResult Put(Track track, Int32 backupTime) { | ||
ActionResult result; | ||
Object userObj = this.HttpContext.Items["user"]; | ||
if(userObj != null) { | ||
User user = (User)userObj; | ||
try { | ||
DateTime time = DateTimeHelper.FromUnixTime(backupTime); | ||
BackupHelper.BackupTrack(track, user.Username, time); | ||
result = this.Ok(); | ||
} catch (Exception ex) { | ||
result = this.StatusCode(StatusCodes.Status500InternalServerError, ex.Message); | ||
Logger.Log(ex, this.HttpContext.Request.Path.ToString()); | ||
} | ||
} else { | ||
result = this.Unauthorized(); | ||
} | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using Npgsql; | ||
using PiLot.Backup.Client.Model; | ||
using PiLot.Model.Nav; | ||
using PiLot.Utils.Logger; | ||
|
||
namespace PiLot.Backup.Client.Data { | ||
|
||
/// <summary> | ||
/// Helper for reading tracks for backup | ||
/// </summary> | ||
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 | ||
/// </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> | ||
internal BackupTaskData<List<Int32>> GetChangedTracks(DateTime pChangedAfter) { | ||
Logger.Log("TrackDataconnector.ReadLatestChange", LogLevels.DEBUG); | ||
Int32 tracksCount = 0; | ||
List<Int32?> changedTrackIDs = new List<Int32?>(0); | ||
NpgsqlConnection connection = this.dbHelper.GetConnection(); | ||
if (connection != null) { | ||
connection.Open(); | ||
NpgsqlTransaction transaction = connection.BeginTransaction(IsolationLevel.RepeatableRead); | ||
try { | ||
String tracksCountQuery = "SELECT count(id) FROM tracks WHERE start_utc IS NOT NULL;"; | ||
tracksCount = this.dbHelper.ReadValue<Int32?>(tracksCountQuery, transaction) ?? 0; | ||
String changedTrackIDsQuery = "SELECT id FROM tracks WHERE date_changed >= @p_date_changed"; | ||
List<(String, Object)> changedTrackIDsPars = new List<(String, Object)>(); | ||
changedTrackIDsPars.Add(("@p_date_changed", pChangedAfter)); | ||
changedTrackIDs = this.dbHelper.ReadData<Int32?>(changedTrackIDsQuery, new Func<NpgsqlDataReader, Int32?>(this.dbHelper.ReadNullableField<Int32?>), changedTrackIDsPars); | ||
transaction.Commit(); | ||
connection.Close(); | ||
} catch (Exception ex) { | ||
Logger.Log(ex, "TrackDataConnector.InsertTrack"); | ||
transaction.Rollback(); | ||
connection.Close(); | ||
throw; | ||
} | ||
} | ||
return new BackupTaskData<List<Int32>>() { | ||
ChangedItems = changedTrackIDs.FindAll(id => id != null).Select(id => id.Value).ToList(), | ||
TotalItems = tracksCount | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.