-
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.
Added a simple tool to migrate files data to the db
- Loading branch information
Showing
6 changed files
with
142 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using PiLot.Model.Nav; | ||
|
||
namespace PiLot.DataMigrator.Connectors.Files { | ||
|
||
internal class TrackDataConnector: Data.Files.TrackDataConnector { | ||
|
||
internal TrackDataConnector(String pDataRoot) : base(pDataRoot) { } | ||
|
||
/// <summary> | ||
/// Returns all GPS Tracks in an iterable way | ||
/// </summary> | ||
internal IEnumerable<Track> ReadAllTracks() { | ||
string dataPath = this.helper.GetDataPath(DATASOURCENAME); | ||
Track track; | ||
DirectoryInfo dataDir = new DirectoryInfo(dataPath); | ||
if (dataDir.Exists) { | ||
foreach (var aFile in dataDir.EnumerateFiles()) { | ||
track = this.ReadTrackPointsFromFile(aFile); | ||
if (track != null && track.HasTrackPoints) { | ||
track.StartUTC = track.FirstTrackPoint.UTC; | ||
track.EndUTC = track.LastTrackPoint.UTC; | ||
track.StartBoatTime = track.FirstTrackPoint.BoatTime.Value; | ||
track.EndBoatTime = track.LastTrackPoint.BoatTime.Value; | ||
yield return track; | ||
} | ||
} | ||
} else { | ||
Console.WriteLine($"TrackDataConnector.ReadAllTracks: gps directory not found at {dataPath}"); | ||
} | ||
} | ||
} | ||
} |
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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>disable</Nullable> | ||
<RootNamespace>PiLot.DataMigrator</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\PiLotDataFiles\PiLotDataFiles.csproj" /> | ||
<ProjectReference Include="..\PiLotDataPostgres\PiLotDataPostgres.csproj" /> | ||
<ProjectReference Include="..\PiLotData\PiLotData.csproj" /> | ||
<ProjectReference Include="..\PiLotModel\PiLotModel.csproj" /> | ||
<ProjectReference Include="..\PiLotUtils\PiLotUtils.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,81 @@ | ||
using PiLot.Model.Nav; | ||
using PiLot.Model.Logbook; | ||
using PiLot.Utils.DateAndTime; | ||
|
||
namespace PiLot.DataMigrator { | ||
|
||
/// <summary> | ||
/// Simple app to copy tracks from files based storage to the DB. Might come in | ||
/// handy for restoring data from backup. | ||
/// </summary> | ||
internal class Program { | ||
|
||
private static Connectors.Files.TrackDataConnector trackDataReader; | ||
private static Data.Files.LogbookDataConnector logbookDataReader; | ||
private static Data.Postgres.Nav.TrackDataConnector trackDataWriter; | ||
private static String defaultBoat; | ||
|
||
static void Main(string[] args) { | ||
ReadUserInput(); | ||
MoveData(); | ||
Console.WriteLine("Done"); | ||
|
||
} | ||
|
||
private static void ReadUserInput() { | ||
Console.WriteLine("Files root path:"); | ||
String input = Console.ReadLine(); | ||
trackDataReader = new Connectors.Files.TrackDataConnector(input); | ||
logbookDataReader = new Data.Files.LogbookDataConnector(input); | ||
Console.WriteLine("DB connection String:"); | ||
input = Console.ReadLine(); | ||
trackDataWriter = new Data.Postgres.Nav.TrackDataConnector(input); | ||
Console.WriteLine("Default boat:"); | ||
defaultBoat = Console.ReadLine(); | ||
Console.WriteLine(); | ||
} | ||
|
||
private static void MoveData() { | ||
DateTime dateTime; | ||
Double readMS, writeMS, analyzeMS; | ||
LogbookDay logbookDay; | ||
Model.Common.Date day; | ||
TrackAnalyzer analyzer; | ||
List<TrackSegment> segments; | ||
List<TrackSegmentType> segmentTypes = trackDataWriter.ReadTrackSegmentTypes(); | ||
dateTime = DateTime.UtcNow; | ||
foreach(Track aTrack in trackDataReader.ReadAllTracks()) { | ||
if(aTrack?.StartBoatTime != null) { | ||
try { | ||
day = new Model.Common.Date(DateTimeHelper.FromJSTime(aTrack.StartBoatTime)); | ||
logbookDay = logbookDataReader.ReadLogbookDay(day); | ||
aTrack.Boat = logbookDay?.LogbookEntries.FirstOrDefault()?.BoatSetup.BoatConfigName ?? defaultBoat; | ||
readMS = (DateTime.UtcNow - dateTime).TotalMilliseconds; | ||
dateTime = DateTime.UtcNow; | ||
trackDataWriter.InsertTrack(aTrack); | ||
writeMS = (DateTime.UtcNow - dateTime).TotalMilliseconds; | ||
dateTime = DateTime.UtcNow; | ||
analyzer = new TrackAnalyzer(aTrack); | ||
trackDataWriter.DeleteTrackSegments(aTrack.ID, null); | ||
segments = analyzer.GetTrackSegments(segmentTypes); | ||
foreach (TrackSegment aSegment in segments) { | ||
trackDataWriter.SaveTrackSegment(aSegment); | ||
} | ||
analyzeMS = (DateTime.UtcNow - dateTime).TotalMilliseconds; | ||
Console.WriteLine($"Migrated track for {day}. Boat: {aTrack.Boat}. Read: {readMS:F0} ms, write: {writeMS:F0} ms, analyze: {analyzeMS:F0} ms"); | ||
dateTime = DateTime.UtcNow; | ||
} catch(Exception ex) { | ||
Console.WriteLine($"Error: {ex.Message}"); | ||
Console.Write("Hit x to quit, any key to continue"); | ||
Char input = (Console.ReadKey().KeyChar); | ||
if((input == 'x') || (input == 'X')){ | ||
break; | ||
} | ||
} | ||
} else { | ||
Console.WriteLine("Track or StartBoatTime is null, skipping"); | ||
} | ||
} | ||
} | ||
} | ||
} |
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