From 07de8a6e84c9bc5e47c9607637e789894a5a8b74 Mon Sep 17 00:00:00 2001 From: dmirgaleev <35151170+dmirgaleev@users.noreply.github.com> Date: Tue, 19 Jul 2022 13:28:37 +0300 Subject: [PATCH] Add `firstActivity` filter to /accounts/count (#119) --- Tzkt.Api/Controllers/AccountsController.cs | 10 ++++++---- Tzkt.Api/Repositories/AccountRepository.cs | 6 +++--- Tzkt.Api/Services/Home/HomeService.cs | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Tzkt.Api/Controllers/AccountsController.cs b/Tzkt.Api/Controllers/AccountsController.cs index 04d70acc6..7136a4bb7 100644 --- a/Tzkt.Api/Controllers/AccountsController.cs +++ b/Tzkt.Api/Controllers/AccountsController.cs @@ -132,16 +132,18 @@ public async Task>> Get( /// Filters accounts by contract kind (`delegator_contract` or `smart_contract`) /// Filters accounts by balance /// Filters accounts by participation in staking + /// Filters accounts by first activity level (where the account was created) /// [HttpGet("count")] public async Task> 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) @@ -149,12 +151,12 @@ public async Task> GetCount( #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); } diff --git a/Tzkt.Api/Repositories/AccountRepository.cs b/Tzkt.Api/Repositories/AccountRepository.cs index 916dcf055..6c5a01806 100644 --- a/Tzkt.Api/Repositories/AccountRepository.cs +++ b/Tzkt.Api/Repositories/AccountRepository.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Data; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; @@ -272,13 +271,14 @@ public async Task Get(string address, bool metadata) } } - public async Task GetCount(AccountTypeParameter type, ContractKindParameter kind, Int64Parameter balance, BoolParameter staked) + public async Task 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(sql.Query, sql.Params); diff --git a/Tzkt.Api/Services/Home/HomeService.cs b/Tzkt.Api/Services/Home/HomeService.cs index bf8a027eb..58c0ca550 100644 --- a/Tzkt.Api/Services/Home/HomeService.cs +++ b/Tzkt.Api/Services/Home/HomeService.cs @@ -481,7 +481,7 @@ async Task 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( $@"SELECT COUNT(*)::integer FROM ""Accounts"" WHERE ""LastLevel"" >= {currPeriod}"), PublicAccounts = await db.ExecuteScalarAsync(