Skip to content

Commit

Permalink
Logs page now has paging (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-j-green authored Sep 25, 2023
1 parent d6d6a5d commit 9064567
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
4 changes: 2 additions & 2 deletions gaseous-server/Controllers/LogsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class LogsController : Controller
{
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public List<Logging.LogItem> Logs()
public List<Logging.LogItem> Logs(long? StartIndex, int PageNumber = 1, int PageSize = 100)
{
return Logging.GetLogs();
return Logging.GetLogs(StartIndex, PageNumber, PageSize);
}
}
}
55 changes: 39 additions & 16 deletions gaseous-server/wwwroot/pages/settings/logs.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,53 @@ <h1 id="gametitle_label">Logs</h1>

</table>

<div style="width: 960px; text-align: center;">
<button value="Load More" onclick="loadLogs(lastStartIndex, currentPage);">Load More</button>
</div>

<script type="text/javascript">
function loadLogs() {
var lastStartIndex = 0;
var currentPage = 1;

function loadLogs(StartIndex, PageNumber) {
var apiQuery = '';

if (StartIndex && PageNumber) {
currentPage += 1;
apiQuery = '?StartIndex=' + StartIndex + '&PageNumber=' + PageNumber;
} else {
currentPage = 1;
}

ajaxCall(
'/api/v1/Logs',
'/api/v1/Logs' + apiQuery,
'GET',
function (result) {
var newTable = document.getElementById('settings_events_table');
newTable.innerHTML = '';
newTable.appendChild(
createTableRow(
true,
[
['Event Time', 'logs_table_cell_150px'],
['Severity', 'logs_table_cell_150px'],
'Process',
'Message'
],
'',
''
)
);
if (currentPage == 1) {
newTable.innerHTML = '';

newTable.appendChild(
createTableRow(
true,
[
//'Id',
['Event Time', 'logs_table_cell_150px'],
['Severity', 'logs_table_cell_150px'],
'Process',
'Message'
],
'',
''
)
);
}

for (var i = 0; i < result.length; i++) {
lastStartIndex = result[i].id;

var newRow = [
//result[i].id,
moment(result[i].eventTime).fromNow(),
result[i].eventType,
result[i].process,
Expand Down
20 changes: 17 additions & 3 deletions gaseous-tools/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,30 @@ static void LogToDisk(LogItem logItem, string TraceOutput, Exception? exception)
File.AppendAllText(Config.LogFilePath, TraceOutput);
}

static public List<LogItem> GetLogs()
static public List<LogItem> GetLogs(long? StartIndex, int PageNumber = 1, int PageSize = 100)
{
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
string sql = "SELECT * FROM ServerLogs ORDER BY Id DESC";
DataTable dataTable = db.ExecuteCMD(sql);
string sql = "";
if (StartIndex == null)
{
sql = "SELECT * FROM ServerLogs ORDER BY Id DESC LIMIT @PageSize OFFSET @PageNumber;";
}
else
{
sql = "SELECT * FROM ServerLogs WHERE Id < @StartIndex ORDER BY Id DESC LIMIT @PageSize OFFSET @PageNumber;";
}
Dictionary<string, object> dbDict = new Dictionary<string, object>();
dbDict.Add("StartIndex", StartIndex);
dbDict.Add("PageNumber", (PageNumber - 1) * PageSize);
dbDict.Add("PageSize", PageSize);
DataTable dataTable = db.ExecuteCMD(sql, dbDict);

List<LogItem> logs = new List<LogItem>();
foreach (DataRow row in dataTable.Rows)
{
LogItem log = new LogItem
{
Id = (long)row["Id"],
EventTime = DateTime.Parse(((DateTime)row["EventTime"]).ToString("yyyy-MM-ddThh:mm:ss") + 'Z'),
EventType = (LogType)row["EventType"],
Process = (string)row["Process"],
Expand All @@ -145,6 +158,7 @@ public enum LogType

public class LogItem
{
public long Id { get; set; }
public DateTime EventTime { get; set; }
public LogType? EventType { get; set; }
public string Process { get; set; } = "";
Expand Down

0 comments on commit 9064567

Please sign in to comment.