-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/transacion_email_opt
- Loading branch information
Showing
11 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
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
src/CAServer.Application.Contracts/CAAccount/Etos/HolderExtraInfoCompletedEto.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 CAServer.Etos; | ||
|
||
public class HolderExtraInfoCompletedEto | ||
{ | ||
public string GrainId { get; set; } | ||
public string Status { get; set; } | ||
public string CaHash { get; set; } | ||
public string CaAddress { get; set; } | ||
} |
11 changes: 11 additions & 0 deletions
11
src/CAServer.Application.Contracts/CAAccount/Etos/HolderExtraInfoEto.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,11 @@ | ||
using System.Collections.Generic; | ||
using CAServer.EnumType; | ||
|
||
namespace CAServer.Etos; | ||
|
||
public class HolderExtraInfoEto | ||
{ | ||
public string GrainId { get; set; } | ||
public AccountOperationType OperationType { get; set; } | ||
public Dictionary<string,object> ExtraInfo { get; set; } | ||
} |
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
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
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,28 @@ | ||
using System; | ||
using AElf.Indexing.Elasticsearch; | ||
using CAServer.Account; | ||
using Nest; | ||
|
||
namespace CAServer.Entities.Es; | ||
|
||
public class HolderStatisticIndex : CAServerEsEntity<string>, IIndexBuild | ||
{ | ||
[Keyword] public override string Id { get; set; } | ||
[Keyword] public string CaHash { get; set; } | ||
[Keyword] public string CaAddress { get; set; } | ||
[Keyword] public string IpAddress { get; set; } | ||
[Keyword] public CountryInfo CountryInfo { get; set; } | ||
[Keyword] public string ActivityId { get; set; } | ||
[Keyword] public string Status { get; set; } | ||
[Keyword] public string OperationType { get; set; } | ||
[Keyword] public DateTime CreateTime { get; set; } | ||
} | ||
|
||
public class CountryInfo | ||
{ | ||
[Keyword] public string CountryCode { get; set; } | ||
[Keyword] public string CountryName { get; set; } | ||
[Keyword] public string RegionCode { get; set; } | ||
[Keyword] public string RegionName { get; set; } | ||
[Keyword] public string City { get; set; } | ||
} |
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
109 changes: 109 additions & 0 deletions
109
src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.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,109 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using AElf.Indexing.Elasticsearch; | ||
using CAServer.Account; | ||
using CAServer.Entities.Es; | ||
using CAServer.Etos; | ||
using CAServer.IpInfo; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.EventBus.Distributed; | ||
using Volo.Abp.ObjectMapping; | ||
|
||
namespace CAServer.EntityEventHandler.Core; | ||
|
||
public class HolderStatisticHandler : IDistributedEventHandler<HolderExtraInfoEto>, | ||
IDistributedEventHandler<HolderExtraInfoCompletedEto>, ITransientDependency | ||
{ | ||
private readonly INESTRepository<HolderStatisticIndex, string> _holderStatisticRepository; | ||
private readonly ILogger<HolderStatisticHandler> _logger; | ||
private readonly IIpInfoClient _infoClient; | ||
private readonly IObjectMapper _objectMapper; | ||
|
||
private const string IpPattern = | ||
@"^([0,1]?\d{1,2}|2([0-4][0-9]|5[0-5]))(\.([0,1]?\d{1,2}|2([0-4][0-9]|5[0-5]))){3}$"; | ||
|
||
private const string IpKey = "ip"; | ||
private const string ActivityIdKey = "activityId"; | ||
|
||
public HolderStatisticHandler(INESTRepository<HolderStatisticIndex, string> holderStatisticRepository, | ||
ILogger<HolderStatisticHandler> logger, IIpInfoClient infoClient, IObjectMapper objectMapper) | ||
{ | ||
_holderStatisticRepository = holderStatisticRepository; | ||
_logger = logger; | ||
_infoClient = infoClient; | ||
_objectMapper = objectMapper; | ||
} | ||
|
||
public async Task HandleEventAsync(HolderExtraInfoEto eventData) | ||
{ | ||
if (eventData.ExtraInfo.IsNullOrEmpty()) return; | ||
|
||
var statisticIndex = new HolderStatisticIndex | ||
{ | ||
Id = eventData.GrainId, | ||
OperationType = eventData.OperationType.ToString(), | ||
CreateTime = DateTime.UtcNow, | ||
Status = AccountOperationStatus.Pending.ToString() | ||
}; | ||
|
||
if (eventData.ExtraInfo.TryGetValue(ActivityIdKey, out var activityId)) | ||
{ | ||
statisticIndex.ActivityId = activityId.ToString(); | ||
} | ||
|
||
if (eventData.ExtraInfo.TryGetValue(IpKey, out var ip)) | ||
{ | ||
statisticIndex.IpAddress = ip.ToString(); | ||
statisticIndex.CountryInfo = await GetCountryInfoAsync(ip.ToString()); | ||
} | ||
|
||
await _holderStatisticRepository.AddOrUpdateAsync(statisticIndex); | ||
_logger.LogInformation( | ||
"save HolderExtraInfo success, grainId:{grainId},ip:{ip},country:{country},activityId:{activityId}", | ||
statisticIndex.Id, statisticIndex.IpAddress ?? "-", statisticIndex.CountryInfo?.CountryName ?? "-", | ||
statisticIndex.ActivityId ?? "-"); | ||
} | ||
|
||
public async Task HandleEventAsync(HolderExtraInfoCompletedEto eventData) | ||
{ | ||
var statisticIndex = await _holderStatisticRepository.GetAsync(eventData.GrainId); | ||
if (statisticIndex == null) return; | ||
|
||
statisticIndex.CaAddress = eventData.CaAddress; | ||
statisticIndex.CaHash = eventData.CaHash; | ||
statisticIndex.Status = eventData.Status; | ||
|
||
await _holderStatisticRepository.AddOrUpdateAsync(statisticIndex); | ||
_logger.LogInformation( | ||
"save completed HolderExtraInfo success, grainId:{grainId},ip:{ip},country:{country},activityId:{activityId}", | ||
statisticIndex.Id, statisticIndex.IpAddress ?? "-", statisticIndex.CountryInfo?.CountryName ?? "-", | ||
statisticIndex.ActivityId ?? "-"); | ||
} | ||
|
||
private async Task<CountryInfo> GetCountryInfoAsync(string ip) | ||
{ | ||
if (!(new Regex(IpPattern).IsMatch(ip))) | ||
{ | ||
return null; | ||
} | ||
|
||
var countryInfo = await _infoClient.GetCountryInfoAsync(ip); | ||
if (countryInfo == null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (countryInfo.Error != null) | ||
{ | ||
_logger.LogError("get ip info error, ip:{0}, error info:{1}", ip, | ||
JsonConvert.SerializeObject(countryInfo.Error)); | ||
return null; | ||
} | ||
|
||
return _objectMapper.Map<IpInfoDto, CountryInfo>(countryInfo); | ||
} | ||
} |