Skip to content

Commit

Permalink
Merge pull request #20 from davewalker5/BSR-36-Row-Limit
Browse files Browse the repository at this point in the history
Support optional row limit
  • Loading branch information
davewalker5 authored Aug 29, 2023
2 parents ffab8bd + 5c4c9c3 commit 21d982a
Show file tree
Hide file tree
Showing 18 changed files with 449 additions and 192 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
| ApplicationSettings | EnableSqlWriter | --enable-sql-writer | -w | Set to true to enable the SQL writer or false to disable it |
| ApplicationSettings | WriterInterval | --writer-interval | -i | Interval, in ms, at which the writer writes batches of changes from the queue to the database |
| ApplicationSettings | WriterBatchSize | --writer-batch-size | -b | Maximum number of changes to consider on each WriterInterval |
| ApplicationSettings | MaximumRows | --max-rows | -m | Maximum rows in the live table view at any one time or 0 for unlimited rows |
| ApplicationSettings | Columns | - | - | Set of column definitions for columns to be included in the output |
| ConnectionStrings | BaseStationReaderDB | - | - | SQLite connection string for the database |

Expand Down
4 changes: 2 additions & 2 deletions src/BaseStationReader.Data/BaseStationReader.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>BaseStationReader.Data</PackageId>
<PackageVersion>1.16.0.0</PackageVersion>
<PackageVersion>1.17.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) Dave Walker 2023</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -17,7 +17,7 @@
<PackageProjectUrl>https://github.com/davewalker5/ADS-B-BaseStationReader</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>1.16.0.0</ReleaseVersion>
<ReleaseVersion>1.17.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>BaseStationReader.Entities</PackageId>
<PackageVersion>1.16.0.0</PackageVersion>
<PackageVersion>1.17.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) Dave Walker 2023</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -17,7 +17,7 @@
<PackageProjectUrl>https://github.com/davewalker5/ADS-B-BaseStationReader</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>1.16.0.0</ReleaseVersion>
<ReleaseVersion>1.17.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ApplicationSettings
public bool EnableSqlWriter { get; set; }
public int WriterInterval { get; set; }
public int WriterBatchSize { get; set; }
public int MaximumRows { get; set; }
public List<TrackerColumn> Columns { get; set; } = new List<TrackerColumn>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum CommandLineOptionType
LogFile,
EnableSqlWriter,
WriterInterval,
WriterBatchSize
WriterBatchSize,
MaximumRows
}
}
4 changes: 2 additions & 2 deletions src/BaseStationReader.Logic/BaseStationReader.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>BaseStationReader.Logic</PackageId>
<PackageVersion>1.16.0.0</PackageVersion>
<PackageVersion>1.17.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) Dave Walker 2023</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -17,7 +17,7 @@
<PackageProjectUrl>https://github.com/davewalker5/ADS-B-BaseStationReader</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>1.16.0.0</ReleaseVersion>
<ReleaseVersion>1.17.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/BaseStationReader.Logic/ConfigReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using BaseStationReader.Entities.Config;
using BaseStationReader.Entities.Tracking;
using Microsoft.Extensions.Configuration;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime;

