Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix large history log issue #422

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,54 @@
import com.cognifide.apm.core.logger.ProgressEntry;
import com.cognifide.apm.core.progress.ProgressHelper;
import com.cognifide.apm.core.utils.CalendarUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class HistoryEntryImpl implements HistoryEntry {

public static final String AUTHOR = "author";

public static final String EXECUTION_TIME = "executionTime";

public static final String EXECUTION_DURATION = "executionDuration";

public static final String EXECUTOR = "executor";

public static final String SCRIPT_PATH = "scriptPath";

public static final String SCRIPT_NAME = "scriptName";

public static final String IS_RUN_SUCCESSFUL = "isRunSuccessful";

public static final String MODE = "mode";

public static final String CHECKSUM = "checksum";

public static final String PROGRESS_LOG = "summaryJSON";

public static final String UPLOAD_TIME = "uploadTime";

public static final String SCRIPT_CONTENT_PATH = "scriptContentPath";

public static final String INSTANCE_NAME = "instanceName";

@Self
private Resource resource;

@Inject
@Named(AUTHOR)
private String author;
Expand Down Expand Up @@ -91,8 +111,6 @@ public class HistoryEntryImpl implements HistoryEntry {
@Named(UPLOAD_TIME)
private Date uploadTime;

@Inject
@Named(PROGRESS_LOG)
private String executionSummaryJson;

@Inject
Expand All @@ -103,25 +121,22 @@ public class HistoryEntryImpl implements HistoryEntry {
@Named(INSTANCE_NAME)
private String instanceName;

private final String path;
private String path;

private Calendar executionTimeCalendar;

private List<ProgressEntry> executionSummary;

public HistoryEntryImpl(Resource resource) {
this.path = resource.getPath();
}

public List<ProgressEntry> getExecutionSummary() {
if (this.executionSummary == null) {
this.executionSummary = ProgressHelper.fromJson(getExecutionSummaryJson());
if (executionSummary == null) {
executionSummary = ProgressHelper.fromJson(getExecutionSummaryJson());
}
return this.executionSummary;
return executionSummary;
}

@PostConstruct
private void afterCreated() {
path = resource.getPath();
executionTimeCalendar = CalendarUtils.asCalendar(executionTime);
}

Expand Down Expand Up @@ -166,6 +181,18 @@ public Date getUploadTime() {
}

public String getExecutionSummaryJson() {
if (executionSummaryJson == null) {
Object progressLog = resource.getValueMap().get(PROGRESS_LOG);
if (progressLog instanceof InputStream) {
try {
executionSummaryJson = IOUtils.toString((InputStream) progressLog, StandardCharsets.UTF_8);
} catch (IOException e) {
executionSummaryJson = "[]";
}
} else {
executionSummaryJson = (String) progressLog;
}
}
return executionSummaryJson;
}

Expand All @@ -184,4 +211,4 @@ public String getPath() {
public Calendar getExecutionTimeCalendar() {
return executionTimeCalendar;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,34 @@

package com.cognifide.apm.core.history;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;

public final class HistoryEntryWriter {

private final String author;

private final Calendar executionTime;

private final String executor;

private final long executionDuration;

private final String fileName;

private final String filePath;

private final Boolean isRunSuccessful;

private final String mode;

private final String progressLog;

private final String instanceName;

private HistoryEntryWriter(String author, Calendar executionTime, String executor, long executionDuration, String fileName, String filePath, Boolean isRunSuccessful, String mode, String progressLog, String instanceName) {
Expand All @@ -54,13 +67,15 @@ public static HistoryEntryWriterBuilder builder() {
return new HistoryEntryWriterBuilder();
}

public void writeTo(Resource historyLogResource) {
public void writeTo(Resource historyLogResource) throws IOException {
ModifiableValueMap valueMap = historyLogResource.adaptTo(ModifiableValueMap.class);
valueMap.put(HistoryEntryImpl.SCRIPT_NAME, fileName);
valueMap.put(HistoryEntryImpl.SCRIPT_PATH, filePath);
valueMap.put(HistoryEntryImpl.AUTHOR, author);
valueMap.put(HistoryEntryImpl.MODE, mode);
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLog);
try (InputStream progressLogInput = IOUtils.toInputStream(progressLog, StandardCharsets.UTF_8)) {
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLogInput);
}
valueMap.put(HistoryEntryImpl.IS_RUN_SUCCESSFUL, isRunSuccessful);
valueMap.put(HistoryEntryImpl.EXECUTION_TIME, executionTime);
valueMap.put(HistoryEntryImpl.EXECUTION_DURATION, executionDuration);
Expand All @@ -71,14 +86,23 @@ public void writeTo(Resource historyLogResource) {
public static class HistoryEntryWriterBuilder {

private String author;

private Calendar executionTime;

private String executor;

private long executionDuration;

private String fileName;

private String filePath;

private Boolean isRunSuccessful;

private String mode;

private String progressLog;

private String instanceName;

private HistoryEntryWriterBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.cognifide.apm.core.services.version.VersionService;
import com.cognifide.apm.core.utils.sling.SlingHelper;
import com.day.cq.commons.jcr.JcrConstants;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.Calendar;
import java.util.LinkedList;
Expand All @@ -40,7 +41,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.resource.AbstractResourceVisitor;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
Expand Down Expand Up @@ -164,18 +164,16 @@ private HistoryEntry createHistoryEntry(ResourceResolver resolver, Script script
session.save();
resolver.commit();
return resolver.getResource(historyEntryNode.getPath()).adaptTo(HistoryEntryImpl.class);
} catch (PersistenceException | RepositoryException e) {
} catch (IOException | RepositoryException e) {
LOG.error("Issues with saving to repository while logging script execution", e);
return null;
}
}

private Resource writeProperties(ResourceResolver resolver, Node historyEntry, HistoryEntryWriter
historyEntryWriter)
throws RepositoryException {
private void writeProperties(ResourceResolver resolver, Node historyEntry, HistoryEntryWriter historyEntryWriter)
throws RepositoryException, IOException {
Resource entryResource = resolver.getResource(historyEntry.getPath());
historyEntryWriter.writeTo(entryResource);
return entryResource;
}

private Node createHistoryEntryNode(Node scriptHistoryNode, Script script, ExecutionMode mode)
Expand Down Expand Up @@ -204,5 +202,4 @@ private String getModeName(ExecutionMode mode) {
private String getScriptHistoryPath(Script script) {
return HISTORY_FOLDER + "/" + script.getPath().replace("/", "_").substring(1);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class ScriptModel implements MutableScript {

private static final Logger LOGGER = LoggerFactory.getLogger(ScriptModel.class);

private final String path;
private String path;

@Self
private Resource resource;
Expand Down Expand Up @@ -120,12 +120,9 @@ public class ScriptModel implements MutableScript {

private String data;

public ScriptModel(Resource resource) {
this.path = resource.getPath();
}

@PostConstruct
private void afterCreated() {
path = resource.getPath();
if (verified == null) {
try {
scriptManager.process(this, ExecutionMode.VALIDATION, resource.getResourceResolver());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,24 @@

import javax.inject.Inject;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;

@Model(adaptables = Resource.class)
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ScriptVersionModel implements ScriptVersion {

private final String scriptPath;

private final String lastChecksum;
@Inject
private String scriptPath;

@Inject
public ScriptVersionModel(String scriptPath, String lastChecksum) {
private String lastChecksum;

public ScriptVersionModel() {
// intentionally empty
}

public ScriptVersionModel(String scriptPath) {
this.scriptPath = scriptPath;
this.lastChecksum = lastChecksum;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ScriptVersion getScriptVersion(ResourceResolver resolver, Script script)
String scriptVersionPath = getScriptVersionPath(script);
return Optional.ofNullable(resolver.getResource(scriptVersionPath))
.map(resource -> resource.adaptTo(ScriptVersionModel.class))
.orElse(new ScriptVersionModel(script.getPath(), null));
.orElse(new ScriptVersionModel(script.getPath()));
}

@Override
Expand Down Expand Up @@ -135,14 +135,12 @@ private Node createVersionNode(Node parent, Script script, Session session) thro
return JcrUtils.getOrCreateByPath(path, "sling:OrderedFolder", "sling:OrderedFolder", session, true);
}

private Node copyScriptContent(Node parent, Script script, Session session) throws RepositoryException {
private void copyScriptContent(Node parent, Script script, Session session) throws RepositoryException {
if (!parent.hasNode(SCRIPT_NODE_NAME)) {
Node source = session.getNode(script.getPath());
Node file = JcrUtil.copy(source, parent, SCRIPT_NODE_NAME);
file.addMixin(ScriptNode.APM_SCRIPT);
return file;
}
return parent.getNode(SCRIPT_NODE_NAME);
}

private String normalizedPath(Script script) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

import javax.inject.Inject;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;

@Model(adaptables = Resource.class)
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public final class DashboardTileModel {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;

@Model(adaptables = Resource.class)
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public final class ScriptsRowModel {

private static final Set<String> FOLDER_TYPES = ImmutableSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@
jcr:primaryType="nt:unstructured"
href.uritemplate="/apm/viewer.html{+item}"/>
</view>
<deletepage
granite:class="foundation-collection-action"
granite:rel="cq-siteadmin-admin-actions-delete-activator"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/collection/action"
action="cq.wcm.delete"
icon="delete"
relScope="collection"
target=".cq-experience-fragments-admin-childpages"
text="Delete"
variant="actionBar"/>
</selection>
</actions>
</jcr:content>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0"
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
xmlns:granite="http://www.adobe.com/jcr/granite/1.0"
jcr:primaryType="cq:Page">
<jcr:content
jcr:primaryType="nt:unstructured"
Expand Down
3 changes: 1 addition & 2 deletions gradle/META-INF/vault/definition/.content.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
xmlns:vlt="http://www.day.com/jcr/vault/1.0"
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:vlt="http://www.day.com/jcr/vault/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="vlt:PackageDefinition"
sling:resourceType="cq/packaging/components/pack/definition"
Expand Down
Loading