<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Xsl;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using StackExchange.DataExplorer.Models;

namespace StackExchange.DataExplorer.Helpers
{
    public enum ResultColumnType
    {
        Default,
        Post,
        User,
        Number,
        Date,
        Text,
        Url, 
        SuggestedEdit
    }

    public class ResultColumnInfo
    {
        public ResultColumnInfo()
        {
            Type = ResultColumnType.Default;
        }

        public string Name { get; set; }

        [JsonConverter(typeof (StringEnumConverter))]
        public ResultColumnType Type { get; set; }
    }

    public class ResultSet
    {
        public ResultSet()
        {
            Columns = new List<ResultColumnInfo>();
            Rows = new List<List<object>>();
        }

        public List<ResultColumnInfo> Columns { get; set; }
        public List<List<object>> Rows { get; set; }
        // the position of the message when we started rendering this result set
        //  required so we can render in text
        public int MessagePosition { get; set; }
    }

    public class QueryResults
    {
        private const int MAX_TEXT_COLUMN_WIDTH = 512;

        private static readonly List<ResultColumnType> nativeTypes = new List<ResultColumnType>
                                                                         {
                                                                             ResultColumnType.Date,
                                                                             ResultColumnType.Default,
                                                                             ResultColumnType.Number,
                                                                             ResultColumnType.Text
                                                                         };

        public QueryResults()
        {
            ResultSets = new List<ResultSet>();
            FirstRun = DateTime.UtcNow.ToString("MMM %d yyyy");
            Messages = "";
        }

        public List<ResultSet> ResultSets { get; set; }

        /// <summary>
        /// Gets and sets the xml query execution plan associated with the query results.
        /// </summary>
        public string ExecutionPlan { get; set; }

        public bool MultiSite { get; set; }
        public bool ExcludeMetas { get; set; }
        public string Messages { get; set; }
        public string Url { get; set; }
        public int SiteId { get; set; }
        public string SiteName { get; set; }
        public int QueryId { get; set; }
        public int MaxResults { get; set; }
        public string FirstRun { get; set; }
        public bool Truncated { get; set; }
        public string Slug { get; set; }
        public bool TextOnly { get; set; }
        public int? ParentId { get; set; }
        public int RevisionId { get; set; }
        public bool FromCache { get; set; }

        [JsonConverter(typeof(IsoDateTimeConverter))]
        public DateTime? Created { get; set; }

        /// <summary>
        /// Execution time in Millisecs
        /// </summary>
        public long ExecutionTime { get; set; }

        public static JsonSerializerSettings GetSettings()
        {
            return new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()};
        }

        public static QueryResults FromJson(string json)
        {
            return JsonConvert.DeserializeObject<QueryResults>(json, GetSettings());
        }

        public QueryResults WithCache(CachedResult cache)
        {
            if (cache == null || cache.Results == null)
            {
                return this;
            }

            this.Messages = cache.Messages;
            this.ResultSets = JsonConvert.DeserializeObject<List<ResultSet>>(cache.Results);
            this.ExecutionPlan = cache.ExecutionPlan;

            return this;
        }

        public QueryResults ToTextResults()
        {
            var results = new QueryResults();
            results.ExecutionTime = ExecutionTime;
            results.FirstRun = FirstRun;
            results.MaxResults = MaxResults;
            results.QueryId = QueryId;
            results.SiteId = SiteId;
            results.SiteName = SiteName;
            results.TextOnly = true;
            results.Truncated = Truncated;
            results.Url = Url;
            results.Slug = Slug;
            results.MultiSite = MultiSite;
            results.ExcludeMetas = ExcludeMetas;
            results.ExecutionPlan = this.ExecutionPlan;
            results.ParentId = ParentId;
            results.RevisionId = RevisionId;
            results.Created = Created;

            results.Messages = FormatTextResults(Messages, ResultSets);

            return results;
        }<commit_msg>multisite:  force results tab for pivot table<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Xsl;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using StackExchange.DataExplorer.Models;

namespace StackExchange.DataExplorer.Helpers
{
    public enum ResultColumnType
    {
        Default,
        Post,
        User,
        Number,
        Date,
        Text,
        Url, 
        SuggestedEdit
    }

