From bc284177b705cc98f47ed719e3e9beff490979c7 Mon Sep 17 00:00:00 2001 From: felix03zhao Date: Thu, 26 Sep 2024 18:05:39 +0800 Subject: [PATCH 1/3] feat: add holder info statistic. --- .../CAAccount/Dtos/RegisterRequestDto.cs | 2 + .../Etos/HolderExtraInfoCompletedEto.cs | 9 ++ .../CAAccount/Etos/HolderExtraInfoEto.cs | 11 +++ .../CAAccount/CAAccountAppService.cs | 14 +++ .../Entities/Es/HolderStatisticIndex.cs | 18 ++++ .../CAAccountHandler.cs | 8 ++ .../HolderStatisticHandler.cs | 87 +++++++++++++++++++ 7 files changed, 149 insertions(+) create mode 100644 src/CAServer.Application.Contracts/CAAccount/Etos/HolderExtraInfoCompletedEto.cs create mode 100644 src/CAServer.Application.Contracts/CAAccount/Etos/HolderExtraInfoEto.cs create mode 100644 src/CAServer.Domain/Entities/Es/HolderStatisticIndex.cs create mode 100644 src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs diff --git a/src/CAServer.Application.Contracts/CAAccount/Dtos/RegisterRequestDto.cs b/src/CAServer.Application.Contracts/CAAccount/Dtos/RegisterRequestDto.cs index f013da6d6..148000aa9 100644 --- a/src/CAServer.Application.Contracts/CAAccount/Dtos/RegisterRequestDto.cs +++ b/src/CAServer.Application.Contracts/CAAccount/Dtos/RegisterRequestDto.cs @@ -27,6 +27,8 @@ public class RegisterRequestDto : IValidatableObject public string AccessToken { get; set; } public ZkLoginInfoRequestDto ZkLoginInfo { get; set; } + + public Dictionary ExtraInfo { get; set; } = new(); public IEnumerable Validate( ValidationContext validationContext) diff --git a/src/CAServer.Application.Contracts/CAAccount/Etos/HolderExtraInfoCompletedEto.cs b/src/CAServer.Application.Contracts/CAAccount/Etos/HolderExtraInfoCompletedEto.cs new file mode 100644 index 000000000..afb8a3923 --- /dev/null +++ b/src/CAServer.Application.Contracts/CAAccount/Etos/HolderExtraInfoCompletedEto.cs @@ -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; } +} \ No newline at end of file diff --git a/src/CAServer.Application.Contracts/CAAccount/Etos/HolderExtraInfoEto.cs b/src/CAServer.Application.Contracts/CAAccount/Etos/HolderExtraInfoEto.cs new file mode 100644 index 000000000..b7f0ac72e --- /dev/null +++ b/src/CAServer.Application.Contracts/CAAccount/Etos/HolderExtraInfoEto.cs @@ -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 ExtraInfo { get; set; } +} \ No newline at end of file diff --git a/src/CAServer.Application/CAAccount/CAAccountAppService.cs b/src/CAServer.Application/CAAccount/CAAccountAppService.cs index f8133e998..893a51884 100644 --- a/src/CAServer.Application/CAAccount/CAAccountAppService.cs +++ b/src/CAServer.Application/CAAccount/CAAccountAppService.cs @@ -14,6 +14,7 @@ using CAServer.ContractService; using CAServer.Device; using CAServer.Dtos; +using CAServer.EnumType; using CAServer.Etos; using CAServer.Grains; using CAServer.Grains.Grain.Account; @@ -143,6 +144,7 @@ public async Task RegisterRequestAsync(RegisterRequestDto inpu registerCreateEto.IpAddress = _ipInfoAppService.GetRemoteIp(input.ReferralInfo?.Random); await CheckAndResetReferralInfo(input.ReferralInfo, registerCreateEto.IpAddress); await _distributedEventBus.PublishAsync(registerCreateEto); + await PublishExtraInfoAsync(registerCreateEto.ChainId, input.ExtraInfo); return new AccountResultDto(registerDto.Id.ToString()); } @@ -793,4 +795,16 @@ await _contractProvider.GetHolderInfoAsync(null, Hash.LoadFromHex(loginGuardianI loginGuardianIdentifierHash, chainId, JsonConvert.SerializeObject(output)); return output?.CaHash?.ToHex(); } + + private async Task PublishExtraInfoAsync(string grainId, Dictionary extraInfo) + { + if (extraInfo.IsNullOrEmpty()) return; + + await _distributedEventBus.PublishAsync(new HolderExtraInfoEto + { + GrainId = grainId, + OperationType = AccountOperationType.Register, + ExtraInfo = extraInfo + }); + } } \ No newline at end of file diff --git a/src/CAServer.Domain/Entities/Es/HolderStatisticIndex.cs b/src/CAServer.Domain/Entities/Es/HolderStatisticIndex.cs new file mode 100644 index 000000000..22ce2a27d --- /dev/null +++ b/src/CAServer.Domain/Entities/Es/HolderStatisticIndex.cs @@ -0,0 +1,18 @@ +using System; +using AElf.Indexing.Elasticsearch; +using Nest; + +namespace CAServer.Entities.Es; + +public class HolderStatisticIndex : CAServerEsEntity, 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 string CountryName { 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; } +} \ No newline at end of file diff --git a/src/CAServer.EntityEventHandler.Core/CAAccountHandler.cs b/src/CAServer.EntityEventHandler.Core/CAAccountHandler.cs index dcd3db7a1..fbacf03c6 100644 --- a/src/CAServer.EntityEventHandler.Core/CAAccountHandler.cs +++ b/src/CAServer.EntityEventHandler.Core/CAAccountHandler.cs @@ -185,6 +185,14 @@ await _distributedEventBus.PublishAsync(new AccountRegisterCompletedEto }, Context = Context }); + + await _distributedEventBus.PublishAsync(new HolderExtraInfoCompletedEto + { + Status = GetAccountStatus(register.RegisterSuccess), + CaAddress = register.CaAddress, + CaHash = register.CaHash, + GrainId = register.GrainId + }); } public async Task HandleEventAsync(SocialRecoveryEto eventData) diff --git a/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs b/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs new file mode 100644 index 000000000..84e2cb1a5 --- /dev/null +++ b/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AElf.Indexing.Elasticsearch; +using CAServer.Account; +using CAServer.Entities.Es; +using CAServer.Etos; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; + +namespace CAServer.EntityEventHandler.Core; + +public class HolderStatisticHandler : IDistributedEventHandler, + IDistributedEventHandler, ITransientDependency +{ + private readonly INESTRepository _holderStatisticRepository; + private readonly ILogger _logger; + + public HolderStatisticHandler(INESTRepository holderStatisticRepository, + ILogger logger) + { + _holderStatisticRepository = holderStatisticRepository; + _logger = logger; + } + + public async Task HandleEventAsync(HolderExtraInfoEto eventData) + { + try + { + 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("activityId", out var activityId)) + { + statisticIndex.ActivityId = activityId.ToString(); + } + + if (eventData.ExtraInfo.TryGetValue("ip", out var ip)) + { + // get country from ip + statisticIndex.CountryName = ""; + } + + await _holderStatisticRepository.AddOrUpdateAsync(statisticIndex); + _logger.LogInformation( + "save HolderExtraInfo success, grainId:{grainId},ip:{ip},country:{country},activityId:{activityId}", + statisticIndex.Id, statisticIndex.IpAddress ?? "-", statisticIndex.CountryName ?? "-", + statisticIndex.ActivityId ?? "-"); + } + catch (Exception e) + { + _logger.LogError(e, "save HolderExtraInfo error, data:{data}", JsonConvert.SerializeObject(eventData)); + } + } + + public async Task HandleEventAsync(HolderExtraInfoCompletedEto eventData) + { + try + { + 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.CountryName ?? "-", + statisticIndex.ActivityId ?? "-"); + } + catch (Exception e) + { + _logger.LogError(e, "save completed HolderExtraInfo error, data:{data}", JsonConvert.SerializeObject(eventData)); + } + } +} \ No newline at end of file From ae66ddced1f3aacf264df9447796ec97811cc6b6 Mon Sep 17 00:00:00 2001 From: felix03zhao Date: Sun, 29 Sep 2024 01:41:54 +0800 Subject: [PATCH 2/3] modify: statistic holder country. --- .../CAAccount/CAAccountAppService.cs | 2 +- .../IpInfo/IIpInfoClient.cs | 1 + .../IpInfo/IpInfoClient.cs | 25 ++++++++- .../IpInfo/IpServiceSettingOptions.cs | 1 + .../Entities/Es/HolderStatisticIndex.cs | 12 ++++- .../CAServerEventHandlerAutoMapperProfile.cs | 3 ++ .../HolderStatisticHandler.cs | 52 +++++++++++++++---- 7 files changed, 84 insertions(+), 12 deletions(-) diff --git a/src/CAServer.Application/CAAccount/CAAccountAppService.cs b/src/CAServer.Application/CAAccount/CAAccountAppService.cs index 893a51884..606ac4ccc 100644 --- a/src/CAServer.Application/CAAccount/CAAccountAppService.cs +++ b/src/CAServer.Application/CAAccount/CAAccountAppService.cs @@ -144,7 +144,7 @@ public async Task RegisterRequestAsync(RegisterRequestDto inpu registerCreateEto.IpAddress = _ipInfoAppService.GetRemoteIp(input.ReferralInfo?.Random); await CheckAndResetReferralInfo(input.ReferralInfo, registerCreateEto.IpAddress); await _distributedEventBus.PublishAsync(registerCreateEto); - await PublishExtraInfoAsync(registerCreateEto.ChainId, input.ExtraInfo); + await PublishExtraInfoAsync(registerCreateEto.GrainId, input.ExtraInfo); return new AccountResultDto(registerDto.Id.ToString()); } diff --git a/src/CAServer.Application/IpInfo/IIpInfoClient.cs b/src/CAServer.Application/IpInfo/IIpInfoClient.cs index 6cc41bfa6..c217550d1 100644 --- a/src/CAServer.Application/IpInfo/IIpInfoClient.cs +++ b/src/CAServer.Application/IpInfo/IIpInfoClient.cs @@ -5,4 +5,5 @@ namespace CAServer.IpInfo; public interface IIpInfoClient { Task GetIpInfoAsync(string ip); + Task GetCountryInfoAsync(string ip); } \ No newline at end of file diff --git a/src/CAServer.Application/IpInfo/IpInfoClient.cs b/src/CAServer.Application/IpInfo/IpInfoClient.cs index 65f9f229b..2dd05a6fc 100644 --- a/src/CAServer.Application/IpInfo/IpInfoClient.cs +++ b/src/CAServer.Application/IpInfo/IpInfoClient.cs @@ -1,9 +1,11 @@ +using System; using System.Net.Http; using System.Threading.Tasks; using CAServer.Common; using CAServer.Http; using CAServer.Signature.Options; using CAServer.Signature.Provider; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace CAServer.IpInfo; @@ -14,16 +16,21 @@ public class IpInfoClient : IIpInfoClient private readonly IOptionsMonitor _ipServiceSetting; private readonly IOptionsMonitor _signatureOptions; private readonly ISecretProvider _secretProvider; + private readonly IHttpClientProvider _httpClientProvider; + private readonly ILogger _logger; public IpInfoClient( IOptionsMonitor ipServiceSettingOption, ISecretProvider secretProvider, IHttpProvider httpProvider, - IOptionsMonitor signatureOptions) + IOptionsMonitor signatureOptions, IHttpClientProvider httpClientProvider, + ILogger logger) { _secretProvider = secretProvider; _httpProvider = httpProvider; _signatureOptions = signatureOptions; + _httpClientProvider = httpClientProvider; + _logger = logger; _ipServiceSetting = ipServiceSettingOption; } @@ -36,4 +43,20 @@ public async Task GetIpInfoAsync(string ip) AssertHelper.IsTrue(response.Error == null, response.Error?.Info ?? "Get ip info failed {}", ip); return response; } + + public async Task GetCountryInfoAsync(string ip) + { + try + { + var requestUrl = $"{_ipServiceSetting.CurrentValue.BaseUrl.TrimEnd('/')}/{ip}"; + requestUrl += + $"?access_key={_ipServiceSetting.CurrentValue.HolderStatisticAccessKey}&language={_ipServiceSetting.CurrentValue.Language}"; + return await _httpClientProvider.GetAsync(requestUrl); + } + catch (Exception e) + { + _logger.LogError("get ip info error, ip:{ip}", ip); + return null; + } + } } \ No newline at end of file diff --git a/src/CAServer.Application/IpInfo/IpServiceSettingOptions.cs b/src/CAServer.Application/IpInfo/IpServiceSettingOptions.cs index fe5c600a6..47b660467 100644 --- a/src/CAServer.Application/IpInfo/IpServiceSettingOptions.cs +++ b/src/CAServer.Application/IpInfo/IpServiceSettingOptions.cs @@ -6,4 +6,5 @@ public class IpServiceSettingOptions public string AccessKey { get; set; } public string Language { get; set; } public int ExpirationDays { get; set; } + public string HolderStatisticAccessKey { get; set; } } \ No newline at end of file diff --git a/src/CAServer.Domain/Entities/Es/HolderStatisticIndex.cs b/src/CAServer.Domain/Entities/Es/HolderStatisticIndex.cs index 22ce2a27d..1cb15a2de 100644 --- a/src/CAServer.Domain/Entities/Es/HolderStatisticIndex.cs +++ b/src/CAServer.Domain/Entities/Es/HolderStatisticIndex.cs @@ -1,5 +1,6 @@ using System; using AElf.Indexing.Elasticsearch; +using CAServer.Account; using Nest; namespace CAServer.Entities.Es; @@ -10,9 +11,18 @@ public class HolderStatisticIndex : CAServerEsEntity, IIndexBuild [Keyword] public string CaHash { get; set; } [Keyword] public string CaAddress { get; set; } [Keyword] public string IpAddress { get; set; } - [Keyword] public string CountryName { 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; } } \ No newline at end of file diff --git a/src/CAServer.EntityEventHandler.Core/CAServerEventHandlerAutoMapperProfile.cs b/src/CAServer.EntityEventHandler.Core/CAServerEventHandlerAutoMapperProfile.cs index 0d8d54c7a..0aa093fdf 100644 --- a/src/CAServer.EntityEventHandler.Core/CAServerEventHandlerAutoMapperProfile.cs +++ b/src/CAServer.EntityEventHandler.Core/CAServerEventHandlerAutoMapperProfile.cs @@ -10,6 +10,7 @@ using CAServer.Grains.Grain.Account; using CAServer.Grains.Grain.Contacts; using CAServer.Growth.Etos; +using CAServer.IpInfo; using CAServer.Notify.Etos; using CAServer.RedDot.Etos; using CAServer.Security.Etos; @@ -87,5 +88,7 @@ public CAServerEventHandlerAutoMapperProfile() .ForMember(t => t.ClientType, m => m.MapFrom(f => f.ClientType.ToString())) .ForMember(t => t.OperationType, m => m.MapFrom(f => f.OperationType.ToString())) .ForMember(t => t.CreateTime, f => f.MapFrom(f => DateTime.UtcNow)); + + CreateMap(); } } \ No newline at end of file diff --git a/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs b/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs index 84e2cb1a5..68051952d 100644 --- a/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs +++ b/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs @@ -1,14 +1,17 @@ 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; @@ -17,12 +20,20 @@ public class HolderStatisticHandler : IDistributedEventHandler _holderStatisticRepository; private readonly ILogger _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 holderStatisticRepository, - ILogger logger) + ILogger logger, IIpInfoClient infoClient, IObjectMapper objectMapper) { _holderStatisticRepository = holderStatisticRepository; _logger = logger; + _infoClient = infoClient; + _objectMapper = objectMapper; } public async Task HandleEventAsync(HolderExtraInfoEto eventData) @@ -39,21 +50,21 @@ public async Task HandleEventAsync(HolderExtraInfoEto eventData) Status = AccountOperationStatus.Pending.ToString() }; - if (eventData.ExtraInfo.TryGetValue("activityId", out var activityId)) + if (eventData.ExtraInfo.TryGetValue(ActivityIdKey, out var activityId)) { statisticIndex.ActivityId = activityId.ToString(); } - if (eventData.ExtraInfo.TryGetValue("ip", out var ip)) + if (eventData.ExtraInfo.TryGetValue(IpKey, out var ip)) { - // get country from ip - statisticIndex.CountryName = ""; + 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.CountryName ?? "-", + statisticIndex.Id, statisticIndex.IpAddress ?? "-", statisticIndex.CountryInfo?.CountryName ?? "-", statisticIndex.ActivityId ?? "-"); } catch (Exception e) @@ -68,15 +79,15 @@ 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.CountryName ?? "-", + statisticIndex.Id, statisticIndex.IpAddress ?? "-", statisticIndex.CountryInfo?.CountryName ?? "-", statisticIndex.ActivityId ?? "-"); } catch (Exception e) @@ -84,4 +95,27 @@ public async Task HandleEventAsync(HolderExtraInfoCompletedEto eventData) _logger.LogError(e, "save completed HolderExtraInfo error, data:{data}", JsonConvert.SerializeObject(eventData)); } } + + private async Task 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(countryInfo); + } } \ No newline at end of file From fa1533d64e9d3fda85bff26a5df1a74e7d1bf829 Mon Sep 17 00:00:00 2001 From: felix03zhao Date: Sun, 29 Sep 2024 12:01:32 +0800 Subject: [PATCH 3/3] modify: remove useless. --- .../HolderStatisticHandler.cs | 86 ++++++++----------- 1 file changed, 37 insertions(+), 49 deletions(-) diff --git a/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs b/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs index 68051952d..29e88c6a7 100644 --- a/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs +++ b/src/CAServer.EntityEventHandler.Core/HolderStatisticHandler.cs @@ -23,7 +23,9 @@ public class HolderStatisticHandler : IDistributedEventHandler hold public async Task HandleEventAsync(HolderExtraInfoEto eventData) { - try + if (eventData.ExtraInfo.IsNullOrEmpty()) return; + + var statisticIndex = new HolderStatisticIndex { - 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 ?? "-"); + 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(); } - catch (Exception e) + + if (eventData.ExtraInfo.TryGetValue(IpKey, out var ip)) { - _logger.LogError(e, "save HolderExtraInfo error, data:{data}", JsonConvert.SerializeObject(eventData)); + 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) { - try - { - 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 ?? "-"); - } - catch (Exception e) - { - _logger.LogError(e, "save completed HolderExtraInfo error, data:{data}", JsonConvert.SerializeObject(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 GetCountryInfoAsync(string ip)