edu.iu.uits.lms.iuonly.services.business.ErrorContactBusinessService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lms-canvas-iu-custom-services Show documentation
Show all versions of lms-canvas-iu-custom-services Show documentation
Shared services for the lms-canvas LTI tools to interact with custom IU things
package edu.iu.uits.lms.iuonly.services.business;
/*-
* #%L
* lms-canvas-iu-custom-services
* %%
* Copyright (C) 2015 - 2022 Indiana University
* %%
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the Indiana University nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
import edu.iu.uits.lms.email.model.EmailDetails;
import edu.iu.uits.lms.email.model.Priority;
import edu.iu.uits.lms.email.service.EmailService;
import edu.iu.uits.lms.email.service.LmsEmailTooBigException;
import edu.iu.uits.lms.iuonly.config.DerdackConfig;
import edu.iu.uits.lms.iuonly.model.errorcontact.ErrorContactEvent;
import edu.iu.uits.lms.iuonly.model.errorcontact.ErrorContactJobProfile;
import edu.iu.uits.lms.iuonly.model.errorcontact.ErrorContactResponse;
import edu.iu.uits.lms.iuonly.repository.ErrorContactEventRepository;
import edu.iu.uits.lms.iuonly.repository.ErrorContactJobProfileRepository;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.mail.MessagingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@Profile("derdack")
@Service
@Slf4j
public class ErrorContactBusinessService {
@Autowired
@Qualifier("DerdackRestTemplate")
private RestTemplate restTemplate;
@Autowired
private DerdackConfig derdackConfig;
@Autowired
private ErrorContactEventRepository errorContactEventRepository;
@Autowired
private ErrorContactJobProfileRepository errorContactJobProfileRepository;
@Autowired
private EmailService emailService;
public ErrorContactResponse postEvent(@NonNull String jobCode, @NonNull String message) {
return postEvent(jobCode, message, false);
}
public ErrorContactResponse postEvent(@NonNull String jobCode, @NonNull String message, boolean alwaysPage) {
ErrorContactResponse resultErrorContactResponse = null;
String subject = emailService.getStandardHeader() + " LMS Microservices job " + jobCode;
try {
ErrorContactJobProfile errorContactJobProfile = errorContactJobProfileRepository.findByJobCode(jobCode);
if (errorContactJobProfile == null) {
log.error("No job profile found for jobCode = {}", jobCode);
resultErrorContactResponse = new ErrorContactResponse();
resultErrorContactResponse.setExternalId("-1");
resultErrorContactResponse.setStatus("jobCode not found");
return resultErrorContactResponse;
}
log.info("Found job profile = {}", errorContactJobProfile);
log.info("Always page = {}", alwaysPage);
StringBuilder emailBody = new StringBuilder();
Integer duplicateThresholdMinutes = errorContactJobProfile.getDuplicateMinutesThreshold();
Integer duplicateMaxCount = errorContactJobProfile.getDuplicateMaxCount();
boolean doPage = alwaysPage;
// We need to start at 1 because this current error isn't in this count
int jobcodeEventsWithinThreshold = 1;
StringBuilder emailFirstPartMessage = new StringBuilder();
emailFirstPartMessage.append("\nThe job ");
emailFirstPartMessage.append(jobCode);
emailFirstPartMessage.append(" has failed");
if (duplicateThresholdMinutes != null && duplicateMaxCount != null) {
if (!alwaysPage) {
jobcodeEventsWithinThreshold = jobcodeEventsWithinThreshold +
errorContactEventRepository.numberOfJobCodesNoOlderThanMinutes(jobCode, duplicateThresholdMinutes);
log.error("checked for past jobs and got job # = {}", jobcodeEventsWithinThreshold);
}
emailFirstPartMessage.append(" ");
emailFirstPartMessage.append(jobcodeEventsWithinThreshold);
emailFirstPartMessage.append(" times.");
if (jobcodeEventsWithinThreshold >= duplicateMaxCount) {
// it's paging time
log.info("Paging for jobCode = {}", jobCode);
doPage = true;
}
} else {
emailFirstPartMessage.append(".");
}
ErrorContactEvent errorContactEvent = new ErrorContactEvent();
errorContactEvent.setErrorContactJobProfile(errorContactJobProfile);
errorContactEvent.setMessage(message);
errorContactEvent.setAction(doPage ? "PAGED" : "EMAILED");
// Page and email
if (doPage) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
Map formMap = new HashMap<>();
formMap.put("team", derdackConfig.getTeam());
formMap.put("subject", subject);
formMap.put("body", subject + " - " + message);
HttpEntity
© 2015 - 2024 Weber Informatics LLC | Privacy Policy