Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions IntelliTrader.Trading.Tests/HomeControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.IO;
using IntelliTrader.Core;
using IntelliTrader.Web.Controllers;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Xunit;

namespace IntelliTrader.Trading.Tests
{
public class HomeControllerTests
{
[Fact]
public void DownloadLog_ReturnsNotFound_WhenLogFileDoesNotExist()
{
// Arrange
var controller = new HomeController();
string tempDir = Path.Combine(Directory.GetCurrentDirectory(), "log");

// Ensure directory exists or create temporary directory context if needed
if (!Directory.Exists(tempDir))
{
Directory.CreateDirectory(tempDir);
}

// Act
var result = controller.DownloadLog("nonexistent_log_type");

// Assert
var notFoundResult = Assert.IsType<NotFoundObjectResult>(result);
Assert.Equal("Log file not found.", notFoundResult.Value);
}

[Fact]
public void DownloadLog_ReturnsFileStreamResult_WhenLogFileExists()
{
// Arrange
var controller = new HomeController();
string logsPath = Path.Combine(Directory.GetCurrentDirectory(), "log");
if (!Directory.Exists(logsPath))
{
Directory.CreateDirectory(logsPath);
}

string testLogFile = Path.Combine(logsPath, $"{DateTime.Now:yyyy-MM-dd}-general.txt");
File.WriteAllText(testLogFile, "Log line 1\nLog line 2\n");

try
{
// Act
var result = controller.DownloadLog("general");

// Assert
var fileResult = Assert.IsType<FileStreamResult>(result);
Assert.Equal("text/plain", fileResult.ContentType);
Assert.Equal(Path.GetFileName(testLogFile), fileResult.FileDownloadName);

fileResult.FileStream.Dispose();
}
finally
{
if (File.Exists(testLogFile))
{
File.Delete(testLogFile);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<ItemGroup>
<ProjectReference Include="..\IntelliTrader.Core\IntelliTrader.Core.csproj" />
<ProjectReference Include="..\IntelliTrader.Trading\IntelliTrader.Trading.csproj" />
<ProjectReference Include="..\IntelliTrader.Web\IntelliTrader.Web.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions IntelliTrader.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,29 @@ public IActionResult PollLogs(string type = "general", int maxLines = 100)
}
}

[HttpGet]
public IActionResult DownloadLog(string type = "general")
{
try
{
string pattern = "general".Equals(type, StringComparison.OrdinalIgnoreCase) ? "*-general.txt" : "*-trades.txt";
string filePath = GetLatestLogFilePath(pattern);

if (string.IsNullOrEmpty(filePath) || !System.IO.File.Exists(filePath))
{
return NotFound("Log file not found.");
}

string fileName = Path.GetFileName(filePath);
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
return File(stream, "text/plain", fileName);
}
catch (Exception ex)
{
return BadRequest($"Error downloading log file: {ex.Message}");
}
}

private string GetLatestLogFilePath(string pattern)
{
var logsPath = Path.Combine(Directory.GetCurrentDirectory(), "log");
Expand Down
1 change: 1 addition & 0 deletions IntelliTrader.Web/Static/Scripts/Views/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ function setLogType(type) {
$("#logTypeGeneralBtn").removeClass("active");
}

$("#downloadLogFileBtn").attr("href", "/Home/DownloadLog?type=" + type);
$("#logTerminal").html('<div class="text-muted">Loading logs...</div>');
pollLiveLogs();
}
Expand Down
11 changes: 8 additions & 3 deletions IntelliTrader.Web/Views/Home/Dashboard.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,14 @@
<div class="card mt-4" style="background-color: var(--card-bg); color: var(--text-color); border-radius: 4px; border: 1px solid var(--card-border); margin-top: 20px;">
<div class="card-header" style="background-color: var(--card-header-bg); border-bottom: 1px solid var(--card-border); padding: 10px 15px; display: flex; align-items: center; justify-content: space-between;">
<h5 class="mb-0" style="color: var(--text-light); font-size: 16px; margin: 0;"><i class="fas fa-terminal mr-2"></i>Live Log Monitor</h5>
<div class="btn-group btn-group-sm" role="group">
<button type="button" class="btn btn-sm btn-outline-light active" id="logTypeGeneralBtn" style="border-color: var(--card-border); margin-right: 5px;" onclick="setLogType('general')">General Log</button>
<button type="button" class="btn btn-sm btn-outline-light" id="logTypeTradesBtn" style="border-color: var(--card-border);" onclick="setLogType('trades')">Trades Log</button>
<div class="d-flex align-items-center">
<a id="downloadLogFileBtn" href="/Home/DownloadLog?type=general" class="btn btn-sm btn-outline-info mr-2" style="border-color: #7aa2f7; color: #7aa2f7;" title="Download full log file">
<i class="fas fa-download mr-1"></i> Скачать лог
</a>
<div class="btn-group btn-group-sm" role="group">
<button type="button" class="btn btn-sm btn-outline-light active" id="logTypeGeneralBtn" style="border-color: var(--card-border); margin-right: 5px;" onclick="setLogType('general')">General Log</button>
<button type="button" class="btn btn-sm btn-outline-light" id="logTypeTradesBtn" style="border-color: var(--card-border);" onclick="setLogType('trades')">Trades Log</button>
</div>
</div>
</div>
<div class="card-body p-0">
Expand Down
18 changes: 17 additions & 1 deletion magda_agent_system/agent_tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@
},
{
"id": "web-dashboard-log-download",
"status": "todo",
"status": "done",
"area": "web",
"risk": "low",
"title": "Add log file download button on Web Dashboard",
Expand Down Expand Up @@ -505,6 +505,22 @@
"acceptance": [
"Users can click export on the Stats page to download summary statistics."
]
},
{
"id": "web-dashboard-log-search-filter",
"status": "todo",
"area": "web",
"risk": "low",
"title": "Add text search filter to Live Log Monitor on Web Dashboard",
"description": "Add an inline search input field to the Live Log Monitor card allowing users to filter log lines dynamically in real-time.",
"allowed_paths": [
"IntelliTrader.Web/Views/**",
"IntelliTrader.Web/Controllers/**",
"magda_agent_system/agent_tasks.json"
],
"acceptance": [
"Users can type keywords into a filter box to instantly highlight or filter log terminal output."
]
}
]
}