Feature: Repository Monitor

Cached Job Index, Live Job Message Update
This commit is contained in:
Gary Sharp
2013-04-11 19:10:38 +10:00
parent b5531e64bd
commit b79723d432
34 changed files with 803 additions and 262 deletions
+14 -6
View File
@@ -11,8 +11,10 @@ namespace Disco.BI.Extensions
public static class JobTableExtensions
{
public static void Fill(this JobTableModel model, DiscoDataContext dbContext, IQueryable<Job> Jobs)
public static List<JobTableModel.JobTableItemModel> DetermineItems(this JobTableModel model, DiscoDataContext dbContext, IQueryable<Job> Jobs)
{
List<JobTableModel.JobTableItemModel> items;
if (model.ShowStatus)
{
@@ -50,18 +52,18 @@ namespace Disco.BI.Extensions
JobMetaNonWarranty_RepairerName = j.JobMetaNonWarranty.RepairerName
});
model.Items = new List<JobTableModel.JobTableItemModel>();
items = new List<JobTableModel.JobTableItemModel>();
foreach (var j in jobItems)
{
j.StatusId = j.CalculateStatusId();
j.StatusDescription = JobBI.Utilities.JobStatusDescription(j.StatusId, j);
model.Items.Add(j);
items.Add(j);
}
}
else
{
model.Items = Jobs.Select(j => new JobTableModel.JobTableItemModel()
items = Jobs.Select(j => new JobTableModel.JobTableItemModel()
{
Id = j.Id,
DeviceAddressId = j.Device.DeviceProfile.DefaultOrganisationAddress,
@@ -84,11 +86,17 @@ namespace Disco.BI.Extensions
if (model.ShowDeviceAddress.Value)
{
foreach (var j in model.Items)
foreach (var j in items)
if (j.DeviceAddressId.HasValue)
j.DeviceAddress = dbContext.DiscoConfiguration.OrganisationAddresses.GetAddress(j.DeviceAddressId.Value).Name;
}
}
return items;
}
public static void Fill(this JobTableModel model, DiscoDataContext dbContext, IQueryable<Job> Jobs)
{
model.Items = model.DetermineItems(dbContext, Jobs);
}
}
}
@@ -0,0 +1,37 @@
using Disco.Data.Repository.Monitor;
using SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Disco.BI.Interop.SignalRHandlers
{
public class RepositoryMonitorNotifications : PersistentConnection
{
public static void Initialize()
{
RepositoryMonitor.StreamAfterCommit.Subscribe(AfterCommit);
}
protected override System.Threading.Tasks.Task OnReceivedAsync(IRequest request, string connectionId, string data)
{
// Add to Group
if (!string.IsNullOrWhiteSpace(data) && data.StartsWith("/addToGroups:") && data.Length > 13)
{
var groups = data.Substring(13).Split(',');
foreach (var g in groups)
{
this.Groups.Add(connectionId, g);
}
}
return base.OnReceivedAsync(request, connectionId, data);
}
private static void AfterCommit(RepositoryMonitorEvent e)
{
GlobalHost.ConnectionManager.GetConnectionContext<RepositoryMonitorNotifications>().Groups.Send(e.EntityType.Name, e);
}
}
}
+80
View File
@@ -0,0 +1,80 @@
using Disco.Data.Repository;
using Disco.Data.Repository.Monitor;
using Disco.Models.BI.Job;
using Disco.Models.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Disco.BI.Extensions;
using System.Reactive.Linq;
namespace Disco.BI.JobBI
{
public class ManagedJobList : JobTableModel, IDisposable
{
public string Name { get; set; }
public Func<IQueryable<Job>, IQueryable<Job>> FilterFunction {get;set;}
public Func<IEnumerable<JobTableItemModel>, IEnumerable<JobTableItemModel>> SortFunction { get; set; }
private IDisposable unsubscribeToken;
public ManagedJobList Initialize(DiscoDataContext dbContext)
{
// Initially fill table
this.Items = this.SortFunction(this.DetermineItems(dbContext, this.FilterFunction(dbContext.Jobs))).ToList();
// Subscribe for Changes
unsubscribeToken = RepositoryMonitor.StreamAfterCommit
.Where(n => n.EntityType == typeof(Job) ||
n.EntityType == typeof(JobMetaWarranty) ||
n.EntityType == typeof(JobMetaNonWarranty) ||
n.EntityType == typeof(JobMetaInsurance))
.Subscribe(JobNotification);
return this;
}
private void JobNotification(RepositoryMonitorEvent e)
{
int jobId;
if (e.EntityType == typeof(Job))
jobId = ((Job)e.Entity).Id;
else
if (e.EntityType == typeof(JobMetaWarranty))
jobId = ((JobMetaWarranty)e.Entity).JobId;
else
if (e.EntityType == typeof(JobMetaNonWarranty))
jobId = ((JobMetaNonWarranty)e.Entity).JobId;
else
if (e.EntityType == typeof(JobMetaInsurance))
jobId = ((JobMetaInsurance)e.Entity).JobId;
else
return; // Subscription should never reach
var existingItem = this.Items.FirstOrDefault(i => i.Id == jobId);
var updatedItem = this.DetermineItems(e.dbContext, this.FilterFunction(e.dbContext.Jobs.Where(j => j.Id == jobId)));
var updatedItems = this.Items.ToList();
// Remove Existing
if (existingItem != null)
updatedItems.Remove(existingItem);
if (updatedItem.Count > 0)
{
// Add Item
updatedItems.Add(updatedItem.First());
}
// Reorder
this.Items = this.SortFunction(updatedItems).ToList();
}
public void Dispose()
{
unsubscribeToken.Dispose();
}
}
}
+17
View File
@@ -78,6 +78,21 @@
<Reference Include="System.DirectoryServices" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Reactive.Core, Version=2.1.30214.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Rx-Core.2.1.30214.0\lib\Net45\System.Reactive.Core.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Interfaces, Version=2.1.30214.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Rx-Interfaces.2.1.30214.0\lib\Net45\System.Reactive.Interfaces.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Linq, Version=2.1.30214.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Rx-Linq.2.1.30214.0\lib\Net45\System.Reactive.Linq.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.PlatformServices">
<HintPath>..\packages\Rx-PlatformServices.2.1.30214.0\lib\Net45\System.Reactive.PlatformServices.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Web" />
@@ -157,7 +172,9 @@
<Compile Include="BI\Interop\Pdf\PdfImporter.cs" />
<Compile Include="BI\Interop\PluginServices\IDiscoScheduledTask.cs" />
<Compile Include="BI\Interop\PluginServices\Utilities.cs" />
<Compile Include="BI\Interop\SignalRHandlers\RepositoryMonitorNotifications.cs" />
<Compile Include="BI\Interop\SignalRHandlers\UserHeldDevices.cs" />
<Compile Include="BI\JobBI\ManagedJobList.cs" />
<Compile Include="BI\JobBI\Searching.cs" />
<Compile Include="BI\JobBI\Statistics\DailyOpenedClosed.cs" />
<Compile Include="BI\JobBI\Utilities.cs" />
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0405.1309")]
[assembly: AssemblyFileVersion("1.2.0405.1309")]
[assembly: AssemblyVersion("1.2.0411.1833")]
[assembly: AssemblyFileVersion("1.2.0411.1833")]
+5
View File
@@ -3,6 +3,11 @@
<package id="EntityFramework" version="5.0.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
<package id="Rx-Core" version="2.1.30214.0" targetFramework="net45" />
<package id="Rx-Interfaces" version="2.1.30214.0" targetFramework="net45" />
<package id="Rx-Linq" version="2.1.30214.0" targetFramework="net45" />
<package id="Rx-Main" version="2.1.30214.0" targetFramework="net45" />
<package id="Rx-PlatformServices" version="2.1.30214.0" targetFramework="net45" />
<package id="SignalR.Hosting.AspNet" version="0.5.3" targetFramework="net45" />
<package id="SignalR.Hosting.Common" version="0.5.3" targetFramework="net45" />
<package id="SignalR.Server" version="0.5.3" targetFramework="net45" />