Skip to content

Commit

Permalink
Add firstActivity filter to /accounts/count (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmirgaleev authored Jul 19, 2022
1 parent d80e338 commit 07de8a6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions Tzkt.Api/Controllers/AccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,31 @@ public async Task<ActionResult<IEnumerable<Account>>> Get(
/// <param name="kind">Filters accounts by contract kind (`delegator_contract` or `smart_contract`)</param>
/// <param name="balance">Filters accounts by balance</param>
/// <param name="staked">Filters accounts by participation in staking</param>
/// <param name="firstActivity">Filters accounts by first activity level (where the account was created)</param>
/// <returns></returns>
[HttpGet("count")]
public async Task<ActionResult<int>> GetCount(
AccountTypeParameter type,
ContractKindParameter kind,
Int64Parameter balance,
BoolParameter staked)
BoolParameter staked,
Int32Parameter firstActivity)
{
#region optimize
if (type == null && kind == null && balance == null && staked == null)
if (type == null && kind == null && balance == null && staked == null && firstActivity == null)
return Ok(State.Current.AccountsCount);

if (kind?.Eq != null && type == null)
type = new AccountTypeParameter { Eq = 2 };
#endregion

var query = ResponseCacheService.BuildKey(Request.Path.Value,
("type", type), ("kind", kind), ("balance", balance), ("staked", staked));
("type", type), ("kind", kind), ("balance", balance), ("staked", staked), ("firstActivity", firstActivity));

if (ResponseCache.TryGet(query, out var cached))
return this.Bytes(cached);

var res = await Accounts.GetCount(type, kind, balance, staked);
var res = await Accounts.GetCount(type, kind, balance, staked, firstActivity);
cached = ResponseCache.Set(query, res);
return this.Bytes(cached);
}
Expand Down
6 changes: 3 additions & 3 deletions Tzkt.Api/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -272,13 +271,14 @@ public async Task<Account> Get(string address, bool metadata)
}
}

public async Task<int> GetCount(AccountTypeParameter type, ContractKindParameter kind, Int64Parameter balance, BoolParameter staked)
public async Task<int> GetCount(AccountTypeParameter type, ContractKindParameter kind, Int64Parameter balance, BoolParameter staked, Int32Parameter firstActivity)
{
var sql = new SqlBuilder(@"SELECT COUNT(*) FROM ""Accounts""")
.Filter("Type", type)
.Filter("Kind", kind)
.Filter("Balance", balance)
.Filter("Staked", staked);
.Filter("Staked", staked)
.Filter("FirstLevel", firstActivity);

using var db = GetConnection();
return await db.QueryFirstAsync<int>(sql.Query, sql.Params);
Expand Down
2 changes: 1 addition & 1 deletion Tzkt.Api/Services/Home/HomeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ async Task<AccountsData> GetAccountsData(IDbConnection db)
return new AccountsData
{
TotalAccounts = State.Current.AccountsCount,
FundedAccounts = await AccountsRepo.GetCount(null, null, new Int64Parameter { Ge = 1_000_000 }, null),
FundedAccounts = await AccountsRepo.GetCount(null, null, new Int64Parameter { Ge = 1_000_000 }, null, null),
ActiveAccounts = await db.ExecuteScalarAsync<int>(
$@"SELECT COUNT(*)::integer FROM ""Accounts"" WHERE ""LastLevel"" >= {currPeriod}"),
PublicAccounts = await db.ExecuteScalarAsync<int>(
Expand Down

0 comments on commit 07de8a6

Please sign in to comment.