Example #1
0
        //static MiniProfiler mps = MiniProfiler.Current;

        public string CallSpotifyAPIPassingToken(string access_token, string url)
        {
            using (mp.CustomTiming("http", url)) {
                var client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token);
                var httpResponse = client.GetAsync(url);
                var result       = httpResponse.Result.Content.ReadAsStringAsync().Result;
                return(result);
            }
        }
Example #2
0
        private List <TopTracksVM> GetTopTracks()
        {
            var vm = new List <TopTracksVM>();

            using (var db = DBHelper.GetOpenConnection())
            {
                vm = db.GetList <TopTracksVM>("ORDER BY AlbumDate Desc").ToList();
            }

            // If logged in
            if (Session["AccessToken"] != null)
            {
                ViewBag.access_token = Session["AccessToken"].ToString();

                var access_token = Session["AccessToken"].ToString();

                // User clicks via LoginRedirect to come back here
                // If this is the first time back to this page after logging in, need to get the UserID
                if (Session["UserID"] == null)
                {
                    var    url = "https://api.spotify.com/v1/me";
                    string json;
                    using (mp.CustomTiming("http", url))
                    {
                        var sh = new SpotifyHelper();
                        json = sh.CallSpotifyAPIPassingToken(access_token, url);
                    }

                    var meReponse = JsonConvert.DeserializeObject <MeResponse>(json);
                    meReponse.access_token = access_token;

                    Session["UserID"] = meReponse.id;
                }
                // Grab the userID as we'll use that in our database to remember what a user has selected
                ViewBag.user_id = Session["UserID"];

                // Find out what the user has already added to their playlist
                List <string> listTracksAlreadyAdded;
                var           userID = Session["UserID"];
                using (IDbConnection db = DBHelper.GetOpenConnection())
                {
                    listTracksAlreadyAdded =
                        db.Query <string>("SELECT TrackID FROM UserPlaylists WHERE UserID = @UserID", new { @UserID = userID })
                        .ToList();
                }

                foreach (var track in vm)
                {
                    if (listTracksAlreadyAdded.Contains(track.TrackID))
                    {
                        track.AddedInUserPlaylist = true;
                    }
                }
            }
            return(vm);
        }
        /// <summary>
        /// Gets a <see cref=" CustomTiming"/> for the relational parameters passed.
        /// </summary>
        /// <param name="command">The <see cref="DbCommand"/> to time.</param>
        /// <param name="commandType">The command execution type (e.g. ExecuteNonQuery).</param>
        /// <param name="profiler">The <see cref="MiniProfiler"/> to attach the timing to.</param>
        /// <param name="customType">The type for this command to show up as (which custom column).</param>
        /// <returns>A custom timing (which should be disposed or stopped!) for <paramref name="command"/>.</returns>
        public static CustomTiming GetTiming(this IDbCommand command, string commandType, MiniProfiler profiler, string customType = "sql")
        {
            if (command == null || profiler == null)
            {
                return(null);
            }

            var commandText = profiler.Options.SqlFormatter?.GetFormattedSql(command) ?? command.GetReadableCommand();

            return(profiler.CustomTiming(customType, commandText, commandType));
        }
Example #4
0
        private MiniProfiler GetMiniProfiler()
        {
            var mp = new MiniProfiler("Test");

            using (mp.Step("Foo"))
            {
                using (mp.CustomTiming("Hey", "There"))
                {
                    // heyyyyyyyyy
                }
            }
            return(mp);
        }
Example #5
0
        private static void CallApi(HttpClient client, MiniProfiler profiler)
        {
            using (profiler.CustomTiming("CallApi", "Call cmd/account/create"))
            {
                string json = @"{ Owner: ""Hello world!!!"" }";

                StringContent content;
                using (profiler.CustomTiming("StringContent", "Create http content"))
                    content = new StringContent(json, Encoding.UTF8, "application/json");

                Task <HttpResponseMessage> postTask;
                using (profiler.CustomTiming("PostAsync", "cmd/account/create"))
                    postTask = client.PostAsync("cmd/account/create", content, CancellationToken.None);

                HttpResponseMessage response;
                using (profiler.CustomTiming("WaitResponse", ""))
                    response = postTask.GetAwaiter().GetResult();

                string message;
                using (profiler.CustomTiming("GetMessage", ""))
                    message = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                // Logger.Trace(message);
            }
        }
Example #6
0
        protected MiniProfiler GetMiniProfiler(string name = "Test", [CallerMemberName] string user = null)
        {
            var mp = new MiniProfiler(name, Options)
            {
                User = user
            };

            using (mp.Step("Foo"))
            {
                using (mp.CustomTiming("Hey", "There"))
                {
                    // heyyyyyyyyy
                }
            }
            mp.Stop();
            return(mp);
        }
        /// <summary>
        /// Gets a <see cref=" CustomTiming"/> for the relational parameters passed.
        /// </summary>
        /// <param name="command">The <see cref="DbCommand"/> to time.</param>
        /// <param name="commandType">The command execution type (e.g. ExecuteNonQuery).</param>
        /// <param name="profiler">The miniprofiler to attach the timing to.</param>
        /// <param name="customType">The type for this command to show up as (which custom column).</param>
        /// <returns>A custom timing (which should be disposed or stopped!) for <paramref name="command"/>.</returns>
        public static CustomTiming GetTiming(this IDbCommand command, string commandType, MiniProfiler profiler, string customType = "sql")
        {
            if (command == null || profiler == null)
            {
                return(null);
            }

            var commandText = command.GetReadableCommand();
            var parameters  = command.GetParameters();

            if (MiniProfiler.Settings.SqlFormatter != null)
            {
                commandText = MiniProfiler.Settings.SqlFormatter.GetFormattedSql(commandText, parameters, command);
            }

            return(profiler.CustomTiming(customType, commandText, commandType));
        }
