DTs Dashboard Charts

Adds charts to the mturk dashboard

目前为 2014-07-11 提交的版本。查看 最新版本

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