Skip to content

Commit

Permalink
Logging von nodeId für wiki, Stats für wiki
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernd Ritter committed May 3, 2024
1 parent e6e7e4a commit 09f129c
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package de.holarse.backend.db.repositories;

import de.holarse.backend.view.NodeStatisticsView;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface NodeAwareRepository {

@Query("SELECT nextval('node_sequence')")
int nextNodeId();


@Query(value = "SELECT to_char(np.accessed, 'YYYY-MM-DD') as time, sum(1) as amount from node_pagevisits np " +
"WHERE np.nodeid = :nodeId and np.accessed >= now() - interval '1 day' * :days " +
"GROUP BY to_char(np.accessed, 'YYYY-MM-DD') " +
"ORDER BY to_char(np.accessed, 'YYYY-MM-DD')", nativeQuery = true)
List<NodeStatisticsView> getDailyStats(@Param("nodeId") final Integer nodeId, @Param("days") final int days);

@Query(value = "SELECT to_char(np.accessed, 'YYYY-MM') as time, sum(1) as amount from node_pagevisits np " +
"WHERE np.nodeid = :nodeId and np.accessed >= now() - interval '1 month' * :months " +
"GROUP BY to_char(np.accessed, 'YYYY-MM') " +
"ORDER BY to_char(np.accessed, 'YYYY-MM')", nativeQuery = true)
List<NodeStatisticsView> getMonthlyStats(@Param("nodeId") final Integer nodeId, @Param("months") final int months);


}
10 changes: 10 additions & 0 deletions src/main/java/de/holarse/backend/types/StatIntervalType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.holarse.backend.types;

public enum StatIntervalType {

// Statistiken in täglicher Auflösung
daily,
// Statistiken in monatlicher Auflösung
monthly

}
8 changes: 8 additions & 0 deletions src/main/java/de/holarse/backend/view/NodeStatisticsView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.holarse.backend.view;

public interface NodeStatisticsView {

String getTime();
Integer getAmount();

}
46 changes: 39 additions & 7 deletions src/main/java/de/holarse/web/controller/WikiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.holarse.backend.db.repositories.*;
import de.holarse.backend.types.AttachmentGroupType;
import de.holarse.backend.types.NodeType;
import de.holarse.backend.types.StatIntervalType;
import de.holarse.backend.view.*;

import static de.holarse.config.JmsQueueTypes.QUEUE_SEARCH;
Expand Down Expand Up @@ -34,11 +35,7 @@
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

