I write a script to show current assigned status.
It will add a button in toolbar when view testrun.
A new window contains the assigned status popup when click on the button
name: Easy Assign
description: Add a 'Show Assigned Status' button to show the assigned status
author: Alston
version: 1.0
includes: ^runs/view/
excludes:
js:
var index;
var UserLists;
var myWindow;
function Users (id)
{
this.id = id;
this.name = "unassigned";
this.count = 0;
this.notimecount = 0;
this.estimate = 0;
}
function TimeList (section_id, timevalue, name)
{
this.SecId = section_id;
this.Time = timevalue;
this.SubTime = function ()
{
var Time = 0;
for (var i = 0; i < this.Childs.length; i++)
{
Time = Time + this.Childs[i].TotalTime;
}
return Time;
}
this.Name = name;
this.Childs = new Array();
this.TotalTime = function ()
{
return this.Time + this.SubTime;
}
}
function CheckExistSectionId (objTimeLists, SecId)
{
for (var i = 0; i < objTimeLists.length; i++)
{
if (SecId == objTimeLists[i].SecId)
{
return i;
}
}
return -1;
}
function ConvertToSecond (value)
{
var values = value.split(" ")
var time = 0;
var patt = new RegExp(/\d/);
var num = 0;
var measure;
for (var i = 0; i < values.length; i++) {
patt = /\d+/;
num = patt.exec(values[i]);
patt = /\D+/;
measure = patt.exec(values[i]);
switch(measure[0]) {
case "d":
time = time + num * 8 * 60 * 60;
break;
case "h":
time = time + num * 60 * 60;
break;
case "m":
time = time + num * 60;
break;
case "s":
time = time + num * 1;
break;
}
}
return time;
}
function CalculateEstimate (objTimeList, cases)
{
var time = 0;
for (var i = 0; i < cases.length; i++) {
if (cases[i].estimate)
{
var nMatchId = CheckExistSectionId (objTimeList, cases[i].section_id);
if (nMatchId == -1)//check if id not exist
{
objTimeList[objTimeList.length] = new TimeList (cases[i].section_id, ConvertToSecond(cases[i].estimate), "");
}
else
{
objTimeList[nMatchId].Time = objTimeList[nMatchId].Time + ConvertToSecond(cases[i].estimate);
}
}
}
return time;
}
function CheckExistUserId (objUserLists, id)
{
for (var i = 0; i < objUserLists.length; i++)
{
if (id== objUserLists[i].id)
{
return i;
}
}
return -1;
}
function ConstructUserList (objUserList, data)
{
for (var i = 0; i < data.length; i++)
{
if (!data[i].assignedto_id)
{
data[i].assignedto_id = -1;
}
var nMatchId = CheckExistUserId (objUserList, data[i].assignedto_id);
if (nMatchId == -1)//check if id not exist
{
objUserList[objUserList.length] = new Users(data[i].assignedto_id);
nMatchId = objUserList.length - 1;
}
objUserList[nMatchId].count = objUserList[nMatchId].count + 1;
if (data[i].estimate)
{
objUserList[nMatchId].estimate = objUserList[nMatchId].estimate + ConvertToSecond(data[i].estimate);
}
else
{
objUserList[nMatchId].notimecount++;
}
}
}
function FillName (UserLists, data)
{
var nCountDown = UserLists.length - 1;
for (var i = 0; i < data.length, nCountDown > 0; i++)
{
for (var j = 0; j < UserLists.length; j++)
{
if (UserLists[j].id == data[i].id)
{
UserLists[j].name = data[i].name;
nCountDown--;
break;
}
}
}
}
function ConvertTime (timevalue)
{
var time = "";
var units = [ "s", "m ", "h " ];
var divids = [ 60, 60, 8 ];
for (var i = 0; i < units.length; i++)
{
time = timevalue % divids[i] + units[i] + time;
timevalue = parseInt(timevalue / divids[i]);
if (timevalue == 0)
{
return time;
}
}
time = timevalue + "d " + time;
return time;
}
function GenerateContent (UserLists)
{
var content;
var head;
var TotalCount = 0;
var TotalEstimate = 0;
head= "<Title> Assigned Status</Title><Head>Assigned Status</Head>";
content = "<style>table, th, td {border: 1px solid black;}</style><Table><tr><th>Name</th><th>Cases Count</th><th>Cases without estimate</th><th>Estimate</th><th>Percentage</th></tr>";
for (var i = 0; i < UserLists.length; i++)
{
TotalCount += UserLists[i].count;
TotalEstimate += UserLists[i].estimate;
}
head += "<script>function ConvertTime (timevalue){ var time = \"\"; var units = [ \"s\", \"m \", \"h \" ]; var divids = [ 60, 60, 8 ]; for (var i = 0; i < units.length; i++) { time = timevalue % divids[i] + units[i] + time; timevalue = parseInt(timevalue \/ divids[i]); if (timevalue == 0) { return time;}} time = timevalue + \"d \" + time; return time; } function Recalculate() { var TotalTime; TotalTime = " + TotalEstimate + "; document.getElementById(\"SharedTime\").innerHTML = ConvertTime(parseInt(TotalTime / parseInt(document.getElementById(\"ShareCount\").value))); } <\/script>";
head += "<p> Total cases: " + TotalCount + " <\/p>";
head += "<p> Total estimate: " + ConvertTime(TotalEstimate) + " Share by <input type=\"text\" id=\"ShareCount\" value = " + UserLists.length.toString() + " size = 1> is : <label id=\"SharedTime\">" + ConvertTime(parseInt(TotalEstimate/UserLists.length)) + "<\/label> <input type = button onclick=Recalculate() value = \"Re-calulate\"> <\/p>";
for (var i = 0; i < UserLists.length; i++)
{
content += "<tr><td>"+UserLists[i].name+"</td>";
content += "<td>"+UserLists[i].count+"</td>";
content += "<td>"+UserLists[i].notimecount+"</td>";
content += "<td>"+ConvertTime (UserLists[i].estimate) + "</td>";
content += "<td>"+ ((UserLists[i].estimate / TotalEstimate) * 100).toFixed(2) + "% </td></tr>";
}
content += "</table>";
return head + content;
}
function readUserName (UserLists)
{
$.ajax(
{
url: uiscripts.env.page_base + '/api/v2/get_users',
dataType: 'json',
beforeSend: function(xhr)
{
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("X-Requested-With", "API Client");
},
success: function(data, status)
{
FillName (UserLists, data);
if (myWindow)
{
myWindow.close();
}
myWindow = window.open("", "Assigned Status", "width=700, height=400, location=no");
var content = GenerateContent (UserLists);
myWindow.location = 'about:blank';
myWindow.document.write(content);
console.log(data); // Contains the suite
},
error: function(error)
{
console.log(error);
}
});
}
function readTests (UserLists)
{
$.ajax(
{
url: uiscripts.env.page_base + '/api/v2/get_tests/' +uiscripts.context.run.id,
dataType: 'json',
beforeSend: function(xhr)
{
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("X-Requested-With", "API Client");
},
success: function(data, status)
{
ConstructUserList (UserLists, data);
readUserName (UserLists);
console.log(data); // Contains the suite
},
error: function(error)
{
alert("readTests Fail");
console.log(error);
}
});
}
$(document).ready(
function()
{
$("<a class='show-attention-required toolbar-button content-header-button button-view'>Show Assigned Status</a>").insertBefore('.toolbar.content-header-toolbar a:last-child');
$(".show-attention-required").click(
function()
{
UserLists = new Array();
readTests (UserLists);
}
);
}
);
css:
div.some-class {
}