Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docker #7

Merged
merged 5 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crude-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM amazoncorretto:11-alpine-jdk
COPY target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
24 changes: 24 additions & 0 deletions crude-server/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: '3.8'

services:

db:
image: postgres:13.7-alpine
container_name: db
ports:
- "6541:5432"
environment:
- POSTGRES_PASSWORD=root
- POSTGRES_USER=root
- POSTGRES_DB=CalibrationTable

server:

build: .

ports:
- "8080:8080"
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/CalibrationTable
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=root
6 changes: 3 additions & 3 deletions crude-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package ru.oil.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.ServletException;
import javax.validation.ValidationException;
import java.util.NoSuchElementException;

Expand All @@ -23,4 +25,10 @@ public ErrorResponse stateException(final RuntimeException e) {
return new ErrorResponse("Request Exception: ", e.getMessage());
}

@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse requestParamException(final ServletException e) {
return new ErrorResponse("Request Parameter Error.", e.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private UllageDtoFull calculateUllageWithFullInfo(UllageDto dto, Api api, Temper
default:
throw new IllegalStateException("Table not exist");
}
Wcf wcf = Wcf.create(vcf);
Wcf wcf = Wcf.create(api.getApi());
if (tables.equals(Tables.Table54A) || tables.equals(Tables.Table54B)) {
result.setGsvCub(result.getGovCub() * vcf.getVcf());
result.setGsvBbls(result.getGsvCub() / wcf.getT52());
Expand Down
10 changes: 5 additions & 5 deletions crude-server/src/main/java/ru/oil/units/wcf/Wcf.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package ru.oil.units.wcf;

import lombok.AllArgsConstructor;
import ru.oil.units.vcf.Vcf;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor(staticName = "create")
@AllArgsConstructor(staticName = "create")
public class Wcf {

private final Vcf vcf;
private double api;

public double getT11() {
return (0.0375 * ((589.943 / (vcf.getApi().getApi() + 131.5)) - 0.0050789) * 100000 + 0.5) / 100000;
return (0.0375 * ((589.943 / (api + 131.5)) - 0.0050789) * 100000 + 0.5) / 100000;
}

public double getT13() {
return (0.042 * ((535.1911 / (vcf.getApi().getApi() + 131.5)) - 0.0046189) * 100000 + 0.5) / 100000;
return (0.042 * ((535.1911 / (api + 131.5)) - 0.0046189) * 100000 + 0.5) / 100000;
}

public double getT52() {
double api = vcf.getApi().getApi();
double t52 = 0.15893;
if (api <= 15.29) {
return t52;
Expand Down
11 changes: 10 additions & 1 deletion crude-server/src/main/java/ru/oil/utils/UtilsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@
import org.springframework.web.bind.annotation.*;
import ru.oil.enums.Tables;
import ru.oil.utils.model.VcfDto;
import ru.oil.utils.model.WcfDto;
import ru.oil.utils.service.VcfService;
import ru.oil.utils.service.WcfService;

@RestController
@RequestMapping("/utils")
public class UtilsController {

private final VcfService vcfService;
private final WcfService wcfService;

@Autowired
public UtilsController(VcfService vcfService) {
public UtilsController(VcfService vcfService, WcfService wcfService) {
this.vcfService = vcfService;
this.wcfService = wcfService;
}

@GetMapping("/vcf")
public VcfDto getVcf(@RequestParam Double api, @RequestParam Double temp, @RequestParam Tables table,
@RequestHeader("Is-Api") boolean isApi, @RequestHeader("Is-Celsius") boolean isCelsius) {
return vcfService.getVcf(api, temp, table, isApi, isCelsius);
}

@GetMapping("/wcf")
public WcfDto getWcf(@RequestParam Double api) {
return wcfService.getWcf(api);
}
}
12 changes: 12 additions & 0 deletions crude-server/src/main/java/ru/oil/utils/mapper/WcfMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.oil.utils.mapper;

import ru.oil.units.wcf.Wcf;
import ru.oil.utils.model.WcfDto;

public class WcfMapper {

public static WcfDto wcfToDto(Wcf wcf) {
return WcfDto.create(wcf.getT11(), wcf.getT13(), wcf.getT52());
}

}
15 changes: 15 additions & 0 deletions crude-server/src/main/java/ru/oil/utils/model/WcfDto.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package ru.oil.utils.model;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldDefaults;

@AllArgsConstructor(staticName = "create")
@FieldDefaults(level = AccessLevel.PRIVATE)
@Getter
@Setter
public class WcfDto {

double t11;
double t13;
double t52;

}
12 changes: 12 additions & 0 deletions crude-server/src/main/java/ru/oil/utils/service/IWcfService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.oil.utils.service;

import ru.oil.units.api.Api;
import ru.oil.units.wcf.Wcf;
import ru.oil.utils.model.WcfDto;

public interface IWcfService {

WcfDto getWcf(double api);

WcfDto getWcfByDensity(double density);
}
22 changes: 22 additions & 0 deletions crude-server/src/main/java/ru/oil/utils/service/WcfService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.oil.utils.service;

import org.springframework.stereotype.Service;
import ru.oil.units.api.Api;
import ru.oil.units.wcf.Wcf;
import ru.oil.utils.mapper.WcfMapper;
import ru.oil.utils.model.WcfDto;

@Service
public class WcfService implements IWcfService {

@Override
public WcfDto getWcf(double api) {
return WcfMapper.wcfToDto(Wcf.create(api));
}

@Override
public WcfDto getWcfByDensity(double density) {
double api = Api.formDens(density).getApi();
return WcfMapper.wcfToDto(Wcf.create(api));
}
}
15 changes: 5 additions & 10 deletions crude-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=create
spring.sql.init.mode=always

spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:CalibrationTable
spring.datasource.username=test
spring.datasource.password=test

server.port=9090
spring.datasource.url=${SPRING_DATASOURCE_URL}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
server.port=8080

5 changes: 4 additions & 1 deletion crude-server/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ CREATE TABLE IF NOT EXISTS Calibration_Table
CREATE TABLE IF NOT EXISTS Cargo (
ID INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY NOT NULL,
NAME VARCHAR NOT NULL,
TYPE INT NOT NULL,
TYPE VARCHAR NOT NULL,
API FLOAT NOT NULL,
DENSITY FLOAT NOT NULL,
TEMP_C FLOAT NOT NULL,
TEMP_F FLOAT NOT NULL
);

/*
TODO Fix Fill table from CSV using docker
*/
INSERT INTO Calibration_table(ULLAGE, TANK, Cub1F, Cub, Cub1A, Cub2A, Cub3A, Cub4A) SELECT * FROM CSVREAD('crude-server/src/main/resources/static/1P.csv');
INSERT INTO Calibration_table(ULLAGE, TANK, Cub1F, Cub, Cub1A, Cub2A, Cub3A, Cub4A) SELECT * FROM CSVREAD('crude-server/src/main/resources/static/1S.csv');
INSERT INTO Calibration_table(ULLAGE, TANK, Cub1F, Cub, Cub1A, Cub2A, Cub3A, Cub4A) SELECT * FROM CSVREAD('crude-server/src/main/resources/static/2P.csv');
Expand Down