DTs Dashboard Charts

Adds charts to the mturk dashboard

נכון ליום 11-07-2014. ראה הגרסה האחרונה.

// ==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();