Last active
April 29, 2019 19:41
-
-
Save JogoShugh/20b1273655cea73f1216532523f0a281 to your computer and use it in GitHub Desktop.
Card aging with duration in each status stint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).arrive('.story-card', function() { | |
const story = $(this); | |
const applyStatusAging = () => { | |
const number = story.find('.number').text().trim(); | |
const title = story.find('.title'); | |
const query = ` | |
from: PrimaryWorkitem | |
where: | |
Number: ${number} | |
select: | |
- Status.Name | |
- ChangeDateUTC | |
- ChangedBy.Username | |
sort: | |
- ChangeDate | |
asof: All`; | |
const calcDuration = (start, end) => moment.duration(moment(start).diff(moment(end))).asDays(); | |
axios.post(queryV1, query).then(function (response) { | |
const data = response.data[0]; | |
const info = data.reduce((acc, curr) => { | |
const status = curr["Status.Name"]; | |
const changeDate = curr.ChangeDateUTC; | |
const changedBy = curr["ChangedBy.Username"] | |
const last = acc[acc.length-1]; | |
if (last === undefined || last.status !== status) { | |
let lastDuration = 0; | |
if (last) { | |
lastDuration = calcDuration(changeDate, last.changeDate); | |
last.duration = lastDuration; | |
} | |
const totalTimeInState = _.filter(acc, i => i.status === status).reduce((acc, curr) => acc + curr.duration, lastDuration); | |
acc.push({ changeDate, changedBy, status, duration: 0, totalTimeInState}); | |
} | |
return acc; | |
}, []); | |
// Adjust the last duration to be reflective of now - changeDate | |
const finalState = info[info.length-1]; | |
finalState.duration = calcDuration(new Date(), finalState.changeDate); | |
const getStatusHtml = status => { | |
const statusImg = _.find(statusIcons.icons, s => s.Description === status); | |
console.log("si:", statusImg); | |
if (statusImg !== undefined) { | |
const oid = statusImg._oid.substring("Attachment:".length); | |
const url = `/${v1Instance}/attachment.img/${oid}`; | |
const img = `<img src='${url}' height='20' width='20'title='${status}' />`; | |
return img; | |
} | |
return status; | |
} | |
const infoList = info.reduceRight((acc, curr) => acc + `<tr><td>${moment(curr.changeDate).format('l')}</td><td>${getStatusHtml(curr.status)}</td><td>${curr.changedBy}</td><td>${numeral(curr.duration).format('0.00')}</td><td align='right'>${numeral(curr.totalTimeInState).format('0.00')}</tr>`,'<tr style="font-weight: bold"><th>when</th><th>status</th><th>who</th><th>age</th><th align="right">status total</th></tr>'); | |
const statusHist = $(`<div style="color: white !important; font-size: 80%"><table>${infoList}</table></div>`); | |
statusHist.insertAfter(title); | |
}); | |
}; | |
const statusIconQuery = ` | |
from: Team | |
where: | |
Name: openAgile | |
select: | |
- from: Attachments | |
where: | |
Name: StoryStatusIcon | |
select: | |
- Description`; | |
if (statusIcons.icons === null) { | |
axios.post(queryV1, statusIconQuery).then(function (resp) { | |
const icons = resp.data[0][0].Attachments.map(i => { i.Description = i.Description.replace("<p>","").replace("</p>", ""); return i;}); | |
statusIcons.icons = icons; | |
applyStatusAging(); | |
}); | |
} else { | |
applyStatusAging(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment