/// <summary>
 /// Attaches the diagnostic component so it can be monitored
 /// </summary>
 public void AttachMonitor(HealthMonitor monitor)
 {
     // bind to healthMonitor
     healthMonitor = monitor;
     healthMonitor.EdgeServerCompleted += healthMonitor_EdgeServerChanged;
     healthMonitor.EventCreated        += healthMonitor_EventCreated;
     healthMonitor.ReportSampledData   += healthMonitor_ReportSampledData;
     healthMonitor.LatencyAlert        += healthMonitor_LatencyAlert;
     healthMonitor.ReportTraceLogs     += healthMonitor_ReportTraceLogs;
     // guarantee that we're not using an old instance anymore
     streamLoadedLog = null;
 }
        void ProcessEvent(Log log)
        {
            if (log.Type != VideoLogTypes.Trace && log.Type != VideoLogTypes.VideoQualitySnapshot)
            {
                // weed these out, there are way too many of them. This is consistent with what a logagent that sends to the server would do too.
                Logs.Add(log);
            }

            IEnumerable <ChartViewModel> charts = ChartViewModel.Charts;

            switch (log.Type)
            {
            case VideoLogTypes.VideoQuality:
                QualityData = new QualityDataViewModel(log.CastLog <VideoQualityLog>());
                break;

            case VideoLogTypes.VideoQualitySnapshot:
                VideoQualitySnapshotLog qualityLog = log.CastLog <VideoQualitySnapshotLog>();

                foreach (ChartViewModel chartVM in charts)
                {
                    if (log.Data.ContainsKey(chartVM.QualityAttribute))
                    {
                        if (chartVM.QualityAttribute == VideoLogAttributes.PerceivedBandwidth)
                        {
                            chartVM.AddDataPoint(qualityLog.PerceivedBandwidth.GetValueOrDefault(0) / 1024);
                        }
                        else
                        {
                            chartVM.AddDataPoint(Convert.ToDouble(log.Data[chartVM.QualityAttribute]));
                        }
                    }
                }

                ClientIP           = qualityLog.ClientIP.ToString();
                EdgeServerIP       = qualityLog.EdgeIP;
                SmoothStreamingUrl = qualityLog.VideoUrl;
                ProcessCPU         = qualityLog.ProcessCPULoad.GetValueOrDefault(0);
                SystemCPU          = qualityLog.SystemCPULoad.GetValueOrDefault(0);

                break;

            case VideoLogTypes.VideoStarted:
            case VideoLogTypes.VideoLoaded:
                VideoLoadLog loadLog = log.CastLog <VideoLoadLog>();

                var bitrateChartData = charts.First(vm => vm.QualityAttribute == VideoLogAttributes.BitRate);
                if (loadLog.MaxBitRate.HasValue)
                {
                    bitrateChartData.MaxValue = loadLog.MaxBitRate.Value;
                }
                ClientIP           = loadLog.ClientIP.ToString();
                EdgeServerIP       = loadLog.EdgeIP;
                SmoothStreamingUrl = loadLog.VideoUrl;
                break;

            case VideoLogTypes.Trace:
                TraceLogs.Add(log.CastLog <TraceLog>());
                break;
            }
        }
        void healthMonitor_EventCreated(object sender, SimpleEventArgs <SmoothStreamingEvent> e)
        {
            try
            {
                switch (e.Result.EventType)
                {
                case EventType.StreamStarted:
                    // we keep this object around for reference
                    var streamStartedLog = new VideoStartLog();
                    PopulateVideoEventLog(streamStartedLog);
                    if (streamLoadedLog != null)
                    {
                        streamStartedLog.EdgeIP     = streamLoadedLog.EdgeIP;
                        streamStartedLog.ClientIP   = streamLoadedLog.ClientIP;
                        streamStartedLog.VideoUrl   = streamLoadedLog.VideoUrl;
                        streamStartedLog.MaxBitRate = streamLoadedLog.MaxBitRate;
                    }
                    streamStartedLog.MediaElementId = e.Result.MediaElementId;
                    streamStartedLog.IsLive         = e.Result.IsLive;
                    SendLog(streamStartedLog);
                    break;

                case EventType.StreamLoaded:
                    // we keep this object around for reference
                    streamLoadedLog = new VideoLoadLog();
                    PopulateVideoEventLog(streamLoadedLog);
                    streamLoadedLog.EdgeIP         = healthMonitor.EdgeServer;
                    streamLoadedLog.ClientIP       = healthMonitor.ClientIP;
                    streamLoadedLog.VideoUrl       = e.Result.Data1;
                    streamLoadedLog.MediaElementId = e.Result.MediaElementId;
                    streamLoadedLog.IsLive         = e.Result.IsLive;
                    {
                        double maxBitRate = 0;
                        if (double.TryParse(e.Result.Data2, out maxBitRate))
                        {
                            streamLoadedLog.MaxBitRate = maxBitRate;
                        }
                    }
                    //streamStartedLog.CDNBlocked = healthMonitor.AnonymousProxy;
                    SendLog(streamLoadedLog);
                    break;

                case EventType.StreamEnded:
                    VideoStopLog streamEndedLog = new VideoStopLog();
                    PopulateVideoEventLog(streamEndedLog);
                    streamEndedLog.MediaElementId = e.Result.MediaElementId;
                    streamEndedLog.IsLive         = e.Result.IsLive;
                    //streamEndedLog.VideoUrl = e.Result.Data1;
                    SendLog(streamEndedLog);
                    break;

                case EventType.ClipStarted:
                    VideoClipStartedLog clipStartLog = new VideoClipStartedLog();
                    PopulateVideoEventLog(clipStartLog);
                    //clipStartLog.VideoUrl = e.Result.Data1;
                    clipStartLog.MediaElementId = e.Result.MediaElementId;
                    clipStartLog.IsLive         = e.Result.IsLive;
                    SendLog(clipStartLog);
                    break;

                case EventType.ClipEnded:
                    VideoClipEndedLog clipEndLog = new VideoClipEndedLog();
                    PopulateVideoEventLog(clipEndLog);
                    clipEndLog.MediaElementId = e.Result.MediaElementId;
                    clipEndLog.IsLive         = e.Result.IsLive;
                    //clipEndLog.VideoUrl = e.Result.Data1;
                    SendLog(clipEndLog);
                    break;

                case EventType.MediaFailed:
                    MediaFailedLog mediaFailed = new MediaFailedLog();
                    PopulateVideoEventLog(mediaFailed);
                    mediaFailed.Reason         = e.Result.Data1;
                    mediaFailed.EdgeIP         = healthMonitor.EdgeServer;
                    mediaFailed.IsLive         = e.Result.IsLive;
                    mediaFailed.MediaElementId = e.Result.MediaElementId;

                    //mediaFailed.VideoUrl = e.Result.Data2;
                    //mediaFailed.VideoTimelineMarker = Convert.ToInt64(e.Result.Data3);
                    SendLog(mediaFailed);
                    break;

                case EventType.FullScreenChanged:
                    Log fullScreenLog;
                    if (Convert.ToBoolean(Convert.ToInt32(e.Result.Value)))
                    {
                        fullScreenLog = CreateVideoLog(VideoLogTypes.FullScreenEntered);
                    }
                    else
                    {
                        fullScreenLog = CreateVideoLog(VideoLogTypes.FullScreenExit);
                    }
                    SendLog(fullScreenLog);
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                LoggingService.Current.BroadcastException(ex);
            }
        }