Created
January 20, 2020 13:24
-
-
Save Hamzali/a04d643eb9b5fd7c8bf7ddd2eef4de98 to your computer and use it in GitHub Desktop.
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
/** | |
* Calculates test related statistics for given jira project. | |
*/ | |
export const testWorkflowStats = async ( | |
projectCode: string | |
): Promise<TestWorkFlowStatsResult> => { | |
const project = await readProjectById(projectCode); | |
const [stories, uiTests] = await Promise.all([ | |
readAllStories(projectCode), | |
readAllUiTests(projectCode), | |
]); | |
const totalWorkTimeSpentSeconds = { | |
[constant.JIRA_ISSUE_TYPES.STORY]: 0, | |
[constant.JIRA_ISSUE_TYPES.UI_TEST]: 0, | |
}; | |
const workTimeSpentSecondsByTeam = new Map<string, number>(); | |
let totalUITestStoryLink = 0; | |
const issues = await utils.bulkAsyncEach< | |
Jira.Schema.IssueBean, | |
AsyncResultIssue | |
>( | |
stories.concat(uiTests), | |
async (issue: Jira.Schema.IssueBean): Promise<AsyncResultIssue> => { | |
// No issue id short circuit | |
if (!issue.id) { | |
return {}; | |
} | |
const currWorklogs = await getAllIssueWorklogsById(issue.id); | |
const dmTeamWorklogMap = new Map<string, number>(); | |
const dmTeamUserWorklogMap = new Map<string, Record<string, number>>(); | |
// sum worklog durations by issue type | |
const worklogTimeSpent = currWorklogs.reduce( | |
(acc: number, worklog: Jira.Schema.WorklogJsonBean): number => { | |
const { author, timeSpentSeconds = 0 } = worklog; | |
const authorName = author?.name ?? ''; | |
const teams = cacheStore.readMemberTeamsByUsername(authorName); | |
// split the work for each team | |
teams.forEach((team: CommencisTeam): void => { | |
const teamTimeSpent = workTimeSpentSecondsByTeam.get(team.name); | |
if (!teamTimeSpent) { | |
workTimeSpentSecondsByTeam.set(team.name, timeSpentSeconds); | |
} else { | |
workTimeSpentSecondsByTeam.set( | |
team.name, | |
teamTimeSpent + timeSpentSeconds | |
); | |
} | |
// Collect detailed infor for DM Teams | |
if (team.type === CommencisTeamType.DM) { | |
// team based sum | |
const currentValue = dmTeamWorklogMap.get(team.name) ?? 0; | |
dmTeamWorklogMap.set(team.name, currentValue + timeSpentSeconds); | |
// user based sum | |
const currentUserValue = dmTeamUserWorklogMap.get(team.name) ?? { | |
[authorName]: 0, | |
}; | |
dmTeamUserWorklogMap.set(team.name, { | |
...currentUserValue, | |
[authorName]: currentUserValue[authorName] + timeSpentSeconds, | |
}); | |
} | |
}); | |
return timeSpentSeconds + acc; | |
}, | |
0 | |
); | |
// No issue fields short circuit | |
if (!issue.fields) { | |
return {}; | |
} | |
const issueType = issue.fields.issuetype.name; | |
if (totalWorkTimeSpentSeconds[issueType] != null) { | |
totalWorkTimeSpentSeconds[issueType] += worklogTimeSpent; | |
} | |
if (issueType === constant.JIRA_ISSUE_TYPES.UI_TEST) { | |
totalUITestStoryLink += issue.fields.issuelinks.length; | |
} | |
// format DM detailed info for response | |
const dmWorklogTimespent = Object.entries( | |
Object.fromEntries(dmTeamWorklogMap) | |
).map( | |
([teamKey, timeSpentSeconds]): DmWorklogTimespentRecord => ({ | |
team: teamKey, | |
timeSpentSeconds, | |
userTimeSpentSeconds: dmTeamUserWorklogMap.get(teamKey) ?? {}, | |
}) | |
); | |
return { | |
...issue, | |
worklogs: currWorklogs, | |
dmWorklogTimespent, | |
}; | |
} | |
); | |
return { | |
project, | |
uiTestsCount: uiTests.length, | |
storyCount: stories.length, | |
issues, | |
totalWorkTimeSpentSeconds, | |
workTimeSpentSecondsByTeam: Object.fromEntries(workTimeSpentSecondsByTeam), | |
uiTestStoryCoverage: totalUITestStoryLink, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment