Skip to content

Commit

Permalink
Combine the counts into a single endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
martian0x80 committed May 15, 2024
1 parent 8187990 commit cc35424
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 110 deletions.
114 changes: 8 additions & 106 deletions IPUSenpaiBackend/Controllers/IPUSenpaiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,30 +556,31 @@ await _cache.SetStringAsync($"SearchStudent_{name}_{institute}_{programme}_{batc
}

[HttpGet]
[Route("count/student")]
public async Task<int> GetStudentCount()
[Route("count/")]
public async Task<Dictionary<string, int>> GetCounts()
{
if (_enableCache)
{
var cachedCount = await _cache.GetStringAsync("GetStudentCount");
var cachedCount = await _cache.GetStringAsync("GetCounts");
if (!string.IsNullOrEmpty(cachedCount))
{
try
{
return JsonSerializer.Deserialize<int>(cachedCount);
return JsonSerializer.Deserialize<Dictionary<string, int>>(cachedCount) ??
new Dictionary<string, int>();
}
catch (JsonException e)
{
_logger.LogError(e, "Error deserializing cached student count");
_logger.LogError(e, "Error deserializing cached counts");
}
}
}

var count = await _api.GetStudentCount();
var count = await _api.GetCounts();

if (_enableCache)
{
await _cache.SetStringAsync("GetStudentCount", JsonSerializer.Serialize(count), CacheOptions);
await _cache.SetStringAsync("GetCounts", JsonSerializer.Serialize(count), CacheOptions);
}

return count;
Expand Down Expand Up @@ -678,103 +679,4 @@ public async Task<Dictionary<string, int>> GetStudentByBatchCount()

return count;
}

[HttpGet]
[Route("count/result")]
public async Task<int> GetResultCount()
{
if (_enableCache)
{
var cachedCount = await _cache.GetStringAsync("GetResultCount");
if (!string.IsNullOrEmpty(cachedCount))
{
try
{
return JsonSerializer.Deserialize<int>(cachedCount);
}
catch (JsonException e)
{
_logger.LogError(e, "Error deserializing cached result count");
}
}
}

var count = await _api.GetResultCount();

if (_enableCache)
{
await _cache.SetStringAsync("GetResultCount", JsonSerializer.Serialize(count), CacheOptions);
}

return count;
}

[HttpGet]
[Route("count/result/actual")]
public int GetResultCountActual()
{
// select count(*) from (select enrolno, count(*) from results group by enrolno, exam);
// Well, the query is too slow, so I'm just going to return a constant value
return 413725;
}

[HttpGet]
[Route("count/programme")]
public async Task<int> GetProgrammeCount()
{
if (_enableCache)
{
var cachedCount = await _cache.GetStringAsync("GetProgrammeCount");
if (!string.IsNullOrEmpty(cachedCount))
{
try
{
return JsonSerializer.Deserialize<int>(cachedCount);
}
catch (JsonException e)
{
_logger.LogError(e, "Error deserializing cached programme count");
}
}
}

var count = await _api.GetProgrammeCount();

if (_enableCache)
{
await _cache.SetStringAsync("GetProgrammeCount", JsonSerializer.Serialize(count), CacheOptions);
}

return count;
}

[HttpGet]
[Route("count/institute")]
public async Task<int> GetInstituteCount()
{
if (_enableCache)
{
var cachedCount = await _cache.GetStringAsync("GetInstituteCount");
if (!string.IsNullOrEmpty(cachedCount))
{
try
{
return JsonSerializer.Deserialize<int>(cachedCount);
}
catch (JsonException e)
{
_logger.LogError(e, "Error deserializing cached institute count");
}
}
}

var count = await _api.GetInstituteCount();

if (_enableCache)
{
await _cache.SetStringAsync("GetInstituteCount", JsonSerializer.Serialize(count), CacheOptions);
}

return count;
}
}
11 changes: 7 additions & 4 deletions IPUSenpaiBackend/IPUSenpai/IIPUSenpaiAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ public Task<List<PartialResponse>> GetSemestersByProgrammeInstnameBatch(string p

public StudentSenpai? GetStudent(string enrollment);
public Task<List<StudentSearchSenpai>> GetSearchStudent(StudentSearchFilterOptionsSenpai filter);
public Task<int> GetStudentCount();

// No need to expose these methods
// public Task<int> GetStudentCount();
// public Task<int> GetResultCount();
// public Task<int> GetProgrammeCount();
// public Task<int> GetInstituteCount();
public Task<Dictionary<string, int>> GetCounts();
public Task<Dictionary<string, int>> GetStudentByProgrammeCount();
public Task<Dictionary<string, int>> GetStudentByInstituteCount(int limit);
public Task<Dictionary<string, int>> GetStudentByBatchCount();
public Task<int> GetResultCount();
public Task<int> GetProgrammeCount();
public Task<int> GetInstituteCount();
}
19 changes: 19 additions & 0 deletions IPUSenpaiBackend/IPUSenpai/IPUSenpaiAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,25 @@ await connection.QueryAsync<StudentSearchSenpai>(
return new List<StudentSearchSenpai>();
}

public async Task<Dictionary<string, int>> GetCounts()
{
int studentCount = await GetStudentCount();
int resultCount = await GetResultCount();
int programmeCount = await GetProgrammeCount();
int instituteCount = await GetInstituteCount();
// select count(*) from (select enrolno, count(*) from results group by enrolno, exam);
// Well, the query is too slow, so I'm just going to return a constant value
int actualResultCount = 413725;
return new()
{
["student"] = studentCount,
["result"] = resultCount,
["programme"] = programmeCount,
["institute"] = instituteCount,
["actualResult"] = actualResultCount
};
}

public async Task<int> GetStudentCount()
{
var query = "SELECT reltuples::bigint FROM pg_class where relname = 'student'";
Expand Down

0 comments on commit cc35424

Please sign in to comment.