DTs Dashboard Charts

Adds charts to the mturk dashboard

目前为 2014-07-17 提交的版本,查看 最新版本

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