Skip to content

Commit

Permalink
Added "read back" feature - re-fetching last log lines if no new line…
Browse files Browse the repository at this point in the history
…s loaded
  • Loading branch information
Toparvion committed Dec 7, 2016
1 parent 0128808 commit e60023e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 32 deletions.
16 changes: 13 additions & 3 deletions src/main/java/ru/ftc/upc/testing/analog/model/ReadingMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
* Time: 12:48
*/
public class ReadingMetaData implements Serializable {
private long prependingCounter = -1L;
private long appendingCounter = 0L;
private long fileSavedSize = 0L;
private long prependingCounter;
private long appendingCounter;
private long fileSavedSize;

public ReadingMetaData() {
reset();
}

public long getPrependingCounter() {
return prependingCounter;
Expand Down Expand Up @@ -39,4 +43,10 @@ public void setFileSavedSize(long fileSavedSize) {
public boolean isPrependingCounterSet() {
return (prependingCounter != -1L);
}

public void reset() {
prependingCounter = -1L;
appendingCounter = 0L;
fileSavedSize = 0L;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ public static String detectMessageType(String curLine) {
return "UNKNOWN";
}

public static List<String> getRawLines(String inputFileName,
String encoding,
ReadingMetaData readingMetaData,
Long prependingSnippetSizePercent) throws Exception {
static List<String> getRawLines(String inputFileName,
String encoding,
ReadingMetaData readingMetaData,
Long prependingSnippetSizePercent) throws Exception {
// проверяем наличие указанного файла
File inputFile = new File(inputFileName);
if (!inputFile.exists() || !inputFile.isFile()) {
Expand Down
44 changes: 29 additions & 15 deletions src/main/java/ru/ftc/upc/testing/analog/service/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,19 @@ public MainController(ChoicesProperties choicesProperties, EncodingDetector enco

@RequestMapping("/provide")
public Part provide(@RequestParam("log") String inputFileName,
@RequestParam(name = "prependingSize", required = false) Long prependingSize,
@RequestParam(required = false, name = "prependingSize") Long prependingSize,
@RequestParam(required = false, defaultValue = "UTF-8") String encoding,
@RequestParam(required = false, defaultValue = "false") boolean readBackAllowed,
HttpSession session) {
// получаем данные о предыдущем чтении
ReadingMetaData readingMetaData = AnaLogUtils.retrieveMetaData(session, inputFileName);

// получаем сырой набор строк из файла
List<String> rawLines;
try {
rawLines = AnaLogUtils.getRawLines(inputFileName, encoding, readingMetaData, prependingSize);

} catch (FileNotFoundException e) {
log.warn("Ошибка при чтении заданного файла: " + e.getMessage());
throw new RuntimeException(e);

} catch (Exception e) {
log.error("Internal application error: ", e);
throw new RuntimeException(e);
}
if (!rawLines.isEmpty()) {
log.trace("Raw lines read: {}", rawLines.size());
List<String> rawLines = fetchRawLines(inputFileName, prependingSize, encoding, readingMetaData);
if (rawLines.isEmpty() && readBackAllowed) {
log.debug("No new lines fetched. Attempting to read back...");
readingMetaData.reset();
rawLines = fetchRawLines(inputFileName, prependingSize, encoding, readingMetaData);
}

List<Line> parsedLines = new ArrayList<>();
Expand All @@ -83,6 +75,28 @@ public Part provide(@RequestParam("log") String inputFileName,
return new Part(parsedLines);
}

private List<String> fetchRawLines(String inputFileName,
Long prependingSize,
String encoding,
ReadingMetaData readingMetaData) {
List<String> rawLines;
try {
rawLines = AnaLogUtils.getRawLines(inputFileName, encoding, readingMetaData, prependingSize);

} catch (FileNotFoundException e) {
log.warn("Ошибка при чтении заданного файла: " + e.getMessage());
throw new RuntimeException(e);

} catch (Exception e) {
log.error("Internal application error: ", e);
throw new RuntimeException(e);
}
if (!rawLines.isEmpty()) {
log.trace("Raw lines read: {}", rawLines.size());
}
return rawLines;
}

@RequestMapping("/choices")
public List<LogChoice> choices() {
return choices.stream()
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<label>
<input type="checkbox" name="updateToggler" ng-model="onAir"/>Обновлять ежесекундно&nbsp;
</label>
<button name="updateNowButton" ng-click="updateLog()" ng-disabled="onAir">Обновить сейчас</button>
<button name="updateNowButton" ng-click="updateLog(false)" ng-disabled="onAir">Обновить сейчас</button>
&nbsp;
<!--
<label>
Expand Down
14 changes: 7 additions & 7 deletions src/main/resources/static/js/main-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ app.controller('controlPanelController', function ($scope, $rootScope,
$location.path($scope.selectedLog.path);
$scope.onAir = false;
renderingService.clearQueue();
$scope.updateLog($scope.selectedLog);
$scope.updateLog(/*readBackAllowed:*/true);
};
$scope.updateLog = function () {
providerService($scope.selectedLog.path, $scope.encoding, $scope.stopOnAir)
$scope.updateLog = function (isReadBackAllowed) {
providerService($scope.selectedLog.path, $scope.encoding, $scope.stopOnAir, isReadBackAllowed)
};
$scope.disableOnAirPoller = function() {
if (angular.isDefined(onAirPromise)) {
Expand All @@ -36,7 +36,7 @@ app.controller('controlPanelController', function ($scope, $rootScope,
});
$scope.prepend = function () {
$scope.onAir = false;
providerService($scope.selectedLog.path, $scope.encoding, $scope.stopOnAir, $scope.prependingSize);
providerService($scope.selectedLog.path, $scope.encoding, $scope.stopOnAir, false, $scope.prependingSize);
};
$scope.clear = function () {
renderingService.clearQueue();
Expand All @@ -53,7 +53,7 @@ app.controller('controlPanelController', function ($scope, $rootScope,
$scope.selectedLog = selectedChoice;
$scope.encoding = selectedChoice.encoding;
$rootScope.watchingLog = selectedChoice.title + " - АнаЛог";
$scope.updateLog();
$scope.updateLog(/*readBackAllowed:*/true);
});
}
// the following watch allows us to react to URL path change instantly (without opening a new browser tab)
Expand All @@ -76,8 +76,8 @@ app.controller('controlPanelController', function ($scope, $rootScope,
}, function () {
$log.log("Turning onAir to: " + $scope.onAir);
if ($scope.onAir) {
onAirPromise = $interval($scope.updateLog, 1000);
$scope.updateLog(); // in order not to wait for the first interval triggering
onAirPromise = $interval(function() {$scope.updateLog(false)}, 1000);
$scope.updateLog(false); // in order not to wait for the first interval triggering
} else {
$scope.disableOnAirPoller();
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/static/js/provider-service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
function ProviderService($http, $window, renderingService) {
return function (logPath, encoding, errorCallback, prependingSize) {
return function (logPath, encoding, errorCallback, isReadBackAllowed, prependingSize) {
var params = {
log: logPath,
encoding: encoding
encoding: encoding,
readBackAllowed: isReadBackAllowed
};
if (prependingSize) {
params.prependingSize = prependingSize;
Expand Down

0 comments on commit e60023e

Please sign in to comment.