Skip to content

Commit

Permalink
New features (#4)
Browse files Browse the repository at this point in the history
* Added finer time parameters

- code refactoring
- more stats in witnessed report

* refreshed transaction models

* added test project

* introduced local cache
  • Loading branch information
rhenium2 authored Jul 4, 2022
1 parent c7af593 commit 71212ff
Show file tree
Hide file tree
Showing 40 changed files with 1,110 additions and 293 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.idea/
.idea/
HeliumCat.sln.DotSettings.user
26 changes: 26 additions & 0 deletions HeliumCat.Test/HeliumCat.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HeliumCat\HeliumCat.csproj"/>
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions HeliumCat.Test/HotspotTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using HeliumCat.Responses;
using Xunit;

namespace HeliumCat.Test;

public class HotspotTest
{
[Fact]
public void LastPocChallenge_IsNullable()
{
var hotspot = new Hotspot { LastPocChallenge = null };
Assert.Null(hotspot.LastPocChallenge);
}
}
6 changes: 6 additions & 0 deletions HeliumCat.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeliumCat", "HeliumCat\HeliumCat.csproj", "{1410624B-2CAD-430E-AD60-A5591EFAC2E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeliumCat.Test", "HeliumCat.Test\HeliumCat.Test.csproj", "{33522727-F1DA-49EB-909C-48F0DC5799B7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{1410624B-2CAD-430E-AD60-A5591EFAC2E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1410624B-2CAD-430E-AD60-A5591EFAC2E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1410624B-2CAD-430E-AD60-A5591EFAC2E5}.Release|Any CPU.Build.0 = Release|Any CPU
{33522727-F1DA-49EB-909C-48F0DC5799B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{33522727-F1DA-49EB-909C-48F0DC5799B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33522727-F1DA-49EB-909C-48F0DC5799B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33522727-F1DA-49EB-909C-48F0DC5799B7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
7 changes: 1 addition & 6 deletions HeliumCat/CommandOptions/BoxOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
namespace HeliumCat.CommandOptions;

[Verb("box", HelpText = "beacon stats of hotspots in box area")]
public class BoxOptions
public class BoxOptions : TimedOption
{
[Value(0, MetaName = "hotspot name or address", Required = true, HelpText = "hotspot name or address")]
public string Identifier { get; set; }

[Option("past", Default = 1, HelpText = "past n minutes to report")]
public int Past { get; set; }
}
10 changes: 2 additions & 8 deletions HeliumCat/CommandOptions/FrontOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
namespace HeliumCat.CommandOptions;

[Verb("front", HelpText = "beacon stats of hotspots in front semi-circle")]
public class FrontOptions
public class FrontOptions : TimedOption
{
[Value(0, MetaName = "hotspot name or address", Required = true, HelpText = "hotspot name or address")]
public string Identifier { get; set; }

[Option("past", Default = 1, HelpText = "past n minutes to report")]
public int PastMinutes { get; set; }

[Option("radius", Default = 1, HelpText = "radius n km to report")]
public int Radius { get; set; }

public override string ToString()
{
return $"{Identifier}, past {PastMinutes} minutes, {Radius}km";
return $"{Identifier}, {GetPastText()}, {Radius}km";
}
}
10 changes: 2 additions & 8 deletions HeliumCat/CommandOptions/RadiusOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
namespace HeliumCat.CommandOptions;

[Verb("radius", HelpText = "beacon stats of hotspots in a radius")]
public class RadiusOptions
public class RadiusOptions : TimedOption
{
[Value(0, MetaName = "hotspot name or address", Required = true, HelpText = "hotspot name or address")]
public string Identifier { get; set; }

[Option("past", Default = 1, HelpText = "past n minutes to report")]
public int PastMinutes { get; set; }

[Option("radius", Default = 1, HelpText = "radius n km to report")]
public int Radius { get; set; }

public override string ToString()
{
return $"{Identifier}, past {PastMinutes} minutes, {Radius}km";
return $"{Identifier}, {GetPastText()}, {Radius}km";
}
}
53 changes: 53 additions & 0 deletions HeliumCat/CommandOptions/TimedOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using CommandLine;

namespace HeliumCat.CommandOptions;

public abstract class TimedOption
{
[Value(0, MetaName = "hotspot name or address", Required = true, HelpText = "hotspot name or address")]
public string Identifier { get; set; }

[Option("past-m", Default = 10, HelpText = "past n minutes to report", Group = "past")]
public int PastMinutes { get; set; }

[Option("past-h", HelpText = "past n hours to report", Group = "past")]
public int PastHours { get; set; }

[Option("past-d", HelpText = "past n days to report", Group = "past")]
public int PastDays { get; set; }

public override string ToString()
{
return $"{Identifier}, {GetPastText()}";
}

public string GetPastText()
{
if (PastDays > 0)
{
return $"past {PastDays} days";
}

if (PastHours > 0)
{
return $"past {PastHours} hours";
}

return $"past {PastMinutes} minutes";
}

public int GetPastMinutes()
{
if (PastDays > 0)
{
return PastDays * 1440;
}

if (PastHours > 0)
{
return PastHours * 60;
}

return PastMinutes;
}
}
12 changes: 1 addition & 11 deletions HeliumCat/CommandOptions/WitnessedOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@
namespace HeliumCat.CommandOptions;

[Verb("witnessed", HelpText = "witnessed stats of hotspot")]
public class WitnessedOptions
public class WitnessedOptions : TimedOption
{
[Value(0, MetaName = "hotspot name or address", Required = true, HelpText = "hotspot name or address")]
public string Identifier { get; set; }

[Option("past", Default = 10, HelpText = "past n minutes to report")]
public int PastMinutes { get; set; }

public override string ToString()
{
return $"{Identifier}, past {PastMinutes} minutes";
}
}
94 changes: 45 additions & 49 deletions HeliumCat/Commands/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using HeliumCat.CommandOptions;
using HeliumCat.Helpers;
using HeliumCat.Responses;
using HeliumCat.Responses.Transactions;
using HeliumCat.Services;

namespace HeliumCat.Commands;
Expand All @@ -12,7 +14,7 @@ public static async Task FrontBeaconStats(FrontOptions options)
{
Console.WriteLine($"front semi-circle beacon stats ({options.ToString()})");

var minDateTime = DateTime.UtcNow.AddMinutes(-options.PastMinutes);
var minDateTime = DateTime.UtcNow.AddMinutes(-options.GetPastMinutes());

var myHotspot = await GetHotspotByIdentifier(options.Identifier);

Expand All @@ -30,9 +32,9 @@ public static async Task FrontBeaconStats(FrontOptions options)

public static async Task BoxBeaconStats(BoxOptions options)
{
Console.WriteLine($"box beacon stats for the past {options.Past} minutes ...");
Console.WriteLine($"box beacon stats for the {options.GetPastText()} ...");

var minDateTime = DateTime.UtcNow.AddMinutes(-options.Past);
var minDateTime = DateTime.UtcNow.AddMinutes(-options.GetPastMinutes());

var myHotspot = await GetHotspotByIdentifier(options.Identifier);
var challenges = await HotspotService.GetWitnessed(myHotspot.Address);
Expand All @@ -56,13 +58,13 @@ public static async Task RadiusBeaconStats(RadiusOptions options)
{
Console.WriteLine($"radius beacon stats ({options.ToString()})");

var minDateTime = DateTime.UtcNow.AddMinutes(-options.PastMinutes);
var minDateTime = DateTime.UtcNow.AddMinutes(-options.GetPastMinutes());

Console.Write("Fetching hotspots in the radius ... ");
var myHotspot = await GetHotspotByIdentifier(options.Identifier);

var hotspots = await HotspotService.GetHotspotsByRadius(myHotspot.Lat, myHotspot.Lng, options.Radius);
Console.WriteLine($"{hotspots.Count()}");
Console.WriteLine($"{hotspots.Count}");

await BeaconStats(hotspots.ToArray(), myHotspot, minDateTime);
}
Expand All @@ -76,76 +78,70 @@ public static async Task Direction(DirectionOptions options)
Console.WriteLine($"- {hotspot2.ToString()} {Extensions.GetDirectionString(hotspot1, hotspot2)}");
}

[Obsolete]
public static async Task Distance(DistanceOptions options)
{
Console.WriteLine($"distance stats ({options.ToString()}) in the last 5 days");

var myHotspot = await GetHotspotByIdentifier(options.Identifier);
Console.Write("Fetching witnessed ... ");
var witnessed = await HotspotService.GetWitnessed(myHotspot.Address);
Console.WriteLine($"{witnessed.Count}");

var witnessedDistances = witnessed.Select(w => Extensions.CalculateDistance(myHotspot, w)).ToList();

var min = Extensions.ToDistanceText(witnessedDistances.Min());
var average = Extensions.ToDistanceText(witnessedDistances.Average());
var max = Extensions.ToDistanceText(witnessedDistances.Max());
Console.WriteLine("");
Console.WriteLine("--- distance statistics ---");
Console.WriteLine($"total: {witnessedDistances.Count} min: {min} avg: {average} max: {max}");
}

public static async Task Witnessed(WitnessedOptions options)
{
var minDateTime = DateTime.UtcNow.AddMinutes(-options.PastMinutes);
var minDateTime = DateTime.UtcNow.AddMinutes(-options.GetPastMinutes());
Console.WriteLine($"witnessed stats ({options.ToString()})");

var myHotspot = await GetHotspotByIdentifier(options.Identifier);
Console.Write("Fetching witnessed ... ");
var transactions = await HotspotService.GetWitnessedTransactions(myHotspot.Address, minDateTime);
Console.WriteLine($"{transactions.Count}");

var witnessedDistances = new List<double>();
var witnessedHeights = new List<int>();
await WitnessedStats(transactions, myHotspot);
}

private static async Task WitnessedStats(List<PocReceiptsV2Transaction> transactions, Hotspot myHotspot)
{
var witnessedDistances = new Stats("distance", StatsKind.Distance);
var rewardScales = new Stats("reward-scale");
var witnessedHeights = new Stats("elevation", StatsKind.Length);
var rssiStats = new Stats("rssi");
var snrStats = new Stats("snr");
var witnessedTimes = new List<int>();

foreach (var transaction in transactions)
{
var hotspotId = transaction.Path[0].Challengee;
var witnessed = transaction.Path[0].Witnesses.Single(x => x.Gateway.Equals(myHotspot.Address));
var hotspot = await HotspotService.GetHotspot(hotspotId);
var distance = Extensions.CalculateDistance(myHotspot, hotspot);

witnessedDistances.Add(Extensions.CalculateDistance(myHotspot, hotspot));
witnessedDistances.Add(distance);
rewardScales.Add(hotspot.RewardScale);
witnessedHeights.Add(hotspot.Elevation);

Console.WriteLine(
$"- {hotspot.ToString()} {Extensions.GetDirectionString(myHotspot, hotspot)} (rssi: {witnessed.Signal}dBm, snr: {witnessed.Snr.ToString("F")}dB, freq: {witnessed.Frequency.ToString("F2")}) - {Extensions.GetRelativeTimeString(transaction.Time)}");
witnessedTimes.Add(transaction.Time);
rssiStats.Add(witnessed.Signal);
snrStats.Add(witnessed.Snr);

var directionString = Extensions.GetDirectionString(myHotspot, hotspot);
var signalString =
$"(signal: {witnessed.Signal}dBm/{witnessed.Snr.ToString("F1")}dB/{witnessed.Frequency.ToString("F1")}MHz/{Extensions.GetSignalGoodness(witnessed.Signal, distance)})";
var timeString = Extensions.GetRelativeTimeString(transaction.Time);
Console.WriteLine($"- {hotspot.ToString()} {directionString} {signalString} - {timeString}");
}

if (witnessedDistances.Any())
{
var minDistance = Extensions.ToDistanceText(witnessedDistances.Min());
var averageDistance = Extensions.ToDistanceText(witnessedDistances.Average());
var maxDistance = Extensions.ToDistanceText(witnessedDistances.Max());
Console.WriteLine("");
Console.WriteLine("--- distance statistics ---");
Console.WriteLine($"min: {minDistance} avg: {averageDistance} max: {maxDistance}");
}
Console.WriteLine("");
Console.WriteLine("--- statistics ---");

witnessedDistances.WriteToConsole();
rewardScales.WriteToConsole("F1");
witnessedHeights.WriteToConsole();
rssiStats.WriteToConsole("F1");
snrStats.WriteToConsole("F1");

if (witnessedHeights.Any())
if (witnessedTimes.Count > 1)
{
var minHeight = Extensions.ToDistanceText(witnessedHeights.Min());
var averageHeight = Extensions.ToDistanceText(witnessedHeights.Average());
var maxHeight = Extensions.ToDistanceText(witnessedHeights.Max());
var averageWitness = (witnessedTimes.Max() - witnessedTimes.Min()) / witnessedTimes.Count;
var time = TimeSpan.FromSeconds(averageWitness);

Console.WriteLine("");
Console.WriteLine("--- elevation statistics ---");
Console.WriteLine($"min: {minHeight} avg: {averageHeight} max: {maxHeight}");
Console.WriteLine($"witnessed avg every {Extensions.GetTimeSpanString(time)}");
}
}

private static async Task BeaconStats(Hotspot[] hotspots, Hotspot myHotspot, DateTime minTime)
{
Console.Write("fetching all beacons in the world ... ");
Console.Write("fetching beacons in the network ... ");
var challenges = await HotspotService.GetNetworkChallenges(minTime);
Console.WriteLine($"{challenges.Count}");

Expand Down
Loading

0 comments on commit 71212ff

Please sign in to comment.