Skip to content

Commit

Permalink
Added a simple tool to migrate files data to the db
Browse files Browse the repository at this point in the history
  • Loading branch information
Didosa committed Jul 8, 2024
1 parent 70db108 commit ddf8d60
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 4 deletions.
32 changes: 32 additions & 0 deletions DataMigrator/Connectors/Files/TrackDataConnector.cs
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}");
}
}
}
}
19 changes: 19 additions & 0 deletions DataMigrator/DataMigrator.csproj
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>
81 changes: 81 additions & 0 deletions DataMigrator/Program.cs
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");
}
}
}
}
}
8 changes: 7 additions & 1 deletion PiLot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiLotGPIO", "PiLotGPIO\PiLo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiLotTilesDownloader", "PiLotTilesDownloader\PiLotTilesDownloader.csproj", "{A1E25012-96D1-49AE-9E13-119B4239FB0D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PiLotData", "PiLotData\PiLotData.csproj", "{3E0B653F-0490-4D5A-A6EE-DA2F4BD3B192}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiLotData", "PiLotData\PiLotData.csproj", "{3E0B653F-0490-4D5A-A6EE-DA2F4BD3B192}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataMigrator", "DataMigrator\DataMigrator.csproj", "{A1375BDF-2A07-4171-9321-156CFA1067DE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -217,6 +219,10 @@ Global
{3E0B653F-0490-4D5A-A6EE-DA2F4BD3B192}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E0B653F-0490-4D5A-A6EE-DA2F4BD3B192}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E0B653F-0490-4D5A-A6EE-DA2F4BD3B192}.Release|Any CPU.Build.0 = Release|Any CPU
{A1375BDF-2A07-4171-9321-156CFA1067DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1375BDF-2A07-4171-9321-156CFA1067DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1375BDF-2A07-4171-9321-156CFA1067DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1375BDF-2A07-4171-9321-156CFA1067DE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 2 additions & 2 deletions PiLotDataFiles/Nav/TrackDataConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public Track ReadTrack(Int32 pTrackId) {
}

/// <summary>
/// Reads the track for a certain period from files. If there are no track points,
/// the result will be a track with an empty track points list
/// Reads the track for a certain period from files. Returns no more than one track!
/// If there are no track points, the result will be a track with an empty track points list.
/// </summary>
/// <param name="pStartTime">The start time in milliseconds, either UTC or BoatTime</param>
/// <param name="pEndTime">The end time in milliseconds, either UTC or BoatTime</param>
Expand Down
2 changes: 1 addition & 1 deletion installScripts/sql/tracks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ $BODY$;
GRANT EXECUTE ON FUNCTION read_track_points TO pilotweb;

/*-----------FUNCTION insert_track_point-----------------*/
-- inserts a track_point and updates the distance and start/end of the track
-- inserts a track_point and optionally updates the distance and start/end of the track

CREATE OR REPLACE FUNCTION public.insert_track_point(
p_track_id integer,
Expand Down

0 comments on commit ddf8d60

Please sign in to comment.