Skip to content

Commit

Permalink
No exact execution start timestamp and duration #436
Browse files Browse the repository at this point in the history
  • Loading branch information
dprzybyl committed Mar 18, 2024
1 parent 3caccd0 commit 1a9f46f
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.cognifide.apm.api.services;

import com.cognifide.apm.api.status.Status;
import java.util.Calendar;
import java.util.List;

public interface ExecutionResult {
Expand All @@ -32,6 +33,8 @@ public interface ExecutionResult {

String getExecutor();

Calendar getStartTime();

long determineExecutionDuration();

interface Entry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse r
return ResponseEntity.ok("Script successfully executed")
.addEntry("status", status.getStatus())
.addEntry("output", ((ExecutionStatus.FinishedSuccessfulExecution) status).getEntries())
.addEntry("path", ((ExecutionStatus.FinishedSuccessfulExecution) status).getPath());
.addEntry("path", ((ExecutionStatus.FinishedSuccessfulExecution) status).getPath())
.addEntry("timestamp", ((ExecutionStatus.FinishedSuccessfulExecution) status).getTimestamp())
.addEntry("formattedDate", ((ExecutionStatus.FinishedSuccessfulExecution) status).getFormattedDate());
} else if (status instanceof ExecutionStatus.FinishedFailedExecution) {
return ResponseEntity.internalServerError("Errors while executing script")
.addEntry("status", status.getStatus())
.addEntry("output", ((ExecutionStatus.FinishedFailedExecution) status).getEntries())
.addEntry("path", ((ExecutionStatus.FinishedFailedExecution) status).getPath())
.addEntry("timestamp", ((ExecutionStatus.FinishedFailedExecution) status).getTimestamp())
.addEntry("formattedDate", ((ExecutionStatus.FinishedFailedExecution) status).getFormattedDate())
.addEntry("errors", ((ExecutionStatus.FinishedFailedExecution) status).getError().getMessages());
} else {
return ResponseEntity.ok("Script is still being processed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class HistoryImpl implements History {
public HistoryEntry logLocal(Script script, ExecutionMode mode, Progress progressLogger) {
return SlingHelper.resolveDefault(resolverProvider, resolver -> {
HistoryEntryWriter historyEntryWriter = createBuilder(resolver, script, mode, progressLogger)
.executionTime(Calendar.getInstance())
.executionTime(progressLogger.getStartTime())
.executionDuration(progressLogger.determineExecutionDuration())
.build();
return createHistoryEntry(resolver, script, mode, historyEntryWriter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.cognifide.apm.core.logger.ProgressEntry;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -40,7 +41,7 @@ public class ProgressImpl implements Progress {

private final String executor;

private final long startTime;
private final Calendar startTime;

public ProgressImpl(String executor) {
this(executor, new LinkedList<>());
Expand All @@ -49,7 +50,7 @@ public ProgressImpl(String executor) {
public ProgressImpl(String executor, List<ProgressEntry> entries) {
this.executor = executor;
this.entries = entries;
this.startTime = System.currentTimeMillis();
this.startTime = Calendar.getInstance();
}

@Override
Expand Down Expand Up @@ -131,8 +132,13 @@ public String getExecutor() {
return executor;
}

@Override
public Calendar getStartTime() {
return startTime;
}

@Override
public long determineExecutionDuration() {
return (System.currentTimeMillis() - startTime) / 1000;
return (System.currentTimeMillis() - startTime.getTimeInMillis()) / 1000;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.cognifide.apm.core.jobs.JobResultsCache;
import com.cognifide.apm.core.jobs.JobResultsCache.ExecutionSummary;
import com.cognifide.apm.core.jobs.ScriptRunnerJobConsumer;
import com.cognifide.apm.core.ui.utils.DateFormatter;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -86,12 +87,14 @@ public ExecutionStatus checkStatus(String id) {

private ExecutionStatus finishedExecution(ExecutionSummary executionSummary) {
String path = executionSummary.getPath();
long timestamp = executionSummary.getResult().getStartTime().getTimeInMillis();
String formattedDate = DateFormatter.determineFormattedDate(executionSummary.getResult().getStartTime());
List<ExecutionResult.Entry> entries = executionSummary.getResult().getEntries();
ExecutionResult.Entry errorEntry = executionSummary.getResult().getLastError();
if (errorEntry != null) {
return new ExecutionStatus.FinishedFailedExecution(path, entries, errorEntry);
return new ExecutionStatus.FinishedFailedExecution(path, timestamp, formattedDate, entries, errorEntry);
} else {
return new ExecutionStatus.FinishedSuccessfulExecution(path, entries);
return new ExecutionStatus.FinishedSuccessfulExecution(path, timestamp, formattedDate, entries);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,50 +48,57 @@ public UnknownExecution() {
}
}

public static class FinishedSuccessfulExecution extends ExecutionStatus {
protected static class FinishedExecutionStatus extends ExecutionStatus {

private final String path;

private final long timestamp;

private final String formattedDate;

private final List<ExecutionResult.Entry> entries;

public FinishedSuccessfulExecution(String path, List<ExecutionResult.Entry> entries) {
public FinishedExecutionStatus(String path, long timestamp, String formattedDate, List<ExecutionResult.Entry> entries) {
super("finished");
this.path = path;
this.timestamp = timestamp;
this.formattedDate = formattedDate;
this.entries = entries;
}

public String getPath() {
return path;
}

public long getTimestamp() {
return timestamp;
}

public String getFormattedDate() {
return formattedDate;
}

public List<ExecutionResult.Entry> getEntries() {
return entries;
}
}

public static class FinishedFailedExecution extends ExecutionStatus {
public static class FinishedSuccessfulExecution extends FinishedExecutionStatus {

private final String path;
public FinishedSuccessfulExecution(String path, long timestamp, String formattedDate, List<ExecutionResult.Entry> entries) {
super(path, timestamp, formattedDate, entries);
}
}

private final List<ExecutionResult.Entry> entries;
public static class FinishedFailedExecution extends FinishedExecutionStatus {

private final ExecutionResult.Entry error;

public FinishedFailedExecution(String path, List<ExecutionResult.Entry> entries, ExecutionResult.Entry error) {
super("finished");
this.path = path;
this.entries = entries;
public FinishedFailedExecution(String path, long timestamp, String formattedDate, List<ExecutionResult.Entry> entries, ExecutionResult.Entry error) {
super(path, timestamp, formattedDate, entries);
this.error = error;
}

public String getPath() {
return path;
}

public List<ExecutionResult.Entry> getEntries() {
return entries;
}

public ExecutionResult.Entry getError() {
return error;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.cognifide.apm.core.ui.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.annotation.PostConstruct;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;

@Model(adaptables = {SlingHttpServletRequest.class})
public class DateFormatter {

@RequestAttribute
private Calendar date;

private String formattedDate;

@PostConstruct
private void afterCreated() {
formattedDate = determineFormattedDate(date);
}

public static String determineFormattedDate(Calendar date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(date.getTime());
}

public String getFormattedDate() {
return formattedDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,11 @@
this.$cell.html('<coral-wait/>');
},

showRunStatus: function (success, summaryPath) {
showRunStatus: function (success, summaryPath, timestamp, formattedDate) {
let icon = success ? 'check' : 'close';
let href = '/apm/summary.html' + summaryPath;
this.$cell.html('<a data-sly-test="${run.time}" '
+ 'is="coral-anchorbutton" '
+ 'iconsize="S" '
+ 'icon="' + icon + '"'
+ 'href="' + href + '"></a>'
+ '<time>1 second ago</time>');
this.$cell.html('<a is="coral-anchorbutton" iconsize="S" icon="' + icon + '" href="' + href + '"></a>'
+ '<time datetime="' + timestamp + '">' + formattedDate + '</time>');
},

updateScript: function () {
Expand Down Expand Up @@ -131,11 +127,11 @@
this.status = ScriptStatus.FINISHED;
const runStatus = getRunStatus(data);
showMessageOnFinished(this.mode, runStatus);
this.showRunStatus(runStatus !== RunStatus.ERROR, data.path);
this.showRunStatus(runStatus !== RunStatus.ERROR, data.path, data.timestamp, data.formattedDate);
} else if (data.type === 'unknown') {
this.status = ScriptStatus.FINISHED;
showMessageOnUnknown(this.mode, self.job.message);
this.showRunStatus(false, '');
this.showRunStatus(false, '', '', '');
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
</td>
<td is="coral-table-cell" value="${item.executionTime.timeInMillis || 0}">
<time data-sly-test="${item.executionTime}" datetime="${item.executionTime.timeInMillis || 0}"
class="foundation-collection-item-time"
data-sly-use.execLast="${'com.adobe.cq.xf.ui.DateFormatter' @ date=item.executionTimeCalendar, simpleFormat='yyyy-MM-dd HH:mm:ss'}">
${execLast.date}
data-sly-use.execLast="${'com.cognifide.apm.core.ui.utils.DateFormatter' @ date=item.executionTimeCalendar}">
${execLast.formattedDate}
</time>
</td>
<td is="coral-table-cell" value="${item.author}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
</td>
<td is="coral-table-cell" value="${item.lastModified.timeInMillis || 0}">
<time data-sly-test="${item.lastModified}" datetime="${item.lastModified.timeInMillis || 0}"
data-sly-use.lastMod="${'com.adobe.cq.xf.ui.DateFormatter' @ date=item.lastModified, simpleFormat='yyyy-MM-dd HH:mm:ss'}">
${lastMod.date}
data-sly-use.lastMod="${'com.cognifide.apm.core.ui.utils.DateFormatter' @ date=item.lastModified}">
${lastMod.formattedDate}
</time>
</td>
<td is="coral-table-cell" value="${item.author}">
Expand All @@ -53,8 +53,8 @@
<a data-sly-test="${run.time}" is="coral-anchorbutton" iconsize="S" icon="${run.success ? 'check' : 'close'}"
href="/apm/summary.html${run.summary}"></a>
<time data-sly-test="${run.time}" datetime="${run.time.timeInMillis || 0}"
data-sly-use.runTime="${'com.adobe.cq.xf.ui.DateFormatter' @ date=run.time, simpleFormat='yyyy-MM-dd HH:mm:ss'}">
${runTime.date}
data-sly-use.runTime="${'com.cognifide.apm.core.ui.utils.DateFormatter' @ date=run.time}">
${runTime.formattedDate}
</time>
</td>
<sly data-sly-test="${item.runs.size == 0}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
<coral-list>
<coral-list-item icon="file">Script: <strong>${item.entry.scriptName}</strong></coral-list-item>
<coral-list-item icon="user">User: <strong>${item.entry.executor}</strong></coral-list-item>
<coral-list-item icon="clock" data-sly-test="${item.entry.executionDuration}">Duration: <strong>${item.entry.executionDuration}s</strong></coral-list-item>
<coral-list-item icon="calendar">Start time: <strong>
<time datetime="${item.entry.executionTimeCalendar.timeInMillis || 0}"
data-sly-use.startTime="${'com.cognifide.apm.core.ui.utils.DateFormatter' @ date=item.entry.executionTimeCalendar}">
${startTime.formattedDate}
</time>
</strong></coral-list-item>
<coral-list-item icon="clock">Duration: <strong>${item.entry.executionDuration}s</strong></coral-list-item>
</coral-list>
<table class="_coral-Table">
<thead class="_coral-Table-divider--row _coral-Table-head">
Expand Down

0 comments on commit 1a9f46f

Please # to comment.