2018-03-29 23:30:56 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Bit.Admin.Models;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2018-03-29 23:30:56 -04:00
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.Azure.Documents.Client;
|
|
|
|
|
|
using Microsoft.Azure.Documents.Linq;
|
2018-03-30 08:38:15 -04:00
|
|
|
|
using Serilog.Events;
|
2018-03-29 23:30:56 -04:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Admin.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
|
[SelfHosted(NotSelfHostedOnly = true)]
|
|
|
|
|
|
public class LogsController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private const string Database = "Diagnostics";
|
|
|
|
|
|
private const string Collection = "Logs";
|
|
|
|
|
|
|
|
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
|
|
|
|
|
|
|
|
|
|
|
public LogsController(GlobalSettings globalSettings)
|
|
|
|
|
|
{
|
|
|
|
|
|
_globalSettings = globalSettings;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-30 08:38:15 -04:00
|
|
|
|
public async Task<IActionResult> Index(string cursor = null, int count = 50,
|
2019-10-01 09:02:36 -04:00
|
|
|
|
LogEventLevel? level = null, string project = null, DateTime? start = null, DateTime? end = null)
|
2018-03-29 23:30:56 -04:00
|
|
|
|
{
|
|
|
|
|
|
var collectionLink = UriFactory.CreateDocumentCollectionUri(Database, Collection);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var client = new DocumentClient(new Uri(_globalSettings.DocumentDb.Uri),
|
2018-03-29 23:30:56 -04:00
|
|
|
|
_globalSettings.DocumentDb.Key))
|
|
|
|
|
|
{
|
|
|
|
|
|
var options = new FeedOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
MaxItemCount = count,
|
|
|
|
|
|
RequestContinuation = cursor
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-03-30 08:38:15 -04:00
|
|
|
|
var query = client.CreateDocumentQuery<LogModel>(collectionLink, options).AsQueryable();
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (level.HasValue)
|
2018-03-30 08:38:15 -04:00
|
|
|
|
{
|
|
|
|
|
|
query = query.Where(l => l.Level == level.Value.ToString());
|
|
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(project))
|
2018-03-30 08:38:15 -04:00
|
|
|
|
{
|
|
|
|
|
|
query = query.Where(l => l.Properties != null && l.Properties["Project"] == (object)project);
|
|
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (start.HasValue)
|
2019-10-01 09:02:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
query = query.Where(l => l.Timestamp >= start.Value);
|
|
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (end.HasValue)
|
2019-10-01 09:02:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
query = query.Where(l => l.Timestamp <= end.Value);
|
|
|
|
|
|
}
|
2018-03-30 08:38:15 -04:00
|
|
|
|
|
|
|
|
|
|
var docQuery = query.OrderByDescending(l => l.Timestamp).AsDocumentQuery();
|
|
|
|
|
|
var response = await docQuery.ExecuteNextAsync<LogModel>();
|
2018-03-29 23:30:56 -04:00
|
|
|
|
|
2018-03-30 08:38:15 -04:00
|
|
|
|
return View(new LogsModel
|
2018-03-29 23:30:56 -04:00
|
|
|
|
{
|
2018-03-30 08:38:15 -04:00
|
|
|
|
Level = level,
|
|
|
|
|
|
Project = project,
|
2019-10-01 09:02:36 -04:00
|
|
|
|
Start = start,
|
|
|
|
|
|
End = end,
|
2018-03-29 23:30:56 -04:00
|
|
|
|
Items = response.ToList(),
|
|
|
|
|
|
Count = count,
|
|
|
|
|
|
Cursor = cursor,
|
|
|
|
|
|
NextCursor = response.ResponseContinuation
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-30 10:18:50 -04:00
|
|
|
|
public async Task<IActionResult> View(Guid id)
|
2018-03-29 23:30:56 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var client = new DocumentClient(new Uri(_globalSettings.DocumentDb.Uri),
|
2018-03-29 23:30:56 -04:00
|
|
|
|
_globalSettings.DocumentDb.Key))
|
|
|
|
|
|
{
|
2018-03-30 10:18:50 -04:00
|
|
|
|
var uri = UriFactory.CreateDocumentUri(Database, Collection, id.ToString());
|
2018-03-30 17:35:07 -04:00
|
|
|
|
var response = await client.ReadDocumentAsync<LogDetailsModel>(uri);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (response?.Document == null)
|
2018-03-29 23:30:56 -04:00
|
|
|
|
{
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return View(response.Document);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|