<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;
using Kudu.Client.Editor;
using Microsoft.WindowsAzure.Management.WebSites.Models;
using Newtonsoft.Json;
using SimpleWAWS.Kudu;

namespace SimpleWAWS.Code
{
    public class Site
    {
        private WebSpace _webSpace;
        private WebSite _webSite;
        private WebSiteGetConfigurationResponse _config;
        private WebSiteGetPublishProfileResponse _publishingProfile;

        private const string IsSimpleWAWSKey = "SIMPLE_WAWS";
        private const string UserIdMetadataKey = "USERID";
        private const string SiteUniqueIdMetadataKey = "SITEUNIQUEID";

        public Site(WebSpace webSpace, WebSite webSite)
        {
            _webSpace = webSpace;
            _webSite = webSite;
            if (_webSite.EnabledHostNames.Count < 2)
            {
                Trace.TraceError("Odd bug, we get an incomplete site object. see comment in LoadAndCreateSitesAsync()");
            }
        }

        public Site(WebSite webSite, WebSiteGetConfigurationResponse config)
        {
            _webSite = webSite;
            _config = config;
        }

        public async Task LoadConfigurationAsync()
        {
            _config = await _webSpace.GetConfigurationAsync(Name);
            _publishingProfile = await _webSpace.GetPublishingProfile(Name);
        }

        public async Task InitializeNewSite()
        {
            var updateParams = Util.CreateWebSiteUpdateConfigurationParameters();

            // Mark it as our site
            updateParams.Metadata = new Dictionary<string, string> {
                {IsSimpleWAWSKey, "1"},
                {SiteUniqueIdMetadataKey, Guid.NewGuid().ToString()}
            };

            // Turn on Monaco
            updateParams.AppSettings = new Dictionary<string, string> {
                {"WEBSITE_NODE_DEFAULT_VERSION", "0.10.32"},
                {"MONACO_EXTENSION_VERSION", "beta"},
                {"WEBSITE_TRY_MODE", "1"}
            };

            // Mark site's ScmType as LocalGit so that cloning an empty repo gives back the site content
            updateParams.ScmType = "LocalGit";

            await _webSpace.UpdateConfigurationAsync(Name, updateParams);
            Trace.TraceInformation("Updated initial config for site '{0}' in {1}", this, _webSpace);

            // Get all the configuration
            await LoadConfigurationAsync();<commit_msg>when reading sites from Antares, if the site doesn't have MONACO_EXTENSION_VERSION or WEBSITE_TRY_MODE make sure to add it<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;
using Kudu.Client.Editor;
using Microsoft.WindowsAzure.Management.WebSites.Models;
using Newtonsoft.Json;
using SimpleWAWS.Kudu;

namespace SimpleWAWS.Code
{
    public class Site
    {
        private WebSpace _webSpace;
        private WebSite _webSite;
        private WebSiteGetConfigurationResponse _config;
        private WebSiteGetPublishProfileResponse _publishingProfile;

        private const string IsSimpleWAWSKey = "SIMPLE_WAWS";
        private const string UserIdMetadataKey = "USERID";
        private const string SiteUniqueIdMetadataKey = "SITEUNIQUEID";

        public Site(WebSpace webSpace, WebSite webSite)
        {
            _webSpace = webSpace;
            _webSite = webSite;
            if (_webSite.EnabledHostNames.Count < 2)
            {
                Trace.TraceError("Odd bug, we get an incomplete site object. see comment in LoadAndCreateSitesAsync()");
            }
        }

        public Site(WebSite webSite, WebSiteGetConfigurationResponse config)
        {
            _webSite = webSite;
            _config = config;
        }

        public async Task LoadConfigurationAsync()
        {
            _config = await _webSpace.GetConfigurationAsync(Name);

            // This is done to make sure every site we read has Monaco enabled.
            // Otherwise the users will hit a 404 when they browse to Monaco
            var updateConfig = false;
            if (!_config.AppSettings.ContainsKey("MONACO_EXTENSION_VERSION"))
            {
                _config.AppSettings.Add("MONACO_EXTENSION_VERSION", "beta");
                updateConfig = true;
            }

            if (!_config.AppSettings.ContainsKey("WEBSITE_TRY_MODE"))
            {
                _config.AppSettings.Add("WEBSITE_TRY_MODE", "1");
                updateConfig = true;
            }

            if (updateConfig)
            {
                var updateParams = Util.CreateWebSiteUpdateConfigurationParameters();
                updateParams.Metadata = _config.Metadata;
                updateParams.AppSettings = _config.AppSettings;
                await _webSpace.UpdateConfigurationAsync(Name, updateParams);
            }

            _publishingProfile = await _webSpace.GetPublishingProfile(Name);
        }

        public async Task InitializeNewSite()
        {
            var updateParams = Util.CreateWebSiteUpdateConfigurationParameters();

            // Mark it as our site
            updateParams.Metadata = new Dictionary<string, string> {
                {IsSimpleWAWSKey, "1"},
                {SiteUniqueIdMetadataKey, Guid.NewGuid().ToString()}
            };

            // Turn on Monaco
            updateParams.AppSettings = new Dictionary<string, string> {
                {"WEBSITE_NODE_DEFAULT_VERSION", "0.10.32"},
                {"MONACO_EXTENSION_VERSION", "beta"},
                {"WEBSITE_TRY_MODE", "1"}
            };

            // Mark site's ScmType as LocalGit so that cloning an empty repo gives back the site content
            updateParams.ScmType = "LocalGit";

            await _webSpace.UpdateConfigurationAsync(Name, updateParams);
            Trace.TraceInformation("Updated initial config for site '{0}' in {1}", this, _webSpace);

            // Get all the configuration
            await LoadConfigurationAsync();
{'fraction_non_alphanumeric': 0.07893522080320049, 'fraction_numerical': 0.0036928758270503154, 'mean_word_length': 2.321410321921308, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '2813572', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1652, 'n_words': 430}