DTs Dashboard Charts

Adds charts to the mturk dashboard

目前為 2014-07-11 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         DTs Dashboard Charts
// @namespace    localhost
// @version      0.1b
// @description  Adds charts to the mturk dashboard
// @match        https://www.mturk.com/mturk/dashboard
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js
// @require      http://cdn.jsdelivr.net/jqplot/1.0.8/jquery.jqplot.js
// @require      http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.pieRenderer.js
// @require      http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.dateAxisRenderer.min.js
// @require      http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.canvasAxisTickRenderer.js
// @require      http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.canvasAxisLabelRenderer.js
// @require      http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.canvasTextRenderer.js
// @require      http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.categoryAxisRenderer.js
// @require      http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.barRenderer.js
// @require      http://cdn.jsdelivr.net/jqplot/1.0.8/plugins/jqplot.pointLabels.js
// @resource     http://cdn.jsdelivr.net/jqplot/1.0.8/jquery.jqplot.css
// @grant        GM_addStyle
// @run-at       document-end
// @copyright    2014+, DeliriumTremens
// ==/UserScript==
 
// Display dollar and hit count charts
 
var dates = [];
$('a[href*="statusdetail"]').each( function () {
    dates.push($(this).attr('href'));
});
 
var HITStorage = {};
HITStorage.indexedDB = {};
HITStorage.indexedDB.db = null;
 
var curr = new Date;
var first = curr.getDate() - curr.getDay();
var last = first + 6;
 
var firstday = new Date(curr.setDate(first));
 
firstdy = (firstday.getFullYear() + '-' + ('0' + (firstday.getMonth()+1)).slice(-2) + '-' + ('0' + (firstday.getDay()+6)).slice(-2));
seconddy = (firstday.getFullYear() + '-' + ('0' + (firstday.getMonth()+1)).slice(-2) + '-' + ('0' + (firstday.getDay()+7)).slice(-2));
thirddy = (firstday.getFullYear() + '-' + ('0' + (firstday.getMonth()+1)).slice(-2) + '-' + ('0' + (firstday.getDay()+8)).slice(-2));
fourthdy = (firstday.getFullYear() + '-' + ('0' + (firstday.getMonth()+1)).slice(-2) + '-' + ('0' + (firstday.getDay()+9)).slice(-2));
fifthdy = (firstday.getFullYear() + '-' + ('0' + (firstday.getMonth()+1)).slice(-2) + '-' + ('0' + (firstday.getDay()+10)).slice(-2));
sixthdy = (firstday.getFullYear() + '-' + ('0' + (firstday.getMonth()+1)).slice(-2) + '-' + ('0' + (firstday.getDay()+11)).slice(-2));
seventhdy = (firstday.getFullYear() + '-' + ('0' + (firstday.getMonth()+1)).slice(-2) + '-' + ('0' + (firstday.getDay()+12)).slice(-2));
 
var hitMeter = {};
hitMeter[firstdy.toString()] = 0;
hitMeter[seconddy.toString()] = 0;
hitMeter[thirddy.toString()] = 0;
hitMeter[fourthdy.toString()] = 0;
hitMeter[fifthdy.toString()] = 0;
hitMeter[sixthdy.toString()] = 0;
hitMeter[seventhdy.toString()] = 0;
 
var hitCounter = {};
hitCounter[firstdy.toString()] = 0;
hitCounter[seconddy.toString()] = 0;
hitCounter[thirddy.toString()] = 0;
hitCounter[fourthdy.toString()] = 0;
hitCounter[fifthdy.toString()] = 0;
hitCounter[sixthdy.toString()] = 0;
hitCounter[seventhdy.toString()] = 0;
 
function HITpull() {
        var request = indexedDB.open("HITDB", 4);
        request.onsuccess = function(e) {
                HITStorage.indexedDB.db = e.target.result;
                var db = HITStorage.indexedDB.db;
        var results = [];
        var tmp_results = {};
                var transaction = db.transaction('HIT','readonly');
                var store = transaction.objectStore('HIT');
                var index = store.index('date');
                var range = IDBKeyRange.bound(firstdy, seventhdy, false, false);
                index.openCursor(range).onsuccess = function(event) {
                        var cursor = event.target.result;
                        if (cursor) {
                                var hit = cursor.value;
                                if (tmp_results[hit.date] === undefined) {
                                        tmp_results[hit.date] = [];
                                        tmp_results[hit.date][0] = hit.reward;
                                        tmp_results[hit.date][1] = 1;
                    tmp_results[hit.date][2] = hit.date;
                                        }
                                else if (hit.status === 'Rejected'){}
                                else {
                                        tmp_results[hit.date][0] += hit.reward;
                                        tmp_results[hit.date][1] += 1;
                    tmp_results[hit.date][2] = hit.date;
                                        }
                                cursor.continue();
                        }
                        else {
                                for (var key in tmp_results) {
                                results.push(tmp_results[key]);
                                }
                printHitCount(results);
                                printDollarCount(results);
                        }
                db.close();
                };
                request.onerror = HITStorage.indexedDB.onerror;
        };
}
 
