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

Purge traces of a given environment over a specified period through the endpoint #103

Merged
merged 44 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f5c23d2
edit
YoussefDahi Dec 3, 2024
59666f8
edit
YoussefDahi Dec 4, 2024
5553c23
Create Partition EndPoint
antonin77 Dec 5, 2024
94fa903
Merge pull request #92 from oneteme/44-partition-batch
antonin77 Dec 5, 2024
987c4eb
Merge branch 'main' into develop
antonin77 Dec 9, 2024
637749c
edit
YoussefDahi Dec 10, 2024
12adfbb
edit
YoussefDahi Dec 10, 2024
4589f38
edit
YoussefDahi Dec 10, 2024
3ed25eb
edit
YoussefDahi Dec 11, 2024
10ce644
edit
YoussefDahi Dec 11, 2024
2b129b7
edit
YoussefDahi Dec 12, 2024
4d5ec4a
edit
YoussefDahi Dec 12, 2024
4db9829
edit
YoussefDahi Dec 12, 2024
ab1d9b3
edit
YoussefDahi Dec 13, 2024
3a5ccf8
Merge branch 'hotfix_outofmem' into develop
antonin77 Dec 17, 2024
b30bbfc
Merge branch 'hotfix_outofmem' into develop
antonin77 Dec 18, 2024
a915750
Merge branch 'hotfix_outofmem' into develop
antonin77 Dec 18, 2024
c44fc58
hotfix
antonin77 Dec 18, 2024
fc1ea2d
hotfix id mail
antonin77 Dec 18, 2024
980bad8
edit
usfalami Dec 19, 2024
cdfbf6b
Merge branch 'hotfix_outofmem' into develop
usfalami Dec 19, 2024
f7b548e
clean code
antonin77 Dec 19, 2024
00f5eed
merge conflict
antonin77 Dec 19, 2024
4ca275a
edit
usfalami Dec 20, 2024
fd27c48
Merge remote-tracking branch 'origin/develop' into develop
usfalami Dec 20, 2024
2831d1f
edit
usfalami Dec 20, 2024
5b51c1d
edit
usfalami Dec 20, 2024
672cf89
Merge remote-tracking branch 'origin/develop' into 81-purge-endpoint
YoussefDahi Dec 23, 2024
57b5286
edit
YoussefDahi Dec 23, 2024
0f59d6f
Merge pull request #95 from oneteme/81-purge-endpoint
usfalami Dec 23, 2024
a1dc5f8
refacto
antonin77 Dec 23, 2024
3583ba4
Merge branch 'main' into develop
antonin77 Dec 23, 2024
5fe4d81
Merge branch 'main' into develop
antonin77 Dec 23, 2024
6ca1658
optim
antonin77 Dec 24, 2024
f43579d
Merge branch 'main' into develop
antonin77 Dec 24, 2024
81648a7
Merge branch 'develop' into 96-clean-properties-unused
antonin77 Dec 24, 2024
5d3ca8c
optimisation recup
antonin77 Dec 24, 2024
f975f71
Merge pull request #98 from oneteme/96-clean-properties-unused
antonin77 Dec 24, 2024
db95346
Merge branch 'main' into develop
usfalami Dec 24, 2024
1f7e9ef
Merge remote-tracking branch 'origin/develop' into develop
usfalami Dec 24, 2024
9501594
clean crossOrigin
antonin77 Dec 24, 2024
f030efa
Merge branch 'main' into 81-purge-endpoint
usfalami Dec 31, 2024
7837895
Merge remote-tracking branch 'origin/81-purge-endpoint' into 81-purge…
YoussefDahi Jan 2, 2025
e385a71
Merge branch 'main' into 81-purge-endpoint
usfalami Jan 2, 2025
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
231 changes: 231 additions & 0 deletions src/main/java/org/usf/inspect/server/QueryLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package org.usf.inspect.server;

