tech stats
This commit is contained in:
parent
ecbf2f07d6
commit
965b693a19
@ -27,6 +27,12 @@ namespace OpenCand.API.Controllers
|
|||||||
{
|
{
|
||||||
return await openCandService.GetDataAvailabilityStatsAsync();
|
return await openCandService.GetDataAvailabilityStatsAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("tech")]
|
||||||
|
public async Task<DatabaseTechStats> GetDatabaseTechStats()
|
||||||
|
{
|
||||||
|
return await openCandService.GetDatabaseTechStatsAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ namespace OpenCand.API.Repository
|
|||||||
{
|
{
|
||||||
result.Partidos = (await connection.QueryAsync<string>(@"SELECT DISTINCT sigla FROM partido ORDER BY sigla ASC;")).AsList();
|
result.Partidos = (await connection.QueryAsync<string>(@"SELECT DISTINCT sigla FROM partido ORDER BY sigla ASC;")).AsList();
|
||||||
result.SiglasUF = (await connection.QueryAsync<string>(@"SELECT DISTINCT siglauf FROM candidato_mapping ORDER BY siglauf ASC;")).AsList();
|
result.SiglasUF = (await connection.QueryAsync<string>(@"SELECT DISTINCT siglauf FROM candidato_mapping ORDER BY siglauf ASC;")).AsList();
|
||||||
result.Anos = (await connection.QueryAsync<int>(@"SELECT DISTINCT ano FROM candidato_mapping ORDER BY ano ASC;")).AsList();
|
result.Anos = (await connection.QueryAsync<int>(@"SELECT DISTINCT ano FROM candidato_mapping ORDER BY ano DESC;")).AsList();
|
||||||
result.Cargos = (await connection.QueryAsync<string>(@"SELECT DISTINCT cargo FROM candidato_mapping ORDER BY cargo ASC;")).AsList();
|
result.Cargos = (await connection.QueryAsync<string>(@"SELECT DISTINCT cargo FROM candidato_mapping ORDER BY cargo ASC;")).AsList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,5 +81,63 @@ namespace OpenCand.API.Repository
|
|||||||
|
|
||||||
return result ?? new DataAvailabilityStats();
|
return result ?? new DataAvailabilityStats();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<DatabaseTechStats> GetDatabaseTechStatsAsync()
|
||||||
|
{
|
||||||
|
string cacheKey = GenerateCacheKey("DatabaseTechStats");
|
||||||
|
|
||||||
|
var result = await GetOrSetCacheAsync(cacheKey, async () =>
|
||||||
|
{
|
||||||
|
using (var connection = new NpgsqlConnection(ConnectionString))
|
||||||
|
{
|
||||||
|
var stats = new DatabaseTechStats();
|
||||||
|
|
||||||
|
|
||||||
|
// Get table stats using reltuples for entries
|
||||||
|
var tableStats = await connection.QueryAsync<TableStats>(@"
|
||||||
|
SELECT
|
||||||
|
pt.schemaname||'.'||pt.tablename as Name,
|
||||||
|
pg_total_relation_size(pt.schemaname||'.'||pt.tablename) as TotalSize,
|
||||||
|
COALESCE(c.reltuples,0)::bigint as Entries
|
||||||
|
FROM pg_tables pt
|
||||||
|
JOIN pg_class c ON c.relname = pt.tablename AND c.relkind = 'r'
|
||||||
|
WHERE pt.schemaname = 'public'
|
||||||
|
ORDER BY pg_total_relation_size(pt.schemaname||'.'||pt.tablename) DESC;");
|
||||||
|
|
||||||
|
|
||||||
|
var tableStatsList = tableStats.ToList();
|
||||||
|
stats.Tables = tableStatsList;
|
||||||
|
stats.TotalSize = tableStatsList.Sum(t => t.TotalSize);
|
||||||
|
stats.TotalEntries = tableStatsList.Sum(t => t.Entries);
|
||||||
|
|
||||||
|
// Get materialized view stats using reltuples for entries
|
||||||
|
var materializedViewStats = await connection.QueryAsync<TableStats>(@"
|
||||||
|
SELECT
|
||||||
|
pmv.schemaname||'.'||pmv.matviewname as Name,
|
||||||
|
pg_total_relation_size(pmv.schemaname||'.'||pmv.matviewname) as TotalSize,
|
||||||
|
COALESCE(c.reltuples,0)::bigint as Entries
|
||||||
|
FROM pg_matviews pmv
|
||||||
|
JOIN pg_class c ON c.relname = pmv.matviewname AND c.relkind = 'm'
|
||||||
|
WHERE pmv.schemaname = 'public'
|
||||||
|
ORDER BY pg_total_relation_size(pmv.schemaname||'.'||pmv.matviewname) DESC;");
|
||||||
|
|
||||||
|
stats.MaterializedViews = materializedViewStats.ToList();
|
||||||
|
|
||||||
|
// Get index stats
|
||||||
|
var indexStats = await connection.QueryFirstOrDefaultAsync<IndexStats>(@"
|
||||||
|
SELECT
|
||||||
|
COUNT(*) as Amount,
|
||||||
|
SUM(pg_relation_size(indexrelid)) as Size
|
||||||
|
FROM pg_stat_user_indexes
|
||||||
|
WHERE schemaname = 'public';");
|
||||||
|
|
||||||
|
stats.Indexes = indexStats ?? new IndexStats();
|
||||||
|
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return result ?? new DatabaseTechStats();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,7 @@ namespace OpenCand.API.Services
|
|||||||
|
|
||||||
await PerformPreLoad("GetOpenCandStatsAsync", estatisticaService.GetMaioresEnriquecimentos);
|
await PerformPreLoad("GetOpenCandStatsAsync", estatisticaService.GetMaioresEnriquecimentos);
|
||||||
await PerformPreLoad("GetOpenCandStatsAsync", openCandService.GetOpenCandStatsAsync);
|
await PerformPreLoad("GetOpenCandStatsAsync", openCandService.GetOpenCandStatsAsync);
|
||||||
|
await PerformPreLoad("GetDatabaseTechStatsAsync", openCandService.GetDatabaseTechStatsAsync);
|
||||||
await PerformPreLoad("GetDataAvailabilityStatsAsync", openCandService.GetDataAvailabilityStatsAsync);
|
await PerformPreLoad("GetDataAvailabilityStatsAsync", openCandService.GetDataAvailabilityStatsAsync);
|
||||||
|
|
||||||
logger.LogInformation("Single-call endpoints preload completed");
|
logger.LogInformation("Single-call endpoints preload completed");
|
||||||
|
@ -47,6 +47,16 @@ namespace OpenCand.API.Services
|
|||||||
return stats;
|
return stats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<DatabaseTechStats> GetDatabaseTechStatsAsync()
|
||||||
|
{
|
||||||
|
var stats = await openCandRepository.GetDatabaseTechStatsAsync();
|
||||||
|
|
||||||
|
stats.Tables = stats.Tables.OrderBy(t => t.Name).ToList();
|
||||||
|
stats.MaterializedViews = stats.MaterializedViews.OrderBy(mv => mv.Name).ToList();
|
||||||
|
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<CandidatoSearchResult> SearchCandidatosAsync(string query)
|
public async Task<CandidatoSearchResult> SearchCandidatosAsync(string query)
|
||||||
{
|
{
|
||||||
return new CandidatoSearchResult()
|
return new CandidatoSearchResult()
|
||||||
|
@ -11,12 +11,34 @@
|
|||||||
|
|
||||||
public class DataAvailabilityStats
|
public class DataAvailabilityStats
|
||||||
{
|
{
|
||||||
public List<int> Candidatos { get; set; }
|
public List<int> Candidatos { get; set; } = new List<int>();
|
||||||
public List<int> BemCandidatos { get; set; }
|
public List<int> BemCandidatos { get; set; } = new List<int>();
|
||||||
public List<int> DespesaCandidatos { get; set; }
|
public List<int> DespesaCandidatos { get; set; } = new List<int>();
|
||||||
public List<int> ReceitaCandidatos { get; set; }
|
public List<int> ReceitaCandidatos { get; set; } = new List<int>();
|
||||||
public List<int> RedeSocialCandidatos { get; set; }
|
public List<int> RedeSocialCandidatos { get; set; } = new List<int>();
|
||||||
|
|
||||||
public List<int> FotosCandidatos { get; set; }
|
public List<int> FotosCandidatos { get; set; } = new List<int>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DatabaseTechStats
|
||||||
|
{
|
||||||
|
public List<TableStats> Tables { get; set; } = new List<TableStats>();
|
||||||
|
public List<TableStats> MaterializedViews { get; set; } = new List<TableStats>();
|
||||||
|
public IndexStats Indexes { get; set; } = new IndexStats();
|
||||||
|
public long TotalSize { get; set; }
|
||||||
|
public long TotalEntries { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TableStats
|
||||||
|
{
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
public long TotalSize { get; set; }
|
||||||
|
public long Entries { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class IndexStats
|
||||||
|
{
|
||||||
|
public int Amount { get; set; }
|
||||||
|
public long Size { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user