Skip to content

Commit

Permalink
Added a log cleaner #4180
Browse files Browse the repository at this point in the history
Signed-off-by: ClementBouvierN <clement.bouvierneveu@rte-france.com>
  • Loading branch information
ClementBouvierN committed Mar 14, 2023
1 parent 38f40a2 commit d07759b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 7 deletions.
5 changes: 4 additions & 1 deletion config/dev/users-dev.yml
Expand Up @@ -156,4 +156,7 @@ operatorfabric.users.default:
description: Da Operator Rulez
#logging:
# level:
# ROOT: DEBUG
# ROOT: DEBUG

daysBeforeLogExpiration: 61
hourScheduledForLogsCleaning: 0 30 0 * * *
@@ -1,4 +1,4 @@
/* Copyright (c) 2018-2022, RTE (http://www.rte-france.com)
/* Copyright (c) 2018-2023, RTE (http://www.rte-france.com)
* See AUTHORS.txt
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -16,7 +16,6 @@
import org.opfab.cards.consultation.model.LightCardConsultationData;
import org.opfab.cards.model.CardOperationTypeEnum;
import org.opfab.users.model.CurrentUserWithPerimeters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
Expand Down
@@ -0,0 +1,36 @@
/* Copyright (c) 2023, RTE (http://www.rte-france.com)
* See AUTHORS.txt
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
* This file is part of the OperatorFabric project.
*/


package org.opfab.users.scheduledtask;

import lombok.extern.slf4j.Slf4j;

import org.opfab.useractiontracing.services.UserActionLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@EnableScheduling
public class DeleteExpiredLogsScheduler {
@Autowired
UserActionLogService userActionLogService;

@Value("${daysBeforeLogExpiration:61}") Integer daysBeforeLogExpiration;
@Value("${hourScheduledForLogsCleaning:0 30 0 * * *}") String hourScheduledForLogsCleaning;

@Scheduled(cron = "${hourScheduledForLogsCleaning:0 30 0 * * *}") //0 30 0 * * *
public void deleteExpiredLogs() {
userActionLogService.deleteLogsByExpirationDate(daysBeforeLogExpiration);
}
}
@@ -1,4 +1,4 @@
/* Copyright (c) 2022, RTE (http://www.rte-france.com)
/* Copyright (c) 2023, RTE (http://www.rte-france.com)
* See AUTHORS.txt
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -9,6 +9,8 @@

package org.opfab.useractiontracing.repositories;

import java.time.Instant;

import org.opfab.useractiontracing.model.UserActionLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -17,5 +19,6 @@
public interface UserActionLogCustomRepository {

Page<UserActionLog> findByParams(MultiValueMap<String, String> params, Pageable pageable);
abstract void deleteExpiredLogs(Instant expirationDate);

}
@@ -1,4 +1,4 @@
/* Copyright (c) 2022, RTE (http://www.rte-france.com)
/* Copyright (c) 2023, RTE (http://www.rte-france.com)
* See AUTHORS.txt
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -16,6 +16,7 @@
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.MultiValueMap;

import java.time.Instant;
Expand Down Expand Up @@ -102,4 +103,10 @@ else if (key.equals("dateTo"))
return criteria;
}

public void deleteExpiredLogs (Instant expirationDate) {
Query expiredLogs = new Query();
expiredLogs.addCriteria(Criteria.where("date").lt(expirationDate));
this.template.remove(expiredLogs, UserActionLog.class, "userActionLogs");
}

}
@@ -1,4 +1,4 @@
/* Copyright (c) 2022, RTE (http://www.rte-france.com)
/* Copyright (c) 2023, RTE (http://www.rte-france.com)
* See AUTHORS.txt
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -18,9 +18,13 @@
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
@Service
public class UserActionLogService {

Expand All @@ -30,7 +34,8 @@ public UserActionLogService(UserActionLogRepository userActionLogRepository) {
this.userActionLogRepository = userActionLogRepository;
}

public void insertUserActionLog(String login, UserActionEnum actionType, List<String> entities, String cardUid, String comment) {
public void insertUserActionLog(String login, UserActionEnum actionType, List<String> entities, String cardUid,
String comment) {
UserActionLog action = UserActionLog.builder().login(login)
.action(actionType)
.date(Instant.now())
Expand All @@ -40,6 +45,7 @@ public void insertUserActionLog(String login, UserActionEnum actionType, List<St
.build();
this.insertUserActionLog(action);
}

public void insertUserActionLog(UserActionLog action) {
this.userActionLogRepository.save(action);
}
Expand All @@ -52,4 +58,10 @@ public Page<UserActionLog> getUserActionLogsByParams(MultiValueMap<String, Strin
return this.userActionLogRepository.findByParams(params, pageable);
}

public void deleteLogsByExpirationDate(Integer daysStored) {
Instant expirationDate = Instant.now().minus(daysStored, ChronoUnit.DAYS);
this.userActionLogRepository.deleteExpiredLogs(expirationDate);
log.info("Expired logs have been deleted");
}

}

0 comments on commit d07759b

Please sign in to comment.