-
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.
- Loading branch information
n.bitounis
committed
Jun 21, 2020
1 parent
5fa7d1a
commit 116d1de
Showing
21 changed files
with
536 additions
and
14 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
Projects/SesNotifications.App/Controllers/MonitorRuleController.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Text.RegularExpressions; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace SesNotifications.App.Helpers | ||
{ | ||
public static class MatchingHelpers | ||
{ | ||
public static JToken TokenizeJson(this string json) | ||
{ | ||
return JToken.Parse(json); | ||
} | ||
|
||
public static JToken FindToken(this JToken token, string jsonMatcher) | ||
{ | ||
return token.SelectToken(jsonMatcher); | ||
} | ||
|
||
public static bool IsMatch(this string value, string regex) | ||
{ | ||
return new Regex(regex).IsMatch(value); | ||
} | ||
} | ||
} |
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,13 @@ | ||
namespace SesNotifications.App.Models | ||
{ | ||
public enum MonitorRuleType | ||
{ | ||
BounceEvent = 0, | ||
ComplaintNotification, | ||
ComplaintEvent, | ||
DeliveryNotification, | ||
DeliveryEvent, | ||
OpenEvent, | ||
SendEvent | ||
} | ||
} |
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,10 @@ | ||
namespace SesNotifications.App.Models | ||
{ | ||
public class SqsConfiguration | ||
{ | ||
public virtual string QueueUrl { get; set; } | ||
public virtual string Region { get; set; } | ||
public virtual string AccessKey { get; set; } | ||
public virtual string SecretKey { get; set; } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Projects/SesNotifications.App/Services/Interfaces/IRuleService.cs
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,9 @@ | ||
using SesNotifications.App.Models; | ||
|
||
namespace SesNotifications.App.Services.Interfaces | ||
{ | ||
public interface IRuleService | ||
{ | ||
void ProcessMessage(string json, MonitorRuleType ruleType); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Projects/SesNotifications.App/Services/Interfaces/ISqsNotifier.cs
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,9 @@ | ||
using SesNotifications.App.Models; | ||
|
||
namespace SesNotifications.App.Services.Interfaces | ||
{ | ||
public interface ISqsNotifier | ||
{ | ||
void Notify(string header, string message, SqsConfiguration configuration); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using NLog; | ||
using SesNotifications.App.Helpers; | ||
using SesNotifications.App.Models; | ||
using SesNotifications.App.Services.Interfaces; | ||
using SesNotifications.DataAccess.Repositories.Interfaces; | ||
|
||
namespace SesNotifications.App.Services | ||
{ | ||
public class RuleService : IRuleService | ||
{ | ||
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); | ||
|
||
private readonly IMonitorRuleRepository _monitorRuleRepository; | ||
private readonly ISqsNotifier _sqsNotifier; | ||
private readonly SqsConfiguration _sqsConfiguration; | ||
|
||
public RuleService(IConfigurationRepository configurationRepository, | ||
IMonitorRuleRepository monitorRuleRepository, | ||
ISqsNotifier sqsNotifier) | ||
{ | ||
_monitorRuleRepository = monitorRuleRepository; | ||
_sqsNotifier = sqsNotifier; | ||
|
||
var sqsConfig = configurationRepository.GetByKey("sqs_notification_config"); | ||
if (sqsConfig == null) | ||
{ | ||
Logger.Debug("No SQS notification configuration found"); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
_sqsConfiguration = JsonConvert.DeserializeObject<SqsConfiguration>(sqsConfig.Value); | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.Error(e, "SQS notification configuration is invalid"); | ||
_sqsConfiguration = null; | ||
} | ||
} | ||
|
||
public void ProcessMessage(string json, MonitorRuleType ruleType) | ||
{ | ||
if (_sqsConfiguration == null) | ||
{ | ||
return; | ||
} | ||
|
||
var rules = _monitorRuleRepository.GetAll(); | ||
var typeRules = rules.Where(x => x.SesMessage.ToLower() == ruleType.ToString().ToLower()).ToList(); | ||
|
||
if (typeRules.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
var o = json.TokenizeJson(); | ||
|
||
foreach (var rule in typeRules) | ||
{ | ||
var extracted = o.FindToken(rule.JsonMatcher); | ||
if (extracted == null) | ||
{ | ||
break; | ||
} | ||
|
||
var isMatch = extracted.ToString().IsMatch(rule.Regex); | ||
|
||
if (isMatch) | ||
{ | ||
_sqsNotifier.Notify($"Rule {rule.Name} match", extracted.ToString(), _sqsConfiguration); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Threading; | ||
using Amazon; | ||
using Amazon.Runtime; | ||
using Amazon.SQS; | ||
using Amazon.SQS.Model; | ||
using NLog; | ||
using SesNotifications.App.Models; | ||
using SesNotifications.App.Services.Interfaces; | ||
|
||
namespace SesNotifications.App.Services | ||
{ | ||
public class SqsNotifier : ISqsNotifier, IDisposable | ||
{ | ||
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); | ||
|
||
private AmazonSQSClient _sqsClient; | ||
|
||
public void Notify(string header, string message, SqsConfiguration configuration) | ||
{ | ||
var client = CreateClient(configuration); | ||
|
||
try | ||
{ | ||
var response = client.SendMessageAsync(new SendMessageRequest | ||
{ | ||
QueueUrl = configuration.QueueUrl, | ||
MessageBody = message, | ||
MessageAttributes = new Dictionary<string, MessageAttributeValue> | ||
{ | ||
["Title"] = new MessageAttributeValue { DataType = "String", StringValue = header } | ||
} | ||
}, CancellationToken.None).Result; | ||
|
||
if (response.HttpStatusCode != HttpStatusCode.OK) | ||
{ | ||
Logger.Error($"Error while sending SNS notification, status code {response.HttpStatusCode}"); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.Error(e, "Unexpected error while sending SQS notification"); | ||
} | ||
} | ||
|
||
private AmazonSQSClient CreateClient(SqsConfiguration configuration) | ||
{ | ||
if (_sqsClient == null) | ||
{ | ||
_sqsClient = string.IsNullOrEmpty(configuration.AccessKey) | ||
? new AmazonSQSClient(new AmazonSQSConfig | ||
{ | ||
RegionEndpoint = RegionEndpoint.GetBySystemName(configuration.Region) | ||
}) | ||
: new AmazonSQSClient(new BasicAWSCredentials(configuration.AccessKey, configuration.SecretKey), | ||
RegionEndpoint.GetBySystemName(configuration.Region)); | ||
} | ||
|
||
return _sqsClient; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_sqsClient?.Dispose(); | ||
} | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
Projects/SesNotifications.DataAccess/Entities/ConfigurationItem.cs
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,9 @@ | ||
namespace SesNotifications.DataAccess.Entities | ||
{ | ||
public class ConfigurationItem | ||
{ | ||
public virtual int Id { get; set; } | ||
public virtual string Key { get; set; } | ||
public virtual string Value { get; set; } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Projects/SesNotifications.DataAccess/Mappings/ConfigurationMap.cs
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,17 @@ | ||
using FluentNHibernate.Mapping; | ||
using SesNotifications.DataAccess.Entities; | ||
|
||
namespace SesNotifications.DataAccess.Mappings | ||
{ | ||
public class ConfigurationMap : ClassMap<ConfigurationItem> | ||
{ | ||
public ConfigurationMap() | ||
{ | ||
Table("ses_notifications.configuration"); | ||
Id(x => x.Id).Column("id").GeneratedBy.Identity(); | ||
Map(x => x.Key).Column("key"); | ||
Map(x => x.Value).Column("value"); | ||
Cache.ReadOnly(); | ||
} | ||
} | ||
} |
Oops, something went wrong.