Skip to content

Commit

Permalink
EPMRPP-87382 || delete expired user's photo
Browse files Browse the repository at this point in the history
  • Loading branch information
grabsefx committed Nov 12, 2023
1 parent 56fa3b2 commit 31a5cfc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 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 @@ -95,6 +97,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 +113,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 +122,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 +146,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 +157,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("userIds", 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 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:siarhei_hrabko@epam.com">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 31a5cfc

Please sign in to comment.