<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data; 
using System.Reflection; 
using System.Web;
using Glimpse.AspNet.Extensibility;
using Glimpse.Core.Extensibility;
using Glimpse.Core.Extensions;
using Glimpse.Core.Tab.Assist;
using Glimpse.WebForms.Model;

namespace Glimpse.WebForms.Tab
{
    public class ControlTree : AspNetTab, ITabLayout, IKey
    {
        private static readonly object Layout = TabLayout.Create()
                .Row(r =>
                {
                    r.Cell("controlId").AsKey().WithTitle("Control ID");
                    r.Cell("type").WithTitle("Type");
                    r.Cell("renderSize").Class("mono").AlignRight().WidthInPixels(150).WithTitle("Render (w/ children)").Suffix(" Bytes");
                    r.Cell("viewstateSize").Class("mono").AlignRight().WidthInPixels(125).WithTitle("ViewState").Suffix(" Bytes");
                    r.Cell("controlstateSize").Class("mono").AlignRight().WidthInPixels(125).WithTitle("ControlState").Suffix(" Bytes");
                }).Build();

        public override string Name
        {
            get { return "Control Tree"; }
        }

        public string Key
        {
            get { return "glimpse_webforms_controltree"; }
        }

        public override RuntimeEvent ExecuteOn
        {
            get { return RuntimeEvent.BeginSessionAccess | RuntimeEvent.EndRequest; }
        }

        public object GetLayout()
        {
            return Layout;
        }

        public override object GetData(ITabContext context)
        { 
            var trace = HttpContext.Current.Trace;

            var hasRun = context.TabStore.Get("hasRun");
            if (hasRun == null)
            {
                context.TabStore.Set("hasRun", "true");

                var previouslyEnabled = trace.IsEnabled;
                 
                trace.IsEnabled = true;
                trace.TraceFinished += (sender, args) => trace.IsEnabled = previouslyEnabled; 
 
                var traceContextVerifyStartMethod = typeof(System.Web.TraceContext).GetMethod("VerifyStart", BindingFlags.Instance | BindingFlags.NonPublic);
                traceContextVerifyStartMethod.Invoke(trace, null);

                return null;
            }

            var requestDataField = typeof(System.Web.TraceContext).GetField("_requestData", BindingFlags.Instance | BindingFlags.NonPublic);
            if (requestDataField != null) 
            { 
                var requestData = requestDataField.GetValue(trace) as DataSet;
                if (requestData != null)
                {
                    var treeData = ProcessData(requestData.Tables["Trace_Control_Tree"]);
                    return treeData;
                }
            }
             
            return null;
        }

        private object ProcessData(DataTable dataTable)
        {
            if (dataTable != null)
            {
                var result = new List<ControlTreeItem>();
                var treeTracker = new Dictionary<string, int>();

                var enumerator = dataTable.Rows.GetEnumerator();<commit_msg>Minor optimization which ensures reflection logic doesn't run more than required<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data; 
using System.Reflection; 
using System.Web;
using Glimpse.AspNet.Extensibility;
using Glimpse.Core.Extensibility;
using Glimpse.Core.Extensions;
using Glimpse.Core.Tab.Assist;
using Glimpse.WebForms.Model;

namespace Glimpse.WebForms.Tab
{
    public class ControlTree : AspNetTab, ITabLayout, IKey
    {
        private static readonly MethodInfo traceContextVerifyStartMethod = typeof(System.Web.TraceContext).GetMethod("VerifyStart", BindingFlags.Instance | BindingFlags.NonPublic);
        private static readonly FieldInfo requestDataField = typeof(System.Web.TraceContext).GetField("_requestData", BindingFlags.Instance | BindingFlags.NonPublic);

        private static readonly object Layout = TabLayout.Create()
                .Row(r =>
                {
                    r.Cell("controlId").AsKey().WithTitle("Control ID");
                    r.Cell("type").WithTitle("Type");
                    r.Cell("renderSize").Class("mono").AlignRight().WidthInPixels(150).WithTitle("Render (w/ children)").Suffix(" Bytes");
                    r.Cell("viewstateSize").Class("mono").AlignRight().WidthInPixels(125).WithTitle("ViewState").Suffix(" Bytes");
                    r.Cell("controlstateSize").Class("mono").AlignRight().WidthInPixels(125).WithTitle("ControlState").Suffix(" Bytes");
                }).Build();

        public override string Name
        {
            get { return "Control Tree"; }
        }

        public string Key
        {
            get { return "glimpse_webforms_controltree"; }
        }

        public override RuntimeEvent ExecuteOn
        {
            get { return RuntimeEvent.BeginSessionAccess | RuntimeEvent.EndRequest; }
        }

        public object GetLayout()
        {
            return Layout;
        }

        public override object GetData(ITabContext context)
        { 
            var trace = HttpContext.Current.Trace;

            var hasRun = context.TabStore.Get("hasRun");
            if (hasRun == null)
            {
                context.TabStore.Set("hasRun", "true");

                var previouslyEnabled = trace.IsEnabled;
                
                //Turn tracing on at the begining of the request
                trace.IsEnabled = true;

                //When request is finished, lets put things back to the way they where 
                trace.TraceFinished += (sender, args) => trace.IsEnabled = previouslyEnabled; 
 
                //Make sure sate of trace is setup
                traceContextVerifyStartMethod.Invoke(trace, null);

                return null;
            }

            if (requestDataField != null) 
            { 
                var requestData = requestDataField.GetValue(trace) as DataSet;
                if (requestData != null)
                {
                    var treeData = ProcessData(requestData.Tables["Trace_Control_Tree"]);
                    return treeData;
                }
            }
             
            return null;
        }

        private object ProcessData(DataTable dataTable)
        {
            if (dataTable != null)
            {
                var result = new List<ControlTreeItem>();
                var treeTracker = new Dictionary<string, int>();

                var enumerator = dataTable.Rows.GetEnumerator();
{'fraction_non_alphanumeric': 0.10086586662615829, 'fraction_numerical': 0.0027343156615524835, 'mean_word_length': 1.9236234458259325, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6333939', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1619, 'n_words': 346}