Skip to content

Commit

Permalink
Log result
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed May 31, 2024
1 parent f0c03bb commit 8855109
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace NineChronicles.DataProvider.Queries
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraphQL;
using GraphQL.Types;
using Libplanet.Crypto;
Expand All @@ -20,7 +21,7 @@ public NineChroniclesSummaryMutation(MySqlStore store, StateContext stateContext
Store = store;
StateContext = stateContext;

Field<NonNullGraphType<BooleanGraphType>>(
Field<NonNullGraphType<StringGraphType>>(
name: "migrateMoca",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<ListGraphType<StringGraphType>>>
Expand All @@ -34,8 +35,15 @@ public NineChroniclesSummaryMutation(MySqlStore store, StateContext stateContext
var mocas = Store.GetMocasBySigner(signers);
var collectionSheet = StateContext.WorldState.GetSheet<CollectionSheet>();
var blockIndex = Store.GetTip();
MigrateMoca(mocas, Store, collectionSheet, blockIndex, StateContext);
return true;
var result = MigrateMoca(mocas, Store, collectionSheet, blockIndex, StateContext);
StringBuilder sb = new StringBuilder(string.Empty);
foreach (var kv in result)
{
var log = $"{kv.Key},{kv.Value.Item1},{kv.Value.Item2}\n";
sb.Append(log);
}
return sb;
}
);
}
Expand All @@ -44,8 +52,9 @@ public NineChroniclesSummaryMutation(MySqlStore store, StateContext stateContext

private StateContext StateContext { get; }

public static void MigrateMoca(ICollection<MocaIntegrationModel> mocas, MySqlStore mySqlStore, CollectionSheet collectionSheet, long blockIndex, StateContext stateContext)
public static Dictionary<string, (int, int)> MigrateMoca(ICollection<MocaIntegrationModel> mocas, MySqlStore mySqlStore, CollectionSheet collectionSheet, long blockIndex, StateContext stateContext)
{
var result = new Dictionary<string, (int, int)>();
foreach (var moca in mocas)
{
var avatars = mySqlStore.GetAvatarsFromSigner(moca.Signer);
Expand All @@ -57,7 +66,8 @@ public static void MigrateMoca(ICollection<MocaIntegrationModel> mocas, MySqlSto
var collectionState = stateContext.WorldState.GetCollectionState(new Address(avatar.Address!));
var existIds = avatar.ActivateCollections.Select(i => i.CollectionId).ToList();
var targetIds = collectionState.Ids.Except(existIds).ToList();
Log.Information("[MigrateMoca] migration targets: {Address}, {ExistIds}, {TargetIds}", avatar.Address, string.Join(",", existIds), string.Join(",", targetIds));
Log.Information("[MigrateMoca] migration targets: {Address}, [{ExistIds}]/[{TargetIds}]", avatar.Address, string.Join(",", existIds), string.Join(",", targetIds));
var previous = avatar.ActivateCollections.Count;
foreach (var collectionId in targetIds)
{
var row = collectionSheet[collectionId];
Expand Down Expand Up @@ -87,7 +97,10 @@ public static void MigrateMoca(ICollection<MocaIntegrationModel> mocas, MySqlSto
if (targetIds.Any())
{
mySqlStore.UpdateAvatar(avatar);
Log.Information("[MigrateMoca] Update Avatar: {Address}, [{Previous}]/[{New}]", avatar.Address, previous, avatar.ActivateCollections.Count);
}

result[avatar.Address!] = (previous, avatar.ActivateCollections.Count);
}
catch (Exception e)
{
Expand All @@ -102,6 +115,8 @@ public static void MigrateMoca(ICollection<MocaIntegrationModel> mocas, MySqlSto
mySqlStore.UpdateMoca(moca.Signer);
}
}

return result;
}
}
}

0 comments on commit 8855109

Please sign in to comment.