    public class ResultColumnInfo
    {
        public ResultColumnInfo()
        {
            Type = ResultColumnType.Default;
        }

        public string Name { get; set; }

        [JsonConverter(typeof (StringEnumConverter))]
        public ResultColumnType Type { get; set; }
    }

    public class ResultSet
    {
        public ResultSet()
        {
            Columns = new List<ResultColumnInfo>();
            Rows = new List<List<object>>();
        }

        public List<ResultColumnInfo> Columns { get; set; }
        public List<List<object>> Rows { get; set; }
        // the position of the message when we started rendering this result set
        //  required so we can render in text
        public int MessagePosition { get; set; }
    }

    public class QueryResults
    {
        private const int MAX_TEXT_COLUMN_WIDTH = 512;

        private static readonly List<ResultColumnType> nativeTypes = new List<ResultColumnType>
                                                                         {
                                                                             ResultColumnType.Date,
                                                                             ResultColumnType.Default,
                                                                             ResultColumnType.Number,
                                                                             ResultColumnType.Text
                                                                         };

        public QueryResults()
        {
            ResultSets = new List<ResultSet>();
            FirstRun = DateTime.UtcNow.ToString("MMM %d yyyy");
            Messages = "";
        }

        public List<ResultSet> ResultSets { get; set; }

        /// <summary>
        /// Gets and sets the xml query execution plan associated with the query results.
        /// </summary>
        public string ExecutionPlan { get; set; }

        public bool MultiSite { get; set; }
        public bool ExcludeMetas { get; set; }
        public string Messages { get; set; }
        public string Url { get; set; }
        public int SiteId { get; set; }
        public string SiteName { get; set; }
        public int QueryId { get; set; }
        public int MaxResults { get; set; }
        public string FirstRun { get; set; }
        public bool Truncated { get; set; }
        public string Slug { get; set; }
        public bool TextOnly { get; set; }
        public int? ParentId { get; set; }
        public int RevisionId { get; set; }
        public bool FromCache { get; set; }

        [JsonConverter(typeof(IsoDateTimeConverter))]
        public DateTime? Created { get; set; }

        /// <summary>
        /// Execution time in Millisecs
        /// </summary>
        public long ExecutionTime { get; set; }

        public static JsonSerializerSettings GetSettings()
        {
            return new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()};
        }

        public static QueryResults FromJson(string json)
        {
            return JsonConvert.DeserializeObject<QueryResults>(json, GetSettings());
        }

        public QueryResults WithCache(CachedResult cache)
        {
            if (cache == null || cache.Results == null)
            {
                return this;
            }

            this.Messages = cache.Messages;
            this.ResultSets = JsonConvert.DeserializeObject<List<ResultSet>>(cache.Results);
            this.ExecutionPlan = cache.ExecutionPlan;

            return this;
        }

        public QueryResults ToTextResults()
        {
            var results = new QueryResults();
            results.ExecutionTime = ExecutionTime;
            results.FirstRun = FirstRun;
            results.MaxResults = MaxResults;
            results.QueryId = QueryId;
            results.SiteId = SiteId;
            results.SiteName = SiteName;
            results.TextOnly = true;
            results.Truncated = Truncated;
            results.Url = Url;
            results.Slug = Slug;
            results.MultiSite = MultiSite;
            results.ExcludeMetas = ExcludeMetas;
            results.ExecutionPlan = this.ExecutionPlan;
            results.ParentId = ParentId;
            results.RevisionId = RevisionId;
            results.Created = Created;

            results.Messages = FormatTextResults(Messages, ResultSets);

            // force the results tab in multi-site mode if it's a pivot table
            if (ResultSets.Count == 1)
            {
                results.ResultSets = ResultSets;
            }

            return results;
        }
{'fraction_non_alphanumeric': 0.07726525481094672, 'fraction_numerical': 0.0006769171260032879, 'mean_word_length': 1.533561979421852, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5007817', 'n_tokens_mistral': 2470, 'n_tokens_neox': 2395, 'n_words': 683}