Ejemplo n.º 1
0
        public TransferredDataAmountPerHourMetrics(IKCVDBClient client)
            : base("MetricsTitleDataAmountPerHour")
        {
            Subscriptions.Add(Observable.FromEventPattern <ApiDataSentEventArgs>(client, nameof(client.ApiDataSent))
                              .Select(x => x.EventArgs)
                              .Buffer(WindowSpan)
                              .Subscribe(list => {
                try {
                    var now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                    lock (ListLock) {
                        foreach (var e in list)
                        {
                            History.AddLast(new Tuple <long, int>(now, e.SentApiData.PayloadByteCount));
                            isListUpdated_ = true;
                        }
                    }
                }
                catch (Exception ex) {
                    if (ex.IsCritical())
                    {
                        throw;
                    }
                    TelemetryClient.TrackException("Failed to add transferred API info to the list.", ex);
                }
            }));

            Subscriptions.Add(Observable.Timer(DueTime, Period)
                              .Select(x => {
                try {
                    CleanupAndUpdate(DateTimeOffset.Now);
                    return(x);
                }
                catch (Exception ex) {
                    TelemetryClient.TrackException("Failed to update (or clean-up) the history queue.", ex);
                    return(-1);
                }
            })
                              .Where(x => x > 0)
                              .SubscribeOnDispatcher(System.Windows.Threading.DispatcherPriority.Normal)
                              .Subscribe(_ => {
                try {
                    UpdateValueText();
                }
                catch (Exception ex) {
                    if (ex.IsCritical())
                    {
                        throw;
                    }
                    TelemetryClient.TrackException("Failed to update transferred data mount per hour metrics.", ex);
                }
            }));

            UpdateValueText();
        }
Ejemplo n.º 2
0
        public ApiSender(string sessionId)
        {
            Disposable.Add(KanColleClient.Current.Proxy.ApiSessionSource
                           .Subscribe(OnSession));

            KcvdbClient = KCVDBClientService.Instance.CreateClient(Constants.KCVDB.AgentId, sessionId);
            Disposable.Add(KcvdbClient);

            KcvdbClient.FatalError    += KcvdbClient_FatalError;
            KcvdbClient.InternalError += KcvdbClient_InternalError;
            KcvdbClient.SendingError  += KcvdbClient_SendingError;
        }
Ejemplo n.º 3
0
        public FailureCountMetrics(IKCVDBClient client)
            : base("MetricsTitleFailureCount")
        {
            Subscriptions.Add(Observable.FromEventPattern <SendingErrorEventArgs>(client, nameof(client.SendingError))
                              .Select(x => x.EventArgs)
                              .Buffer(WindowSpan)
                              .SubscribeOnDispatcher(System.Windows.Threading.DispatcherPriority.Normal)
                              .Subscribe(list => {
                try {
                    count_ += list.Count;
                    UpdateValueText();
                }
                catch (Exception ex) {
                    if (ex.IsCritical())
                    {
                        throw;
                    }
                    TelemetryClient.TrackException("Failed to update failure count metrics.", ex);
                }
            }));

            UpdateValueText();
        }
        public TransferredDataAmountMetrics(IKCVDBClient client)
            : base("MetricsTitleTotalDataAmountSent")
        {
            Subscriptions.Add(Observable.FromEventPattern <ApiDataSentEventArgs>(client, nameof(client.ApiDataSent))
                              .Select(x => x.EventArgs)
                              .Buffer(WindowSpan)
                              .SubscribeOnDispatcher(System.Windows.Threading.DispatcherPriority.Normal)
                              .Subscribe(list => {
                try {
                    bytesSent_ += list.Sum(e => e.SentApiData.PayloadByteCount);
                    UpdateValueText();
                }
                catch (Exception ex) {
                    if (ex.IsCritical())
                    {
                        throw;
                    }
                    TelemetryClient.TrackException("Failed to update transferred data amount metrics.", ex);
                }
            }));

            UpdateValueText();
        }
        public ToolViewViewModel(
            IKCVDBClient client,
            string sessionId,
            IDispatcher dispatcher)
            : base(dispatcher)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (sessionId == null)
            {
                throw new ArgumentNullException(nameof(sessionId));
            }
            Client    = client;
            SessionId = sessionId;

            // Register metrics
            CustomMetrics.Add(new StartedTimeMetrics(DateTimeOffset.Now));
            CustomMetrics.Add(new TransferredApiCountMetrics(Client));
            CustomMetrics.Add(new SuccessCountMetrics(Client));
            CustomMetrics.Add(new FailureCountMetrics(Client));
            CustomMetrics.Add(new TransferredDataAmountMetrics(Client));
            CustomMetrics.Add(new TransferredDataAmountPerHourMetrics(Client));


            // Register a listener to update history
            Observable.FromEventPattern <ApiDataSentEventArgs>(Client, nameof(Client.ApiDataSent))
            .Select(x => x.EventArgs)
            .Select(x => {
                var now = DateTimeOffset.Now;
                return(x.ApiData.Select(apiData => new HistoryItem {
                    Time = now,
                    Body = apiData.RequestUri.PathAndQuery,
                    Success = true,
                }));
            })
            .SubscribeOnDispatcher(System.Windows.Threading.DispatcherPriority.Normal)
            .Subscribe(historyItems => {
                try {
                    HistoryItems = new ObservableCollection <HistoryItem>(
                        historyItems.Reverse().Concat(HistoryItems).Take(MaxHistoryCount));
                }
                catch (Exception ex) {
                    TelemetryClient.TrackException("Failed to update history.", ex);
                    if (ex.IsCritical())
                    {
                        throw;
                    }
                }
            });

            // Register a listener to notify chinese option chaning
            Subscriptions.Add(Observable.FromEventPattern <PropertyChangedEventArgs>(Settings.Default, nameof(Settings.PropertyChanged))
                              .Where(x => x.EventArgs.PropertyName == nameof(Settings.ShowTraditionalChinese))
                              .SubscribeOnDispatcher()
                              .Subscribe(_ => {
                try {
                    RaisePropertyChanged(nameof(EnableSendingTelemetry));
                    RaisePropertyChanged(nameof(ShowTraditionalChinese));
                }
                catch (Exception ex) {
                    TelemetryClient.TrackException("Failed to raise property chaning of show traditional chinese option", ex);
                    if (ex.IsCritical())
                    {
                        throw;
                    }
                }
            }));

            // Register a listener to receive language switching event
            Subscriptions.Add(Observable.FromEventPattern <PropertyChangedEventArgs>(ResourceHolder.Instance, nameof(ResourceHolder.PropertyChanged))
                              .Where(x => x.EventArgs.PropertyName == nameof(ResourceHolder.Culture))
                              .SubscribeOnDispatcher()
                              .Subscribe(_ => {
                try {
                    CurrentLanguageTwoLetterName = ResourceHolder.Instance.Culture?.TwoLetterISOLanguageName;
                }
                catch (Exception ex) {
                    TelemetryClient.TrackException("Failed to update current language two letter name.", ex);
                    if (ex.IsCritical())
                    {
                        throw;
                    }
                }
            }));

            CurrentLanguageTwoLetterName = ResourceHolder.Instance.Culture?.TwoLetterISOLanguageName;
        }