namespace BaseStationReader.Logic
{
Expand All @@ -13,12 +16,23 @@ public static class ConfigReader
/// <returns></returns>
public static ApplicationSettings? Read(string jsonFileName)
{
// Set up the configuration reader
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile(jsonFileName)
.Build();

// Read the application settings section
IConfigurationSection section = configuration.GetSection("ApplicationSettings");
var settings = section.Get<ApplicationSettings>();

// Add to the column definitions the property info objects associated with the associated property
// of the Aircraft object
var allProperties = typeof(Aircraft).GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var column in settings!.Columns)
{
column.Info = allProperties.FirstOrDefault(x => x.Name == column.Property);
}

return settings;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ReleaseVersion>1.16.0.0</ReleaseVersion>
<FileVersion>1.16.0.0</FileVersion>
<ProductVersion>1.16.0</ProductVersion>
<ReleaseVersion>1.17.0.0</ReleaseVersion>
<FileVersion>1.17.0.0</FileVersion>
<ProductVersion>1.17.0</ProductVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BaseStationReader.Terminal.Interfaces
{
internal interface ITrackerIndexManager
{
void AddAircraft(string address, int rowNumber);
int FindAircraft(string address);
int RemoveAircraft(string address);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using BaseStationReader.Entities.Config;

namespace BaseStationReader.Terminal.Interfaces
{
internal interface ITrackerSettingsBuilder
{
ApplicationSettings? BuildSettings(IEnumerable<string> args, string configJsonPath);
}
}
15 changes: 15 additions & 0 deletions src/BaseStationReader.Terminal/Interfaces/ITrackerTableManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using BaseStationReader.Entities.Tracking;
using Spectre.Console;

namespace BaseStationReader.Terminal.Interfaces
{
internal interface ITrackerTableManager
{
Table? Table { get; }

int AddAircraft(Aircraft aircraft);
void CreateTable(string title);
int RemoveAircraft(Aircraft aircraft);
void UpdateAircraft(Aircraft aircraft);
}
}
83 changes: 83 additions & 0 deletions src/BaseStationReader.Terminal/Logic/TrackerIndexManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using BaseStationReader.Terminal.Interfaces;
using System.Diagnostics.CodeAnalysis;

namespace BaseStationReader.Terminal.Logic
{
[ExcludeFromCodeCoverage]
internal class TrackerIndexManager : ITrackerIndexManager
{
private readonly Dictionary<string, int> _rowIndex = new();

/// <summary>
/// Add an aircraft ICAO address to the index associated with a given row number
/// </summary>
/// <param name="address"></param>
/// <param name="rowNumber"></param>
public void AddAircraft(string address, int rowNumber)
{
lock (_rowIndex)
{
Shuffle(rowNumber, 1);
_rowIndex.Add(address, rowNumber);
}
}

/// <summary>
/// Find an aircraft ICAO address in the index and return its row number
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int FindAircraft(string address)
{
int rowNumber = -1;

lock (_rowIndex)
{
if (_rowIndex.ContainsKey(address))
{
rowNumber = _rowIndex[address];
}
}

return rowNumber;
}

/// <summary>
/// Remove the entry for the specified ICAO address and return the row it was at before removal
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int RemoveAircraft(string address)
{
int rowNumber = -1;

lock (_rowIndex)
{
if (_rowIndex.ContainsKey(address))
{
rowNumber = _rowIndex[address];
_rowIndex.Remove(address);
Shuffle(rowNumber, -1);
}
}

return rowNumber;
}

/// <summary>
/// Shuffle the index for all rows *after* the one specified, either up or down. The assumption is that
/// the caller will take out a lock to prevent concurrent attempts
/// </summary>
/// <param name="fromRow"></param>
private void Shuffle(int fromRow, int increment)
{
foreach (var entry in _rowIndex)
{
if (entry.Value >= fromRow)
{
_rowIndex[entry.Key] += increment;
}
}
}
}
}
78 changes: 78 additions & 0 deletions src/BaseStationReader.Terminal/Logic/TrackerSettingsBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using BaseStationReader.Entities.Config;
using BaseStationReader.Logic;
using BaseStationReader.Terminal.Interfaces;
using System.Diagnostics.CodeAnalysis;

namespace BaseStationReader.Terminal.Logic
{
[ExcludeFromCodeCoverage]
internal class TrackerSettingsBuilder : ITrackerSettingsBuilder
{
/// <summary>
/// Construct the application settings from the configuration file and any command line arguments
/// </summary>
/// <param name="args"></param>
/// <param name="configJsonPath"></param>
/// <returns></returns>
public ApplicationSettings? BuildSettings(IEnumerable<string> args, string configJsonPath)
{
// Read the config file to provide default settings
var settings = ConfigReader.Read(configJsonPath);

// Parse the command line
var parser = new CommandLineParser();
parser.Add(CommandLineOptionType.Host, false, "--host", "-h", "Host to connect to for data stream", 1, 1);
parser.Add(CommandLineOptionType.Port, false, "--port", "-p", "Port to connect to for data stream", 1, 1);
parser.Add(CommandLineOptionType.SocketReadTimeout, false, "--read-timeout", "-t", "Timeout (ms) for socket read operations", 1, 1);
parser.Add(CommandLineOptionType.ApplicationTimeout, false, "--app-timeout", "-a", "Timeout (ms) after which the application will quit of no messages are recieved", 1, 1);
parser.Add(CommandLineOptionType.TimeToRecent, false, "--recent", "-r", "Time (ms) to 'recent' staleness", 1, 1);
parser.Add(CommandLineOptionType.TimeToStale, false, "--stale", "-s", "Time (ms) to 'stale' staleness", 1, 1);
parser.Add(CommandLineOptionType.TimeToRemoval, false, "--remove", "-x", "Time (ms) removal of stale records", 1, 1);
parser.Add(CommandLineOptionType.LogFile, false, "--log-file", "-l", "Log file path and name", 1, 1);
parser.Add(CommandLineOptionType.EnableSqlWriter, false, "--enable-sql-writer", "-w", "Log file path and name", 1, 1);
parser.Add(CommandLineOptionType.WriterInterval, false, "--writer-interval", "-i", "SQL write interval (ms)", 1, 1);
parser.Add(CommandLineOptionType.WriterBatchSize, false, "--writer-batch-size", "-b", "SQL write batch size", 1, 1);
parser.Add(CommandLineOptionType.MaximumRows, false, "--max-rows", "-m", "Maximum number of rows displayed", 1, 1);
parser.Parse(args);

// Apply the command line values over the defaults
var values = parser.GetValues(CommandLineOptionType.Host);
if (values != null) settings!.Host = values[0];

values = parser.GetValues(CommandLineOptionType.Port);
if (values != null) settings!.Port = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.SocketReadTimeout);
if (values != null) settings!.SocketReadTimeout = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.ApplicationTimeout);
if (values != null) settings!.ApplicationTimeout = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.TimeToRecent);
if (values != null) settings!.TimeToRecent = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.TimeToStale);
if (values != null) settings!.TimeToStale = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.TimeToRemoval);
if (values != null) settings!.TimeToRemoval = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.LogFile);
if (values != null) settings!.LogFile = values[0];

values = parser.GetValues(CommandLineOptionType.EnableSqlWriter);
if (values != null) settings!.EnableSqlWriter = bool.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.WriterInterval);
if (values != null) settings!.WriterInterval = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.WriterBatchSize);
if (values != null) settings!.WriterBatchSize = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.MaximumRows);
if (values != null) settings!.MaximumRows = int.Parse(values[0]);

return settings;
}
}
}
Loading

0 comments on commit 21d982a

Please sign in to comment.