<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NLog;
using Solutionizer.Services;

namespace Solutionizer.Infrastructure {
    public interface IUpdateManager {
        Task CheckForUpdatesAsync();
        IReadOnlyCollection<ReleaseInfo> Releases { get; }
        event EventHandler UpdatesAvailable;
        Task<string> DownloadReleaseAsync(ReleaseInfo releaseInfo, Action<int> downloadProgressCallback, CancellationToken cancellationToken);
    }

    public class UpdateManager : IUpdateManager {
        private readonly Version _currentVersion;
        private static readonly Logger _log = LogManager.GetCurrentClassLogger();

        private readonly IReleaseProvider _reader;
        private readonly List<ReleaseInfo> _releases = new List<ReleaseInfo>();

        public UpdateManager(ISettings settings) {
            _currentVersion = AppEnvironment.CurrentVersion;
            //_reader = new FakeReleaseProvider();
            _reader = new GithubReleaseProvider(settings);

            _releases = LoadReleases();
            _releases.ForEach(r => r.IsNew = r.Version > _currentVersion);
        }

        public async Task CheckForUpdatesAsync() {
            _log.Debug("Checking for updates.");
            IReadOnlyCollection<ReleaseInfo> newReleases;
            try {
                newReleases = await _reader.GetReleaseInfosAsync();
            } catch (Exception ex) {
                _log.ErrorException("Getting release informations failed", ex);
                return;
            }

            // remove re-published versions
            foreach (var release in newReleases) {
                _releases.RemoveAll(r => r.Version == release.Version);
            }

            _releases.AddRange(newReleases);
            _releases.ForEach(r => r.IsNew = r.Version > _currentVersion);
            SaveReleases();
            if (_releases.Any(r => r.IsNew)) {
                _log.Debug("{0} new updates detected.", _releases.Count(r => r.IsNew));<commit_msg>proper sorting of updates<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NLog;
using Solutionizer.Services;

namespace Solutionizer.Infrastructure {
    public interface IUpdateManager {
        Task CheckForUpdatesAsync();
        IReadOnlyCollection<ReleaseInfo> Releases { get; }
        event EventHandler UpdatesAvailable;
        Task<string> DownloadReleaseAsync(ReleaseInfo releaseInfo, Action<int> downloadProgressCallback, CancellationToken cancellationToken);
    }

    public class UpdateManager : IUpdateManager {
        private readonly Version _currentVersion;
        private static readonly Logger _log = LogManager.GetCurrentClassLogger();

        private readonly IReleaseProvider _reader;
        private readonly List<ReleaseInfo> _releases = new List<ReleaseInfo>();

        public UpdateManager(ISettings settings) {
            _currentVersion = AppEnvironment.CurrentVersion;
            //_reader = new FakeReleaseProvider();
            _reader = new GithubReleaseProvider(settings);

            _releases = LoadReleases();
            _releases.Sort((r1, r2) => r2.Version.CompareTo(r1.Version));
            _releases.ForEach(r => r.IsNew = r.Version > _currentVersion);
        }

        public async Task CheckForUpdatesAsync() {
            _log.Debug("Checking for updates.");
            IReadOnlyCollection<ReleaseInfo> newReleases;
            try {
                newReleases = await _reader.GetReleaseInfosAsync();
            } catch (Exception ex) {
                _log.ErrorException("Getting release informations failed", ex);
                return;
            }

            // remove re-published versions
            foreach (var release in newReleases) {
                _releases.RemoveAll(r => r.Version == release.Version);
            }

            _releases.AddRange(newReleases);
            _releases.Sort((r1, r2) => r2.Version.CompareTo(r1.Version));
            _releases.ForEach(r => r.IsNew = r.Version > _currentVersion);
            SaveReleases();
            if (_releases.Any(r => r.IsNew)) {
                _log.Debug("{0} new updates detected.", _releases.Count(r => r.IsNew));
{'fraction_non_alphanumeric': 0.08922101449275362, 'fraction_numerical': 0.0022644927536231885, 'mean_word_length': 2.6086601307189543, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6071569', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1085, 'n_words': 262}