Skip to content

Commit

Permalink
Can now lookup lon/lat using two different services.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgauffin committed Sep 22, 2020
1 parent e32b53f commit 93d3da3
Show file tree
Hide file tree
Showing 16 changed files with 480 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public static void AssignProperties(this IConfigurationSection section, IDiction
foreach (var kvp in settings)
{
var property = type.GetProperty(kvp.Key);
if (property == null)
continue;

var propertyType = property.PropertyType;
if (propertyType == typeof(Uri))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Data;
using System.Diagnostics.CodeAnalysis;

namespace Coderr.Server.Domain.Modules.ErrorOrigins
Expand All @@ -9,6 +8,9 @@ namespace Coderr.Server.Domain.Modules.ErrorOrigins
/// </summary>
public class ErrorOrigin
{
public const double EmptyLatitude = 91;
public const double EmptyLongitude = 181;

/// <summary>
/// Creates a new instance of <see cref="ErrorOrigin" />.
/// </summary>
Expand All @@ -25,6 +27,19 @@ public ErrorOrigin(string ipAddress, double longitude, double latitude)
Latitude = latitude;
}

public ErrorOrigin(string ipAddress)
{
IpAddress = ipAddress ?? throw new ArgumentNullException(nameof(ipAddress));
Longitude = EmptyLongitude;
Latitude = EmptyLatitude;
}

/// <summary>
/// For the mapper.
/// </summary>
protected ErrorOrigin()
{
}

/// <summary>
/// City reported by the lookup service.
Expand All @@ -42,17 +57,29 @@ public ErrorOrigin(string ipAddress, double longitude, double latitude)
/// </summary>
public string CountryName { get; set; }

public int Id { get; set; }

/// <summary>
/// IP address that we received the report from
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Ip")]
public string IpAddress { get; private set; }

/// <summary>Longitude that the IP lookup service returned.</summary>
public double Latitude { get; private set; }
/// <remarks>
/// 91 if not specified
/// </remarks>
public double Latitude { get; set; }

/// <summary>Latitude that the IP lookup service returned.</summary>
public double Longitude { get; private set; }
/// <summary>
/// Latitude that the IP lookup service returned.
/// </summary>
/// <remarks>
/// <para>
/// 181 if not specified.
/// </para>
/// </remarks>
public double Longitude { get; set; }

/// <summary>
/// TODO: WTF IS THIS?!
Expand Down
10 changes: 5 additions & 5 deletions src/Server/Coderr.Server.ReportAnalyzer/Boot/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ namespace Coderr.Server.ReportAnalyzer.Boot
{
public class Bootstrapper : IAppModule
{
private readonly ReportAnalyzerModuleStarter _reportAnalyzerReportAnalyzerModuleStarter;
private readonly ReportAnalyzerModuleStarter _reportAnalyzerModuleStarter;
private ServiceProvider _serviceProvider;

public Bootstrapper()
{
_reportAnalyzerReportAnalyzerModuleStarter = new ReportAnalyzerModuleStarter();
_reportAnalyzerModuleStarter = new ReportAnalyzerModuleStarter();
}

public void Configure(ConfigurationContext context)
Expand All @@ -32,7 +32,7 @@ public void Configure(ConfigurationContext context)
Services = serviceCollection
};
var ignoredModules = context.Configuration.GetSection("DisabledModules:ReportAnalyzer");
_reportAnalyzerReportAnalyzerModuleStarter.Configure(ourContext, new ServerConfigSectionWrapper(ignoredModules));
_reportAnalyzerModuleStarter.Configure(ourContext, new ServerConfigSectionWrapper(ignoredModules));

RegisterConfigurationStores(ourContext);
ourContext.Services.AddScoped<IPrincipalAccessor, MessagingPrincipalWrapper>();
Expand All @@ -56,13 +56,13 @@ public void Start(StartContext context)
{
ServiceProvider = _serviceProvider
};
_reportAnalyzerReportAnalyzerModuleStarter.Start(ourStartContext);
_reportAnalyzerModuleStarter.Start(ourStartContext);
}


public void Stop()
{
_reportAnalyzerReportAnalyzerModuleStarter.Stop();
_reportAnalyzerModuleStarter.Stop();
}

private IServiceProvider GetServiceProvider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ private QueueListener ConfigureQueueListener(ConfigurationContext context, strin
listener.ScopeCreated += (sender, args) =>
{
args.Scope.ResolveDependency<IPrincipalAccessor>().First().Principal = args.Principal;

_logger.Debug(
$"[{inboundQueueName}] Running {args.Message.Body}, Credentials: {args.Principal.ToFriendlyString()}");
};
listener.ScopeClosing += (sender, args) =>
{
Expand Down Expand Up @@ -160,6 +157,7 @@ private void DiagnosticLog(LogLevel level, string queueNameOrMessageName, string

private void HaveRun(Task obj)
{
_logger.Info("Stop completed for a listener in the ReportQueueModule. " + obj);
}

private IMessageInvoker MessageInvokerFactory(IHandlerScope arg)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
using System.Collections.Generic;
using Coderr.Server.Abstractions.Config;
using Coderr.Server.Infrastructure.Configuration;

namespace Coderr.Server.ReportAnalyzer.ErrorOrigins.Handlers
{
public class OriginsConfiguration : IConfigurationSection
{
/// <summary>
/// API key for ipstack
/// </summary>
public string ApiKey { get; set; }

/// <summary>
/// API key for LocationIQ.com
/// </summary>
public string LocationIqApiKey { get; set; }

/// <summary>
/// API key for mapquest.com
/// </summary>
public string MapQuestApiKey { get; set; }

string IConfigurationSection.SectionName { get; } = "Origins";

IDictionary<string, string> IConfigurationSection.ToDictionary()
{
return this.ToConfigDictionary();
}


void IConfigurationSection.Load(IDictionary<string, string> settings)
{
this.AssignProperties(settings);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Coderr.Server.Abstractions.Config;
using Coderr.Server.Domain.Modules.ErrorOrigins;
using Coderr.Server.ReportAnalyzer.Abstractions.ErrorReports;
using Coderr.Server.ReportAnalyzer.Abstractions.Incidents;
using DotNetCqs;
using log4net;
using Newtonsoft.Json.Linq;
using Coderr.Server.Abstractions.Config;
using Coderr.Server.ReportAnalyzer;
using Coderr.Server.ReportAnalyzer.Abstractions.ErrorReports;

namespace Coderr.Server.ReportAnalyzer.ErrorOrigins.Handlers
{
Expand All @@ -21,14 +17,15 @@ public class StorePositionFromNewReport : IMessageHandler<ReportAddedToIncident>
{
private readonly ILog _logger = LogManager.GetLogger(typeof(StorePositionFromNewReport));
private readonly IErrorOriginRepository _repository;
private IConfiguration<OriginsConfiguration> _originConfiguration;
private readonly IConfiguration<OriginsConfiguration> _originConfiguration;

/// <summary>
/// Creates a new instance of <see cref="StorePositionFromNewReport" />.
/// </summary>
/// <param name="repository">repos</param>
/// <exception cref="ArgumentNullException">repository</exception>
public StorePositionFromNewReport(IErrorOriginRepository repository, IConfiguration<OriginsConfiguration> originConfiguration)
public StorePositionFromNewReport(IErrorOriginRepository repository,
IConfiguration<OriginsConfiguration> originConfiguration)
{
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
_originConfiguration = originConfiguration;
Expand All @@ -43,16 +40,23 @@ public StorePositionFromNewReport(IErrorOriginRepository repository, IConfigurat
/// </returns>
public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
{
// Random swedish IP for testing purposes
if (e.Report.RemoteAddress == "::1" || e.Report.RemoteAddress == "127.0.0.1")
e.Report.RemoteAddress = "94.254.57.227";

var numberStyles = NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign;
var collection = e.Report.GetCoderrCollection();
if (collection != null)
{
var latitude = 0d;
var longitude = 0d;
var gotLat = collection.Properties.TryGetValue("Longitude", out var longitudeStr)
&& double.TryParse(longitudeStr, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out longitude);
&& double.TryParse(longitudeStr, numberStyles,
CultureInfo.InvariantCulture, out longitude);

var gotLong = collection.Properties.TryGetValue("Latitude", out var latitudeStr)
&& double.TryParse(latitudeStr, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out latitude);
&& double.TryParse(latitudeStr, numberStyles,
CultureInfo.InvariantCulture, out latitude);
if (gotLat && latitude > 0 && gotLong && longitude > 0)
{
var errorOrigin2 = new ErrorOrigin(e.Report.RemoteAddress, longitude, latitude);
Expand All @@ -65,66 +69,24 @@ public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
var latitude1 = e.Report.FindCollectionProperty("ReportLatitude");
var longitude1 = e.Report.FindCollectionProperty("ReportLongitude");
if (longitude1 != null
&& double.TryParse(latitude1, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var latitude2)
&& double.TryParse(latitude1, numberStyles, CultureInfo.InvariantCulture,
out var latitude2)
&& latitude1 != null
&& double.TryParse(longitude1, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var longitude2))
&& double.TryParse(longitude1, numberStyles, CultureInfo.InvariantCulture,
out var longitude2))
{
var errorOrigin2 = new ErrorOrigin(e.Report.RemoteAddress, longitude2, latitude2);
await _repository.CreateAsync(errorOrigin2, e.Incident.ApplicationId, e.Incident.Id, e.Report.Id);
return;
}


if (string.IsNullOrEmpty(e.Report.RemoteAddress) || string.IsNullOrEmpty(_originConfiguration.Value?.ApiKey))
{
return;
}


// Random swedish IP for testing purposes
if (e.Report.RemoteAddress == "::1" || e.Report.RemoteAddress == "127.0.0.1")
e.Report.RemoteAddress = "94.254.57.227";
if (string.IsNullOrEmpty(e.Report.RemoteAddress) ||
string.IsNullOrEmpty(_originConfiguration.Value?.ApiKey)) return;

var errorOrigin = await LookupIpAddress(e);
await _repository.CreateAsync(errorOrigin, e.Incident.ApplicationId, e.Incident.Id, e.Report.Id);
}

private async Task<ErrorOrigin> LookupIpAddress(ReportAddedToIncident e)
{
var url = $"http://api.ipstack.com/{e.Report.RemoteAddress}?access_key={_originConfiguration.Value.ApiKey}";
var request = WebRequest.CreateHttp(url);
var json = "";
ErrorOrigin errorOrigin;
try
{
var response = await request.GetResponseAsync();
var stream = response.GetResponseStream();
var reader = new StreamReader(stream);
json = await reader.ReadToEndAsync();
var jsonObj = JObject.Parse(json);

/* /*{"ip":"94.254.21.175","country_code":"SE","country_name":"Sweden","region_code":"10","region_name":"Dalarnas Lan","city":"Falun","zipcode":"",
* "latitude":60.6,"longitude":15.6333,
* "metro_code":"","areacode":""}*/

var lat = double.Parse(jsonObj["latitude"].Value<string>(), CultureInfo.InvariantCulture);
var lon = double.Parse(jsonObj["longitude"].Value<string>(), CultureInfo.InvariantCulture);
errorOrigin = new ErrorOrigin(e.Report.RemoteAddress, lon, lat)
{
City = jsonObj["city"].ToString(),
CountryCode = jsonObj["country_code"].ToString(),
CountryName = jsonObj["country_name"].ToString(),
RegionCode = jsonObj["region_code"].ToString(),
RegionName = jsonObj["region_name"].ToString(),
ZipCode = jsonObj["zip"].ToString()
};
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to call lookupService or parse the JSON: {json}.", exception);
}

return errorOrigin;
var origin = new ErrorOrigin(e.Report.RemoteAddress);
await _repository.CreateAsync(origin, e.Incident.ApplicationId, e.Incident.Id, e.Report.Id);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Coderr.Server.Domain.Modules.ErrorOrigins;

Expand All @@ -22,6 +23,9 @@ public interface IErrorOriginRepository
/// <returns>task</returns>
/// <exception cref="ArgumentNullException">origin</exception>
Task CreateAsync(ErrorOrigin entity, int applicationId, int incidentId, int reportId);


Task<IList<ErrorOrigin>> GetPendingOrigins();

Task Update(ErrorOrigin entity);
}
}
Loading

0 comments on commit 93d3da3

Please sign in to comment.