1
0

index.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <html>
  2. <head>
  3. <script>
  4. // Bed
  5. function importRssFeed (feedUrl) {
  6. var rssRequest = new XMLHttpRequest();
  7. rssRequest.onreadystatechange = function (e) {
  8. var r = rssRequest;
  9. if (r.readyState == XMLHttpRequest.DONE) {
  10. if (r.status >= 200 && r.status < 300) {
  11. console.log("Got RSS (" + r.status + "): ");
  12. var items = importRSSDoc(r.responseXML);
  13. populateItems( items );
  14. createPlot( items );
  15. importTvFeed( "https://harlanji.com/tv.xml" );
  16. }
  17. }
  18. };
  19. //rssRequest.open("GET", "bed-harlanji.xml");
  20. rssRequest.open("GET", feedUrl);
  21. rssRequest.send();
  22. }
  23. function importRSSDoc( rssDoc ) {
  24. console.log("importRSSDoc:");
  25. console.log( rssDoc );
  26. var feedLink = rssDoc.querySelector("link").textContent.trim();
  27. var items = Array.from( rssDoc.querySelectorAll("item") );
  28. items = items.map(function (i) {
  29. var item = {
  30. title: i.querySelector("title").textContent.trim(),
  31. date: new Date( i.querySelector("pubDate").textContent.trim() ),
  32. description: i.querySelector("description").textContent.trim(),
  33. link: i.querySelector("link").textContent.trim(),
  34. feedLink: feedLink
  35. };
  36. var occurenceParts = splitString( item.description, ":", 2);
  37. item.occurence = parseInt( occurenceParts[0] ) || 1;
  38. item.occurenceNote = occurenceParts[1].trim();
  39. if (item.occurenceNote == "") {
  40. item.occurenceNote = "Morning routine";
  41. }
  42. return item;
  43. });
  44. items = items.sort(function (a, b) {
  45. return b.date - a.date;
  46. });
  47. console.log("items:");
  48. console.log(items);
  49. return items;
  50. }
  51. function populateItems( items ) {
  52. console.log("populateItems");
  53. console.log( items );
  54. var rows = items.map(function (item) {
  55. var row = document.createElement( "tr" );
  56. var date = document.createElement( "td" );
  57. var occurence = document.createElement( "td" );
  58. var note = document.createElement( "td" );
  59. date.textContent = dateString( item.date );
  60. occurence.textContent = item.occurence;
  61. note.textContent = item.occurenceNote;
  62. row.appendChild( date );
  63. row.appendChild( occurence );
  64. row.appendChild( note );
  65. return row;
  66. });
  67. var tableBody = document.querySelector("#bed-makings tbody");
  68. console.log("populate: ");
  69. console.log(tableBody);
  70. console.log(rows);
  71. while (tableBody.lastChild) {
  72. tableBody.removeChild(tableBody.lastChild);
  73. }
  74. rows.forEach(function (row) {
  75. tableBody.appendChild(row);
  76. });
  77. }
  78. var dataset;
  79. function initDataset () {
  80. dataset = new vis.DataSet();
  81. }
  82. var groups = [];
  83. function groupFor (str) {
  84. var idx = groups.indexOf(str);
  85. if (idx == -1) {
  86. idx = groups.length;
  87. groups.push(str);
  88. }
  89. return idx;
  90. }
  91. var graph2d;
  92. function createPlot ( feedItems ) {
  93. console.log("createPlot");
  94. var container = document.getElementById('visualization');
  95. while( container.lastChild ) {
  96. container.removeChild( container.lastChild );
  97. }
  98. if (feedItems.length == 0) {
  99. return;
  100. }
  101. var items = feedItems.map(function (item) {
  102. var startDate = startOfDay(item.date);
  103. return {
  104. x: startDate,
  105. y: (item.date - startDate) / (1000 * 60),
  106. group: groupFor(item.feedLink),
  107. id: item.feedLink + item.link
  108. };
  109. });
  110. // feed items are in descending order
  111. var firstDate = startOfDay( feedItems[ feedItems.length - 1 ].date );
  112. var lastDate = startOfDay( feedItems[ 0 ].date );
  113. var currentFirstDate = dataset.get("firstDateSunrise");
  114. if (!currentFirstDate || currentFirstDate.x > firstDate) {
  115. var firstTimes = SunCalc.getTimes(firstDate, 44.986656, -93.258133)
  116. items.push({id: "firstDateSunrise",
  117. x: firstDate,
  118. y: (firstTimes.sunrise - firstDate) / (1000 * 60),
  119. group: groupFor("sunrise")});
  120. }
  121. var currentLastDate = dataset.get("lastDateSunrise");
  122. if (!currentLastDate || currentLastDate.x < lastDate) {
  123. var lastTimes = SunCalc.getTimes(lastDate, 44.986656, -93.258133)
  124. items.push({id: "lastDateSunrise",
  125. x: lastDate,
  126. y: (lastTimes.sunrise - lastDate) / (1000 * 60),
  127. group: groupFor("sunrise")});
  128. }
  129. dataset.update(items);
  130. var options = {
  131. sort: false,
  132. sampling:false,
  133. style:'points',
  134. dataAxis: {
  135. visible: false,
  136. left: {
  137. range: {
  138. min: 0, max: (60 * 24)
  139. }
  140. }
  141. },
  142. drawPoints: {
  143. enabled: true,
  144. size: 6,
  145. style: 'circle' // square, circle
  146. },
  147. defaultGroup: 'Scatterplot',
  148. height: '400px',
  149. width: '90%'
  150. };
  151. graph2d = new vis.Graph2d(container, dataset, options);
  152. // sunrise is the earliest and latest feed items
  153. var windowStartDate = new Date(dataset.get("firstDateSunrise").x);
  154. var windowEndDate = new Date(dataset.get("lastDateSunrise").x);
  155. // day before first and after the latest day
  156. windowStartDate.setDate( windowStartDate.getDate() - 1 );
  157. windowEndDate.setDate( windowEndDate.getDate() + 1 );
  158. graph2d.setWindow(windowStartDate, windowEndDate, {animation: false});
  159. }
  160. // TV
  161. function importTvFeed (feedUrl) {
  162. var rssRequest = new XMLHttpRequest();
  163. rssRequest.onreadystatechange = function (e) {
  164. var r = rssRequest;
  165. if (r.readyState == XMLHttpRequest.DONE) {
  166. if (r.status >= 200 && r.status < 300) {
  167. console.log("Got TV RSS (" + r.status + "): ");
  168. var items = importTvDoc(r.responseXML);
  169. addTvToPlot( items );
  170. }
  171. }
  172. };
  173. //rssRequest.open("GET", "bed-harlanji.xml");
  174. rssRequest.open("GET", feedUrl);
  175. rssRequest.send();
  176. }
  177. function importTvDoc( rssDoc ) {
  178. console.log("importTvDoc:");
  179. console.log( rssDoc );
  180. var items = Array.from( rssDoc.querySelectorAll("item") );
  181. items = items.map(function (i) {
  182. var title = i.querySelector("title").textContent.trim();
  183. var state = title.indexOf("on") > -1;
  184. var item = {
  185. title: title,
  186. state: state,
  187. date: new Date( i.querySelector("pubDate").textContent.trim() ),
  188. description: i.querySelector("description").textContent.trim()
  189. };
  190. return item;
  191. });
  192. items = items.sort(function (a, b) {
  193. return b.date - a.date;
  194. });
  195. console.log("tv items:");
  196. console.log(items);
  197. return items;
  198. }
  199. // Steps CSV
  200. function importStepsFeed (feedUrl) {
  201. var rssRequest = new XMLHttpRequest();
  202. rssRequest.onreadystatechange = function (e) {
  203. var r = rssRequest;
  204. if (r.readyState == XMLHttpRequest.DONE) {
  205. if (r.status >= 200 && r.status < 300) {
  206. var parseHeader = true;
  207. console.log("Got Steps CSV (" + r.status + ", parseHeader=" + parseHeader + "): ");
  208. var items = r.responseText.split("\n")
  209. .map(function (line) { return line.trim(); })
  210. .filter(function (line, i) { return !(parseHeader && i == 0) && line != ""; })
  211. .map(function (line, i, arr) {
  212. var parts = line.split(",");
  213. var date = new Date(parts[0]),
  214. steps = parseInt(parts[1]);
  215. return {
  216. date: date,
  217. steps: steps
  218. }
  219. });
  220. addStepsToPlot( items );
  221. }
  222. }
  223. };
  224. //rssRequest.open("GET", "bed-harlanji.xml");
  225. rssRequest.open("GET", feedUrl);
  226. rssRequest.send();
  227. }
  228. function dateString (date) {
  229. // return "d: " + date;
  230. var str = (date.getYear() + 1900) + "-"
  231. + new String(date.getMonth() + 1).padStart(2, "0") + "-"
  232. + new String(date.getDate()).padStart(2, "0") + " "
  233. + new String(date.getHours()).padStart(2, "0") + ":"
  234. + new String(date.getMinutes()).padStart(2, "0") + ":"
  235. + new String(date.getSeconds()).padStart(2, "0") + " ";
  236. var tzOffset = date.getTimezoneOffset() / 60;
  237. if (tzOffset >= 0) {
  238. str += "+";
  239. }
  240. str += new String(tzOffset * 100).padStart(4, "0");
  241. return str;
  242. }
  243. function addStepsToPlot ( feedItems ) {
  244. console.log("addStepsToPlot");
  245. var items = feedItems.map(function (item) {
  246. var startDate = startOfDay(item.date);
  247. var group = 99;
  248. return {
  249. x: startDate,
  250. y: item.steps / 8, // fixme: normalize over 1440 seconds
  251. group: group,
  252. id: "steps-" + item.date
  253. };
  254. });
  255. dataset.update( items );
  256. }
  257. function addTvToPlot ( feedItems ) {
  258. console.log("addTvToPlot");
  259. var items = feedItems.map(function (item) {
  260. var startDate = startOfDay(item.date);
  261. var group = item.state ? 3 : 4;
  262. return {
  263. x: startDate,
  264. y: (item.date - startDate) / (1000 * 60),
  265. group: group
  266. };
  267. });
  268. dataset.add( items );
  269. }
  270. function splitString(string, delimiter, n) {
  271. var parts = string.split(delimiter);
  272. return parts.slice(0, n - 1).concat([parts.slice(n - 1).join(delimiter)]);
  273. }
  274. </script>
  275. <!--
  276. <script defer data-domain="cityapper.com" src="https://pa.cityapper.com/js/plausible.js"></script>
  277. -->
  278. <script src="vis-timeline-graph2d.min.js"></script>
  279. <link href="vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
  280. <script src="suncalc.js"></script>
  281. </head>
  282. <body>
  283. <script>
  284. function importAuthorSubmitted( form ) {
  285. console.log("submitted:");
  286. console.log(form);
  287. }
  288. var authors = {
  289. "HarlanJI": "https://harlanji.com/bed.xml",
  290. "Marty": "https://harlanji.com/bed-marty.xml"
  291. }
  292. function populateAuthors () {
  293. var authorsElem = document.querySelector("#author");
  294. while(authorsElem.lastChild) {
  295. authorsElem.removeChild(authorsElem.lastChild);
  296. }
  297. Object.entries(authors).forEach(function (e, i) {
  298. var option = document.createElement("option");
  299. option.value = e[1];
  300. option.textContent = e[0];
  301. if ( i == 0 ) {
  302. option.selected = "selected";
  303. }
  304. authorsElem.appendChild(option);
  305. console.log(e);
  306. });
  307. authorsElem.dispatchEvent(new Event('change'));
  308. }
  309. function startOfDay (date) {
  310. var startDate = new Date(date);
  311. startDate.setHours(0);
  312. startDate.setMinutes(0);
  313. startDate.setSeconds(0);
  314. startDate.setMilliseconds(0);
  315. return startDate;
  316. }
  317. </script>
  318. <h1>Author Made Bed</h1>
  319. <form id="import-author" action="#" onsubmit="console.log('s')">
  320. <p>
  321. Author:
  322. <select id="author" onchange="importRssFeed(this.options[this.selectedIndex].value)">
  323. </select>
  324. </p>
  325. </form>
  326. <div id="visualization"></div>
  327. <div id="visualization2"></div>
  328. <table id="bed-makings">
  329. <thead>
  330. <tr>
  331. <th>Date Time</th>
  332. <th>#</th>
  333. <th>Note</th>
  334. </tr>
  335. </thead>
  336. <tbody>
  337. </tbody>
  338. </table>
  339. <script>
  340. //importRssFeed( document.querySelector("#author").value );
  341. populateAuthors();
  342. initDataset();
  343. </script>
  344. </body>
  345. </html>