1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.DTO;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.Core;
using Tango.Core.DI;
using Tango.Integration.ExternalBridge;
using Tango.PPC.Common.Connection;
using Tango.PPC.Common.ExternalBridge;
using Tango.PPC.Shared.Statistics;
namespace Tango.PPC.Common.Statistics
{
[TangoCreateWhenRegistered]
public class DefaultStatisticsService : ExtendedObject, IStatisticsService, IExternalBridgeRequestHandler
{
private IMachineProvider _machineProvider;
public bool Enabled { get; set; } = true;
public DefaultStatisticsService(IPPCExternalBridgeService externalBridge, IMachineProvider machineProvider)
{
try
{
_machineProvider = machineProvider;
externalBridge.RegisterRequestHandler(this);
}
catch (Exception ex)
{
LogManager.Log(ex, "Error initializing the statistics service.");
Enabled = false;
}
}
[ExternalBridgeRequestHandlerMethod(typeof(GetStatisticsRequiredFiltersRequest), RequestHandlerLoggingMode.LogRequestName)]
public async Task OnGetStatisticsRequiredFiltersRequest(GetStatisticsRequiredFiltersRequest request, String token, ExternalBridgeReceiver receiver)
{
this.ThrowIfDisabled();
var response = new GetStatisticsRequiredFiltersResponse();
using (ObservablesContext db = ObservablesContext.CreateDefault())
{
var jobNames = (await db.JobRuns.Select(x => x.JobName).ToListAsync()).Distinct().ToList();
var threadNames = await db.Rmls.Select(x => new { x.Guid, x.Name, x.DisplayName }).ToListAsync();
if (_machineProvider.Machine.SiteGuid != null)
{
var siteRmlsGuids = (await db.SitesRmls.Where(x => x.SiteGuid == _machineProvider.Machine.SiteGuid).ToListAsync()).Select(x => x.RmlGuid).Where(x => x != null).Distinct().ToList();
threadNames.RemoveAll(x => !siteRmlsGuids.Contains(x.Guid));
}
response.FiltersData.Jobs = jobNames;
response.FiltersData.Rmls = threadNames.Select(x => new ThreadFilterData() { Guid = x.Guid, Name = x.DisplayName.IsNotNullOrEmpty() ? x.DisplayName : x.Name }).ToList();
}
await receiver.SendGenericResponse(response, token);
}
[ExternalBridgeRequestHandlerMethod(typeof(GetStatisticsRequest), RequestHandlerLoggingMode.LogRequestName)]
public async Task OnGetStatisticsRequest(GetStatisticsRequest request, String token, ExternalBridgeReceiver receiver)
{
this.ThrowIfDisabled();
var response = new GetStatisticsResponse();
using (ObservablesContext db = ObservablesContext.CreateDefault())
{
var filters = request.Filters;
var db_JobRuns = db.JobRuns.Where(x => (x.StartDate <= filters.EndDateUTC && x.StartDate >= filters.StartDateUTC));
int[] jobRunStatusArr = filters.EndStatuses.Distinct().ToArray();
if (jobRunStatusArr.Length > 0)
{
db_JobRuns = db_JobRuns.Where(x => jobRunStatusArr.Contains(x.Status));
}
if (!filters.IncludeHeadCleaning)
{
db_JobRuns = db_JobRuns.Where(x => !x.IsHeadCleaning);
}
List<String> rmlGuids = filters.RmlGuids;
if (filters.RmlGuids.Count > 0)
{
db_JobRuns = db_JobRuns.Where(x => rmlGuids.Contains(x.RmlGuid));
}
if (filters.JobName.IsNotNullOrEmpty())
{
if (!filters.ExactJobName)
{
db_JobRuns = db_JobRuns.Where(x => x.JobName.ToLower().StartsWith(filters.JobName.ToLower()));
}
else
{
db_JobRuns = db_JobRuns.Where(x => x.JobName.ToLower() == filters.JobName.ToLower());
}
}
db_JobRuns = db_JobRuns.Where(x => x.JobLength >= filters.MinLength && x.JobLength <= filters.MaxLength);
var jobRuns = await db_JobRuns.ToListAsync();
var colorSpaces = await db.ColorSpaces.ToListAsync();
var rmls = await db.Rmls.ToListAsync();
var catalogs = await db.ColorCatalogs.ToListAsync();
var color_catalog_Items = await db.ColorCatalogsItems.ToListAsync();
foreach (var jobRun in jobRuns)
{
JobRunComposition jobRunComposition = new JobRunComposition();
jobRunComposition.JobRun = JobRunDTO.FromObservable(jobRun);
PresentationJob pj = new PresentationJob();
pj.ID = jobRun.ID;
jobRunComposition.Job = pj;
if (jobRun.JobFile != null)
{
foreach (var segment in jobRun.JobFile.Segments)
{
PresentationSegment ps = new PresentationSegment();
ps.Length = (int)segment.Length;
pj.Segments.Add(ps);
foreach (var stop in segment.BrushStops.OrderBy(x => x.StopIndex).DistinctBy(x => x.OffsetPercent).ToList())
{
PresentationBrushStop pbs = new PresentationBrushStop();
pbs.ColorSpace = colorSpaces.First(x => x.Guid == stop.ColorSpaceGuid).Space;
pbs.StartMeters = segment.Length * (stop.OffsetPercent / 100d); ;
foreach (var liquidType in stop.LiquidVolumes)
{
PresentationLiquidVolume plt = new PresentationLiquidVolume();
plt.LiquidType = (LiquidTypes)Enum.Parse(typeof(LiquidTypes), liquidType.LiquidTypeName.Replace(" ", ""));
plt.Volume = liquidType.Volume;
pbs.LiquidVolumes.Add(plt);
}
switch (pbs.ColorSpace)
{
case ColorSpaces.RGB:
pbs.Red = stop.Red;
pbs.Green = stop.Green;
pbs.Blue = stop.Blue;
break;
case ColorSpaces.LAB:
pbs.L = stop.L;
pbs.A = stop.B;
pbs.B = stop.B;
break;
case ColorSpaces.Volume:
pbs.Cyan = pbs.LiquidVolumes.First(x => x.LiquidType == LiquidTypes.Cyan).Volume;
pbs.Magenta = pbs.LiquidVolumes.First(x => x.LiquidType == LiquidTypes.Magenta).Volume;
pbs.Yellow = pbs.LiquidVolumes.First(x => x.LiquidType == LiquidTypes.Yellow).Volume;
pbs.Black = pbs.LiquidVolumes.First(x => x.LiquidType == LiquidTypes.Black).Volume;
break;
case ColorSpaces.Catalog:
pbs.Catalog = catalogs.FirstOrDefault(x => x.Guid == stop.ColorCatalogGuid)?.Name;
pbs.CatalogItem = color_catalog_Items.FirstOrDefault(x => x.Guid == stop.ColorCatalogItemGuid)?.Name;
break;
}
pbs.BestMatchR = stop.BestMatchR;
pbs.BestMatchG = stop.BestMatchG;
pbs.BestMatchB = stop.BestMatchB;
ps.Stops.Add(pbs);
}
}
}
jobRunComposition.JobRun.JobString = null;
jobRunComposition.JobRun.LiquidQuantityString = null;
response.Result.JobRuns.Add(jobRunComposition);
}
}
await receiver.SendGenericResponse(response, token);
}
public void OnReceiverDisconnected(ExternalBridgeReceiver receiver)
{
}
}
}
|