Skip to content

Commit

Permalink
added a rudimentary stats method
Browse files Browse the repository at this point in the history
  • Loading branch information
antleb committed Jan 26, 2024
1 parent ba1ca6e commit a22c080
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public class CacheController {
public void dropCache() throws Exception {
cacheService.dropCache();
}

@GetMapping("stats")
public void getStats() throws Exception {
cacheService.getStats();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.security.MessageDigest;
import java.util.List;
import java.util.Map;

public interface StatsCache {
static String getCacheKey(String query, List<Object> parameters, String dbId) throws Exception {
Expand Down Expand Up @@ -44,4 +45,6 @@ static String MD5(String string) throws Exception {

return sb.toString();
}

Map<String, Object> stats() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.*;

@Repository
@ConditionalOnProperty(
Expand Down Expand Up @@ -191,4 +189,41 @@ public void deleteEntry(String key) {

jdbcTemplate.update("delete from cache_entry where key=?", key);
}

public Map<String, Object> stats() {
DatasourceContext.setContext(CACHE_DB_NAME);

Map<String, Object> stats = new HashMap<>();

stats.put("total", jdbcTemplate.queryForObject("select count(*) from cache_entry",new Object[] {}, Integer.class));
stats.put("with_shadow", jdbcTemplate.queryForObject("select count(*) from cache_entry where shadow is not null",new Object[] {}, Integer.class));
stats.put("top10", jdbcTemplate.query("select * from cache_entry where key not in ('SHADOW_STATS_NUMBERS', 'STATS_NUMBERS') order by total_hits", (rs, rowNum) -> {
CacheEntry entry = null;

try {
QueryWithParameters query = new ObjectMapper().readValue(rs.getString("query"), QueryWithParameters.class);
String key = rs.getString("key");
Result result = new ObjectMapper().readValue(rs.getString("result"), Result.class);

entry = new CacheEntry(key, query, result);

if (rs.getTimestamp("created") != null)
entry.setCreated(new Date(rs.getTimestamp("created").getTime()));
if (rs.getTimestamp("updated") != null)
entry.setUpdated(new Date(rs.getTimestamp("updated").getTime()));
if (rs.getString("shadow") != null)
entry.setShadowResult(new ObjectMapper().readValue(rs.getString("shadow"), Result.class));

entry.setTotalHits(rs.getInt("total_hits"));
entry.setSessionHits(rs.getInt("session_hits"));
entry.setPinned(rs.getBoolean("pinned"));
} catch (IOException e) {
log.error("Error reading entry", e);
}

return entry;
}));

return stats;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,10 @@ public void deleteEntry(String key) {

redisTemplate.delete(key);
}

@Override
public Map<String, Object> stats() throws Exception {
// TODO implement
return new HashMap<>();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package gr.uoa.di.madgik.statstool.services;

import java.util.Map;

public interface CacheService {

public void updateCache();

public void promoteCache();

public void dropCache() throws Exception;

public Map<String, Object> getStats() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public void dropCache() throws Exception {
this.statsCache.dropCache();
}

public Map<String, Object> getStats() throws Exception {
return this.statsCache.stats();
}

private void doUpdateCache() {
log.info("Starting cache update");
List<CacheEntry> entries = statsCache.getEntries();
Expand Down

0 comments on commit a22c080

Please sign in to comment.