Skip to content

Commit

Permalink
Add modelers blog view count API
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Sacker committed Dec 27, 2024
1 parent 68335eb commit 9cd806d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/datastat/controller/QueryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -839,4 +839,15 @@ public String monthDownCount(HttpServletRequest request,
public String viewCount(HttpServletRequest request, @Valid final RequestParams condition) {
return queryService.getViewCount(request, condition);
}

/**
* Handles the request to view the modelers blog count.
*
* @param request the HttpServletRequest object containing client request data
* @return a String representing the view count of the modelers blog
*/
@RequestMapping(value = "/modelers/blog/view")
public String modelersBlogView(HttpServletRequest request) {
return queryService.getModelersBlogViewCount(request);
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/datastat/dao/QueryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3962,4 +3962,37 @@ public String getViewCount(CustomPropertiesConfig queryConf, RequestParams condi
}
return ResultUtil.resultJsonStr(statusCode, buckets, statusText);
}

/**
* Retrieves the view count for modeler's blog.
*
* @param queryConf Custom properties configuration object containing the necessary configuration information for the query.
* @return A JSON string containing the status code, result data, and status text.
* @throws Exception If an exception occurs during the execution of the query or processing of the response.
*/
@SneakyThrows
public String getModelersBlogViewCount(CustomPropertiesConfig queryConf) {
String query = String.format(queryConf.getModelersBlogViewCountQueryStr());
ListenableFuture<Response> future = esAsyncHttpUtil.executeSearch(esUrl, queryConf.getModelersTrackerIndex(), query);
Response response = future.get();
int statusCode = response.getStatusCode();
String statusText = response.getStatusText();
String responseBody = response.getResponseBody(UTF_8);
JsonNode dataNode = objectMapper.readTree(responseBody);
var buckets = dataNode.get("aggregations").get("group_by_id_and_title").get("buckets");
var resJsonArray = objectMapper.createArrayNode();
try {
for (var bucket : buckets) {
var jsonObject = objectMapper.createObjectNode();
jsonObject.put("id", bucket.get("key").get("id").asText());
jsonObject.put("title", bucket.get("key").get("title").asText());
jsonObject.put("count", bucket.get("doc_count").asInt());
resJsonArray.add(jsonObject);
}
} catch (Exception e) {
logger.error("query/modelers/blog/view get error - {}", e.getMessage());
return ResultUtil.resultJsonStr(statusCode, objectMapper.valueToTree(resJsonArray), "No data found or query error");
}
return ResultUtil.resultJsonStr(statusCode, objectMapper.valueToTree(resJsonArray), statusText);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/datastat/model/CustomPropertiesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public class CustomPropertiesConfig {
private String datasetEventIndex;
private String openmindRepoIndex;
private String searchNpsIndex;
private String modelersTrackerIndex;

// -- query str --
private String extOsQueryStr;
Expand Down Expand Up @@ -227,6 +228,7 @@ public class CustomPropertiesConfig {
private String starCountQueryStr;
private String openmindRepoQueryStr;
private String repoViewCountQueryStr;
private String modelersBlogViewCountQueryStr;

protected static final Map<String, String> contributeTypeMap = new HashMap<>();
protected static final Map<String, String> groupFieldMap = new HashMap<>();
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/datastat/service/QueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1702,4 +1702,26 @@ public String getViewCount(HttpServletRequest request, RequestParams condition)
}
return result;
}

/**
* Retrieves the view count for the modelers blog from cache or database.
*
* This method attempts to fetch the view count of the modelers blog from a Redis cache.
* If the cache does not contain the data, it queries the database to obtain the view count
* and then stores this data in the cache for future requests.
*
* @param request the HttpServletRequest object, which may contain information needed for the database query
* @return the view count of the modelers blog as a String
*/
public String getModelersBlogViewCount(HttpServletRequest request) {
QueryDao queryDao = getQueryDao(request);
CustomPropertiesConfig queryConf = getQueryConf("foundry");
String key = "get_modelers_blogview_count";
String result = (String) redisDao.get(key);
if (result == null) {
result = queryDao.getModelersBlogViewCount(queryConf);
redisDao.set(key, result, redisDefaultExpire);
}
return result;
}
}

0 comments on commit 9cd806d

Please sign in to comment.