import lombok.NonNull;
import org.usf.inspect.server.model.Query;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.*;
import static org.usf.inspect.server.Utils.nArg;
public class QueryLoader {

public static List<Query> loadQueries (@NonNull String env, List<String> appName, @NonNull Instant before, List<String> version) {

List<Object> cteParams = new ArrayList<>();
cteParams.add(Timestamp.from(before));
cteParams.add(env);
String appNameCondition = "";
if(appName != null && !appName.isEmpty()){
appNameCondition = "and va_app in ("+ nArg(appName.size()) +")";
cteParams.addAll(appName);
}

String versionCondition = "";
if(version != null && !version.isEmpty()) {
versionCondition = "and va_vrs in ("+ nArg(version.size()) +")";
cteParams.addAll(version);
}
cteParams.add(Timestamp.from(before));
cteParams.add(Timestamp.from(before));


List<Query> queries = new ArrayList<>();
//create session temp table
queries.add(new Query(
"create temporary table IF NOT EXISTs temp_session_table( id varchar(255), va_typ char(1) );")
);

//create request temp table
queries.add(new Query(
"create temporary table IF NOT EXISTs temp_request_table( id int);"
));

//fill temp session temp table
queries.add(new Query(
"""
with deleted_instances as( select eei.id_ins as id, 'i' as va_typ from e_env_ins eei where dh_str < ? and va_env = ? %s %s )
insert into temp_session_table
select id_ses as id, 'r' as va_typ
from e_rst_ses ers
where ers.cd_ins in (select id from deleted_instances)
and ers.dh_str < ?
union
select id_ses as id, 'm' as va_typ
from e_main_ses ems
where ems.cd_ins in (select id from deleted_instances)
and ems.dh_str < ?
union
select * from deleted_instances;
""".formatted(appNameCondition, versionCondition),cteParams.toArray()));
//fill temp request table
queries.add(new Query(
"insert into temp_request_table "+ createRequestQuery("id_rst_rqt","e_rst_rqt"
)));

// delete rest exception
queries.add(new Query(
"delete from e_exc_inf where cd_rqt in (select cast(id as int) from temp_request_table) and va_typ = 'REST';"
));

// delete rest request
queries.add(new Query(
"delete from e_rst_rqt where id_rst_rqt in (select id from temp_request_table);"
));

// empty temp_request_table
queries.add(new Query(
"delete from temp_request_table;"
));
//---------------------
//fill temp request table
queries.add(new Query(
"insert into temp_request_table " + createRequestQuery("id_dtb_rqt","e_dtb_rqt")
));

// delete jdbc exception
queries.add(new Query(
"delete from e_exc_inf where cd_rqt in (select id from temp_request_table) and va_typ = 'JDBC';"
));

// delete db stage
queries.add(new Query(
"delete from e_dtb_stg where cd_dtb_rqt in (select id from temp_request_table);"));

// delete db request
queries.add(new Query(
"delete from e_dtb_rqt where id_dtb_rqt in (select id from temp_request_table);"
));

// empty temp_request_table
queries.add(new Query(
"delete from temp_request_table;"
));
//---------------------
//fill temp request table
queries.add(new Query(
"insert into temp_request_table " + createRequestQuery("id_ftp_rqt","e_ftp_rqt")
));

// delete ftp exception
queries.add(new Query(
"delete from e_exc_inf where cd_rqt in (select id from temp_request_table) and va_typ = 'FTP';"
));

// delete ftp stage
queries.add(new Query(
"delete from e_ftp_stg where cd_ftp_rqt in (select id from temp_request_table);"
));

// delete ftp request
queries.add(new Query(
"delete from e_ftp_rqt where id_ftp_rqt in (select id from temp_request_table);"
));

// empty temp_request_table
queries.add(new Query(
"delete from temp_request_table;"
));
//---------------------
//fill temp request table
queries.add(new Query(
"insert into temp_request_table "+createRequestQuery("id_smtp_rqt","e_smtp_rqt")
));

// delete smtp exception
queries.add(new Query(
"delete from e_exc_inf where cd_rqt in (select id from temp_request_table) and va_typ = 'SMTP';"
));

// delete smtp stage
queries.add(new Query(
"delete from e_smtp_stg where cd_smtp_rqt in (select id from temp_request_table)"
));

// delete smtp mail
queries.add(new Query(
"delete from e_smtp_mail where cd_smtp_rqt in (select id from temp_request_table);"
));

// delete smtp request
queries.add(new Query(
"delete from e_smtp_rqt where id_smtp_rqt in (select id from temp_request_table);"
));

// empty temp_request_table
queries.add(new Query(
"delete from temp_request_table;"
));
//---------------------
//fill temp request table
queries.add(new Query(
"insert into temp_request_table "+ createRequestQuery("id_ldap_rqt","e_ldap_rqt")
));

// delete ldap exception
queries.add(new Query(
"delete from e_exc_inf where cd_rqt in (select id from temp_request_table) and va_typ = 'LDAP';"
));

// delete ldap stage
queries.add(new Query(
"delete from e_ldap_stg where cd_ldap_rqt in (select id from temp_request_table);"
));

// delete ldap request
queries.add(new Query(
"delete from e_ldap_rqt where id_ldap_rqt in (select id from temp_request_table);"
));
// empty temp_request_table
queries.add(new Query(
"delete from temp_request_table;"
));
//---------------------
//fill temp request table
queries.add(new Query(
"insert into temp_request_table "+createRequestQuery("id_lcl_rqt","e_lcl_rqt")
));

// delete local exception
queries.add(new Query(
"delete from e_exc_inf where cd_rqt in (select id from temp_request_table) and va_typ = 'LOCAL';"
));

// delete local request
queries.add(new Query(
"delete from e_lcl_rqt where id_lcl_rqt in (select id from temp_request_table);"
));

//drop temp request table
queries.add(new Query(
"drop table if exists temp_request_table;"
));
//----------------
// delete rest session
queries.add(new Query(
"delete from e_rst_ses where id_ses in (select id from temp_session_table where va_typ ='r');"
));

// delete main request
queries.add(new Query(
" delete from e_main_ses where id_ses in (select id from temp_session_table where va_typ ='m');"
));

// delete instances
queries.add(new Query(
"delete from e_env_ins where id_ins in (select id from temp_session_table where va_typ ='i');"
));

//delete temp table
queries.add(new Query(
"drop table if exists temp_session_table;"
));
return queries;
}

private static String createRequestQuery(String column, String table){
return """
select %s as id
from %s
where cd_prn_ses in (select id from temp_session_table)""".formatted(column,table);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.usf.inspect.server.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.usf.inspect.server.service.PurgeService;

import java.time.Instant;
import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping(value = "purge/", produces = APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class PurgeController {

private final PurgeService purgeService;

@DeleteMapping("purgedata")
public boolean purgeDate(
@RequestParam(name = "start") Instant start,
@RequestParam(name = "env") String environment,
@RequestParam(required = false, name = "appname") List<String> appNameList,
@RequestParam(required = false, name = "version") List<String> versionList
){
return purgeService.purgeData(environment,appNameList,start,versionList);
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/usf/inspect/server/model/Query.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.usf.inspect.server.model;

import lombok.Getter;
import lombok.RequiredArgsConstructor;


@Getter
public class Query {
private String sql;
private Object[] params;

public Query(String sql, Object[] params){
this.sql = sql;
this.params = params != null ? params : new Object[0];
}

public Query(String sql){
this.sql = sql;
}

}
35 changes: 35 additions & 0 deletions src/main/java/org/usf/inspect/server/service/PurgeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.usf.inspect.server.service;

import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.usf.inspect.server.QueryLoader;
import org.usf.inspect.server.model.Query;
import java.time.Instant;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

@Service
@RequiredArgsConstructor
public class PurgeService {

private final JdbcTemplate template;
private static final Logger logger = Logger.getLogger(PurgeService.class.getName());

@Transactional(rollbackFor = Throwable.class)
public boolean purgeData(String env, List<String> appName, Instant before, List<String> version) {

logger.log(Level.INFO, "+ Purging old Data, parameters in entry");
logger.log(Level.INFO, "\t- Environment: " + env);

Check notice

Code scanning / SonarCloud

Logging should not be vulnerable to injection attacks Low

Change this code to not log user-controlled data. See more on SonarQube Cloud
logger.log(Level.INFO, "\t- Start: " + before);
List<Query> queries = QueryLoader.loadQueries(env,appName,before,version);

for(Query query: queries){
template.update(query.getSql(),query.getParams());
}
return true;
}

}
63 changes: 63 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
INSERT INTO e_dtb_rqt (id_dtb_rqt,va_hst,cd_prt,va_nam,dh_str,dh_end,va_usr,va_thr,va_drv,va_prd_nam,va_prd_vrs,va_cmd,cd_prn_ses,va_stt,va_sch) VALUES
(1,'HOST',-1,'TEST','2024-07-19 11:07:43.386','2024-07-19 11:07:44.085','TEST','ForkJoinPool-1-worker-6','17.1','Teradata','Teradata Database','SELECT','f136f9a0-3487-4169-bd50-1b692a7a2718',true,NULL);
INSERT INTO e_dtb_stg (va_nam,dh_str,dh_end,va_cnt,cd_ord,cd_dtb_rqt) VALUES
('CONNECTION','2024-07-18 17:58:15.601','2024-07-18 17:58:15.684',NULL,1,1),
('STATEMENT','2024-07-18 17:58:15.698','2024-07-18 17:58:15.701',NULL,2,1),
('EXECUTE','2024-07-18 17:58:15.715','2024-07-18 17:58:15.723',NULL,3,1),
('FETCH','2024-07-18 17:58:15.728','2024-07-18 17:58:15.768','629',4,1),
('MORE','2024-07-18 17:58:15.768','2024-07-18 17:58:15.768',NULL,5,1),
('DISCONNECTION','2024-07-18 17:58:15.769','2024-07-18 17:58:15.770',NULL,6,1);
INSERT INTO e_env_ins
(id_ins, va_typ, dh_str, va_app, va_vrs, va_adr, va_env, va_os, va_re, va_usr, va_clr)
VALUES('b8610804-3b52-4913-beb0-a81792914512', 'CLIENT', '2024-11-27 10:35:21.550', 'ihm', '__GIT_BUILD_VERSION__', '0:0:0:0:0:0:0:1', 'dev', 'Windows 10.0', 'Edge', NULL, 'inspect-ng-collector-0.0.1');
INSERT INTO e_exc_inf
(va_typ, va_err_typ, va_err_msg, cd_ord, cd_rqt)
VALUES('JDBC', 'java.sql.SQLTransientConnectionException', 'HikariPool-1 - Connection is not available, request timed out after 180000ms.', 1, 1);
INSERT INTO e_ftp_rqt
(id_ftp_rqt, va_hst, cd_prt, va_pcl, va_srv_vrs, va_clt_vrs, va_usr, dh_str, dh_end, va_thr, cd_prn_ses, va_stt)
VALUES(1, 'host', 2222, 'ftps', 'SSH-2.0tp', 'SSH-2.0-JSCH-0.1.54', 'cosHRDHy', '2024-07-18 23:30:00.875', '2024-07-18 23:30:00.888', 'pool-6-thread-1', 'f136f9a0-3487-4169-bd50-1b692a7a2718', true);
INSERT INTO e_ftp_stg
(va_nam, dh_str, dh_end, va_arg, cd_ord, cd_ftp_rqt)
VALUES('CONNECTION', '2024-07-26 12:30:00.899', '2024-07-26 12:30:00.911', NULL, 1, 1);
INSERT INTO e_lcl_rqt
(id_lcl_rqt, va_nam, va_lct, dh_str, dh_end, va_usr, va_thr, cd_prn_ses, va_stt)
VALUES(1, 'cacheCalendar', 'CalendarDaoImpl', '2024-07-18 17:49:55.168', '2024-07-18 17:49:59.068', NULL, 'main', '120032b3-a1cb-411f-b61c-32efd82b0540', true);
INSERT INTO e_ldap_rqt
(id_ldap_rqt, va_hst, cd_prt, va_pcl, va_usr, dh_str, dh_end, va_thr, cd_prn_ses, va_stt)
VALUES(1, 'host', 636, NULL, 'u', '2024-07-19 09:44:05.948', '2024-07-19 09:44:06.089', 'http-nio-9000-exec-10', 'f136f9a0-3487-4169-bd50-1b692a7a2718', true);
INSERT INTO e_ldap_stg
(va_nam, dh_str, dh_end, va_arg, cd_ord, cd_ldap_rqt)
VALUES('CONNECTION', '2024-07-19 09:44:05.948', '2024-07-19 09:44:06.076', NULL, 1, 1);
INSERT INTO e_main_ses
(id_ses, va_typ, va_nam, va_usr, dh_str, dh_end, va_lct, va_thr, va_err_typ, va_err_msg, va_msk, cd_ins)
VALUES('120032b3-a1cb-411f-b61c-32efd82b0540', 'VIEW', 'f', 'f', '2024-07-30 15:53:00.148', '2024-07-30 15:53:02.807', 'https://host.fr/#/home', NULL, NULL, NULL, 4, 'b8610804-3b52-4913-beb0-a81792914512');
INSERT INTO e_rst_rqt
(id_rst_rqt, va_mth, va_pcl, va_hst, cd_prt, va_pth, va_qry, va_cnt_typ, va_ath_sch, cd_stt, va_i_sze, va_o_sze, va_i_cnt_enc, va_o_cnt_enc, dh_str, dh_end, va_thr, cd_prn_ses, cd_rmt_ses)
VALUES(10622, 'POST', 'https', 'host.fr', 0, '/user/action', '', 'json', NULL, 200, 0, 0, NULL, NULL, '2024-07-23 10:59:56.263', '2024-07-23 10:59:56.397', NULL, 'f136f9a0-3487-4169-bd50-1b692a7a2718', '3d3b241e-0f6d-434c-ba2a-4abc84244e83');
INSERT INTO e_rst_ses
(id_ses, va_mth, va_pcl, va_hst, cd_prt, va_pth, va_qry, va_cnt_typ, va_ath_sch, cd_stt, va_i_sze, va_o_sze, va_i_cnt_enc, va_o_cnt_enc, dh_str, dh_end, va_thr, va_err_typ, va_err_msg, va_nam, va_usr, va_usr_agt, va_cch_ctr, va_msk, cd_ins)
VALUES('f136f9a0-3487-4169-bd50-1b692a7a2718', 'GET', 'http', 'host.svc', 9000, '/RSYERYsdgsdSRY', NULL, 'application/json', 'Bearer', 200, 0, 25, NULL, NULL, '2024-07-19 11:07:43.386', '2024-07-18 17:52:41.612', 'http-nio-9000-exec-2', NULL, NULL, 'authoSERYe_roles', 'fe', 'Apache-HttpClient/5.1.4 (Java/17.0.4.1)', 'no-cache, no-store, max-age=0, must-revalidate', 0, 'b8610804-3b52-4913-beb0-a81792914512');
INSERT INTO e_smtp_mail
(va_sbj, va_cnt_typ, va_frm, va_rcp, va_rpl, va_sze, cd_smtp_rqt)
VALUES('Une erreur est survenue lors de la communication avzf', 'text/html;charset=utf-8', 'zffz', 'Dzdzf', 'zfzfn', -1, 1);
INSERT INTO e_smtp_rqt
(id_smtp_rqt, va_hst, cd_prt, va_usr, dh_str, dh_end, va_thr, cd_prn_ses, va_stt)
VALUES(1, 'mai:ugougvc', 9001, 'webadm', '2024-07-19 08:00:01.633', '2024-07-19 08:00:01.748', 'http-nio-9000-exec-5', 'f136f9a0-3487-4169-bd50-1b692a7a2718', true);
INSERT INTO e_smtp_stg
(va_nam, dh_str, dh_end, cd_ord, cd_smtp_rqt)
VALUES('CONNECTION', '2024-07-19 04:50:01.547', '2024-07-19 04:50:01.732', 1, 1);
--delete from e_dtb_rqt;
--delete from e_dtb_stg;
--delete from e_env_ins;
--delete from e_exc_inf;
--delete from e_ftp_rqt;
--delete from e_ftp_stg;
--delete from e_lcl_rqt;
--delete from e_ldap_rqt;
--delete from e_ldap_stg;
--delete from e_smtp_rqt;
--delete from e_smtp_stg;
--delete from e_smtp_mail;
--delete from e_main_ses;
--delete from e_rst_ses;
--delete from e_rst_rqt;
Loading