<commit_before>using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using log4net;
using Google.Cloud.Storage.V1;

namespace AsimovDeploy.WinAgentUpdater 
{
    public class UpdateInfoCollectorGCP
    {
        private static ILog _log = LogManager.GetLogger(typeof(UpdateInfoCollectorGCP));

        private readonly string _watchFolder;
        private readonly int _port;
        private readonly string bucketName;

        public UpdateInfoCollectorGCP(string watchFolder, int port)
        {
            _port = port;
            _watchFolder = watchFolder;
            bucketName = ParsePath();
        }
        private string ParsePath()
        {
            var regexgs = new Regex(@"(gs:)//(?<bucket>\S*)/", RegexOptions.IgnoreCase);
            var matchgs = regexgs.Match(_watchFolder);
            return matchgs.Result("${bucket}");
        }

        public UpdateInfo Collect()
        {
            return new UpdateInfo()
            {
                LastBuild = GetLatestVersion(),
                LastConfig = GetLatestBuild(),
                Current = new CurrentBuild(_port).GetCurrentBuild()
            };
        }

        private AsimovConfigUpdate GetLatestBuild()
        {
            var storage = StorageClient.Create();

            var regex = new Regex(@"(?<fileName>AsimovDeploy.WinAgent.ConfigFiles-Version-(\d+).zip)");
            var list = new List<AsimovConfigUpdate>();

            foreach (var bucket in storage.ListObjects(bucketName, "WinagentPackages"))
            {
                var match = regex.Match(bucket.Name);
                if (match.Success)
                {

                    list.Add(new AsimovConfigUpdate()
                    {
                        Version = int.Parse(match.Groups[1].Value),
                        FileSource = new GcpAsimovFileSource(bucket, storage)
                    });
                }
            }

            return list.OrderByDescending(x => x.Version).FirstOrDefault();
        }

        private AsimovVersion GetLatestVersion()
        {
            var storage = StorageClient.Create();

            var pattern = @"v(?<major>\d+)\.(?<minor>\d+)\.(?<build>\d+)";
            var regex = new Regex(pattern);
            var list = new List<AsimovVersion>();
            foreach (var bucket in storage.ListObjects(bucketName, "WinagentPackages"))<commit_msg>Make google storage prefix for config files configurable in win agent updater<commit_after>using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using log4net;
using Google.Cloud.Storage.V1;

namespace AsimovDeploy.WinAgentUpdater 
{
    public class UpdateInfoCollectorGCP
    {
        private static ILog _log = LogManager.GetLogger(typeof(UpdateInfoCollectorGCP));
        readonly Regex GoogleStorageUri = new Regex(@"(gs:)//(?<bucket>\S*)/(?<prefix>.*)", RegexOptions.IgnoreCase);


        private readonly int _port;
        private readonly string _bucket;
        private readonly string _prefix;

        public UpdateInfoCollectorGCP(string watchFolder, int port)
        {
            _port = port;
            var matchGoogleStorageUri = GoogleStorageUri.Match(watchFolder);
            if (!matchGoogleStorageUri.Success)
            {
                throw new ArgumentException("Watch folder is not a valid google storage uri. Expected an uri of the for gs://bucket/prefix, was " + watchFolder,nameof(watchFolder));
            }
            _bucket = matchGoogleStorageUri.Result("${bucket}");
            _prefix = matchGoogleStorageUri.Result("${prefix}");
        }

        public UpdateInfo Collect()
        {
            return new UpdateInfo()
            {
                LastBuild = GetLatestVersion(),
                LastConfig = GetLatestBuild(),
                Current = new CurrentBuild(_port).GetCurrentBuild()
            };
        }

        private AsimovConfigUpdate GetLatestBuild()
        {
            var storage = StorageClient.Create();

            var regex = new Regex(@"(?<fileName>AsimovDeploy.WinAgent.ConfigFiles-Version-(\d+).zip)");
            var list = new List<AsimovConfigUpdate>();

            foreach (var bucket in storage.ListObjects(_bucket, _prefix))
            {
                var match = regex.Match(bucket.Name);
                if (match.Success)
                {

                    list.Add(new AsimovConfigUpdate()
                    {
                        Version = int.Parse(match.Groups[1].Value),
                        FileSource = new GcpAsimovFileSource(bucket, storage)
                    });
                }
            }

            return list.OrderByDescending(x => x.Version).FirstOrDefault();
        }

        private AsimovVersion GetLatestVersion()
        {
            var storage = StorageClient.Create();

            var pattern = @"v(?<major>\d+)\.(?<minor>\d+)\.(?<build>\d+)";
            var regex = new Regex(pattern);
            var list = new List<AsimovVersion>();
            foreach (var bucket in storage.ListObjects(_bucket, _prefix))
{'fraction_non_alphanumeric': 0.10182286871662677, 'fraction_numerical': 0.0011047689191677407, 'mean_word_length': 2.03463687150838, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 20, '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': '3106317', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1377, 'n_words': 312}