Simplified history
This commit is contained in:
		@@ -1,7 +1,6 @@
 | 
			
		||||
package com.r11.tools.controller;
 | 
			
		||||
 | 
			
		||||
import com.r11.tools.mappers.RequestHistoryMapper;
 | 
			
		||||
import com.r11.tools.model.HistoryRequestModel;
 | 
			
		||||
import com.r11.tools.model.RequestHistory;
 | 
			
		||||
import com.r11.tools.model.RequestHistoryDTO;
 | 
			
		||||
import com.r11.tools.service.RequestHistoryService;
 | 
			
		||||
@@ -9,9 +8,7 @@ import lombok.AllArgsConstructor;
 | 
			
		||||
import org.springframework.http.ResponseEntity;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -28,37 +25,14 @@ public class RequestHistoryController {
 | 
			
		||||
    private final RequestHistoryMapper mapper;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the list of Events in given time bracket.
 | 
			
		||||
     * The list of objects is received via {@link RequestHistoryDTO}, which contains time brackets,
 | 
			
		||||
     * as well as the key - uuid.
 | 
			
		||||
     * @param historyRequestModel EventRequestDto object that contains data needed to query the database
 | 
			
		||||
     * @return list of {@link RequestHistory}
 | 
			
		||||
     */
 | 
			
		||||
    @PostMapping
 | 
			
		||||
    public ResponseEntity<List<RequestHistoryDTO>> filterHistory(@RequestBody HistoryRequestModel historyRequestModel){
 | 
			
		||||
        return ResponseEntity.ok(
 | 
			
		||||
                service.getHistoryRecordsBetweenDatesAndByUUID(historyRequestModel)
 | 
			
		||||
                        .stream()
 | 
			
		||||
                        .map(mapper::requestHistoryToRequestHistoryDTO)
 | 
			
		||||
                        .collect(Collectors.toList())
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the list of Events of last 24h from given date.
 | 
			
		||||
     * Returns the list of Events.
 | 
			
		||||
     * @param uuid unique id of message list
 | 
			
		||||
     * @return list of {@link RequestHistory}
 | 
			
		||||
     */
 | 
			
		||||
    @GetMapping(path = "/{uuid}")
 | 
			
		||||
    public ResponseEntity<List<RequestHistoryDTO>> getLastDay(@PathVariable UUID uuid){
 | 
			
		||||
        LocalDateTime requestTime = LocalDateTime.now();
 | 
			
		||||
        LocalDateTime dayBeforeRequest = requestTime.minusDays(1L);
 | 
			
		||||
        List<RequestHistoryDTO> requestHistory = service.getHistoryRecordsBetweenDatesAndByUUID(
 | 
			
		||||
                HistoryRequestModel.builder()
 | 
			
		||||
                        .localDateTimeFrom(dayBeforeRequest)
 | 
			
		||||
                        .localDateTimeTo(requestTime)
 | 
			
		||||
                        .clientUUID(uuid)
 | 
			
		||||
                        .build()
 | 
			
		||||
    public ResponseEntity<List<RequestHistoryDTO>> getLastDay(@PathVariable String uuid){
 | 
			
		||||
        List<RequestHistoryDTO> requestHistory = service.getHistoryRecordsByUUID(
 | 
			
		||||
                uuid
 | 
			
		||||
        ).stream()
 | 
			
		||||
                .map(mapper::requestHistoryToRequestHistoryDTO)
 | 
			
		||||
                .collect(Collectors.toList());
 | 
			
		||||
 
 | 
			
		||||
@@ -1,29 +0,0 @@
 | 
			
		||||
package com.r11.tools.model;
 | 
			
		||||
 | 
			
		||||
import lombok.AllArgsConstructor;
 | 
			
		||||
import lombok.Builder;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
import org.springframework.format.annotation.DateTimeFormat;
 | 
			
		||||
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Represents all data needed to get HistoryRecord from database
 | 
			
		||||
 * @author Mikołaj Widła
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
@Builder
 | 
			
		||||
@NoArgsConstructor
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
public class HistoryRequestModel {
 | 
			
		||||
 | 
			
		||||
    private UUID clientUUID;
 | 
			
		||||
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
 | 
			
		||||
    private LocalDateTime localDateTimeFrom;
 | 
			
		||||
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
 | 
			
		||||
    private LocalDateTime localDateTimeTo;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,12 +1,12 @@
 | 
			
		||||
package com.r11.tools.service;
 | 
			
		||||
 | 
			
		||||
import com.r11.tools.controller.RequestHistoryController;
 | 
			
		||||
import com.r11.tools.model.HistoryRequestModel;
 | 
			
		||||
import com.r11.tools.model.RequestHistory;
 | 
			
		||||
import com.r11.tools.model.RequestHistoryDTO;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Spring service interface for {@link RequestHistoryController}
 | 
			
		||||
 * @author Rafał Żukowicz
 | 
			
		||||
@@ -15,9 +15,9 @@ import org.springframework.stereotype.Service;
 | 
			
		||||
public interface RequestHistoryService {
 | 
			
		||||
    /**
 | 
			
		||||
     * Searches for {@link RequestHistory} objects between date brackets
 | 
			
		||||
     * @param historyRequestModel object containing required data for request
 | 
			
		||||
     * @param uuid user uuid
 | 
			
		||||
     * @return list of {@link RequestHistory}
 | 
			
		||||
     */
 | 
			
		||||
    List<RequestHistory> getHistoryRecordsBetweenDatesAndByUUID(HistoryRequestModel historyRequestModel);
 | 
			
		||||
    List<RequestHistory> getHistoryRecordsByUUID(String uuid);
 | 
			
		||||
    void saveRequest(RequestHistoryDTO requestDTO);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,6 @@ package com.r11.tools.service;
 | 
			
		||||
 | 
			
		||||
import com.r11.tools.controller.RequestHistoryController;
 | 
			
		||||
import com.r11.tools.mappers.RequestHistoryMapper;
 | 
			
		||||
import com.r11.tools.model.HistoryRequestModel;
 | 
			
		||||
import com.r11.tools.model.RequestHistory;
 | 
			
		||||
import com.r11.tools.model.RequestHistoryDTO;
 | 
			
		||||
import com.r11.tools.repository.RequestHistoryRepository;
 | 
			
		||||
@@ -11,7 +10,6 @@ import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Spring Service for {@link RequestHistoryController}. Contains logic required for quering
 | 
			
		||||
@@ -26,28 +24,11 @@ public class RequestHistoryServiceImpl implements RequestHistoryService {
 | 
			
		||||
    private final RequestHistoryRepository repository;
 | 
			
		||||
    private final RequestHistoryMapper requestMapper;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * in order to create query via{@link com.r11.tools.repository.RequestHistoryRepository}
 | 
			
		||||
     * @param historyRequestModel object containing required data for request
 | 
			
		||||
     * @return list of {@link RequestHistory}
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<RequestHistory> getHistoryRecordsBetweenDatesAndByUUID(HistoryRequestModel historyRequestModel) {
 | 
			
		||||
        List<RequestHistory> history = repository.findAllByClientUUID(
 | 
			
		||||
                historyRequestModel.getClientUUID().toString()
 | 
			
		||||
                );
 | 
			
		||||
    public List<RequestHistory> getHistoryRecordsByUUID(String uuid) {
 | 
			
		||||
        List<RequestHistory> history = repository.findAllByClientUUID(uuid);
 | 
			
		||||
        Collections.sort(history);
 | 
			
		||||
 | 
			
		||||
        return history.stream()
 | 
			
		||||
                .filter( historyRecord -> historyRecord
 | 
			
		||||
                        .getDateTimeStamp()
 | 
			
		||||
                        .isAfter(historyRequestModel.getLocalDateTimeFrom())
 | 
			
		||||
                ).filter(
 | 
			
		||||
                        historyRecord-> historyRecord
 | 
			
		||||
                                .getDateTimeStamp()
 | 
			
		||||
                                .isBefore(historyRequestModel.getLocalDateTimeTo())
 | 
			
		||||
                )
 | 
			
		||||
                .collect(Collectors.toList());
 | 
			
		||||
        return history;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -1,38 +1,7 @@
 | 
			
		||||
var historyJson = {};
 | 
			
		||||
const maxIterations = 200;
 | 
			
		||||
 | 
			
		||||
function filterHistory(){
 | 
			
		||||
    var dateFrom = new Date($('#historyFrom').val() + 'T' + $('#historyTimeFrom').val());
 | 
			
		||||
    
 | 
			
		||||
    var dateTo = new Date($('#historyTo').val() + 'T' + $('#historyTimeTo').val());
 | 
			
		||||
        
 | 
			
		||||
    loadHistory(dateFrom, dateTo);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const startSearch = function(){
 | 
			
		||||
    filterHistory();
 | 
			
		||||
}
 | 
			
		||||
$('#btn-searchHistory').click(startSearch);
 | 
			
		||||
 | 
			
		||||
function loadHistory(dateFrom, dateTo){
 | 
			
		||||
    
 | 
			
		||||
    var eventRequest = {
 | 
			
		||||
        clientUUID : json.clientUUID,
 | 
			
		||||
        localDateTimeFrom : dateFrom,
 | 
			
		||||
        localDateTimeTo : dateTo,
 | 
			
		||||
    };
 | 
			
		||||
    $.ajax({
 | 
			
		||||
        url: host + '/api/event',
 | 
			
		||||
        type: 'POST',
 | 
			
		||||
        data: JSON.stringify(eventRequest, null, 2),
 | 
			
		||||
        contentType: "application/json"
 | 
			
		||||
    }).done(function(data){
 | 
			
		||||
        historyJson = data;
 | 
			
		||||
        displayHistory();
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getLast24hHistoryData(){
 | 
			
		||||
function getHistoryData(){
 | 
			
		||||
    $.getJSON(host + '/api/event/' + clientUUID, function(data){
 | 
			
		||||
        historyJson = data;
 | 
			
		||||
        displayHistory();
 | 
			
		||||
 
 | 
			
		||||
@@ -48,7 +48,7 @@ function showHistory(){
 | 
			
		||||
 | 
			
		||||
function initializeHistory(){
 | 
			
		||||
    historyFilter.removeClass('active');
 | 
			
		||||
    getLast24hHistoryData();
 | 
			
		||||
    getHistoryData();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function showHeaders(){
 | 
			
		||||
@@ -176,7 +176,7 @@ function focusOutTip(element){
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function refreshHistoryRecords(){
 | 
			
		||||
    getLast24hHistoryData();
 | 
			
		||||
    getHistoryData();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function hidTip(element){
 | 
			
		||||
 
 | 
			
		||||
@@ -127,25 +127,7 @@
 | 
			
		||||
                            <!-- history -->
 | 
			
		||||
                            <div id="history" class="medium-vertical-margin tabcontent">
 | 
			
		||||
                                <div class="block-display max-width">
 | 
			
		||||
                                    <button id="btn-history-filter" class="clickable-text highlight switch">
 | 
			
		||||
                                        <span class="toggleIndicator"></span> filter 
 | 
			
		||||
                                        <button type="button" class="refresh-button" onclick="refreshHistoryRecords();" >↻</button>
 | 
			
		||||
                                    </button>
 | 
			
		||||
                                    <div id ="history-filter" class="display-space-between max-width small-vertical-margin hiddable">
 | 
			
		||||
                                        <div class="three-fourth-width display-space-evenly">
 | 
			
		||||
                                            <div class="block-display half-width with-padding">
 | 
			
		||||
                                                <label for="historyFrom" class="block-label">From</label>
 | 
			
		||||
                                                <input id="historyFrom" type="date" class="bordered-field max-width with-padding">
 | 
			
		||||
                                                <input id="historyTimeFrom" type="time" class="small-vertical-margin bordered-field max-width with-padding">
 | 
			
		||||
                                            </div>
 | 
			
		||||
                                            <div class="block-display half-width with-padding">
 | 
			
		||||
                                                <label for="historyTo" class="block-label">To</label>
 | 
			
		||||
                                                <input id="historyTo" type="date" class="bordered-field max-width with-padding">
 | 
			
		||||
                                                <input id="historyTimeTo" type="time" class="small-vertical-margin bordered-field max-width with-padding">
 | 
			
		||||
                                            </div>
 | 
			
		||||
                                        </div>
 | 
			
		||||
                                        <button id="btn-searchHistory" class="quater-width action-button active small-margins">Search</button>
 | 
			
		||||
                                    </div>
 | 
			
		||||
                                    <button type="button" class="refresh-button" onclick="refreshHistoryRecords();" >↻</button>
 | 
			
		||||
                                
 | 
			
		||||
                                    <div class="max-width centered-content large-vertical-margin overflowedTableContent">
 | 
			
		||||
                                        <table id="historyTable" class="table-default">
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user