function printHitCount (results) {
        $(".container-content:eq(1)").append('<div id="hitCount" style="height:200px;width:350px;float:left;"></div>');
        for (var key in results) {
                hitMeter[results[key][2]] += results[key][1];
        };
        var ticks = [firstdy.toString(),seconddy.toString(),thirddy.toString(),fourthdy.toString(),fifthdy.toString(),sixthdy.toString(),seventhdy.toString()];
        var plot2 = $.jqplot('hitCount', [[hitMeter[firstdy.toString()],hitMeter[seconddy.toString()],hitMeter[thirddy.toString()],hitMeter[fourthdy.toString()],hitMeter[fifthdy.toString()],
                                      hitMeter[sixthdy.toString()],hitMeter[seventhdy.toString()]]], {
        animate: !$.jqplot.use_excanvas,
                seriesDefaults:{
                        renderer:$.jqplot.BarRenderer,
                        rendererOptions: {fillToZero: true},
            pointLabels: {
                show: true,
                hideZeros: true,
                location: 'n'
            }
                },
                series:[
                        {label:firstdy.toString()},
                        {label:seconddy.toString()},
                        {label:thirddy.toString()},
                        {label:fourthdy.toString()},
                        {label:fifthdy.toString()},
            {label:sixthdy.toString()},
            {label:seventhdy.toString()}
                ],
                title: {
                        text: 'Daily Count (Current Week)',
                        fontFamily: '"Trebuchet MS", Arial, Helvetica, sans-serif',
                        fontSize: '10pt',
                        textColor: '#666666'
                },
                axes: {
                        xaxis: {
                                tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
                                tickOptions: {
                                        angle: -15,
                                        fontSize: '8pt',
                    showGridline: false,
                    formatString: '%a'
                                },
                                renderer: $.jqplot.CategoryAxisRenderer,
                                ticks: ticks
                        },
                        yaxis: {
                                tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
                                tickOptions: {
                                        fontSize: '8pt',
                                        markSize: '0'
                                },
                                pad: 1.2,
                                min: 0
                        }
                }
        });
    var imgData = $('#hitCount').jqplotToImageStr({});
    $('#hitCount').on('click',function () {
             $.ajax({
                        url: 'https://api.imgur.com/3/image',
                        headers: {
                                'Authorization': 'Client-ID 6ebabcf714f0bb3'
                        },
                        type: 'POST',
                        data: {
                                'image': imgData.substr(22)
                        },
                        success: function(response) { prompt("Copy to clipboard: Ctrl+C, Enter", response.data.link); }
             });  
        });
};
 
function printDollarCount (results) {
        $(".container-content:eq(1)").append('<div id="dollarCount" style="height:200px;width:350px;float:right;"></div>');
        for (var key in results) {
                hitCounter[results[key][2]] += results[key][0];;
        };
        var ticks = [firstdy.toString(),seconddy.toString(),thirddy.toString(),fourthdy.toString(),fifthdy.toString(),sixthdy.toString(),seventhdy.toString()];
        var plot2 = $.jqplot('dollarCount', [[hitCounter[firstdy.toString()],hitCounter[seconddy.toString()],hitCounter[thirddy.toString()],hitCounter[fourthdy.toString()],
                                          hitCounter[fifthdy.toString()],hitCounter[sixthdy.toString()],hitCounter[seventhdy.toString()]]], {
        animate: !$.jqplot.use_excanvas,
                seriesDefaults:{
                        renderer:$.jqplot.BarRenderer,
                        rendererOptions: {fillToZero: true},
            pointLabels: {
                show: true,
                hideZeros: true,
                location: 'n',
                formatString: '$%#.2f'
            }
                },
                series:[
                        {label:firstdy.toString()},
                        {label:seconddy.toString()},
                        {label:thirddy.toString()},
                        {label:fourthdy.toString()},
                        {label:fifthdy.toString()},
            {label:sixthdy.toString()},
            {label:seventhdy.toString()}
                ],
                title: {
                        text: 'Daily Earnings (Current Week)',
                        fontFamily: '"Trebuchet MS", Arial, Helvetica, sans-serif',
                        fontSize: '10pt',
                        textColor: '#666666'
                },
                axes: {
                        xaxis: {
                                tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
                                tickOptions: {
                                        angle: -15,
                                        fontSize: '8pt',
                    showGridline: false
                                },
                                renderer: $.jqplot.CategoryAxisRenderer,
                                ticks: ticks
                        },
                        yaxis: {
                                tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
                                tickOptions: {
                                        fontSize: '8pt',
                                        prefix: '$',
                                        markSize: '0'
                                },
                                pad: 1.2,
                                min: 0
                        }
                }
        });
    var imgData = $('#dollarCount').jqplotToImageStr({});
    $('#dollarCount').on('click',function () {
             $.ajax({
                        url: 'https://api.imgur.com/3/image',
                        headers: {
                                'Authorization': 'Client-ID 6ebabcf714f0bb3'
                        },
                        type: 'POST',
                        data: {
                                'image': imgData.substr(22)
                        },
                        success: function(response) { prompt("Copy to clipboard: Ctrl+C, Enter", response.data.link); }
             });  
        });
}
 
HITpull();