aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Statistics/DefaultStatisticsService.cs
blob: d05b4c6bf769ebbf5038c96b4d8617b0ac8f3542 (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.IO;
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.Integration.JobRuns;
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;
        private Dictionary<ExternalBridgeReceiver, String> _streamingReceivers;
        private List<ColorCatalog> _catalogs;
        private List<ColorCatalogsItem> _color_catalog_Items;
        private List<ColorSpace> _colorSpaces;
        private Dictionary<String, LiquidType> _liquidTypes;

        public bool Enabled { get; set; } = true;

        public DefaultStatisticsService(IPPCExternalBridgeService externalBridge, IMachineProvider machineProvider)
        {
            _streamingReceivers = new Dictionary<ExternalBridgeReceiver, string>();

            try
            {
                _machineProvider = machineProvider;
                externalBridge.RegisterRequestHandler(this);
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error initializing the statistics service.");
                Enabled = false;
            }

            _machineProvider.MachineOperator.JobRunsLogger.JobRunAvailable += JobRunsLogger_JobRunAvailable;
        }

        private void JobRunsLogger_JobRunAvailable(object sender, JobRunAvailableEventArgs e)
        {
            Task.Factory.StartNew(async () =>
            {
                var jobRunComposition = await CreateJobRunComposition(e.JobRun);

                foreach (var receiver in _streamingReceivers.ToList())
                {
                    try
                    {
                        var response = new StartJobRunsStreamingResponse();
                        response.JobRunComposition = jobRunComposition;
                        await receiver.Key.SendGenericResponse(response, receiver.Value);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
            });
        }

        [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())
            {
                response.Result.LiquidTypes = db.LiquidTypes.ToList().Select(x => LiquidTypeDTO.FromObservable(x)).ToList();

                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));
                }

                int[] jobDesignationArr = filters.JobDesignation.Distinct().ToArray();
                if (jobDesignationArr.Length > 0)
                {
                    db_JobRuns = db_JobRuns.Where(x => jobDesignationArr.Contains(x.JobDesignation));
                }

                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();

                foreach (var jobRun in jobRuns)
                {
                    JobRunComposition jobRunComposition = await CreateJobRunComposition(jobRun);
                    response.Result.JobRuns.Add(jobRunComposition);
                }
            }

            await receiver.SendGenericResponse(response, token);
        }

        private async Task<JobRunComposition> CreateJobRunComposition(JobRun jobRun)
        {
            if (_colorSpaces == null)
            {
                using (ObservablesContext db = ObservablesContext.CreateDefault())
                {
                    _colorSpaces = await db.ColorSpaces.ToListAsync();
                    _catalogs = await db.ColorCatalogs.ToListAsync();
                    _color_catalog_Items = await db.ColorCatalogsItems.ToListAsync();
                }
            }

            if (_liquidTypes == null)
            {
                using (ObservablesContext db = ObservablesContext.CreateDefault())
                {
                    var types = await db.LiquidTypes.ToListAsync();
                    _liquidTypes = new Dictionary<string, LiquidType>();
                    foreach (var t in types)
                    {
                        _liquidTypes.Add(t.Name, t);
                    }
                }
            }

            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();

                            if (_liquidTypes.ContainsKey(liquidType.LiquidTypeName))
                            {
                                plt.LiquidType = _liquidTypes[liquidType.LiquidTypeName];
                                plt.Volume = liquidType.Volume;
                                pbs.LiquidVolumes.Add(plt);
                            }
                        }

                        pbs.LiquidVolumes = pbs.LiquidVolumes.OrderBy(x => x.LiquidType.PreferredIndex).ToList();

                        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.A;
                                pbs.B = stop.B;
                                break;
                            case ColorSpaces.Volume:

                                //foreach (var lightInk in pbs.LiquidVolumes.Where(x => x.LiquidType.IsLightInk && x.Volume > 0))
                                //{
                                //    var darkInk = pbs.LiquidVolumes.FirstOrDefault(x => x.LiquidType.Code == lightInk.LiquidType.DarkInkCode);
                                //    if (darkInk != null)
                                //    {
                                //        darkInk.Volume = (double)((decimal)lightInk.Volume / 10m);
                                //        lightInk.Volume = 0;
                                //    }
                                //}

                                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;

            return jobRunComposition;
        }

        [ExternalBridgeRequestHandlerMethod(typeof(GetJobRunExtendedInfoRequest), RequestHandlerLoggingMode.LogRequestName)]
        public async Task OnGetJobRunExtendedInfoRequest(GetJobRunExtendedInfoRequest request, String token, ExternalBridgeReceiver receiver)
        {
            this.ThrowIfDisabled();

            var response = new GetJobRunExtendedInfoResponse();

            String filePath = Path.Combine((_machineProvider.MachineOperator.JobRunsLogger as BasicJobRunsLogger).JobRunsFolder, $"{request.JobRunID}.run");
            String json = File.ReadAllText(filePath);

            JobRunInfo info = JsonConvert.DeserializeObject<JobRunInfo>(json);

            response.ExtendedInfo.JobRunID = info.JobRunID;
            response.ExtendedInfo.JobTicket = info.JobTicket;
            response.ExtendedInfo.Events = info.Events;
            response.ExtendedInfo.StartMachineStatus = info.StartMachineStatus;
            response.ExtendedInfo.EndMachineStatus = info.EndMachineStatus;

            await receiver.SendGenericResponse(response, token);
        }

        [ExternalBridgeRequestHandlerMethod(typeof(StartJobRunsStreamingRequest), RequestHandlerLoggingMode.LogRequestName)]
        public async Task OnStartJobRunsStreamingRequest(StartJobRunsStreamingRequest request, String token, ExternalBridgeReceiver receiver)
        {
            this.ThrowIfDisabled();

            if (!_streamingReceivers.ContainsKey(receiver))
            {
                _streamingReceivers.Add(receiver, token);
            }

            var response = new StartJobRunsStreamingResponse();

            await receiver.SendGenericResponse(response, token);
        }

        [ExternalBridgeRequestHandlerMethod(typeof(StopJobRunsStreamingRequest), RequestHandlerLoggingMode.LogRequestName)]
        public async Task OnStopJobRunsStreamingRequest(StopJobRunsStreamingRequest request, String token, ExternalBridgeReceiver receiver)
        {
            this.ThrowIfDisabled();

            if (_streamingReceivers.ContainsKey(receiver))
            {
                _streamingReceivers.Remove(receiver);
            }

            var response = new StopJobRunsStreamingResponse();

            await receiver.SendGenericResponse(response, token);
        }

        public void OnReceiverDisconnected(ExternalBridgeReceiver receiver)
        {
            if (_streamingReceivers.ContainsKey(receiver))
            {
                _streamingReceivers.Remove(receiver);
            }
        }
    }
}