DTs Dashboard Charts

Adds charts to the mturk dashboard

  1. // ==UserScript==
  2. // @name DTs Dashboard Charts
  3. // @namespace localhost
  4. // @version 0.4b
  5. // @description Adds charts to the mturk dashboard
  6. // @match https://www.mturk.com/mturk/dashboard
  7. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js
  8. // @require http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.7.0/moment.min.js
  9. // @require http://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.2.0/moment-timezone.min.js
  10. // @require http://cdn.jsdelivr.net/jqplot/1.0.8/jquery.jqplot.js
  11. // @require http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.pieRenderer.js
  12. // @require http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.dateAxisRenderer.min.js
  13. // @require http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.canvasAxisTickRenderer.js
  14. // @require http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.canvasAxisLabelRenderer.js
  15. // @require http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.canvasTextRenderer.js
  16. // @require http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.categoryAxisRenderer.js
  17. // @require http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.barRenderer.js
  18. // @require http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.pointLabels.js
  19. // @resource jqplot http://cdn.jsdelivr.net/jqplot/1.0.8/jquery.jqplot.css
  20. // @grant GM_addStyle
  21. // @grant GM_getResourceText
  22. // @run-at document-end
  23. // @copyright 2014+, DeliriumTremens
  24. // ==/UserScript==
  25.  
  26. var jqplotCSS = GM_getResourceText("jqplot");
  27. GM_addStyle(jqplotCSS);
  28.  
  29.  
  30. // Display dollar and hit count charts
  31. moment.tz.add('America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0'); // Add Amazon timezone to moment.js
  32.  
  33. var dates = [];
  34. $('a[href*="statusdetail"]').each( function () {
  35. dates.push($(this).attr('href'));
  36. });
  37. var HITStorage = {};
  38. HITStorage.indexedDB = {};
  39. HITStorage.indexedDB.db = null;
  40. var curr = new Date();
  41. var firstday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
  42. var secondday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
  43. var thirdday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
  44. var fourthday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
  45. var fifthday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
  46. var sixthday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
  47. var seventhday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
  48.  
  49. /*console.log(firstday.toLocaleDateString({timeZone: 'America/Los_Angeles'}).replace(/\//g, '-')); // Convert date to usable string */
  50.  
  51. secondday.setDate(secondday.getDate() + 1);
  52. thirdday.setDate(thirdday.getDate() + 2);
  53. fourthday.setDate(fourthday.getDate() + 3);
  54. fifthday.setDate(fifthday.getDate() + 4);
  55. sixthday.setDate(sixthday.getDate() + 5);
  56. seventhday.setDate(seventhday.getDate() + 6);
  57.  
  58. firstdy = (firstday.getFullYear()+ '-' + ('0' + (firstday.getMonth()+1)).slice(-2) + '-' + ('0' + (firstday.getDate())).slice(-2));
  59. seconddy = (secondday.getFullYear()+ '-' + ('0' + (secondday.getMonth()+1)).slice(-2) + '-' + ('0' + (secondday.getDate())).slice(-2));
  60. thirddy = (thirdday.getFullYear()+ '-' + ('0' + (thirdday.getMonth()+1)).slice(-2) + '-' + ('0' + (thirdday.getDate())).slice(-2));
  61. fourthdy = (fourthday.getFullYear()+ '-' + ('0' + (fourthday.getMonth()+1)).slice(-2) + '-' + ('0' + (fourthday.getDate())).slice(-2));
  62. fifthdy = (fifthday.getFullYear()+ '-' + ('0' + (fifthday.getMonth()+1)).slice(-2) + '-' + ('0' + (fifthday.getDate())).slice(-2));
  63. sixthdy = (sixthday.getFullYear()+ '-' + ('0' + (sixthday.getMonth()+1)).slice(-2) + '-' + ('0' + (sixthday.getDate())).slice(-2));
  64. seventhdy = (seventhday.getFullYear()+ '-' + ('0' + (seventhday.getMonth()+1)).slice(-2) + '-' + ('0' + (seventhday.getDate())).slice(-2));
  65. var hitMeter = {};
  66. hitMeter[firstdy.toString()] = 0;
  67. hitMeter[seconddy.toString()] = 0;
  68. hitMeter[thirddy.toString()] = 0;
  69. hitMeter[fourthdy.toString()] = 0;
  70. hitMeter[fifthdy.toString()] = 0;
  71. hitMeter[sixthdy.toString()] = 0;
  72. hitMeter[seventhdy.toString()] = 0;
  73. var hitCounter = {};
  74. hitCounter[firstdy.toString()] = 0;
  75. hitCounter[seconddy.toString()] = 0;
  76. hitCounter[thirddy.toString()] = 0;
  77. hitCounter[fourthdy.toString()] = 0;
  78. hitCounter[fifthdy.toString()] = 0;
  79. hitCounter[sixthdy.toString()] = 0;
  80. hitCounter[seventhdy.toString()] = 0;
  81. function HITpull() {
  82. var request = indexedDB.open("HITDB", 4);
  83. request.onsuccess = function(e) {
  84. HITStorage.indexedDB.db = e.target.result;
  85. var db = HITStorage.indexedDB.db;
  86. var results = [];
  87. var tmp_results = {};
  88. var transaction = db.transaction('HIT','readonly');
  89. var store = transaction.objectStore('HIT');
  90. var index = store.index('date');
  91. var range = IDBKeyRange.bound(firstdy, seventhdy, false, false);
  92. index.openCursor(range).onsuccess = function(event) {
  93. var cursor = event.target.result;
  94. if (cursor) {
  95. var hit = cursor.value;
  96. if (tmp_results[hit.date] === undefined) {
  97. tmp_results[hit.date] = [];
  98. tmp_results[hit.date][0] = hit.reward;
  99. tmp_results[hit.date][1] = 1;
  100. tmp_results[hit.date][2] = hit.date;
  101. }
  102. else if (hit.status === 'Rejected'){}
  103. else {
  104. tmp_results[hit.date][0] += hit.reward;
  105. tmp_results[hit.date][1] += 1;
  106. tmp_results[hit.date][2] = hit.date;
  107. }
  108. cursor.continue();
  109. }
  110. else {
  111. for (var key in tmp_results) {
  112. results.push(tmp_results[key]);
  113. }
  114. printHitCount(results);
  115. printDollarCount(results);
  116. }
  117. db.close();
  118. };
  119. request.onerror = HITStorage.indexedDB.onerror;
  120. };
  121. }
  122. function printHitCount (results) {
  123. $(".container-content:eq(1)").append('<div id="hitCount" style="height:200px;width:350px;float:left;"></div>');
  124. for (var key in results) {
  125. hitMeter[results[key][2]] += results[key][1];
  126. };
  127. var ticks = ['Sun','Mon','Tue','Wed','Thur','Fri','Sat'];
  128. var plot2 = $.jqplot('hitCount', [[hitMeter[firstdy.toString()],hitMeter[seconddy.toString()],hitMeter[thirddy.toString()],hitMeter[fourthdy.toString()],hitMeter[fifthdy.toString()],
  129. hitMeter[sixthdy.toString()],hitMeter[seventhdy.toString()]]], {
  130. animate: !$.jqplot.use_excanvas,
  131. seriesDefaults:{
  132. renderer:$.jqplot.BarRenderer,
  133. rendererOptions: {fillToZero: true},
  134. pointLabels: {
  135. show: true,
  136. hideZeros: true,
  137. location: 'n',
  138. ypadding: 1
  139. }
  140. },
  141. series:[
  142. {label:firstdy.toString()},
  143. {label:seconddy.toString()},
  144. {label:thirddy.toString()},
  145. {label:fourthdy.toString()},
  146. {label:fifthdy.toString()},
  147. {label:sixthdy.toString()},
  148. {label:seventhdy.toString()}
  149. ],
  150. title: {
  151. text: 'Daily Count (Current Week)',
  152. fontFamily: '"Trebuchet MS", Arial, Helvetica, sans-serif',
  153. fontSize: '10pt',
  154. textColor: '#666666'
  155. },
  156. axes: {
  157. xaxis: {
  158. tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
  159. tickOptions: {
  160. //angle: -15,
  161. fontSize: '8pt',
  162. showGridline: false,
  163. formatString: '%a'
  164. },
  165. renderer: $.jqplot.CategoryAxisRenderer,
  166. ticks: ticks
  167. },
  168. yaxis: {
  169. tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
  170. tickOptions: {
  171. fontSize: '8pt',
  172. markSize: '0'
  173. },
  174. pad: 1.2,
  175. min: 0
  176. }
  177. }
  178. });
  179. var imgData = $('#hitCount').jqplotToImageStr({});
  180. $('#hitCount').on('click',function () {
  181. $.ajax({
  182. url: 'https://api.imgur.com/3/image',
  183. headers: {
  184. 'Authorization': 'Client-ID 6ebabcf714f0bb3'
  185. },
  186. type: 'POST',
  187. data: {
  188. 'image': imgData.substr(22)
  189. },
  190. success: function(response) { prompt("Copy to clipboard: Ctrl+C, Enter", response.data.link); }
  191. });
  192. });
  193. };
  194. function printDollarCount (results) {
  195. $(".container-content:eq(1)").append('<div id="dollarCount" style="height:200px;width:350px;float:right;"></div>');
  196. for (var key in results) {
  197. hitCounter[results[key][2]] += results[key][0];;
  198. };
  199. var ticks = ['Sun','Mon','Tue','Wed','Thur','Fri','Sat'];
  200. var plot2 = $.jqplot('dollarCount', [[hitCounter[firstdy.toString()],hitCounter[seconddy.toString()],hitCounter[thirddy.toString()],hitCounter[fourthdy.toString()],
  201. hitCounter[fifthdy.toString()],hitCounter[sixthdy.toString()],hitCounter[seventhdy.toString()]]], {
  202. animate: !$.jqplot.use_excanvas,
  203. seriesDefaults:{
  204. renderer:$.jqplot.BarRenderer,
  205. rendererOptions: {fillToZero: true},
  206. pointLabels: {
  207. show: true,
  208. hideZeros: true,
  209. location: 'n',
  210. formatString: '$%#.2f',
  211. ypadding: 1
  212. }
  213. },
  214. series:[
  215. {label:firstdy.toString()},
  216. {label:seconddy.toString()},
  217. {label:thirddy.toString()},
  218. {label:fourthdy.toString()},
  219. {label:fifthdy.toString()},
  220. {label:sixthdy.toString()},
  221. {label:seventhdy.toString()}
  222. ],
  223. title: {
  224. text: 'Daily Earnings (Current Week)',
  225. fontFamily: '"Trebuchet MS", Arial, Helvetica, sans-serif',
  226. fontSize: '10pt',
  227. textColor: '#666666'
  228. },
  229. axes: {
  230. xaxis: {
  231. tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
  232. tickOptions: {
  233. //angle: -15,
  234. fontSize: '8pt',
  235. showGridline: false
  236. },
  237. renderer: $.jqplot.CategoryAxisRenderer,
  238. ticks: ticks
  239. },
  240. yaxis: {
  241. tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
  242. tickOptions: {
  243. fontSize: '8pt',
  244. prefix: '$',
  245. markSize: '0'
  246. },
  247. pad: 1.2,
  248. min: 0
  249. }
  250. }
  251. });
  252. var imgData = $('#dollarCount').jqplotToImageStr({});
  253. $('#dollarCount').on('click',function () {
  254. $.ajax({
  255. url: 'https://api.imgur.com/3/image',
  256. headers: {
  257. 'Authorization': 'Client-ID 6ebabcf714f0bb3'
  258. },
  259. type: 'POST',
  260. data: {
  261. 'image': imgData.substr(22)
  262. },
  263. success: function(response) { prompt("Copy to clipboard: Ctrl+C, Enter", response.data.link); }
  264. });
  265. });
  266. }
  267. HITpull();