Skip to content

Commit

Permalink
EPMRPP-87382 || delete expired user's photo (#115)
Browse files Browse the repository at this point in the history
* EPMRPP-87382 || delete expired user's photo
  • Loading branch information
grabsefx authored Nov 13, 2023
1 parent 56fa3b2 commit 0624fea
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import com.epam.reportportal.jobs.BaseJob;
import com.epam.reportportal.model.BlobNotFoundException;
import com.epam.reportportal.storage.DataStorageService;
import java.nio.charset.StandardCharsets;
import com.epam.reportportal.utils.DataStorageUtils;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
Expand Down Expand Up @@ -85,9 +83,9 @@ public void execute() {
}
try {
storageService.deleteAll(
thumbnails.stream().map(this::decode).collect(Collectors.toList()));
thumbnails.stream().map(DataStorageUtils::decode).collect(Collectors.toList()));
storageService.deleteAll(
attachments.stream().map(this::decode).collect(Collectors.toList()));
attachments.stream().map(DataStorageUtils::decode).collect(Collectors.toList()));
attachments.clear();
thumbnails.clear();
} catch (BlobNotFoundException e) {
Expand All @@ -102,8 +100,4 @@ public void execute() {
}
}

private String decode(String data) {
return StringUtils.isEmpty(data) ? data :
new String(Base64.getUrlDecoder().decode(data), StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.epam.reportportal.model.activity.event.UnassignUserEvent;
import com.epam.reportportal.model.activity.event.UserDeletedEvent;
import com.epam.reportportal.service.MessageBus;
import com.epam.reportportal.storage.DataStorageService;
import com.epam.reportportal.utils.DataStorageUtils;
import com.epam.reportportal.utils.ValidationUtil;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
Expand Down Expand Up @@ -55,6 +57,7 @@
public class DeleteExpiredUsersJob extends BaseJob {

public static final Logger LOGGER = LoggerFactory.getLogger(DeleteExpiredUsersJob.class);
public static final String USER_IDS = "userIds";

private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

Expand Down Expand Up @@ -95,6 +98,11 @@ WHERE id IN (

private static final String DELETE_USERS = "DELETE FROM users WHERE id IN (:userIds)";

private static final String SELECT_USERS_ATTACHMENTS = """
SELECT attachment FROM users WHERE (id IN (:userIds) AND attachment IS NOT NULL)\s
UNION\s
SELECT attachment_thumbnail FROM users WHERE (id IN (:userIds) AND attachment_thumbnail IS NOT NULL)""";

private static final String DELETE_PROJECTS_BY_ID_LIST =
"DELETE FROM project WHERE id IN (:projectIds)";

Expand All @@ -106,6 +114,7 @@ WHERE id IN (

@Value("${rp.environment.variable.clean.expiredUser.retentionPeriod}")
private Long retentionPeriod;
private final DataStorageService dataStorageService;

private final IndexerServiceClient indexerServiceClient;

Expand All @@ -114,9 +123,11 @@ WHERE id IN (
@Autowired
public DeleteExpiredUsersJob(JdbcTemplate jdbcTemplate,
NamedParameterJdbcTemplate namedParameterJdbcTemplate,
IndexerServiceClient indexerServiceClient, MessageBus messageBus) {
DataStorageService dataStorageService, IndexerServiceClient indexerServiceClient,
MessageBus messageBus) {
super(jdbcTemplate);
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
this.dataStorageService = dataStorageService;
this.indexerServiceClient = indexerServiceClient;
this.messageBus = messageBus;
}
Expand All @@ -136,6 +147,7 @@ public void execute() {
List<Long> personalProjectIds = getProjectIds(userProjects);
List<Long> nonPersonalProjectsByUserIds = findNonPersonalProjectIdsByUserIds(userIds);

deleteUsersPhoto(userIds);
deleteUsersByIds(userIds);
publishUnassignUserEvents(nonPersonalProjectsByUserIds);
personalProjectIds.forEach(this::deleteProjectAssociatedData);
Expand All @@ -146,6 +158,23 @@ public void execute() {
LOGGER.info("{} - users was deleted due to retention policy", userIds.size());
}

private void deleteUsersPhoto(List<Long> userIds) {
if (!CollectionUtils.isEmpty(userIds)) {
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue(USER_IDS, userIds);
var userAttachments = namedParameterJdbcTemplate
.queryForList(SELECT_USERS_ATTACHMENTS, parameters, String.class)
.stream()
.map(DataStorageUtils::decode)
.toList();
try {
dataStorageService.deleteAll(userAttachments);
} catch (Exception e) {
LOGGER.error("Failed to delete users photo from data storage: {}", userAttachments, e);
}
}
}

private void publishEmailNotificationEvents(List<String> userEmails) {
List<EmailNotificationRequest> notifications = userEmails.stream()
.map(recipient -> new EmailNotificationRequest(recipient, USER_DELETION_TEMPLATE))
Expand All @@ -161,7 +190,7 @@ private void publishUnassignUserEvents(List<Long> nonPersonalProjectsByUserIds)
private List<Long> findNonPersonalProjectIdsByUserIds(List<Long> userIds) {
return CollectionUtils.isEmpty(userIds) ? Collections.emptyList() :
namedParameterJdbcTemplate.queryForList(FIND_NON_PERSONAL_PROJECTS_BY_USER_IDS,
Map.of("userIds", userIds), Long.class
Map.of(USER_IDS, userIds), Long.class
);
}

Expand Down Expand Up @@ -190,7 +219,7 @@ private void deleteProjectAssociatedData(Long projectId) {
private void deleteUsersByIds(List<Long> userIds) {
if (!userIds.isEmpty()) {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("userIds", userIds);
params.addValue(USER_IDS, userIds);
namedParameterJdbcTemplate.update(DELETE_USERS, params);
messageBus.publishActivity(new UserDeletedEvent(userIds.size()));
}
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/epam/reportportal/utils/DataStorageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2023 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.epam.reportportal.utils;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.apache.commons.lang3.StringUtils;

/**
* Utility class for data storage functionality.
*
* @author <a href="mailto:[email protected]">Siarhe Hrabko</a>
*/
public final class DataStorageUtils {

private DataStorageUtils() {
}

public static String decode(String data) {
return StringUtils.isEmpty(data) ? data :
new String(Base64.getUrlDecoder().decode(data), StandardCharsets.UTF_8);
}
}

0 comments on commit 0624fea

Please sign in to comment.