Fix issues with detecting duplicate ComposerStates while saving history

This commit is contained in:
Rohit Awate 2018-08-18 22:27:11 +05:30
parent 53a42f8de5
commit 6eb4595f91
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
8 changed files with 120 additions and 96 deletions

View file

@ -100,6 +100,7 @@ public class DashboardController implements Initializable {
private IntegerProperty paramsCountProperty;
private Visualizer visualizer;
private ResponseHeadersViewer responseHeadersViewer;
private SyncManager syncManager;
private GETRequest getRequest;
private DataRequest dataRequest;
@ -293,7 +294,7 @@ public class DashboardController implements Initializable {
cancelButton.setOnAction(e -> requestManager.cancel());
requestManager.addHandlers(this::whileRunning, this::onSucceeded, this::onFailed, this::onCancelled);
requestManager.start();
SyncManager.saveState(getState().composer);
syncManager.saveState(getState().composer);
} catch (MalformedURLException MURLE) {
showLayer(ResponseLayer.PROMPT);
snackbar.show("Invalid address. Please verify and try again.", 3000);
@ -614,6 +615,10 @@ public class DashboardController implements Initializable {
}
}
public void setSyncManager(SyncManager syncManager) {
this.syncManager = syncManager;
}
public ComposerTab getVisibleComposerTab() {
int visibleTab = requestOptionsTab.getSelectionModel().getSelectedIndex();
switch (visibleTab) {
@ -628,7 +633,6 @@ public class DashboardController implements Initializable {
}
}
private ResponseTab getVisibleResponseTab() {
int visibleTab = responseTabPane.getSelectionModel().getSelectedIndex();
switch (visibleTab) {

View file

@ -129,7 +129,7 @@ public class HeaderTabController implements Initializable {
/**
* Return a list of the state of all the fields in the Headers tab.
*/
public ArrayList<FieldState> getFieldStates() {
public List<FieldState> getFieldStates() {
ArrayList<FieldState> states = new ArrayList<>();
for (StringKeyValueFieldController controller : controllers) {

View file

@ -29,12 +29,12 @@ import java.util.List;
import java.util.function.Consumer;
public class HistoryPaneController extends SearchablePaneController<ComposerState> {
private List<Consumer<ComposerState>> stateClickHandler = new LinkedList<>();
private SyncManager syncManager;
@Override
protected List<ComposerState> loadInitialEntries() {
return SyncManager.getHistory();
return syncManager.getHistory();
}
protected SearchEntry<ComposerState> createEntryFromState(ComposerState state) throws IOException {
@ -62,4 +62,8 @@ public class HistoryPaneController extends SearchablePaneController<ComposerStat
public void addItemClickHandler(Consumer<ComposerState> handler) {
stateClickHandler.add(handler);
}
public void setSyncManager(SyncManager syncManager) {
this.syncManager = syncManager;
}
}

View file

@ -61,21 +61,24 @@ public class HomeWindowController implements Initializable {
private HistoryPaneController historyController;
private DashboardController dashboard;
private StringProperty addressProperty;
private SyncManager syncManager;
@Override
public void initialize(URL location, ResourceBundle resources) {
new SyncManager(this);
syncManager = new SyncManager(this);
try {
FXMLLoader historyLoader = new FXMLLoader(getClass().getResource("/fxml/homewindow/HistoryPane.fxml"));
Parent historyFXML = historyLoader.load();
splitPane.getItems().add(0, historyFXML);
historyController = historyLoader.getController();
historyController.setSyncManager(syncManager);
historyController.addItemClickHandler(this::addTab);
FXMLLoader dashboardLoader = new FXMLLoader(getClass().getResource("/fxml/homewindow/Dashboard.fxml"));
Parent dashboardFXML = dashboardLoader.load();
dashboard = dashboardLoader.getController();
dashboard.setSyncManager(syncManager);
dashboardContainer.getChildren().add(dashboardFXML);
addressProperty = dashboard.addressField.textProperty();
} catch (IOException e) {

View file

@ -57,12 +57,16 @@ public class ComposerState {
ComposerState state = (ComposerState) o;
if (!target.equals(state.target)) return false;
if (!httpMethod.equals(state.httpMethod)) return false;
if (!params.equals(state.params)) return false;
if (!headers.equals(state.headers)) return false;
if (state.httpMethod.equals(HTTPConstants.GET)
|| state.httpMethod.equals(HTTPConstants.DELETE)) return true;
if (!contentType.equals(state.contentType)) return false;
if (!rawBody.equals(state.rawBody)) return false;
if (!rawBodyBoxValue.equals(state.rawBodyBoxValue)) return false;
if (!binaryFilePath.equals(state.binaryFilePath)) return false;
if (!params.equals(state.params)) return false;
if (!headers.equals(state.headers)) return false;
if (!urlStringTuples.equals(state.urlStringTuples)) return false;
if (!formStringTuples.equals(state.formStringTuples)) return false;
if (!formFileTuples.equals(state.formFileTuples)) return false;

View file

@ -11,14 +11,21 @@ public interface DataManager {
/**
* Saves the state of the Composer when the request was made.
*/
void saveState(ComposerState newState);
void saveState(ComposerState newState) throws Exception;
/**
* Fetches all the states of the Composer when the previous requests were made.
*
* @return A list of the states.
*/
List<ComposerState> getHistory();
List<ComposerState> getHistory() throws Exception;
/**
* Returns the state of the Composer when the last request was made.
* If this DataManager is the primary fetching source, SyncManager uses
* calls this method before attempting to save a new state.
*/
ComposerState getLastAdded();
/**
* Returns the identifier for the DataManager. Preferably, use the source as the identifier.

View file

@ -91,14 +91,8 @@ class SQLiteManager implements DataManager {
* @param newState - The state of the Dashboard while making the request.
*/
@Override
public synchronized void saveState(ComposerState newState) {
ComposerState lastState = getLastRequest();
if (newState.equals(lastState))
return;
try {
statement =
conn.prepareStatement(Queries.saveRequest);
public synchronized void saveState(ComposerState newState) throws SQLException {
statement = conn.prepareStatement(Queries.saveRequest);
statement.setString(1, newState.httpMethod);
statement.setString(2, newState.target);
@ -138,18 +132,14 @@ class SQLiteManager implements DataManager {
saveTuple(newState.formStringTuples, "FormString", requestID);
saveTuple(newState.formFileTuples, "File", requestID);
}
} catch (SQLException e) {
LoggingService.logWarning("Database error.", e, LocalDateTime.now());
}
}
/**
* Returns a list of all the recent requests.
*/
@Override
public synchronized List<ComposerState> getHistory() {
public synchronized List<ComposerState> getHistory() throws SQLException {
List<ComposerState> history = new ArrayList<>();
try {
// Loads the requests from the last x number of days, x being Settings.showHistoryRange
statement = conn.prepareStatement(Queries.selectRecentRequests);
String historyStartDate = LocalDate.now().minusDays(Settings.showHistoryRange).toString();
@ -188,9 +178,6 @@ class SQLiteManager implements DataManager {
history.add(state);
}
} catch (SQLException e) {
LoggingService.logWarning("Database error.", e, LocalDateTime.now());
}
return history;
}
@ -239,7 +226,8 @@ class SQLiteManager implements DataManager {
return fieldStates;
}
private ComposerState getLastRequest() {
@Override
public ComposerState getLastAdded() {
ComposerState lastRequest = new ComposerState();
try {
statement = conn.prepareStatement(Queries.selectMostRecentRequest);

View file

@ -41,7 +41,10 @@ public class SyncManager {
/**
* Asynchronously saves the new state by invoking all the registered DataManagers.
*/
public static void saveState(ComposerState newState) {
public void saveState(ComposerState newState) {
if (newState.equals(managers.get(Settings.fetchSource).getLastAdded()))
return;
historySaver.newState = newState;
executor.execute(historySaver);
@ -53,20 +56,27 @@ public class SyncManager {
*
* @return A list of all the requests
*/
public static List<ComposerState> getHistory() {
public List<ComposerState> getHistory() {
List<ComposerState> history = null;
try {
if (managers.get(Settings.fetchSource) == null) {
LoggingService.logSevere("No such source found: " + Settings.fetchSource, null, LocalDateTime.now());
return managers.get("SQLite").getHistory();
history = managers.get("SQLite").getHistory();
} else {
return managers.get(Settings.fetchSource).getHistory();
history = managers.get(Settings.fetchSource).getHistory();
}
} catch (Exception e) {
LoggingService.logSevere("History could not be fetched.", e, LocalDateTime.now());
}
return history;
}
/**
* Registers a new DataManager to be used for syncing Everest's data
* at various sources.
*/
public static void registerManager(DataManager newManager) throws DuplicateException {
public void registerManager(DataManager newManager) throws DuplicateException {
if (managers.containsKey(newManager.getIdentifier()))
throw new DuplicateException(
"Duplicate DataManager: Manager with identifier \'" + newManager.getIdentifier() + "\' already exists.");
@ -79,8 +89,12 @@ public class SyncManager {
@Override
public void run() {
try {
for (DataManager manager : managers.values())
manager.saveState(newState);
} catch (Exception e) {
LoggingService.logSevere("Could not save history.", e, LocalDateTime.now());
}
}
}
}