|
16 | 16 | package org.spotter.eclipse.ui.navigator;
|
17 | 17 |
|
18 | 18 | import java.io.File;
|
| 19 | +import java.io.IOException; |
| 20 | +import java.text.SimpleDateFormat; |
19 | 21 | import java.util.ArrayList;
|
| 22 | +import java.util.Date; |
20 | 23 | import java.util.List;
|
21 | 24 |
|
22 | 25 | import org.eclipse.core.resources.IFolder;
|
23 | 26 | import org.eclipse.core.resources.IProject;
|
24 | 27 | import org.eclipse.core.resources.IResource;
|
25 | 28 | import org.eclipse.core.runtime.CoreException;
|
26 | 29 | import org.eclipse.swt.graphics.Image;
|
| 30 | +import org.lpe.common.util.LpeFileUtils; |
27 | 31 | import org.slf4j.Logger;
|
28 | 32 | import org.slf4j.LoggerFactory;
|
29 | 33 | import org.spotter.eclipse.ui.Activator;
|
| 34 | +import org.spotter.eclipse.ui.ServiceClientWrapper; |
| 35 | +import org.spotter.eclipse.ui.jobs.JobsContainer; |
| 36 | +import org.spotter.eclipse.ui.util.DialogUtils; |
30 | 37 | import org.spotter.shared.configuration.FileManager;
|
| 38 | +import org.spotter.shared.result.ResultsLocationConstants; |
| 39 | +import org.spotter.shared.result.model.ResultsContainer; |
31 | 40 |
|
32 | 41 | /**
|
33 | 42 | * An element that represents the results node.
|
@@ -125,39 +134,85 @@ public void refreshChildren() {
|
125 | 134 | }
|
126 | 135 |
|
127 | 136 | private ISpotterProjectElement[] initializeChildren(IProject iProject) {
|
128 |
| - String defaultResultsDir = FileManager.DEFAULT_RESULTS_DIR_NAME; |
129 |
| - IFolder resDir = iProject.getFolder(defaultResultsDir); |
| 137 | + IFolder resDir = iProject.getFolder(FileManager.DEFAULT_RESULTS_DIR_NAME); |
130 | 138 |
|
131 | 139 | if (!resDir.isSynchronized(IResource.DEPTH_INFINITE)) {
|
132 | 140 | try {
|
133 | 141 | resDir.refreshLocal(IResource.DEPTH_INFINITE, null);
|
134 | 142 | } catch (CoreException e) {
|
135 |
| - LOGGER.warn("Failed to refresh results directory"); |
| 143 | + String msg = "Failed to refresh results directory."; |
| 144 | + LOGGER.warn(msg); |
| 145 | + DialogUtils.openWarning(msg); |
136 | 146 | return SpotterProjectParent.NO_CHILDREN;
|
137 | 147 | }
|
138 | 148 | }
|
139 | 149 |
|
140 |
| - File res = new File(resDir.getLocation().toString()); |
141 |
| - List<File> runFolders = new ArrayList<>(); |
142 |
| - ISpotterProjectElement[] elements = SpotterProjectParent.NO_CHILDREN; |
| 150 | + return synchronizeRunResults(iProject, resDir.getLocation().toString()); |
| 151 | + } |
| 152 | + |
| 153 | + private ISpotterProjectElement[] synchronizeRunResults(IProject iProject, String resultsLocation) { |
| 154 | + File res = new File(resultsLocation); |
| 155 | + List<ISpotterProjectElement> elements = new ArrayList<>(); |
| 156 | + ServiceClientWrapper client = Activator.getDefault().getClient(iProject.getName()); |
| 157 | + |
| 158 | + if (!res.exists() || !res.isDirectory()) { |
| 159 | + DialogUtils.openWarning("The project's results folder is missing or corrupted!"); |
| 160 | + } else { |
| 161 | + boolean connected = client.testConnection(false); |
| 162 | + if (!connected) { |
| 163 | + DialogUtils.openAsyncWarning("No connection to DS service! New results cannot be fetched from the server."); |
| 164 | + } |
| 165 | + JobsContainer jobsContainer = JobsContainer.readJobsContainer(iProject); |
143 | 166 |
|
144 |
| - if (res.exists() && res.isDirectory()) { |
145 |
| - File[] files = res.listFiles(); |
146 |
| - for (File file : files) { |
147 |
| - if (file.isDirectory()) { |
148 |
| - runFolders.add(file); |
| 167 | + for (Long jobId : jobsContainer.getJobIds()) { |
| 168 | + SpotterProjectRunResult runResult = processJobId(jobId, connected, client, jobsContainer, |
| 169 | + resultsLocation, iProject); |
| 170 | + if (runResult != null) { |
| 171 | + elements.add(runResult); |
149 | 172 | }
|
150 | 173 | }
|
| 174 | + } |
| 175 | + |
| 176 | + return elements.toArray(new ISpotterProjectElement[elements.size()]); |
| 177 | + } |
| 178 | + |
| 179 | + private SpotterProjectRunResult processJobId(Long jobId, boolean connected, ServiceClientWrapper client, |
| 180 | + JobsContainer jobsContainer, String resultsLocation, IProject iProject) { |
| 181 | + if (connected && client.isRunning(true) && jobId.equals(client.getCurrentJobId())) { |
| 182 | + LOGGER.debug("Ignore job " + jobId + " because it is currently running"); |
| 183 | + return null; |
| 184 | + } |
151 | 185 |
|
152 |
| - elements = new ISpotterProjectElement[runFolders.size()]; |
153 |
| - int i = 0; |
154 |
| - for (File runFolder : runFolders) { |
155 |
| - IFolder runResultFolder = iProject.getFolder(defaultResultsDir + File.separator + runFolder.getName()); |
156 |
| - elements[i++] = new SpotterProjectRunResult(this, runFolder.getName(), runResultFolder); |
| 186 | + Long timestamp = jobsContainer.getTimestamp(jobId); |
| 187 | + SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd_HH-mm-ss-SSS"); |
| 188 | + String formattedTimestamp = dateFormat.format(new Date(timestamp)); |
| 189 | + |
| 190 | + String fileName = resultsLocation + "/" + formattedTimestamp; |
| 191 | + File file = new File(fileName); |
| 192 | + boolean success = file.exists(); |
| 193 | + if (!success && connected) { |
| 194 | + // try to fetch data from server |
| 195 | + ResultsContainer resultsContainer = client.requestResults(jobId.toString()); |
| 196 | + if (resultsContainer != null && file.mkdir()) { |
| 197 | + String resultsFile = fileName + "/" + ResultsLocationConstants.RESULTS_SERIALIZATION_FILE_NAME; |
| 198 | + try { |
| 199 | + LpeFileUtils.writeObject(resultsFile, resultsContainer); |
| 200 | + success = true; |
| 201 | + } catch (IOException e) { |
| 202 | + String msg = "Error while saving fetched results for job " + jobId + "!"; |
| 203 | + DialogUtils.openError(msg); |
| 204 | + LOGGER.error(msg + " Cause: {}", e.toString()); |
| 205 | + } |
157 | 206 | }
|
158 | 207 | }
|
159 | 208 |
|
160 |
| - return elements; |
| 209 | + if (success) { |
| 210 | + IFolder runResultFolder = iProject.getFolder(FileManager.DEFAULT_RESULTS_DIR_NAME + "/" |
| 211 | + + formattedTimestamp); |
| 212 | + return new SpotterProjectRunResult(this, jobId, timestamp, runResultFolder); |
| 213 | + } else { |
| 214 | + return null; |
| 215 | + } |
161 | 216 | }
|
162 | 217 |
|
163 | 218 | }
|
0 commit comments