<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Signum.Utilities;

namespace Signum.Web.Profiler
{
    public class TrackTimeFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var action = filterContext.RouteData.Values["controller"] + "." + filterContext.RouteData.Values["action"];

            ViewDataDictionary viewData = filterContext.Controller.ViewData;

            viewData.Add("elapsed", TimeTracker.Start(action));

            IDisposable profiler = HeavyProfiler.Log(role: "MvcRequest", aditionalData: filterContext.HttpContext.Request.Url.PathAndQuery);
            if (profiler != null)
                viewData.Add("profiler", profiler);
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            if (filterContext.Controller.ViewData.ContainsKey("profiler"))
            {
                IDisposable viewProfiler = HeavyProfiler.Log(role: "MvcResult", aditionalData: filterContext.Result.ToString());
                if (viewProfiler != null)
                    filterContext.Controller.ViewData.Add("viewProfiler", viewProfiler);
            }
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            ViewDataDictionary viewData = filterContext.Controller.ViewData;

            Dispose(viewData, "viewProfiler");
            Dispose(viewData, "profiler");
            Dispose(viewData, "elapsed");
        }

        private void Dispose(ViewDataDictionary viewData, string key)
        {
            IDisposable elapsed = (IDisposable)viewData.TryGetC(key);
            if (elapsed != null)
            {<commit_msg>HeavyProfiler TimeLimit (instead of CountLimit). Fix heavy profiler on MVC requests with exceptions<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Signum.Utilities;

namespace Signum.Web.Profiler
{
    public class TrackTimeFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var action = filterContext.RouteData.Values["controller"] + "." + filterContext.RouteData.Values["action"];

            ViewDataDictionary viewData = filterContext.Controller.ViewData;

            viewData.Add("elapsed", TimeTracker.Start(action));

            IDisposable profiler = HeavyProfiler.Log(role: "MvcRequest", aditionalData: filterContext.HttpContext.Request.Url.PathAndQuery);
            if (profiler != null)
                viewData.Add("profiler", profiler);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.Exception != null)
            {
                ViewDataDictionary viewData = filterContext.Controller.ViewData;
                Dispose(viewData, "profiler");
                Dispose(viewData, "elapsed");
            }

            base.OnActionExecuted(filterContext);
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            if (filterContext.Controller.ViewData.ContainsKey("profiler"))
            {
                IDisposable viewProfiler = HeavyProfiler.Log(role: "MvcResult", aditionalData: filterContext.Result.ToString());
                if (viewProfiler != null)
                    filterContext.Controller.ViewData.Add("viewProfiler", viewProfiler);
            }
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            ViewDataDictionary viewData = filterContext.Controller.ViewData;

            Dispose(viewData, "viewProfiler");
            Dispose(viewData, "profiler");
            Dispose(viewData, "elapsed");
        }

        private void Dispose(ViewDataDictionary viewData, string key)
        {
            IDisposable elapsed = (IDisposable)viewData.TryGetC(key);
            if (elapsed != null)
            {
{'fraction_non_alphanumeric': 0.08907077515647568, 'fraction_numerical': 0.0, 'mean_word_length': 2.581896551724138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '518939', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1056, 'n_words': 202}