Skip to content

Commit

Permalink
Merge pull request #53 from YoussefDahi/develop
Browse files Browse the repository at this point in the history
db model update
  • Loading branch information
usfalami authored May 15, 2024
2 parents 0b422a8 + 5cc513f commit 00b43df
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 58 deletions.
91 changes: 51 additions & 40 deletions src/main/java/org/usf/trace/api/server/RequestDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.sql.Types.BIGINT;
import static java.sql.Types.TIMESTAMP;
import static java.sql.Types.VARCHAR;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.Optional.ofNullable;
import static java.util.function.Function.identity;
Expand All @@ -22,33 +23,20 @@
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import java.util.stream.Stream;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.usf.traceapi.core.ApiRequest;
import org.usf.traceapi.core.ApiSession;
import org.usf.traceapi.core.ApplicationInfo;
import org.usf.traceapi.core.DatabaseAction;
import org.usf.traceapi.core.DatabaseRequest;
import org.usf.traceapi.core.ExceptionInfo;
import org.usf.traceapi.core.JDBCAction;
import org.usf.traceapi.core.LaunchMode;
import org.usf.traceapi.core.MainSession;
import org.usf.traceapi.core.RunnableStage;
import org.usf.traceapi.core.Session;
import org.usf.traceapi.core.*;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -172,36 +160,42 @@ public void addOutcomingStages(List<OutcomingStagesWrapper> stagesList){
private void addOutcomingQueries(List<OutcomingQueryWrapper> qryList) {
var maxId = template.queryForObject("SELECT COALESCE(MAX(ID_OUT_QRY), 0) FROM E_DB_REQ", Long.class);
var inc = new AtomicLong(maxId);
template.batchUpdate("INSERT INTO E_DB_REQ(ID_OUT_QRY,VA_HST,VA_SCHMA,DH_DBT,DH_FIN,VA_USR,VA_THRED,VA_DRV,VA_DB_NME,VA_DB_VRS,VA_CMPLT,CD_SES)"
+ " VALUES(?,?,?,?,?,?,?,?,?,?,?,?)", qryList, qryList.size(), (ps, o) -> {

template.batchUpdate("INSERT INTO E_DB_REQ(ID_OUT_QRY,VA_HST,CD_PRT,VA_DB,DH_DBT,DH_FIN,VA_USR,VA_THRED,VA_DRV,VA_DB_NME,VA_DB_VRS,VA_CMD,VA_NME,VA_LOC,VA_CMPLT,CD_SES)"
+ " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", qryList, qryList.size(), (ps, o) -> {
var completed = o.query.isCompleted();// o.getActions().stream().allMatch(a-> isNull(a.getException()));
ps.setLong(1, inc.incrementAndGet());
ps.setString(2, o.getHost());
ps.setString(3, o.getSchema());
ps.setTimestamp(4, fromNullableInstant(o.getStart()));
ps.setTimestamp(5, fromNullableInstant(o.getEnd()));
ps.setString(6, o.getUser());
ps.setString(7, o.getThreadName());
ps.setString(8, o.getDriverVersion());
ps.setString(9, o.getDatabaseName());
ps.setString(10, o.getDatabaseVersion());
ps.setString(11, o.isCompleted() ? "T" : "F");
ps.setString(12, o.getParentId());
ps.setInt(3, Objects.requireNonNullElse(o.getPort(),-1));
ps.setString(4, o.getDatabase());
ps.setTimestamp(5, fromNullableInstant(o.getStart()));
ps.setTimestamp(6, fromNullableInstant(o.getEnd()));
ps.setString(7, o.getUser());
ps.setString(8, o.getThreadName());
ps.setString(9, o.getDriverVersion());
ps.setString(10, o.getDatabaseName());
ps.setString(11, o.getDatabaseVersion());
ps.setString(12, valueOfNullableList(o.getCommands()));
ps.setString(13, o.getName());
ps.setString(14, o.getLocation());
ps.setString(15, completed ? "T" : "F");
ps.setString(16, o.getParentId());
o.setId(inc.get());
});
addDatabaseActions(qryList);
}

private void addDatabaseActions(List<OutcomingQueryWrapper> queries) {
template.batchUpdate("INSERT INTO E_DB_ACT(VA_TYP,DH_DBT,DH_FIN,VA_ERR_CLS,VA_ERR_MSG,CD_OUT_QRY) VALUES(?,?,?,?,?,?)",
template.batchUpdate("INSERT INTO E_DB_ACT(VA_TYP,DH_DBT,DH_FIN,VA_ERR_CLS,VA_ERR_MSG,CD_COUNT,CD_OUT_QRY) VALUES(?,?,?,?,?,?,?)",
queries.stream()
.flatMap(e -> e.getActions().stream().map(da -> {
var exp = nullableException(da.getException());
return new Object[]{da.getType().toString(), fromNullableInstant(da.getStart()), fromNullableInstant(da.getEnd()), exp.getClassname(), exp.getMessage(), e.getId()
return new Object[]{da.getType().toString(), fromNullableInstant(da.getStart()), fromNullableInstant(da.getEnd()), exp.getClassname(), exp.getMessage(),valueOfNullableArray(da.getCount()), e.getId()
};
}
))
.collect(toList()),
new int[]{VARCHAR, TIMESTAMP, TIMESTAMP, VARCHAR, VARCHAR, BIGINT});
new int[]{VARCHAR, TIMESTAMP, TIMESTAMP, VARCHAR, VARCHAR, VARCHAR, BIGINT});
}

@Deprecated // reuse RequestDao::outcomingRequests using criteria
Expand Down Expand Up @@ -407,21 +401,23 @@ public List<OutcomingStagesWrapper> outcomingStages(Set<String> sessionId, Suppl
}

public List<OutcomingQueryWrapper> outcomingQueries(Set<String> incomingId) { // non empty
var query = "SELECT ID_OUT_QRY,VA_HST,CD_PRT,VA_SCHMA,DH_DBT,DH_FIN,VA_USR,VA_THRED,VA_DRV,VA_DB_NME,VA_DB_VRS,VA_CMPLT,CD_SES FROM E_DB_REQ"
var query = "SELECT ID_OUT_QRY,VA_HST,CD_PRT,VA_DB,DH_DBT,DH_FIN,VA_USR,VA_THRED,VA_DRV,VA_DB_NME,VA_DB_VRS,VA_CMD,VA_NME,VA_LOC,CD_SES FROM E_DB_REQ"
+ " WHERE CD_SES IN(" + nArg(incomingId.size()) + ")";
var queries = template.query(query, incomingId.toArray(), newArray(incomingId.size(), VARCHAR), (rs, i) -> {
var out = new OutcomingQueryWrapper(rs.getLong("ID_OUT_QRY"), rs.getString("CD_SES"));
out.setHost(rs.getString("VA_HST"));
out.setPort(rs.getInt("CD_PRT"));
out.setSchema(rs.getString("VA_SCHMA"));
out.setSchema(rs.getString("VA_DB")); // --
out.setStart(fromNullableTimestamp(rs.getTimestamp("DH_DBT")));
out.setEnd(fromNullableTimestamp(rs.getTimestamp("DH_FIN")));
out.setUser(rs.getString("VA_USR"));
out.setThreadName(rs.getString("VA_THRED"));
out.setDriverVersion(rs.getString("VA_DRV"));
out.setDatabaseName(rs.getString("VA_DB_NME"));
out.setDatabaseVersion(rs.getString("VA_DB_VRS"));
// out.setCompleted("T".equals(rs.getString("VA_CMPLT"))); auto calculted
out.setCommands(valueOfNullabletoEnumList(SqlCommand.class, rs.getString("VA_CMD")));
out.setName(rs.getString("VA_NME"));
out.setLocation(rs.getString("VA_LOC"));
out.setActions(new ArrayList<>());
return out;
});
Expand All @@ -433,18 +429,19 @@ public List<OutcomingQueryWrapper> outcomingQueries(Set<String> incomingId) { //
}

public List<DatabaseActionWrapper> databaseActions(Set<Long> queries) { // non empty
var query = "SELECT VA_TYP,DH_DBT,DH_FIN,VA_ERR_CLS,VA_ERR_MSG,CD_OUT_QRY FROM E_DB_ACT"
var query = "SELECT VA_TYP,DH_DBT,DH_FIN,VA_ERR_CLS,VA_ERR_MSG,CD_COUNT,CD_OUT_QRY FROM E_DB_ACT"
+ " WHERE CD_OUT_QRY IN(" + nArg(queries.size()) + ") ORDER BY DH_DBT ASC";
return template.query(query, queries.toArray(), newArray(queries.size(), BIGINT), (rs, i) ->
new DatabaseActionWrapper(
rs.getLong("CD_OUT_QRY"),
JDBCAction.valueOf(rs.getString("VA_TYP")),
rs.getTimestamp("DH_DBT").toInstant(),
ofNullable(rs.getTimestamp("DH_DBT")).map(Timestamp::toInstant).orElse(null),
ofNullable(rs.getTimestamp("DH_FIN")).map(Timestamp::toInstant).orElse(null),
new ExceptionInfo(
rs.getString("VA_ERR_CLS"),
rs.getString("VA_ERR_MSG")
)));
),
ofNullable(rs.getString("CD_COUNT")).map(str -> Arrays.stream(str.split(",")).mapToLong(Long::parseLong).toArray()).orElse(null)));
}

@Getter
Expand Down Expand Up @@ -513,9 +510,9 @@ class DatabaseActionWrapper {
private final DatabaseAction action;
private final long parentId;

public DatabaseActionWrapper(long parentId, JDBCAction type, Instant start, Instant end, ExceptionInfo exception) {
public DatabaseActionWrapper(long parentId, JDBCAction type, Instant start, Instant end, ExceptionInfo exception, long[] count) {
this.parentId = parentId;
this.action = new DatabaseAction(type, start, end, exception);
this.action = new DatabaseAction(type, start, end,exception, count);
}
}

Expand Down Expand Up @@ -559,11 +556,25 @@ private static String valueOfNullable(Object o) {
return ofNullable(o).map(Object::toString).orElse(null);
}

private static <T extends Enum<T>> String valueOfNullableList(List<T> enumList) { return ofNullable(enumList).map(list -> list.stream().map(Enum::toString).collect(Collectors.joining(","))).orElse(null);}
private static String valueOfNullableArray(long[]array){ return ofNullable(array).map(arr -> LongStream.of(arr).mapToObj(Long::toString).collect(Collectors.joining(","))).orElse(null);}

private static <T extends Enum<T>> T valueOfNullable(Class<T> classe, String value) {
return ofNullable(value)
.flatMap(v -> Stream.of(classe.getEnumConstants()).filter(e -> e.name().equals(v)).findAny())
.orElse(null);
}

private static final String[] empty_array = new String[0];
private static String[] splitNullable(String s){
return isNull(s) ? empty_array : s.split(",");
}
private static <T extends Enum<T>> List<T> valueOfNullabletoEnumList(Class<T> classe, String... values){
return Stream.of(values)
.map(v-> valueOfNullable(classe, v))
.filter(Objects::nonNull)
.toList();
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,17 @@ public static String outQryColumns(TraceApiColumn column) {
case ID: return "id_out_qry";
case HOST: return "va_hst";
case PORT: return "cd_prt";
case SCHEMA: return "va_schma";
case DB: return "va_db";
case START: return "dh_dbt";
case END: return "dh_fin";
case USER: return "va_usr";
case THREAD: return "va_thred";
case DRIVER: return "va_drv";
case DB_NAME: return "va_db_nme";
case DB_VERSION: return "va_db_vrs";
case COMMANDS: return "va_cmd";
case NAME: return "va_nme";
case LOCATION: return "va_loc";
case COMPLETE: return "va_cmplt";
case PARENT: return "cd_ses";
default: return null;
Expand Down Expand Up @@ -142,6 +145,7 @@ public static String dbActColumns(TraceApiColumn column){
case ERR_TYPE: return "va_err_cls";
case ERR_MSG: return "va_err_msg";
case PARENT: return "cd_out_qry";
case ACTION_COUNT: return "cd_count";
default: return null;
}
}
Expand Down Expand Up @@ -170,7 +174,7 @@ private static OperationColumn countDbBySucces(TableDecorator table, ComparisonE
return count((complete).when(op).then(complete).end());
}

public static OperationColumn countDbError (TableDecorator table){ return countDbBySucces(table,equal('F'));}
public static OperationColumn countDbError(TableDecorator table){ return countDbBySucces(table,equal('F'));}
public static OperationColumn countDbSucces (TableDecorator table){ return countDbBySucces(table,equal('T'));}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public enum TraceApiColumn implements ColumnDecorator {
ERR_TYPE("errorType"),
ERR_MSG("errorMessage"),

SCHEMA("schema"),
DB("db"),
DRIVER("driver"),
DB_NAME("dbName"),
DB_VERSION("dbVersion"),
COMPLETE ("complete"),
COMPLETE("complete"),
COMMANDS("commands"),
ACTION_COUNT("actionCount"),
PARENT("parent"),

//---
Expand Down
15 changes: 15 additions & 0 deletions src/main/resources/model_update.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ALTER TABLE E_MAIN_SES ALTER COLUMN DH_DBT TYPE TIMESTAMP(6);
ALTER TABLE E_MAIN_SES ALTER COLUMN DH_FIN TYPE TIMESTAMP(6);
ALTER TABLE E_API_SES ALTER COLUMN DH_DBT TYPE TIMESTAMP(6);
ALTER TABLE E_API_SES ALTER COLUMN DH_FIN TYPE TIMESTAMP(6);
ALTER TABLE E_API_REQ ALTER COLUMN DH_DBT TYPE TIMESTAMP(6);
ALTER TABLE E_API_REQ ALTER COLUMN DH_FIN TYPE TIMESTAMP(6);
ALTER TABLE E_DB_REQ RENAME COLUMN VA_SCHMA TO VA_DB;
ALTER TABLE E_DB_REQ ALTER COLUMN DH_DBT TYPE TIMESTAMP(6);
ALTER TABLE E_DB_REQ ALTER COLUMN DH_FIN TYPE TIMESTAMP(6);
ALTER TABLE E_DB_REQ ADD COLUMN VA_CMD VARCHAR, ADD COLUMN VA_NME VARCHAR, ADD VA_LOC VARCHAR;
ALTER TABLE E_STG ALTER COLUMN DH_DBT TYPE TIMESTAMP(6);
ALTER TABLE E_STG ALTER COLUMN DH_FIN TYPE TIMESTAMP(6);
ALTER TABLE E_DB_ACT ALTER COLUMN DH_DBT TYPE TIMESTAMP(6);
ALTER TABLE E_DB_ACT ALTER COLUMN DH_FIN TYPE TIMESTAMP(6);
ALTER TABLE E_DB_ACT ADD COLUMN CD_COUNT VARCHAR;
32 changes: 18 additions & 14 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ CREATE TABLE IF NOT EXISTS E_MAIN_SES (
ID_SES VARCHAR PRIMARY KEY,
VA_NAME VARCHAR,
VA_USR VARCHAR,
DH_DBT TIMESTAMP(3),
DH_FIN TIMESTAMP(3),
DH_DBT TIMESTAMP(6),
DH_FIN TIMESTAMP(6),
LNCH VARCHAR,
LOC VARCHAR,
VA_THRED VARCHAR,
Expand All @@ -31,8 +31,8 @@ CREATE TABLE IF NOT EXISTS E_API_SES (
CD_STT INT,
VA_I_SZE BIGINT,
VA_O_SZE BIGINT,
DH_DBT TIMESTAMP(3),
DH_FIN TIMESTAMP(3),
DH_DBT TIMESTAMP(6),
DH_FIN TIMESTAMP(6),
VA_THRED VARCHAR,
VA_ERR_CLS VARCHAR,
VA_ERR_MSG VARCHAR,
Expand All @@ -58,8 +58,8 @@ CREATE TABLE IF NOT EXISTS E_API_REQ (
CD_STT INT,
VA_I_SZE BIGINT,
VA_O_SZE BIGINT,
DH_DBT TIMESTAMP(3),
DH_FIN TIMESTAMP(3),
DH_DBT TIMESTAMP(6),
DH_FIN TIMESTAMP(6),
VA_THRED VARCHAR,
VA_ERR_CLS VARCHAR,
VA_ERR_MSG VARCHAR,
Expand All @@ -71,23 +71,26 @@ CREATE TABLE IF NOT EXISTS E_DB_REQ (
ID_OUT_QRY BIGINT,
VA_HST VARCHAR,
CD_PRT INT,
VA_SCHMA VARCHAR,
DH_DBT TIMESTAMP(3),
DH_FIN TIMESTAMP(3),
VA_DB VARCHAR,
DH_DBT TIMESTAMP(6),
DH_FIN TIMESTAMP(6),
VA_USR VARCHAR,
VA_THRED VARCHAR,
VA_DRV VARCHAR,
VA_DB_NME VARCHAR,
VA_DB_VRS VARCHAR,
VA_CMPLT CHAR,
VA_CMD VARCHAR,
VA_NME VARCHAR,
VA_LOC VARCHAR,
VA_CMPLT CHAR,
CD_SES VARCHAR
);

CREATE TABLE IF NOT EXISTS E_STG (
VA_NAME VARCHAR,
LOC VARCHAR,
DH_DBT TIMESTAMP(3),
DH_FIN TIMESTAMP(3),
DH_DBT TIMESTAMP(6),
DH_FIN TIMESTAMP(6),
VA_USR VARCHAR,
VA_THRED VARCHAR,
VA_ERR_CLS VARCHAR,
Expand All @@ -97,9 +100,10 @@ CREATE TABLE IF NOT EXISTS E_STG (

CREATE TABLE IF NOT EXISTS E_DB_ACT (
VA_TYP VARCHAR,
DH_DBT TIMESTAMP(3),
DH_FIN TIMESTAMP(3),
DH_DBT TIMESTAMP(6),
DH_FIN TIMESTAMP(6),
VA_ERR_CLS VARCHAR,
VA_ERR_MSG VARCHAR,
CD_COUNT VARCHAR,
CD_OUT_QRY BIGINT
);
Loading

0 comments on commit 00b43df

Please sign in to comment.