<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;

using Windows.ApplicationModel.Background;
using Windows.UI.Notifications;
using Windows.Web.Http;

using jcMPP.PCL.Enums;
using jcMPP.PCL.Objects.KeepAlive;
using jcMPP.UWP.Library.PlatformImplementations;

namespace jcMPP.UWP.BackgroundTask {
    public sealed class KeepAliveBackgroundTask : IBackgroundTask {
        private void displayToast(string content) {
            var toastXML = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
            var toastTextElements = toastXML.GetElementsByTagName("text");
            toastTextElements.First().AppendChild(toastXML.CreateTextNode(content));
            var toastNotification = new ToastNotification(toastXML);
            toastNotification.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(30);
            ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
        }

        public async void Run(IBackgroundTaskInstance taskInstance) {
            var baseIO = new UWPFileIO();

            var keepAliveResult = await baseIO.GetFile<List<KeepAliveListingItem>>(ASSET_TYPES.KEEP_ALIVE_LISTING);

            if (keepAliveResult.HasError || !keepAliveResult.Value.Any(a => a.IsEnabled)) {
                return;
            }

            var httpClient = new HttpClient();

            foreach (var item in keepAliveResult.Value.Where(a => a.IsEnabled)) {
                var fullItem = await baseIO.GetFile<KeepAliveItem>(ASSET_TYPES.KEEP_ALIVE_ITEM, objectGUID: item.ID);

                if (fullItem.HasError) {
                    continue;
                }

                var httpResponse = await httpClient.GetStringAsync(new Uri(fullItem.Value.SiteAddress));

                if (!string.IsNullOrEmpty(httpResponse)) {
                    continue;
                }

                displayToast($"{fullItem.Value.SiteAddress} is down");
            }
        }
    }
}<commit_msg>Added: Updating of timestamp entries in the background task<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;

using Windows.ApplicationModel.Background;
using Windows.UI.Notifications;
using Windows.Web.Http;

using jcMPP.PCL.Enums;
using jcMPP.PCL.Objects.KeepAlive;
using jcMPP.UWP.Library.PlatformImplementations;

namespace jcMPP.UWP.BackgroundTask {
    public sealed class KeepAliveBackgroundTask : IBackgroundTask {
        private void displayToast(string content) {
            var toastXML = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
            var toastTextElements = toastXML.GetElementsByTagName("text");
            toastTextElements.First().AppendChild(toastXML.CreateTextNode(content));
            var toastNotification = new ToastNotification(toastXML);
            toastNotification.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(30);
            ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
        }

        public async void Run(IBackgroundTaskInstance taskInstance) {
            var baseIO = new UWPFileIO();

            var keepAliveResult = await baseIO.GetFile<List<KeepAliveListingItem>>(ASSET_TYPES.KEEP_ALIVE_LISTING);

            if (keepAliveResult.HasError || !keepAliveResult.Value.Any(a => a.IsEnabled)) {
                return;
            }

            var httpClient = new HttpClient();
            
            foreach (var item in keepAliveResult.Value.Where(a => a.IsEnabled)) {
                var fullItem = await baseIO.GetFile<KeepAliveItem>(ASSET_TYPES.KEEP_ALIVE_ITEM, objectGUID: item.ID);

                if (fullItem.HasError) {
                    continue;
                }

                var historyItem = new KeepAliveHistoryListingItem {
                    Timestamp = DateTime.Now,
                    Success = true
                };
                
                var httpResponse = await httpClient.GetStringAsync(new Uri(fullItem.Value.SiteAddress));

                if (fullItem.Value.History == null) {
                    fullItem.Value.History = new List<KeepAliveHistoryListingItem>();
                }
                
                if (!string.IsNullOrEmpty(httpResponse)) {
                    continue;
                }

                historyItem.Success = false;

                fullItem.Value.History.Add(historyItem);

                await baseIO.WriteFile(ASSET_TYPES.KEEP_ALIVE_ITEM, fullItem.Value, objectGUID: fullItem.Value.ID);

                item.LastReport = historyItem.Timestamp;

                var index = keepAliveResult.Value.IndexOf(item);

                keepAliveResult.Value[index] = item;
                
                displayToast($"{fullItem.Value.SiteAddress} is down");
            }

            await baseIO.WriteFile(ASSET_TYPES.KEEP_ALIVE_LISTING, keepAliveResult.Value);
        }
    }
}
{'fraction_non_alphanumeric': 0.08545788292881909, 'fraction_numerical': 0.0016316540893330613, 'mean_word_length': 2.3474402730375425, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6266010', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1305, 'n_words': 222}