simple log viewer

This commit is contained in:
David Brazda
2023-11-21 13:03:16 +01:00
parent 222c85e465
commit fb49847a95
6 changed files with 105 additions and 2 deletions

View File

@ -127,7 +127,7 @@ def init(state: StrategyState):
local_dict_bars = {key: state.bars[key] for key in state.bars.keys() if key != "time"}
state.ind_mapping = {**local_dict_inds, **local_dict_bars}
printanyway("IND MAPPING DONE:", state.ind_mapping)
print("IND MAPPING DONE:", state.ind_mapping)
#30 DAYS historicall data fill - pridat do base pokud se osvedci
# -1 je vždy včerejšek v tomto případě

View File

@ -6,6 +6,9 @@ from pathlib import Path
#directory for generated images and basic reports
MEDIA_DIRECTORY = Path(__file__).parent.parent / "media"
LOG_FILE = Path(__file__).parent.parent / "strat.log"
#'0.0.0.0',
#currently only prod server has acces to LIVE
PROD_SERVER_HOSTNAMES = ['tradingeastcoast','David-MacBook-Pro.local'] #,'David-MacBook-Pro.local'

View File

@ -1,6 +1,6 @@
import os,sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from v2realbot.config import WEB_API_KEY, DATA_DIR, MEDIA_DIRECTORY
from v2realbot.config import WEB_API_KEY, DATA_DIR, MEDIA_DIRECTORY, LOG_FILE
from alpaca.data.timeframe import TimeFrame, TimeFrameUnit
from datetime import datetime
import os
@ -501,6 +501,30 @@ def _get_archived_runner_log_byID(runner_id: UUID, timestamp_from: float, timest
raise HTTPException(status_code=404, detail=f"No logs found with id: {runner_id} and between {timestamp_from} and {timestamp_to}")
# endregion
# A simple function to read the last lines of a file
def tail(file_path, n=10, buffer_size=1024):
with open(file_path, 'rb') as f:
f.seek(0, 2) # Move to the end of the file
file_size = f.tell()
lines = []
buffer = bytearray()
for i in range(file_size // buffer_size + 1):
read_start = max(-buffer_size * (i + 1), -file_size)
f.seek(read_start, 2)
read_size = min(buffer_size, file_size - buffer_size * i)
buffer[0:0] = f.read(read_size) # Prepend to buffer
if buffer.count(b'\n') >= n + 1:
break
lines = buffer.decode(errors='ignore').splitlines()[-n:]
return lines
@app.get("/log", dependencies=[Depends(api_key_auth)])
def read_log(lines: int = 10):
log_path = LOG_FILE
return {"lines": tail(log_path, lines)}
#get alpaca history bars
@app.get("/history_bars/", dependencies=[Depends(api_key_auth)])

View File

@ -234,6 +234,7 @@
<button id="button_stopall" class="btn btn-outline-success btn-sm">Stop All</button>
<button id="button_refresh" class="refresh btn btn-outline-success btn-sm">Refresh</button>
<button id="button_connect" class="btn btn-outline-success btn-sm">Connect</button>
<button id="button_show_log" class="btn btn-outline-success btn-sm">Log</button>
</div>
<div>status info</div>
<table id="runnerTable" class="table-striped table dataTable" style="width:100%; border-color: #dce1dc;">
@ -364,6 +365,29 @@
</form>
</div>
</div>
<div id="logModal" class="modal fade" style="--bs-modal-width: 825px;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"><i class="fa fa-plus"></i>Log</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="logHere" class="form-label">Log</label>
<div id="log-container">
<pre id="log-content"></pre>
</div>
<!-- <input type="text" class="form-control" id="delidarchive" name="delidarchive" placeholder="id"> -->
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="logRefreshButton" value="Refresh">Refresh</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div id="editModalArchive" class="modal fade">
<div class="modal-dialog">
<form method="post" id="editFormArchive">

View File

@ -453,6 +453,41 @@ $(document).ready(function () {
})
});
function refresh_logfile() {
$.ajax({
url:"/log?lines=30",
beforeSend: function (xhr) {
xhr.setRequestHeader('X-API-Key',
API_KEY); },
method:"GET",
contentType: "application/json",
dataType: "json",
success:function(response){
if (response.lines.length == 0) {
$('#log-content').html("no records");
}
else {
$('#log-content').html(response.lines.join('\n'));
}
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
window.alert(JSON.stringify(xhr));
}
})
}
//button to query log
$('#logRefreshButton').click(function () {
refresh_logfile()
});
//button to open log modal
$('#button_show_log').click(function () {
window.$('#logModal').modal('show');
refresh_logfile()
});
//delete button
$('#button_delete_arch').click(function () {

View File

@ -591,6 +591,23 @@ pre {
background-color: #3e8e41;
} */
#log-container {
width: 800px; /* Adjust based on the average line length and font size */
height: auto;
max-height: 800px; /* Adjust the height as needed */
overflow-y: scroll; /* Add scroll for overflow */
border: 1px solid #ccc;
padding: 5px;
/* background-color: #f9f9f9; */
}
#log-content {
font-family: "Courier New", monospace;
font-size: 14px; /* Adjust font size as needed */
line-height: 1.5; /* Adjust line spacing as needed */
}
.recordItem:after {
content: "";
display: table;