Skip to content

Commit

Permalink
Feature/move leva doc cache temporal (#92)
Browse files Browse the repository at this point in the history
* uniform chache and add cache to tempoFolder
  • Loading branch information
s2oBCN authored Apr 5, 2022
1 parent 1b093ac commit 7218b83
Show file tree
Hide file tree
Showing 36 changed files with 65 additions and 2,541 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

- Temporal change with add LevaDoc
- Added slash to the end of nexusURL if it is not present
- Temporal change with add LevaDoc 2


## Unreleased
- Fix TIR and DTR documents are not properly indexed ([#55](https://github.com/opendevstack/ods-document-generation-svc/pull/55))
Expand Down
27 changes: 20 additions & 7 deletions src/main/groovy/org/ods/doc/gen/AppConfiguration.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,45 @@ import java.time.Duration
@Configuration
class AppConfiguration {

private static final String TEMPLATES = "templates"
private static final String TEMPORAL_FOLDER = "temporalFolder"
private static final String PROJECT_DATA = "projectData"
public static final int DAYS_IN_CACHE = 1

@Bean
Clock aClockBeanToMockInTesting() {
return Clock.systemDefaultZone()
}

@Bean
CaffeineCache caffeineTemplatesConfig(@Value('${cache.documents.basePath}') String basePath) {
CaffeineCache caffeineTemplatesFolder(@Value('${cache.documents.basePath}') String basePath) {
FileUtils.deleteDirectory(Paths.get(basePath).toFile())
return new CaffeineCache(
"templates",
TEMPLATES,
Caffeine.newBuilder()
.expireAfterWrite(Duration.ofDays(1))
.expireAfterWrite(Duration.ofDays(DAYS_IN_CACHE))
.removalListener({ version, graph, cause ->
deleteDocTemplatesForder(basePath, version)
FileUtils.deleteDirectory(Paths.get(basePath, version as String).toFile())
}).build()
)
}

private deleteDocTemplatesForder(String basePath, version) {
FileUtils.deleteDirectory(Paths.get(basePath, version as String).toFile())
@Bean
CaffeineCache caffeineTemporalFolder() {
return new CaffeineCache(
TEMPORAL_FOLDER,
Caffeine.newBuilder()
.expireAfterWrite(Duration.ofDays(DAYS_IN_CACHE))
.removalListener({ id, graph, cause ->
FileUtils.deleteDirectory(Paths.get(id as String).toFile())
}).build()
)
}

@Bean
CaffeineCache caffeineProjectDataConfig(@Value('${cache.projectData.expiration.minutes}') Long expirationMinutes) {
return new CaffeineCache(
"projectData",
PROJECT_DATA,
Caffeine.newBuilder().expireAfterWrite(Duration.ofMinutes(expirationMinutes)).build()
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.ods.doc.gen.project.data
package org.ods.doc.gen

import groovy.util.logging.Slf4j
import kong.unirest.Unirest
Expand Down
12 changes: 10 additions & 2 deletions src/main/groovy/org/ods/doc/gen/core/FileSystemHelper.groovy
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package org.ods.doc.gen.core

import groovy.io.FileType
import groovy.util.logging.Slf4j
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service

import java.nio.file.Files
import java.nio.file.Path

@Slf4j
@Service
class FileSystemHelper {

@Cacheable(value = "temporalFolder", key = '#id')
Path createTempDirectory(String id){
return Files.createTempDirectory(id)
}

List<File> loadFilesFromPath(String path, String fileExtension) {
def result = []
try {
new File(path).traverse(nameFilter: ~/.*\.${fileExtension}$/, type: groovy.io.FileType.FILES) { file ->
new File(path).traverse(nameFilter: ~/.*\.${fileExtension}$/, type: FileType.FILES) { file ->
result << file
}
} catch (FileNotFoundException e) {}
} catch (FileNotFoundException e) {
log.warn("No file of type ${fileExtension}, found here:${path}")
}

return result
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,69 +84,6 @@ class JiraService {
}
}

Map createIssueType(String type, String projectKey, String summary, String description, String fixVersion = null) {
if (!type?.trim()) {
throw new IllegalArgumentException('Error: unable to create Jira issue. \'type\' is undefined.')
}

if (!projectKey?.trim()) {
throw new IllegalArgumentException('Error: unable to create Jira issue. \'projectKey\' is undefined.')
}

if (!summary?.trim()) {
throw new IllegalArgumentException('Error: unable to create Jira issue. \'summary\' is undefined.')
}

if (!description?.trim()) {
throw new IllegalArgumentException('Error: unable to create Jira issue. \'description\' is undefined.')
}

def response = Unirest.post("${this.baseURL}/rest/api/2/issue")
.basicAuth(this.username, this.password)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.body(JsonOutput.toJson(
[
fields: [
project: [
key: projectKey.toUpperCase()
],
summary: summary,
description: description,
fixVersions: [
[name: fixVersion]
],
issuetype: [
name: type
]
]
]
))
.asString()

response.ifSuccess {
if (response.getStatus() != 201) {
throw new RuntimeException("Error: unable to create Jira issue. Jira responded with code: '${response.getStatus()}' and message: '${response.getBody()}'.")
}
}

response.ifFailure {
def message = "Error: unable to create Jira issue. Jira responded with code: '${response.getStatus()}' and message: '${response.getBody()}'."

if (response.getStatus() == 404) {
message = "Error: unable to create Jira issue. Jira could not be found at: '${this.baseURL}'."
}

throw new RuntimeException(message)
}

return new JsonSlurperClassic().parseText(response.getBody())
}

Map createIssueTypeBug(String projectKey, String summary, String description, String fixVersion = null) {
return createIssueType("Bug", projectKey, summary, description, fixVersion)
}

Map getDeltaDocGenData(String projectKey, String version) {
if (!projectKey?.trim()) {
throw new IllegalArgumentException('Error: unable to get documentation generation data from Jira. ' +
Expand Down Expand Up @@ -198,7 +135,7 @@ class JiraService {
}

private String changeRLWhenUsingWiremock(String url) {
String finalUrl = (baseURL == targetURL)? baseURL : URLHelper.replaceHostInUrl(url, baseURL.toString())
String finalUrl = (baseURL == targetURL)? url : URLHelper.replaceHostInUrl(url, baseURL.toString())
return finalUrl
}

Expand Down Expand Up @@ -288,31 +225,6 @@ class JiraService {
}
}


Map getProject(String projectKey) {
if (!projectKey?.trim()) {
throw new IllegalArgumentException('Error: unable to get project from Jira. \'projectKey\' is undefined.')
}

def response = Unirest.get("${this.baseURL}/rest/api/2/project/{projectKey}")
.routeParam('projectKey', projectKey.toUpperCase())
.basicAuth(this.username, this.password)
.header('Accept', 'application/json')
.asString()

response.ifFailure {
def message = "Error: unable to get project. Jira responded with code: '${response.getStatus()}' and message: '${response.getBody()}'."

if (response.getStatus() == 404) {
message = "Error: unable to get project. Jira could not be found at: '${this.baseURL}'."
}

throw new RuntimeException(message)
}

return new JsonSlurperClassic().parseText(response.getBody())
}

Map searchByJQLQuery(Map query) {
if (!query) {
throw new IllegalArgumentException('Error: unable to get Jira issues for JQL query. \'query\' is undefined.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.ods.doc.gen.external.modules.xunit
import groovy.util.logging.Slf4j
import org.ods.doc.gen.core.FileSystemHelper
import org.ods.doc.gen.external.modules.nexus.NexusService
import org.ods.doc.gen.project.data.TestType
import org.springframework.stereotype.Service

import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.ods.doc.gen.leva.doc.services



class DocumentHistoryEntry implements Map, Serializable {

private final Long entryId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package org.ods.doc.gen.pdf.builder.repository


import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Repository

import javax.cache.annotation.CacheKey
import javax.cache.annotation.CacheResult
import java.nio.file.Path

@Repository
interface DocumentTemplatesRepository {

@CacheResult(cacheName = "templates")
Path getTemplatesForVersion(@CacheKey String version)
@Cacheable(value = "templates", key = '#version')
Path getTemplatesForVersion(String version)

boolean isApplicableToSystemConfig()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,36 @@ class HtmlToPDFService {

Path convert(Path tmpDir, Path documentHtmlFile, Map data = null) {
Path documentPDFFile = Paths.get(tmpDir.toString(), "document.pdf")
List cmd = generateCmd(data, documentHtmlFile, documentPDFFile)
def cmd = generateCmd(data)
cmd << documentHtmlFile.toFile().absolutePath
cmd << documentPDFFile.toFile().absolutePath
wkhtmltopdfService.executeCmd(tmpDir, documentHtmlFile, cmd)
fixDestinations(documentPDFFile.toFile())
return documentPDFFile
}

private List<String> generateCmd(Map data, Path documentHtmlFile, Path documentPDFFile) {
def cmd = [getServiceName(), "--encoding", "UTF-8", "--no-outline", "--print-media-type"]
private List<String> generateCmd(Map data) {
def cmd = ["wkhtmltopdf", "--encoding", "UTF-8", "--no-outline", "--print-media-type"]
cmd << "--enable-local-file-access"
cmd.addAll(["-T", "40", "-R", "25", "-B", "25", "-L", "25"])
cmd.addAll(controlSize())
cmd.addAll(addHeader(data))
cmd.addAll(["--footer-center", "'Page [page] of [topage]'", "--footer-font-size", "10"])
setOrientation(data, cmd)
cmd << documentHtmlFile.toFile().absolutePath
cmd << documentPDFFile.toFile().absolutePath
return cmd
}

private List controlSize(){
return ["--dpi", "75", "--image-dpi", "600"]
}

private String getServiceName() {
return "wkhtmltopdf"
}

private void setOrientation(Map data, ArrayList<String> cmd) {
if (data?.metadata?.orientation) {
cmd.addAll(["--orientation", data.metadata.orientation])
}
}

private List<String> addHeader(Map data) {
List<String> cmd = []
if (data?.metadata?.header) {
if (data.metadata.header.size() > 1) {
cmd.addAll(["--header-center", "${data.metadata.header[0]}\n${data.metadata.header[1]}"])
cmd.addAll(["--header-center", """${data.metadata.header[0]}
${data.metadata.header[1]}"""])
} else {
cmd.addAll(["--header-center", data.metadata.header[0]] as String)
cmd.addAll(["--header-center", data.metadata.header[0]])
}

cmd.addAll(["--header-font-size", "10", "--header-spacing", "10"])
}

cmd.addAll(["--footer-center", "'Page [page] of [topage]'", "--footer-font-size", "10"])

if (data?.metadata?.orientation) {
cmd.addAll(["--orientation", data.metadata.orientation])
}

return cmd
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.ods.doc.gen.pdf.builder.services

import com.github.benmanes.caffeine.cache.Cache

import org.apache.commons.io.FileUtils
import org.apache.commons.io.FilenameUtils
import org.ods.doc.gen.pdf.builder.repository.DocumentTemplateFactory
Expand All @@ -13,7 +13,6 @@ import java.nio.file.Paths
@Service
class PdfGenerationService {

private final Cache<String, Path> templatesCache
private final HtmlToPDFService htmlToPDFService
private final DocumentTemplateFactory documentTemplateFactory

Expand Down
7 changes: 3 additions & 4 deletions src/main/groovy/org/ods/doc/gen/project/data/Project.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import groovy.util.logging.Slf4j
import org.ods.doc.gen.external.modules.git.BitbucketService
import org.ods.doc.gen.external.modules.jira.JiraService
import org.ods.doc.gen.external.modules.xunit.JUnitReportsService
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service

import javax.cache.annotation.CacheKey
import javax.cache.annotation.CacheResult
import javax.inject.Inject

@Slf4j
Expand All @@ -25,8 +24,8 @@ class Project {
this.jUnitReportsService = jUnitReportsService
}

@CacheResult(cacheName = "projectData")
ProjectData getProjectData(@CacheKey String projectBuildId, Map data){
@Cacheable(value = "projectData", key = '#projectBuildId')
ProjectData getProjectData(String projectBuildId, Map data){
log.info("build project data for projectBuildId:${projectBuildId}")
return new ProjectData(jira, bitbucketService, jUnitReportsService).init(data).load()
}
Expand Down

This file was deleted.

Loading

0 comments on commit 7218b83

Please sign in to comment.