implemented logs view as text

pull/2/head
Vyacheslav N. Boyko 2018-03-10 14:04:01 +03:00
parent bcaa7e7cf8
commit 19ff4520ef
9 changed files with 144 additions and 5 deletions

View File

@ -6,7 +6,7 @@
<groupId>ru.bvn13</groupId>
<artifactId>jircbot</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
<packaging>jar</packaging>

View File

@ -1,6 +1,7 @@
package ru.bvn13.jircbot.database.entities;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Column;
@ -13,6 +14,7 @@ import javax.persistence.Table;
@Entity
@Table(name = "irc_messages")
@Getter @Setter
@NoArgsConstructor
public class IrcMessage extends BaseModel {
@Column

View File

@ -1,11 +1,22 @@
package ru.bvn13.jircbot.database.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import ru.bvn13.jircbot.database.entities.IrcMessage;
import java.util.Date;
import java.util.List;
/**
* Created by bvn13 on 10.03.2018.
*/
public interface IrcMessageRepository extends JpaRepository<IrcMessage, Long> {
List<IrcMessage> findAllByServerHostAndChannelNameAndDtCreatedGreaterThanEqualAndDtCreatedIsLessThanEqual(String serverHost, String channelName, Date dtFrom, Date dtTo);
//@Query("SELECT m FROM IrcMessage m WHERE m.serverHost = :serverHost AND m.channelName SIMILAR TO CONCAT('\\#+', :channelName) AND m.dtCreated >= :dtFrom AND m.dtCreated <= :dtTo")
@Query("SELECT m FROM IrcMessage m WHERE m.serverHost = :serverHost AND (m.channelName = CONCAT('#', :channelName) OR m.channelName = CONCAT('##', :channelName)) AND m.dtCreated >= :dtFrom AND m.dtCreated <= :dtTo")
List<IrcMessage> findAllByServerHostAndChannelNameAndDay(@Param("serverHost") String serverHost, @Param("channelName") String channelName, @Param("dtFrom") Date dtFrom, @Param("dtTo") Date dtTo);
}

View File

@ -4,6 +4,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.bvn13.jircbot.database.entities.IrcMessage;
import ru.bvn13.jircbot.database.repositories.IrcMessageRepository;
import ru.bvn13.jircbot.utilities.DateTimeUtility;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
/**
* Created by bvn13 on 10.03.2018.
@ -18,4 +25,25 @@ public class IrcMessageService {
ircMessageRepository.save(message);
}
public List<IrcMessage> getMessagesOfDay(String serverHost, String channelName, Date date) {
LocalDateTime localDateTime = DateTimeUtility.dateToLocalDateTime(date);
LocalDateTime dtFrom = localDateTime.with(LocalTime.MIN);
LocalDateTime dtTo = localDateTime.with(LocalTime.MAX);
return ircMessageRepository.findAllByServerHostAndChannelNameAndDay(
serverHost,
channelName,
DateTimeUtility.localDateTimeToDate(dtFrom),
DateTimeUtility.localDateTimeToDate(dtTo)
);
/*return ircMessageRepository.findAllByServerHostAndChannelNameAndDtCreatedGreaterThanEqualAndDtCreatedIsLessThanEqual(
serverHost,
channelName,
DateTimeUtility.localDateTimeToDate(dtFrom),
DateTimeUtility.localDateTimeToDate(dtTo)
);*/
}
}

View File

@ -0,0 +1,20 @@
package ru.bvn13.jircbot.utilities;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
/**
* Created by bvn13 on 10.03.2018.
*/
public class DateTimeUtility {
public static LocalDateTime dateToLocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
}

View File

@ -1,8 +1,16 @@
package ru.bvn13.jircbot.web.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ru.bvn13.jircbot.database.entities.IrcMessage;
import ru.bvn13.jircbot.database.services.IrcMessageService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* Created by bvn13 on 10.03.2018.
@ -11,9 +19,53 @@ import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping("/logs")
public class IrcLogController {
@RequestMapping(value = "", method = RequestMethod.GET)
public String index() {
private final SimpleDateFormat DATE_READER = new SimpleDateFormat("yyyy-MM-dd");
private final SimpleDateFormat DATE_WRITER = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
@Autowired
private IrcMessageService ircMessageService;
@RequestMapping(value = "/{serverHost:.+}/{day}", method = RequestMethod.GET)
public String index(@PathVariable String serverHost, @RequestParam(name = "channel") String channelName, @PathVariable String day, Model model) {
model.addAttribute("serverHost", serverHost);
model.addAttribute("channelName", channelName);
model.addAttribute("day", day);
return "irclog";
}
@RequestMapping(value = "/text/{serverHost:.+}/{day}", method = RequestMethod.GET, produces = "plain/text; charset=utf-8")
public @ResponseBody String indexText(@PathVariable String serverHost, @RequestParam(name = "channel") String channelName, @PathVariable String day, Model model) throws Exception {
StringBuilder sb = new StringBuilder();
Date date = null;
try {
date = DATE_READER.parse(day);
} catch (ParseException e) {
throw new Exception("unknown date");
}
List<IrcMessage> messages = ircMessageService.getMessagesOfDay(serverHost, channelName, date);
sb.append("SERVER: "+serverHost+"\n");
sb.append("CHANNEL: "+channelName+"\n");
sb.append("DATE: "+day+"\n");
sb.append("\n");
messages.forEach(msg -> {
sb.append(DATE_WRITER.format(msg.getDtCreated())+" | ");
if (msg.getUsername() != null && !msg.getUsername().isEmpty()) {
sb.append(msg.getUsername()+" | ");
}
sb.append(msg.getMessage()+"\n");
});
return sb.toString();
}
}

View File

@ -3,6 +3,9 @@ name: jircbot
config: config.json
server:
port: 8001
spring:
# for PostgreSQL
dataSource:

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>${day} - ${channelName} on ${serverHost}</title>
</head>
<body>
</body>
</html>