Example #8
0
        private static MiniProfiler GetComplexProfiler()
        {
            var mp = new MiniProfiler("Complex");

            for (var i = 0; i < 50; i++)
            {
                using (mp.Step("Step " + i))
                {
                    for (var j = 0; j < 50; j++)
                    {
                        using (mp.Step("SubStep " + j))
                        {
                            for (var k = 0; k < 50; k++)
                            {
                                using (mp.CustomTiming("Custom " + k, "YOLO!"))
                                {
                                }
                            }
                        }
                    }
                }
            }
            return(mp);
        }
Example #9
0
        internal static MiniProfiler GetComplexProfiler(MiniProfilerBaseOptions options)
        {
            var mp = new MiniProfiler("Complex", options);

            for (var i = 0; i < 50; i++)
            {
                using (mp.Step("Step " + i))
                {
                    for (var j = 0; j < 50; j++)
                    {
                        using (mp.Step("SubStep " + j))
                        {
                            for (var k = 0; k < 50; k++)
                            {
                                using (mp.CustomTiming("Custom " + k, "YOLO!"))
                                {
                                }
                            }
                        }
                    }
                }
            }
            return(mp);
        }
        public void ParentMapping()
        {
            var mp = new MiniProfiler("Test", Options);

            using (mp.Step("Main"))
            {
                using (mp.Step("Sub Step 1"))
                {
                    using (mp.CustomTiming("cat", "Command 1")) {}
                    using (mp.CustomTiming("cat", "Command 2")) {}
                    using (mp.CustomTiming("cat", "Command 3")) {}
                }
                using (mp.Step("Sub Step 2"))
                {
                    using (mp.CustomTiming("cat", "Command 4")) {}
                    using (mp.CustomTiming("cat", "Command 5")) {}
                    using (mp.CustomTiming("cat", "Command 6")) {}
                }
            }
            mp.Stop();
            var json = mp.ToJson();

            var deserialized = MiniProfiler.FromJson(json);
            var root         = deserialized.Root;

            foreach (var t in root.Children)
            {
                Assert.Equal(root, t.ParentTiming);
                Assert.True(root == t.ParentTiming);

                foreach (var tc in t.Children)
                {
                    Assert.Equal(t, tc.ParentTiming);
                    Assert.True(t == tc.ParentTiming);
                }
            }
        }
 public void AddingToMiniProfiler()
 {
     Profiler.CustomTiming("Test", "MyCategory");
 }
Example #12
0
        private async Task <PlaylistSummaryViewModel> GetPlaylistDetailsViewModel(string id, bool doAsync = true)
        {
            ServicePointManager.DefaultConnectionLimit = 5;
            var access_token = Session["AccessToken"].ToString();

            var    url = String.Format("https://api.spotify.com/v1/users/{0}/playlists?limit=50", id);
            string json;

            using (mp.Step("Get Users Playlists first 50")) {
                json = sh.CallSpotifyAPIPassingToken(access_token, url);
            }
            var vm = JsonConvert.DeserializeObject <PlaylistSummaryViewModel>(json);

            var recordsPerPage = 50;

            if (vm.total > recordsPerPage)
            {
                var totalRecords        = vm.total;
                int numberOfTimesToLoop = (totalRecords + recordsPerPage - 1) / recordsPerPage;

                // we've already done the first query to get the total, do skip that
                numberOfTimesToLoop -= 1;
                if (doAsync)
                {
                    using (mp.Step("Async " + numberOfTimesToLoop + " queries"))
                        using (mp.CustomTiming("http", "overall")) {
                            var tasks  = new Task <string> [numberOfTimesToLoop];
                            int offset = 0;
                            for (int i = 0; i < numberOfTimesToLoop; i++)
                            {
                                // start offset at 50
                                offset += recordsPerPage;
                                url     = String.Format("https://api.spotify.com/v1/users/{0}/playlists?limit={2}&offset={1}", id,
                                                        offset, recordsPerPage);
                                tasks[i] = sh.CallSpotifyAPIPassingTokenAsync(access_token, url);
                            }
                            // at this point all tasks will be running at the same time
                            await Task.WhenAll(tasks);
                        }

                    for (int i = 0; i < numberOfTimesToLoop - 1; i++)
                    {
                        var vm2 = JsonConvert.DeserializeObject <PlaylistSummaryViewModel>(json);
                        // merge with vm
                        vm.items = vm.items.Union(vm2.items).ToList();
                    }
                }
                else
                {
                    for (int i = 1; i < numberOfTimesToLoop; i++)
                    {
                        int offset = i * recordsPerPage;
                        url = String.Format("https://api.spotify.com/v1/users/{0}/playlists?limit={2}&offset={1}", id, offset, recordsPerPage);
                        using (mp.Step("Get Users Playlists - " + i)) {
                            json = sh.CallSpotifyAPIPassingToken(access_token, url);
                        }

                        var vm2 = JsonConvert.DeserializeObject <PlaylistSummaryViewModel>(json);
                        // merge with vm
                        vm.items = vm.items.Union(vm2.items).ToList();
                    }
                }
            }
            vm.access_token = access_token;
            return(vm);
        }