Skip to content

Commit

Permalink
Merge branch 'master' into add-data-source-support
Browse files Browse the repository at this point in the history
  • Loading branch information
dprzybyl committed Oct 31, 2023
2 parents cfe8a9d + 102f3d0 commit 36512b3
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ interface Entry {

List<String> getMessages();

List<String> getPrettyMessages();

List<String> getParameters();

List<String> getPrettyParameters();

Status getStatus();
}
}
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 @@ -26,7 +26,6 @@
import com.google.common.collect.ImmutableList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ProgressEntry implements Entry {

Expand Down Expand Up @@ -71,25 +70,11 @@ public List<String> getMessages() {
return messages;
}

@Override
public List<String> getPrettyMessages() {
return messages.stream()
.map(message -> message.replaceAll("\n", "<br>").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;"))
.collect(Collectors.toList());
}

@Override
public List<String> getParameters() {
return parameters;
}

@Override
public List<String> getPrettyParameters() {
return parameters.stream()
.map(message -> message.replaceAll("\n", "<br>").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;"))
.collect(Collectors.toList());
}

@Override
public Status getStatus() {
return status;
Expand Down
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 @@ -71,7 +71,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 @@ -139,14 +139,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
Loading

0 comments on commit 36512b3

Please sign in to comment.