Skip to content

Instantly share code, notes, and snippets.

@Hamzali
Created January 20, 2020 13:26
Show Gist options
  • Save Hamzali/48404d40cd1fa5fad7d2999b35b0af41 to your computer and use it in GitHub Desktop.
Save Hamzali/48404d40cd1fa5fad7d2999b35b0af41 to your computer and use it in GitHub Desktop.
/**
* 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) {
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) {
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 {};
}
);
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