fix: xstore sqlquery add xadmin log request
parent
819523c5ac
commit
869328a33f
|
|
@ -0,0 +1,61 @@
|
|||
package com.example.services;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DatabaseConnectXADMIN implements AutoCloseable {
|
||||
private static final Logger logger = LoggerFactory.getLogger(DatabaseConnectXADMIN.class);
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public DatabaseConnectXADMIN(String username) throws SQLException {
|
||||
try {
|
||||
Properties dbProperties = loadDatabaseProperties();
|
||||
|
||||
String url = dbProperties.getProperty("xadmin.db.url");
|
||||
String userpassword = dbProperties.getProperty("xadmin.db." + username + ".password");
|
||||
|
||||
connection = DriverManager.getConnection(url, username, userpassword);
|
||||
|
||||
logger.info("XADMIN Connection OK for user " + username);
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed to connect to XADMIN database for user " + username, e);
|
||||
throw e; // re-throw the exception
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
connection.close();
|
||||
logger.info("XADMIN Connection closed");
|
||||
}
|
||||
}
|
||||
|
||||
private Properties loadDatabaseProperties() {
|
||||
Properties dbProperties = loadProperties("db.properties");
|
||||
return dbProperties;
|
||||
}
|
||||
|
||||
private Properties loadProperties(String fileName) {
|
||||
Properties properties = new Properties();
|
||||
try (InputStream input = getClass().getClassLoader().getResourceAsStream(fileName)) {
|
||||
properties.load(input);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
// Handle the exception correctly for your application
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
package com.example.services;
|
||||
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@Path("/database")
|
||||
public class DatabaseService {
|
||||
|
||||
@GET
|
||||
@Path("/replication")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getCount(@QueryParam("dbHost") String dbHost) {
|
||||
|
||||
if (dbHost == null) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"dbHost parameter is required\"}").build();
|
||||
}
|
||||
|
||||
DriverManager.setLoginTimeout(5); // Définir le timeout à 5 secondes
|
||||
|
||||
try (DatabaseConnectXSTORE databaseConnection = new DatabaseConnectXSTORE(dbHost, "repqueue")) {
|
||||
String query = "SELECT COUNT(*) FROM CTL_REPLICATION_QUEUE";
|
||||
|
||||
try (PreparedStatement statement = databaseConnection.getConnection().prepareStatement(query);
|
||||
ResultSet resultSet = statement.executeQuery()) {
|
||||
|
||||
if (resultSet.next()) {
|
||||
long count = resultSet.getLong(1);
|
||||
// Construction manuelle de la réponse JSON
|
||||
String jsonResponse = "{\"count\":" + count + "}";
|
||||
return Response.ok(jsonResponse).build();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace(); // Gérer les exceptions correctement dans un environnement de production
|
||||
|
||||
// Construction de la réponse JSON en cas d'erreur
|
||||
String errorResponse = "{\"error\":\"" + e.getMessage() + "\"}";
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorResponse).build();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package com.example.services;
|
||||
|
||||
import com.example.services.store.*;
|
||||
import com.example.services.xadmin.application.*;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
|
|
@ -18,7 +19,10 @@ import java.sql.DriverManager;
|
|||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
|
@ -361,6 +365,149 @@ public class StoreService {
|
|||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{storeId}/log")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getLog(
|
||||
@PathParam("storeId") Integer storeId,
|
||||
@QueryParam("dbHost") String dbHost,
|
||||
@QueryParam("logDate") String logDate) {
|
||||
|
||||
if(storeId == null) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"storeId parameter is required\"}").build();
|
||||
}
|
||||
|
||||
if (logDate == null) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"logDate parameter is required\"}").build();
|
||||
}
|
||||
|
||||
if (dbHost == null) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"dbHost parameter is required\"}").build();
|
||||
}
|
||||
|
||||
DriverManager.setLoginTimeout(5); // Définir le timeout à 5 secondes
|
||||
|
||||
try (DatabaseConnectXSTORE databaseConnection = new DatabaseConnectXSTORE(dbHost,"dtv")) {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
|
||||
java.util.Date startDate;
|
||||
Integer timezoneOffset = 0;
|
||||
|
||||
try {
|
||||
startDate = formatter.parse(logDate);
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(startDate);
|
||||
cal.add(Calendar.DATE, -9);
|
||||
java.util.Date endDate = cal.getTime();
|
||||
|
||||
String queryTZ = "SELECT timezone_offset " +
|
||||
"FROM (" +
|
||||
" SELECT EXTRACT(HOUR FROM CAST(tt.BEGIN_DATETIME AS TIMESTAMP)) - EXTRACT(HOUR FROM CAST(tt.create_date AS TIMESTAMP)) AS timezone_offset " +
|
||||
" FROM dtv.trn_trans tt " +
|
||||
" WHERE" +
|
||||
" tt.ORGANIZATION_ID = 1" +
|
||||
" AND tt.RTL_LOC_ID = ?" +
|
||||
" AND tt.BUSINESS_DATE = TRUNC(?)" +
|
||||
" AND tt.trans_TYPCODE = 'SYSTEM_OPEN'" +
|
||||
" AND tt.TRANS_STATCODE = 'COMPLETE'" +
|
||||
") " +
|
||||
"WHERE ROWNUM = 1";
|
||||
|
||||
while (startDate.compareTo(endDate) >= 0) {
|
||||
try (PreparedStatement pst = databaseConnection.getConnection().prepareStatement(queryTZ)) {
|
||||
pst.setInt(1, storeId);
|
||||
pst.setDate(2, new java.sql.Date(startDate.getTime()));
|
||||
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
Integer tempTimezoneOffset = rs.getInt("timezone_offset");
|
||||
if (tempTimezoneOffset != null) {
|
||||
timezoneOffset = tempTimezoneOffset;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cal.setTime(startDate);
|
||||
cal.add(Calendar.DATE, -1);
|
||||
startDate = (java.util.Date) cal.getTime();
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String query = "SELECT cel.CREATE_DATE + INTERVAL '1' HOUR * ? as CREATE_DATE, " +
|
||||
"cel.CREATE_USER_ID, " +
|
||||
"cel.BUSINESS_DATE, " +
|
||||
"cel.RTL_LOC_ID, " +
|
||||
"cel.WKSTN_ID, " +
|
||||
"cel.LOG_LEVEL, " +
|
||||
"cel.THREAD_NAME, " +
|
||||
"cel.LOG_MESSAGE, " +
|
||||
"CASE WHEN cel.LOGGER_CATEGORY = 'dtv.xstore.app.preflight' THEN 'Pre-flight error' " +
|
||||
" WHEN cel.LOGGER_CATEGORY LIKE 'dtv.xstore.dataloader%' THEN 'DataLoader' " +
|
||||
" WHEN cel.LOGGER_CATEGORY = 'dtv.sysadmin.data.failover' THEN 'Data-failover' " +
|
||||
" WHEN cel.LOGGER_CATEGORY = 'dtv.xstore.order.download.offline' THEN 'Order error' " +
|
||||
" WHEN cel.LOGGER_CATEGORY = 'dtv.xstore.state.app.startup' THEN 'Xstore startup' " +
|
||||
" WHEN cel.LOGGER_CATEGORY = 'dtv.xstore.state.app.shutdown' THEN 'Xstore shutdown' " +
|
||||
" WHEN cel.LOGGER_CATEGORY = 'dtv.xstore.helpdesk.memory' THEN 'Application core' " +
|
||||
" WHEN cel.LOGGER_CATEGORY = 'dtv.xstore.helpdesk' THEN 'Application core' " +
|
||||
"ELSE cel.LOGGER_CATEGORY END as LOGGER_CATEGORY " +
|
||||
"FROM dtv.CTL_EVENT_LOG cel " +
|
||||
"WHERE cel.CREATE_DATE BETWEEN TO_DATE( ?, 'rrrrmmdd') AND TO_DATE( ?, 'rrrrmmdd hh24:mi:ss') " +
|
||||
"AND cel.ORGANIZATION_ID = 1 " +
|
||||
"AND cel.RTL_LOC_ID = ? " +
|
||||
"AND cel.THREAD_NAME IS NOT NULL " +
|
||||
"ORDER BY cel.CREATE_DATE DESC";
|
||||
|
||||
try (PreparedStatement statement = databaseConnection.getConnection().prepareStatement(query)) {
|
||||
statement.setInt(1, timezoneOffset);
|
||||
statement.setString(2, logDate);
|
||||
statement.setString(3, logDate + " 23:59:59");
|
||||
statement.setInt(4, storeId);
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
List<XadminApplicationLog> storeLogList = new ArrayList<>();
|
||||
|
||||
while (resultSet.next()) {
|
||||
XadminApplicationLog storeLog = mapResultSetToStoreLog(resultSet);
|
||||
storeLogList.add(storeLog);
|
||||
}
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String jsonResponse;
|
||||
try {
|
||||
jsonResponse = objectMapper.writeValueAsString(storeLogList);
|
||||
return Response.ok(jsonResponse).build();
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Error processing JSON\"}").build();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
String errorResponse = "{\"error\":\"" + e.getMessage() + "\"}";
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorResponse).build();
|
||||
}
|
||||
}
|
||||
|
||||
public XadminApplicationLog mapResultSetToStoreLog(ResultSet resultSet) throws SQLException {
|
||||
XadminApplicationLog xadminapplicationlog = new XadminApplicationLog();
|
||||
|
||||
xadminapplicationlog.setCreateDate(resultSet.getDate("CREATE_DATE"));
|
||||
xadminapplicationlog.setCreateUserId(resultSet.getString("CREATE_USER_ID"));
|
||||
xadminapplicationlog.setBusinessDate(resultSet.getDate("BUSINESS_DATE"));
|
||||
xadminapplicationlog.setRtlLocId(resultSet.getInt("RTL_LOC_ID"));
|
||||
xadminapplicationlog.setWkstnId(resultSet.getInt("WKSTN_ID"));
|
||||
xadminapplicationlog.setLogLevel(resultSet.getString("LOG_LEVEL"));
|
||||
xadminapplicationlog.setThreadName(resultSet.getString("THREAD_NAME"));
|
||||
xadminapplicationlog.setLogMessage(resultSet.getString("LOG_MESSAGE"));
|
||||
xadminapplicationlog.setLoggerCategory(resultSet.getString("LOGGER_CATEGORY"));
|
||||
|
||||
return xadminapplicationlog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a ResultSet to a store object.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -0,0 +1,127 @@
|
|||
package com.example.services;
|
||||
|
||||
import com.example.services.xadmin.application.XadminApplicationLog;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Path("/xadmin")
|
||||
public class XadminService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(DatabaseConnectXADMIN.class);
|
||||
|
||||
@GET
|
||||
@Path("/application/log/{storeId}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getApplicationLogByStoreId(@PathParam("storeId") Integer storeId,
|
||||
@QueryParam("WkstnId") Integer wkstnId,
|
||||
@QueryParam("BeginDate") String beginDate,
|
||||
@QueryParam("EndDate") String endDate,
|
||||
@QueryParam("SearchText") String searchText) {
|
||||
if (storeId == null) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"storeId parameter is required\"}").build();
|
||||
}
|
||||
|
||||
if (beginDate == null || endDate == null) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"BeginDate and EndDate parameters are required\"}").build();
|
||||
}
|
||||
|
||||
DriverManager.setLoginTimeout(5); // Définir le timeout à 5 secondes
|
||||
|
||||
try (DatabaseConnectXADMIN databaseConnection = new DatabaseConnectXADMIN("dtv")) {
|
||||
String query =
|
||||
"SELECT cel.BUSINESS_DATE, " +
|
||||
"cel.CREATE_DATE, " +
|
||||
"cel.CREATE_USER_ID, " +
|
||||
"cel.RTL_LOC_ID, " +
|
||||
"cel.WKSTN_ID, " +
|
||||
"cel.LOG_LEVEL, " +
|
||||
"cel.THREAD_NAME, " +
|
||||
"cel.LOG_MESSAGE " +
|
||||
"FROM dtv.CTL_EVENT_LOG cel " +
|
||||
"WHERE cel.CREATE_DATE BETWEEN ? AND TRUNC(? + 1) " +
|
||||
"AND cel.ORGANIZATION_ID = 1 " +
|
||||
"AND cel.RTL_LOC_ID = ? " +
|
||||
(wkstnId != null ? "AND cel.WKSTN_ID = ? " : "") +
|
||||
"ORDER BY cel.CREATE_DATE";
|
||||
|
||||
logger.info(query);
|
||||
|
||||
try (PreparedStatement statement = databaseConnection.getConnection().prepareStatement(query)) {
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
||||
java.sql.Date parsedBeginDate = null;
|
||||
java.sql.Date parsedEndDate = null;
|
||||
|
||||
try {
|
||||
parsedBeginDate = new java.sql.Date(format.parse(beginDate).getTime());
|
||||
parsedEndDate = new java.sql.Date(format.parse(endDate).getTime());
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
statement.setDate(1, parsedBeginDate);
|
||||
statement.setDate(2, parsedEndDate);
|
||||
statement.setInt(3, storeId);
|
||||
|
||||
if(wkstnId != null) {
|
||||
statement.setInt(4, wkstnId);
|
||||
}
|
||||
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
List<XadminApplicationLog> xadminApplicationLogList = new ArrayList<>();
|
||||
|
||||
while (resultSet.next()) {
|
||||
XadminApplicationLog xadminApplicationLog = mapResultSetToApplicationLog(resultSet);
|
||||
xadminApplicationLogList.add(xadminApplicationLog);
|
||||
}
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String jsonResponse = objectMapper.writeValueAsString(xadminApplicationLogList);
|
||||
|
||||
return Response.ok(jsonResponse).build();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
String errorResponse = "{\"error\":\"" + e.getMessage() + "\"}";
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorResponse).build();
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace(); // Handle exceptions correctly in a production environment
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Error processing JSON\"}").build();
|
||||
}
|
||||
}
|
||||
|
||||
public XadminApplicationLog mapResultSetToApplicationLog(ResultSet resultSet) throws SQLException {
|
||||
XadminApplicationLog xadminapplicationlog = new XadminApplicationLog();
|
||||
|
||||
xadminapplicationlog.setBusinessDate(resultSet.getDate("BUSINESS_DATE"));
|
||||
xadminapplicationlog.setCreateDate(resultSet.getDate("CREATE_DATE"));
|
||||
xadminapplicationlog.setCreateUserId(resultSet.getString("CREATE_USER_ID"));
|
||||
xadminapplicationlog.setRtlLocId(resultSet.getInt("RTL_LOC_ID"));
|
||||
xadminapplicationlog.setWkstnId(resultSet.getInt("WKSTN_ID"));
|
||||
xadminapplicationlog.setLogLevel(resultSet.getString("LOG_LEVEL"));
|
||||
xadminapplicationlog.setThreadName(resultSet.getString("THREAD_NAME"));
|
||||
xadminapplicationlog.setLogMessage(resultSet.getString("LOG_MESSAGE"));
|
||||
|
||||
return xadminapplicationlog;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
package com.example.services.xadmin.application;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class XadminApplicationLog {
|
||||
private Date businessDate;
|
||||
private Date createDate;
|
||||
private String createUserId;
|
||||
private Integer rtlLocId;
|
||||
private Integer wkstnId;
|
||||
private String logLevel;
|
||||
private String threadName;
|
||||
private String logMessage;
|
||||
private String loggerCategory;
|
||||
|
||||
public Date getBusinessDate() {
|
||||
return businessDate;
|
||||
}
|
||||
|
||||
public void setBusinessDate(Date businessDate) {
|
||||
this.businessDate = businessDate;
|
||||
}
|
||||
|
||||
public Date getCreateDate() {
|
||||
return createDate;
|
||||
}
|
||||
|
||||
public void setCreateDate(Date createDate) {
|
||||
this.createDate = createDate;
|
||||
}
|
||||
|
||||
public String getCreateUserId() {
|
||||
return createUserId;
|
||||
}
|
||||
|
||||
public void setCreateUserId(String createUserId) {
|
||||
this.createUserId = createUserId;
|
||||
}
|
||||
|
||||
public Integer getRtlLocId() {
|
||||
return rtlLocId;
|
||||
}
|
||||
|
||||
public void setRtlLocId(Integer rtlLocId) {
|
||||
this.rtlLocId = rtlLocId;
|
||||
}
|
||||
|
||||
public Integer getWkstnId() {
|
||||
return wkstnId;
|
||||
}
|
||||
|
||||
public void setWkstnId(Integer wkstnId) {
|
||||
this.wkstnId = wkstnId;
|
||||
}
|
||||
|
||||
public String getLogLevel() {
|
||||
return logLevel;
|
||||
}
|
||||
|
||||
public void setLogLevel(String logLevel) {
|
||||
this.logLevel = logLevel;
|
||||
}
|
||||
|
||||
public String getThreadName() {
|
||||
return threadName;
|
||||
}
|
||||
|
||||
public void setThreadName(String threadName) {
|
||||
this.threadName = threadName;
|
||||
}
|
||||
|
||||
public String getLogMessage() {
|
||||
return logMessage;
|
||||
}
|
||||
|
||||
public void setLogMessage(String logMessage) {
|
||||
this.logMessage = logMessage;
|
||||
}
|
||||
|
||||
public String getLoggerCategory() {
|
||||
return loggerCategory;
|
||||
}
|
||||
|
||||
public void setLoggerCategory(String loggerCategory) {
|
||||
this.loggerCategory = loggerCategory;
|
||||
}
|
||||
}
|
||||
|
|
@ -17,3 +17,7 @@ prod.db.com02.password=com20
|
|||
xstore.db.url=jdbc:oracle:thin:@HOST:1521/XSTORE
|
||||
xstore.db.dtv.password=dtv
|
||||
xstore.db.repqueue.password=repqueue
|
||||
|
||||
# XADMIN environment settings
|
||||
xadmin.db.url=jdbc:oracle:thin:@p-ODBG-b01-ipDC.tech.ikks.lan:1522/PXSTOREI
|
||||
xadmin.db.dtv.password=oY3poRSprOuqasO
|
||||
Loading…
Reference in New Issue