Skip to content

Commit

Permalink
Merge pull request #56 from planetarium/improve/issue-55
Browse files Browse the repository at this point in the history
TransferAssets payload size 설정
  • Loading branch information
ipdae authored Apr 18, 2024
2 parents e32e65f + 22feb13 commit 347bff7
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 31 deletions.
13 changes: 9 additions & 4 deletions tests/graphql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ def test_check_balance(fx_session, fx_test_client):
assert result["data"]["checkBalance"] == expected


@pytest.mark.parametrize("size", [100, 50])
def test_generate_ranking_rewards_csv(
fx_test_client, celery_session_worker, httpx_mock: HTTPXMock, fx_ranking_rewards
fx_test_client,
celery_session_worker,
httpx_mock: HTTPXMock,
fx_ranking_rewards,
size: int,
):
requested_rewards = [
{
Expand Down Expand Up @@ -101,7 +106,7 @@ def test_generate_ranking_rewards_csv(
},
)

query = f'mutation {{ generateRankingRewardsCsv(seasonId: 1, totalUsers: 1, startNonce: 1, password: "{config.graphql_password}") }}'
query = f'mutation {{ generateRankingRewardsCsv(seasonId: 1, totalUsers: 1, startNonce: 1, password: "{config.graphql_password}", size: {size}) }}'
with patch("world_boss.app.tasks.client.files_upload_v2") as m:
req = fx_test_client.post("/graphql", json={"query": query})
assert req.status_code == 200
Expand All @@ -114,8 +119,8 @@ def test_generate_ranking_rewards_csv(
kwargs = m.call_args.kwargs
assert kwargs["file"]
assert kwargs["channels"] == config.slack_channel_id
assert kwargs["title"] == f"world_boss_1_1_1_result"
assert kwargs["filename"] == f"world_boss_1_1_1_result.csv"
assert kwargs["title"] == f"world_boss_1_1_1_{size}_result"
assert kwargs["filename"] == f"world_boss_1_1_1_{size}_result.csv"


@pytest.mark.parametrize("has_header", [True, False])
Expand Down
10 changes: 8 additions & 2 deletions tests/raid_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ def test_update_agent_address(


@pytest.mark.parametrize("raid_id", [1, 2])
@pytest.mark.parametrize("start_nonce, bottom, last_nonce", [(1, 100, 4), (2, 4, 2)])
@pytest.mark.parametrize(
"start_nonce, bottom, size, last_nonce",
[(1, 100, 100, 4), (1, 100, 50, 8), (2, 4, 100, 2), (2, 4, 1, 17)],
)
def test_write_ranking_rewards_csv(
tmp_path,
fx_ranking_rewards,
raid_id: int,
start_nonce: int,
bottom: int,
size: int,
last_nonce: int,
):
file_name = tmp_path / "test.csv"
Expand All @@ -94,9 +98,11 @@ def test_write_ranking_rewards_csv(
}
for i in range(0, bottom)
]
write_ranking_rewards_csv(file_name, reward_list, raid_id, start_nonce)
write_ranking_rewards_csv(file_name, reward_list, raid_id, start_nonce, size)
with open(file_name, "r") as f:
rows = f.readlines()
# header + fx_ranking_rewards * bottom
assert len(rows) == 1 + (bottom * 4)
# check header
assert (
rows[0]
Expand Down
32 changes: 17 additions & 15 deletions tests/tasks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ def test_count_users(celery_session_worker, httpx_mock: HTTPXMock):
)


@pytest.mark.parametrize("size", [100, 50])
def test_get_ranking_rewards(
redisdb,
celery_session_worker,
httpx_mock: HTTPXMock,
fx_ranking_rewards,
redisdb, celery_session_worker, httpx_mock: HTTPXMock, fx_ranking_rewards, size: int
):
raid_id = 1
network_type = NetworkType.MAIN
offset = 0
rewards_cache_key = f"world_boss_{raid_id}_{network_type}_{offset}_100"
addresses_cache_key = f"world_boss_agents_{raid_id}_{network_type}_{offset}_100"
rewards_cache_key = f"world_boss_{raid_id}_{network_type}_{offset}_{size}"
addresses_cache_key = f"world_boss_agents_{raid_id}_{network_type}_{offset}_{size}"

# get from cache key
cached_rewards: List[RankingRewardDictionary] = [
Expand All @@ -65,15 +63,15 @@ def test_get_ranking_rewards(
},
"rewards": fx_ranking_rewards,
}
for i in range(0, 100)
for i in range(0, size)
]

# get from service query
requested_rewards: List[RankingRewardDictionary] = [
{
"raider": {
"address": "01A0b412721b00bFb5D619378F8ab4E4a97646Ca",
"ranking": 101,
"ranking": size + 1,
},
"rewards": fx_ranking_rewards,
},
Expand All @@ -88,7 +86,7 @@ def test_get_ranking_rewards(
},
"rewards": fx_ranking_rewards,
}
for i in range(0, 100)
for i in range(0, size)
]
redisdb.set(rewards_cache_key, json.dumps(cached_rewards))
httpx_mock.add_response(
Expand All @@ -112,18 +110,22 @@ def test_get_ranking_rewards(
)

with unittest.mock.patch("world_boss.app.tasks.client.files_upload_v2") as m:
get_ranking_rewards.delay("channel_id", raid_id, 101, 1).get(timeout=10)
get_ranking_rewards.delay("channel_id", raid_id, size + 1, 1, size).get(
timeout=10
)
m.assert_called_once()
# skip check file. because file is temp file.
kwargs = m.call_args.kwargs
assert kwargs["file"]
assert kwargs["channels"] == "channel_id"
assert kwargs["title"] == f"world_boss_{raid_id}_101_1_result"
assert kwargs["filename"] == f"world_boss_{raid_id}_101_1_result.csv"
assert kwargs["title"] == f"world_boss_{raid_id}_{size + 1}_1_{size}_result"
assert (
kwargs["filename"] == f"world_boss_{raid_id}_{size + 1}_1_{size}_result.csv"
)
assert redisdb.exists(rewards_cache_key)
assert redisdb.exists(addresses_cache_key)
assert redisdb.exists(f"world_boss_{raid_id}_{network_type}_100_1")
assert redisdb.exists(f"world_boss_agents_{raid_id}_{network_type}_100_1")
assert redisdb.exists(f"world_boss_{raid_id}_{network_type}_{size}_1")
assert redisdb.exists(f"world_boss_agents_{raid_id}_{network_type}_{size}_1")


def test_get_ranking_rewards_error(
Expand All @@ -148,7 +150,7 @@ def test_get_ranking_rewards_error(
with unittest.mock.patch(
"world_boss.app.tasks.client.chat_postMessage"
) as m, pytest.raises(Exception):
get_ranking_rewards.delay("channel_id", raid_id, 101, 1).get(timeout=10)
get_ranking_rewards.delay("channel_id", raid_id, 101, 1, 100).get(timeout=10)
m.assert_called_once()
kwargs = m.call_args.kwargs
assert (
Expand Down
2 changes: 1 addition & 1 deletion world_boss/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def generate_ranking_rewards_csv(
):
values = text.split()
raid_id, total_users, nonce = [int(v) for v in values]
task = get_ranking_rewards.delay(channel_id, raid_id, total_users, nonce)
task = get_ranking_rewards.delay(channel_id, raid_id, total_users, nonce, 100)
return JSONResponse(task.id)


Expand Down
3 changes: 2 additions & 1 deletion world_boss/app/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ def generate_ranking_rewards_csv(
season_id: int,
total_users: int,
start_nonce: int,
size: int,
password: str,
) -> str:
task = get_ranking_rewards.delay(
config.slack_channel_id, season_id, total_users, start_nonce
config.slack_channel_id, season_id, total_users, start_nonce, size
)
return task.id

Expand Down
3 changes: 2 additions & 1 deletion world_boss/app/raid.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def write_ranking_rewards_csv(
reward_list: List[RankingRewardWithAgentDictionary],
raid_id: int,
start_nonce: int,
size: int,
):
with open(file_name, "w") as f:
writer = csv.writer(f)
Expand Down Expand Up @@ -152,7 +153,7 @@ def write_ranking_rewards_csv(
amount,
currency["ticker"],
currency["decimalPlaces"],
start_nonce + int(i / 100),
start_nonce + int(i / size),
]
)
i += 1
Expand Down
18 changes: 11 additions & 7 deletions world_boss/app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,34 @@ def count_users(channel_id: str, raid_id: int):

@celery.task()
def get_ranking_rewards(
channel_id: str, raid_id: int, total_count: int, start_nonce: int
channel_id: str, raid_id: int, total_count: int, start_nonce: int, size: int
):
offset = 0
size = 100
results: List[RankingRewardWithAgentDictionary] = []
payload_size = size
while len(results) < total_count:
try:
result = data_provider_client.get_ranking_rewards(
raid_id, NetworkType.MAIN, offset, size
raid_id, NetworkType.MAIN, offset, payload_size
)
except Exception as e:
client.chat_postMessage(
channel=channel_id,
text=f"failed to get rewards from {config.data_provider_url} exc: {e}",
)
raise e
rewards = update_agent_address(result, raid_id, NetworkType.MAIN, offset, size)
rewards = update_agent_address(
result, raid_id, NetworkType.MAIN, offset, payload_size
)
results.extend(reward for reward in rewards if reward not in results)
offset = len(results)
size = min(size, total_count - offset)
payload_size = min(payload_size, total_count - offset)
with NamedTemporaryFile(suffix=".csv") as temp_file:
file_name = temp_file.name
write_ranking_rewards_csv(file_name, results, raid_id, start_nonce)
result_format = f"world_boss_{raid_id}_{total_count}_{start_nonce}_result"
write_ranking_rewards_csv(file_name, results, raid_id, start_nonce, size)
result_format = (
f"world_boss_{raid_id}_{total_count}_{start_nonce}_{size}_result"
)
client.files_upload_v2(
channels=channel_id,
title=result_format,
Expand Down

0 comments on commit 347bff7

Please sign in to comment.