{"id":6140,"date":"2020-04-21T13:52:46","date_gmt":"2020-04-21T13:52:46","guid":{"rendered":"https:\/\/max-drake.cc\/?p=6140"},"modified":"2020-04-21T13:52:51","modified_gmt":"2020-04-21T13:52:51","slug":"apis-structure-speed-issues-with-covid-map","status":"publish","type":"post","link":"https:\/\/max-drake.cc\/?p=6140","title":{"rendered":"API&#8217;s, structure &#038; speed issues with Covid Map"},"content":{"rendered":"\n<p>Developing my covid map using API data pulled in in JSON format has caused me some grief. I have spent a couple of days with no progress at all trying to get variable data pushed through a function to use in a JS Template literal. In the end I had to wrap the whole thing in a function to make it work, some very, very messy code. <\/p>\n\n\n\n<p>I finally got some of the things working and then I compared load times with the original map and charts below. 1\/The first image is the<strong><span style=\"color:#cf2e2e\" class=\"color\"><a href=\"https:\/\/pir2.tk\/web\/data\/covid\/covidmap.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" original file (opens in a new tab)\"> original file<\/a><\/span><\/strong> that has the data in the file itself, I rebuild it daily with updated data and then put it on my server. It takes <strong><span style=\"color:#cf2e2e\" class=\"color\">2.18s to load<\/span><\/strong> and calls a chart &amp; table from amCharts.js. 2\/ The second image is the<strong><span style=\"color:#cf2e2e\" class=\"color\"> <a href=\"https:\/\/pir2.tk\/web\/data\/covid\/covidmapAPI.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"map from calling API data (opens in a new tab)\">map from calling API data<\/a><\/span><\/strong>, downloading it as JSON and  processing that and rendering it. It calls 3 api&#8217;s for the data then generates the resulting page. There is only the map on the page so far. It takes <strong><span style=\"color:#cf2e2e\" class=\"color\">4.18s to render<\/span><\/strong>, almost <strong><span style=\"color:#cf2e2e\" class=\"color\"><span style=\"background-color:#fcb900\" class=\"background-color\">2 x as long <\/span><\/span><\/strong>as the HTML page.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"572\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-1024x572.jpg\" alt=\"\" class=\"wp-image-6141 lazyload\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-1024x572.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-300x168.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-768x429.jpg 768w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-1536x859.jpg 1536w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-50x28.jpg 50w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-89x50.jpg 89w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-100x56.jpg 100w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-179x100.jpg 179w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-1145x640.jpg 1145w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-640x358.jpg 640w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type-1374x768.jpg 1374w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_57-Transmission-Type.jpg 1909w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/572;\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"582\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--1024x582.jpg\" alt=\"\" class=\"wp-image-6142 lazyload\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--1024x582.jpg 1024w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--300x171.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--768x437.jpg 768w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--1536x873.jpg 1536w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--50x28.jpg 50w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--88x50.jpg 88w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--100x57.jpg 100w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--176x100.jpg 176w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--1126x640.jpg 1126w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--640x364.jpg 640w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--1351x768.jpg 1351w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40--1900x1080.jpg 1900w, https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40-.jpg 1912w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/582;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Build or call API?<\/h3>\n\n\n\n<p>The HTML website is definitely faster as it has everything on hand. The API has to go fetch the data before it can display it, but it will do it when needed, so no need to rebuild the page with fresh data daily. More automated.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">API info<\/h4>\n\n\n\n<p>I looked at building my own API by scraping the web to get the data, see this article <a href=\"https:\/\/max-drake.cc\/2020\/04\/12\/web-scraping-with-browser-development-console-javascript\/\">Web Scraping with Browser development console &amp; javaScript<\/a>.<\/p>\n\n\n\n<p>It was a bit like what I was doing with Excel in that I had to go and scrape the data daily and add that to my API JSON file, so on reflection I used some API data that had already been scraped by others. Dixon Cheng allowed me to use his API endpoints for his <a rel=\"noreferrer noopener\" aria-label=\"NZ Covid Map. (opens in a new tab)\" href=\"https:\/\/covid19map.nz\/\" target=\"_blank\"><strong>NZ Covid Map<\/strong>.<\/a> <br>One of his endpoints is  https:\/\/api.covid19map.nz\/summary.json .<\/p>\n\n\n\n<p>Now, using his API&#8217;s the data is structured in the method that he wants it. For me, I am having to make longer functions\/formulas to reach in to grab the data, so first I&#8217;m making a call to the API, then I&#8217;m finding its length at the top level, then I&#8217;m getting an objects length at the next level, then I&#8217;m finding the last one in the array then I&#8217;m getting an item in that last array. So a lot of drilling down and very inefficient calls, this is why its taking a long time for the page to load. <\/p>\n\n\n\n<p>I wonder if I just find the last items and make an array and then interrogate that array, it may speed the process up. So load api&#8217;s, put data into arrays then use arrays. But I only want to load the minimal onto the site or that will slow it down. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">API building with Python<\/h4>\n\n\n\n<p>When looking at building an API (basically scraping and collecting data for use, I&#8217;d thought of using JvaScript as I was consuming the api woth JS. On reflection I think I&#8217;d look to use Python to do this andimport something like &#8220;beutiful soup&#8221; to do the scraping. <\/p>\n\n\n\n<p>That is maybe a follow on project that I want to explore. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">End comment<\/h3>\n\n\n\n<p>I&#8217;ve got bogged down cleaning code for my API calling, and in fact I&#8217;m calling more data than I need currently to the page.  <\/p>\n\n\n\n<p>I did a video about the issues here:<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"How NOT to do JS API &amp; Template literal web page\" width=\"678\" height=\"381\" data-src=\"https:\/\/www.youtube.com\/embed\/nVJWxHTNIuQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" data-load-mode=\"1\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>I have had some thoughts about ways to spered up the process, and may explore these in time, as listed below: <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Have API files on Server, so reading local file rather than calling over the web?<\/h4>\n\n\n\n<p>An automated method to copy the API file from their current location and put them on my server. Something like Automation workshop to do so , with a time set for doing it daily, so file is always updated. That process I haven&#8217;t explored yet. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Rebuild API&#8217;s to make them more efficient?<\/h4>\n\n\n\n<p>Is there a way to automate rebuilding the original api&#8217;s so that only relevant data is in a smaller JSON file, that way less load time. An automated method to restructure JSON files on the server?<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. Databases?<\/h4>\n\n\n\n<p>Another method is to import API data into a Database on the server, if that is refreshed at intervals then the latest data is in a table, on a database on the same server as the <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Developing my covid map using API data pulled in in JSON format has caused me some grief. I have spent a couple of days with no progress at all trying to get variable data pushed through a function to use in a JS Template literal. In the end I had to wrap the whole thing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6142,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,43,6,12,29],"tags":[],"class_list":["post-6140","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-api_json","category-javascript","category-maps","category-visualisation","category-web"],"featured_image_src":"https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40-.jpg","featured_image_src_square":"https:\/\/max-drake.cc\/wp-content\/uploads\/2020\/04\/2020-04-18-09_42_40-.jpg","author_info":{"display_name":"Max Drake","author_link":"https:\/\/max-drake.cc\/?author=1"},"_links":{"self":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts\/6140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6140"}],"version-history":[{"count":0,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts\/6140\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/media\/6142"}],"wp:attachment":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}