Filtracja i korygowanie błędnych wyników.

This commit is contained in:
Damian Michalski 2020-01-20 00:26:28 +01:00
parent ef8bf1290c
commit d15e17ce4a
11 changed files with 477 additions and 29 deletions

View File

@ -55,6 +55,12 @@
<version>2.8.0</version>
</dependency>
<!-- Apache commons-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>

View File

@ -11,11 +11,10 @@
let gearbox = document.getElementById('gearbox');
let drive_type = document.getElementById('drive_type');
let request = new XMLHttpRequest();
// let url = "https://jsonplaceholder.typicode.com/photos";
// let url = "https://webhook.site/b1792c3d-cd5a-4c29-8e9c-1ff9f84653eb";
// let url = "http://34.65.132.148:8080";
let url = "http://localhost:8080/api/cars/search";
let url = "http://localhost:8080/car/api/search";
var params = [];
if(fuel_type.value != -1){
params.push("fuel_type="+fuel_type.options[fuel_type.value].text);
}
@ -46,6 +45,7 @@
if(drive_type.value != -1){
params.push("drive_type="+drive_type.value);
}
var buff = "";
if(params.length>0){
buff += "?";

View File

@ -132,8 +132,8 @@
Skrzynia biegów:
<select id="gearbox">
<option value="-1">Dowolna</option>
<option value="manual">Manualna</option>
<option value="automatic">Automatyczna</option>
<option value="Manual">Manualna</option>
<option value="Automatic">Automatyczna</option>
</select>
</div>
</div>
@ -143,9 +143,9 @@
Rodzaj napędu:
<select id="drive_type">
<option value="-1">Dowolny</option>
<option value="1">Na przednie koła</option>
<option value="2">Na tylnie koła</option>
<option value="3">Na cztery koła</option>
<option value="przednie">Na przednie koła</option>
<option value="tylnie">Na tylnie koła</option>
<option value="4x4">Na cztery koła</option>
</select>
</div>
</div>

View File

@ -2,8 +2,6 @@ package com.cars.car4you.api;
import com.cars.car4you.dto.CarDto;
import com.cars.car4you.model.Car;
import com.cars.car4you.model.CarDetails;
import com.cars.car4you.repository.CarDetailRepository;
import com.cars.car4you.repository.CarRepository;
import com.cars.car4you.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;

View File

@ -27,7 +27,6 @@ public class SimpleController {
@GetMapping("/{name}")
public byte[] getPageByName(@PathVariable String name) throws IOException {
System.out.println("Wczytuję stronę: " + name);
byte[] array = Files.readAllBytes(Paths.get("src/main/client/html/" + name));
return array;

View File

@ -11,19 +11,172 @@ public class CarDto {
private String brand;
private String model;
private String version;
private int year;
private int engine_power;
private double engine_capacity;
private Integer year;
private Integer engine_power;
private Double engine_capacity;
private String transmission;
private String drive;
private String body_type;
private int doors;
private int seats;
private long price_from;
private long price_to;
private long average;
private double rating;
private Integer doors;
private Integer seats;
private Long price_from;
private Long price_to;
private Long average;
private Double rating;
private String fuel;
private String[] car_pros;
private String[] car_cons;
private Double fuelEconomy;
public Double getFuelEconomy() {
return fuelEconomy;
}
public void setFuelEconomy(Double fuelEconomy) {
this.fuelEconomy = fuelEconomy;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public Integer getEngine_power() {
return engine_power;
}
public void setEngine_power(Integer engine_power) {
this.engine_power = engine_power;
}
public Double getEngine_capacity() {
return engine_capacity;
}
public void setEngine_capacity(Double engine_capacity) {
this.engine_capacity = engine_capacity;
}
public String getTransmission() {
return transmission;
}
public void setTransmission(String transmission) {
this.transmission = transmission;
}
public String getDrive() {
return drive;
}
public void setDrive(String drive) {
this.drive = drive;
}
public String getBody_type() {
return body_type;
}
public void setBody_type(String body_type) {
this.body_type = body_type;
}
public Integer getDoors() {
return doors;
}
public void setDoors(Integer doors) {
this.doors = doors;
}
public Integer getSeats() {
return seats;
}
public void setSeats(Integer seats) {
this.seats = seats;
}
public Long getPrice_from() {
return price_from;
}
public void setPrice_from(Long price_from) {
this.price_from = price_from;
}
public Long getPrice_to() {
return price_to;
}
public void setPrice_to(Long price_to) {
this.price_to = price_to;
}
public Long getAverage() {
return average;
}
public void setAverage(Long average) {
this.average = average;
}
public Double getRating() {
return rating;
}
public void setRating(Double rating) {
this.rating = rating;
}
public String getFuel() {
return fuel;
}
public void setFuel(String fuel) {
this.fuel = fuel;
}
public String[] getCar_pros() {
return car_pros;
}
public void setCar_pros(String[] car_pros) {
this.car_pros = car_pros;
}
public String[] getCar_cons() {
return car_cons;
}
public void setCar_cons(String[] car_cons) {
this.car_cons = car_cons;
}
}

View File

@ -58,9 +58,152 @@ public class Car {
private Long price_to;
@Column(nullable = false, unique = false)
private Long avarage;
private Double avarage;
@Column(nullable = false, unique = false)
private String fuel;
public CarDetails getCarDetails() {
return carDetails;
}
public void setCarDetails(CarDetails carDetails) {
this.carDetails = carDetails;
}
public Long getCar_details_id() {
return car_details_id;
}
public void setCar_details_id(Long car_details_id) {
this.car_details_id = car_details_id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public Integer getEngine_power() {
return engine_power;
}
public void setEngine_power(Integer engine_power) {
this.engine_power = engine_power;
}
public Double getEngine_capacity() {
return engine_capacity;
}
public void setEngine_capacity(Double engine_capacity) {
this.engine_capacity = engine_capacity;
}
public String getTransmission() {
return transmission;
}
public void setTransmission(String transmission) {
this.transmission = transmission;
}
public String getDrive() {
return drive;
}
public void setDrive(String drive) {
this.drive = drive;
}
public String getBody_type() {
return body_type;
}
public void setBody_type(String body_type) {
this.body_type = body_type;
}
public Integer getDoors() {
return doors;
}
public void setDoors(Integer doors) {
this.doors = doors;
}
public Integer getSeats() {
return seats;
}
public void setSeats(Integer seats) {
this.seats = seats;
}
public Long getPrice_from() {
return price_from;
}
public void setPrice_from(Long price_from) {
this.price_from = price_from;
}
public Long getPrice_to() {
return price_to;
}
public void setPrice_to(Long price_to) {
this.price_to = price_to;
}
public Double getAvarage() {
return avarage;
}
public void setAvarage(Double avarage) {
this.avarage = avarage;
}
public String getFuel() {
return fuel;
}
public void setFuel(String fuel) {
this.fuel = fuel;
}
}

View File

@ -50,4 +50,83 @@ public class CarDetails {
@Column(nullable = true, unique = false)
private Double rating;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Long getPrice_from() {
return price_from;
}
public void setPrice_from(Long price_from) {
this.price_from = price_from;
}
public Long getPrice_to() {
return price_to;
}
public void setPrice_to(Long price_to) {
this.price_to = price_to;
}
public Long getAverage() {
return average;
}
public void setAverage(Long average) {
this.average = average;
}
public String[] getCar_pros() {
return car_pros;
}
public void setCar_pros(String[] car_pros) {
this.car_pros = car_pros;
}
public String[] getCar_cons() {
return car_cons;
}
public void setCar_cons(String[] car_cons) {
this.car_cons = car_cons;
}
public Double getRating() {
return rating;
}
public void setRating(Double rating) {
this.rating = rating;
}
}

View File

@ -35,7 +35,7 @@ public class CarRepositoryImpl implements CustomCarRepository {
LocalDateTime localDateTime = LocalDateTime.now();
if(maxPrice != null) predicates.add(criteriaBuilder.lessThanOrEqualTo(car.get("price_to"), maxPrice));
if(maxYearsOld != null) predicates.add(criteriaBuilder.greaterThanOrEqualTo(car.get("year"),localDateTime.getYear() - maxYearsOld));
if(maxYearsOld != null) predicates.add(criteriaBuilder.greaterThanOrEqualTo(car.get("year"),localDateTime.getYear() - maxYearsOld - 1));
if(enginePowerFrom != null) predicates.add(criteriaBuilder.greaterThanOrEqualTo(car.get("engine_power"), enginePowerFrom));
if(enginePowerTo != null) predicates.add(criteriaBuilder.lessThanOrEqualTo(car.get("engine_power"), enginePowerTo));
if(transmission != null && !transmission.isEmpty()) predicates.add(criteriaBuilder.like(car.get("transmission"), "%" + transmission + "%"));

View File

@ -3,10 +3,11 @@ package com.cars.car4you.service;
import com.cars.car4you.dto.CarDto;
import com.cars.car4you.model.Car;
import com.cars.car4you.repository.CarRepository;
import org.apache.commons.math3.util.Precision;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@Service
@ -32,7 +33,7 @@ public class CarService {
// ceny
carDto.setPrice_from(car.getPrice_from());
carDto.setPrice_to(car.getPrice_to());
carDto.setAverage(car.getAvarage());
carDto.setAverage(car.getAvarage().longValue());
// Parametry konieczne do przepisania z CarDetails (cars_details)
carDto.setBrand(car.getCarDetails().getBrand());
@ -46,16 +47,62 @@ public class CarService {
carDto.setAverage(car.getCarDetails().getAverage());
carDto.setCar_cons(car.getCarDetails().getCar_cons());
carDto.setCar_pros(car.getCarDetails().getCar_pros());
carDto.setRating(car.getCarDetails().getRating());
}
if(carDto.getRating() != null) carDto.setRating(car.getCarDetails().getRating());
return carDto;
}
private List<CarDto> convertListToDtoList(List<Car> cars){
return cars.stream()
List<CarDto> dtoList = cars.stream()
.map(this::mapCarModelToCarDto)
.collect(Collectors.toList());
for(CarDto c : dtoList){
Double fuelEconomy = 0D;
if(c.getFuel() == "Diesel"){
fuelEconomy = c.getEngine_power()*0.052174; //typowe spalanie L/KM dla diesla
if(fuelEconomy < 5) fuelEconomy = 5.1;
if(fuelEconomy > 15 ) fuelEconomy = 14.9;
}else{
fuelEconomy = c.getEngine_power()*0.1; //typowe spalanie L/KM dla benzyny //todo ustalic te parametru dokładniej
if(fuelEconomy < 5) fuelEconomy = 5.1;
if(fuelEconomy > 15) fuelEconomy = 14.9;
}
Precision.round(fuelEconomy, 1);
c.setFuelEconomy(fuelEconomy);
}
//uzupełniam brakujące oceny losowo
Random generator = new Random();
for(CarDto c : dtoList){
if(c.getRating() == null){
Double rating = generator.nextDouble()*2+3; //ocena nie mniejsza niz 3
c.setRating(Precision.round(rating, 1));
}
}
//szukam ogłoszeń z pros and cons
List<CarDto> prosAndCons = new ArrayList<>();
for(CarDto c : dtoList){
if (c.getCar_pros() != null && c.getCar_cons() != null) {
prosAndCons.add(c);
}
}
//ustawiam cons and pros dla aut oktore ich nie mają według losowych isniejących opisów
for(CarDto c : dtoList){
if (c.getCar_pros() == null && c.getCar_cons() == null) {
c.setCar_cons(prosAndCons.get(generator.nextInt(prosAndCons.size())).getCar_cons());
c.setCar_pros(prosAndCons.get(generator.nextInt(prosAndCons.size())). getCar_pros());
}
}
Collections.sort(dtoList, (c2, c1) -> Double.compare(c1.getRating(), c2.getRating())); //sortowanie malejąco
return dtoList;
}
private List<CarDto> processList(List<CarDto> carsToBeProcessed){
@ -68,6 +115,29 @@ public class CarService {
String bodyType, String gearbox, String driveType) {
List<Car> rawCars = carRepository.findByCriterion(maxPrice, maxYearsOld, seats, doors, bodyType, fuelType, gearbox, enginePowerFrom, enginePowerTo, driveType);
//jeżeli wyników jest mniej niż 3
if(rawCars.size() <= 2){
rawCars = carRepository.findByCriterion(maxPrice, maxYearsOld, null, null, bodyType, fuelType, gearbox, enginePowerFrom, enginePowerTo, driveType);
//jeżeli wyników nadal jest mniej niż 3
if(rawCars.size() <= 2){
rawCars = carRepository.findByCriterion(maxPrice, null, null, null, bodyType, fuelType, null, enginePowerFrom, enginePowerTo, driveType);
//jeżeli wyników nadal jest mniej niż 3
if(rawCars.size() <= 2){
rawCars = carRepository.findByCriterion(maxPrice, null, null, null, bodyType, fuelType, null, null, null, null);
}
}
}
//jeżeli wyników jest wiecej niz 50 (to i tak jest już dużo)
if(rawCars.size() > 50){
List<Car> tmp = new ArrayList<>();
for(int i = 0; i < 15; i++){
tmp.add(rawCars.get(i));
}
rawCars = tmp;
}
List<CarDto> carDtos = convertListToDtoList(rawCars);
return processList(carDtos);
}

View File

@ -12,8 +12,8 @@ spring.datasource.password=car4yourootek
spring.jpa.show-sql=true
#spring.jpa.generate-ddl=true
spring.jpa.generate-ddl=true
#spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.ddl-auto=validate
#spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=false