Expand Down Expand Up @@ -104,12 +101,45 @@ public ModelAndView index(@PageableDefault(sort={"title"}, value=WIKI_ARTICLES_D
//
return mv;
}

@GetMapping(value = "{slug}", params="stats")
public RedirectView stats(@PathVariable final String slug, final ModelAndView mv) {
final Article article = articleRepository.findBySlug(slug).orElseThrow(EntityNotFoundException::new);
return new RedirectView(String.format("%d/stats", article.getNodeId()));
}

@GetMapping(value = "{nodeId}/stats")
public ModelAndView stats(@PathVariable final Integer nodeId,
@RequestParam(value="interval", defaultValue = "daily") final StatIntervalType statIntervalType,
@RequestParam(value="amount", defaultValue = "3") final int amount,
final ModelAndView mv) {
mv.setViewName("layouts/bare");
mv.addObject("title", "Die Linuxspiele-Seite für Linuxspieler");
mv.addObject(WebDefines.DEFAULT_VIEW_ATTRIBUTE_NAME, "sites/wiki/stats");

final Article article = articleRepository.findByNodeId(nodeId).orElseThrow(EntityNotFoundException::new);
final ArticleRevision articleRevision = article.getNodeRevision();

final List<NodeStatisticsView> stats = switch (statIntervalType) {
case daily -> articleRepository.getDailyStats(article.getNodeId(), amount);
case monthly -> articleRepository.getMonthlyStats(article.getNodeId(), amount);
};

stats.forEach(s -> logger.debug("Stats: time: {} amount: {}", s.getTime(), s.getAmount()));


mv.addObject("title1", articleRevision.getTitle1());
mv.addObject("interval", statIntervalType);
mv.addObject("amount", amount);
mv.addObject("stats", stats);
return mv;
}

@GetMapping(value = "{slug}")
public ModelAndView show(@PathVariable("slug") final String slug, final ModelAndView mv, final Principal principal) {
mv.setViewName("layouts/bare");
mv.addObject("title", "Die Linuxspiele-Seite für Linuxspieler");
mv.addObject(WebDefines.DEFAULT_VIEW_ATTRIBUTE_NAME, "sites/wiki/show");
mv.addObject(WebDefines.DEFAULT_VIEW_ATTRIBUTE_NAME, "sites/wiki/show");

boolean adminOverride = false;

Expand All @@ -118,7 +148,9 @@ public ModelAndView show(@PathVariable("slug") final String slug, final ModelAnd
final Set<Tag> tags = article.getTags();
final List<TagGroup> relevantTagGroups = tags.stream().map(t -> t.getTagGroup()).toList();
final NodeSlug mainSlug = nodeSlugRepository.findMainSlug(articleRevision.getNodeId(), NodeType.article).orElseThrow(EntityNotFoundException::new);


mv.addObject("nodeid", article.getNodeId());

// TODO
// if (article.getNodeStatus().isDeleted() || !article.getNodeStatus().isPublished()) {
// logger.debug("Principal: {}", principal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class RequestLoggingInterceptor implements HandlerInterceptor {
public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) throws Exception {
final String requestPath = request.getServletPath();
// Ignorierte URLs nun ja... ignorieren
if (IGNORED_URLS.stream().anyMatch(i -> requestPath.startsWith(i))) {
if (IGNORED_URLS.stream().anyMatch(requestPath::startsWith)) {
return;
}

Expand All @@ -80,8 +80,10 @@ public void afterCompletion(final HttpServletRequest request, final HttpServletR
//logger.debug("Admin user browsing");
internal = true;
}
}

}

final Integer nodeId = (Integer) request.getAttribute("nodeid");

final PageVisit pageVisit = new PageVisit();
pageVisit.setUrl(StringUtils.substring(requestPath, 0, 2083));
pageVisit.setHttpStatus(response.getStatus());
Expand All @@ -91,6 +93,7 @@ public void afterCompletion(final HttpServletRequest request, final HttpServletR
pageVisit.setVisitorId(request.getRequestedSessionId());
pageVisit.setSearchWord(StringUtils.substring(request.getParameter("q"), 0, 255));
pageVisit.setInternal(internal);
pageVisit.setNodeId(nodeId);
pageVisit.setBot(false);

if (request.getParameterNames().hasMoreElements()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/templates/sites/wiki/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h4 class="g-font-weight-200" data-th-text="${altTitle}"></h4>
<ul class="list-group list-group-horizontal">
<a href="?edit" class="list-group-item flex-fill">bearbeiten</a>
<a href="#" class="list-group-item flex-fill" data-th-href="@{/wiki/{nodeId}/revisions(nodeId=${view.nodeId})}">Änderungen</a>
<a href="#" class="list-group-item flex-fill">Statistik</a>
<a href="?stats" class="list-group-item flex-fill">Statistik</a>
</ul>

<div class="u-heading-v1-2 g-bg-main g-brd-primary g-mb-20">
Expand Down
28 changes: 28 additions & 0 deletions src/main/webapp/WEB-INF/templates/sites/wiki/stats.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="row justify-content-center">
<div class="col-sm-10">
<div class="u-shadow-v21 g-bg-white rounded">

<header class="u-heading-v2-1--bottom g-mb-30">
<h2 class="u-heading-v2__title g-mb-10" data-th-text="${title1}">Lorem ipsum standard</h2>
</header>
<section>
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Zeit</th>
<th>Zugriffe</th>
</tr>
</thead>
<tbody>
<tr data-th-each="stat : ${stats}">
<td data-th-text="${stat.time}"></td>
<td data-th-text="${stat.amount}"></td>
</tr>
</tbody>
</table>
</div>
</section>
</div>
</div>
</div>

0 comments on commit 09f129c

Please sign in to comment.