Skip to content

Commit

Permalink
Use two separate queries.
Browse files Browse the repository at this point in the history
Move duplicated code to function.
Move interface.
  • Loading branch information
hannes-mk committed Nov 30, 2023
1 parent 6802b27 commit bba6a0d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 36 deletions.
52 changes: 19 additions & 33 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import _ from "lodash";
import { JiraQueryDataForFetchingIssues, getIssueChangelog, runJqlQueryAgainstJira } from "./src/jira-related/jira-client-functions";
import { JiraQueryDataForFetchingIssues, getChangelogsForIssues, runJqlQueryAgainstJira } from "./src/jira-related/jira-client-functions";
import { StateWithDate, createAuthorizationHeaderValue, getAllStatesWithDates, getDateForStartingInProgressOfIssue, mapJiraResponseToBusinessObjects } from "./src/jira-related/jira-service-functions";
import { createPlotDataForCfd, createPlotDataForPercentages, createPlotDataFromCycleTimeHistogram } from "./src/plotting/plotting-functions";
import { getCycleTimeHistogram } from "./src/core/core-functions";
import { Layout, Plot, plot } from "nodeplotlib";
import { log } from "mathjs";
import { stat } from "fs";
import { Issue } from "./src/core/core-interfaces";
import { IssueWithChangelogs } from "./src/core/core-interfaces";


console.log("Started");
Expand All @@ -19,41 +17,29 @@ const envVar: JiraQueryDataForFetchingIssues = {
jiraJqlQueryCfd: process.env.JIRA_JQL_QUERY_CFD!,

}

// ensureDataIsComplete() // missing #thisIsAHack
// ensureAllEnvVarsAreAvailable() // missing #thisIsAHack
listEnvVars(envVar)

const authHeaderValue = createAuthorizationHeaderValue(envVar.jiraAuthEmail, envVar.jiraAuthToken);

// Cycle times
console.log("Fetching items according to JQL queries");

const jqlResultCycleTimes = await runJqlQueryAgainstJira(envVar.jiraJqlQueryCycleTimes, envVar.jiraApiBaseUrl, authHeaderValue)
const issuesCycleTimes = mapJiraResponseToBusinessObjects(jqlResultCycleTimes)
// check if we reached the limit of 50 results // missing #thisIsAHack
console.log(`Found ${issuesCycleTimes.length} issues`);
const issuesForCycleTimes = mapJiraResponseToBusinessObjects(jqlResultCycleTimes)
console.log(`Found ${issuesForCycleTimes.length} issues for the cycle time graphs`);

console.log("Fetching details on items: ");
const jqlResultsCfd = await runJqlQueryAgainstJira(envVar.jiraJqlQueryCfd, envVar.jiraApiBaseUrl, authHeaderValue)
// check if we reached the limit of 50 results // missing #thisIsAHack
const issuesForCfd = mapJiraResponseToBusinessObjects(jqlResultsCfd)
console.log(`Found ${issuesForCfd.length} issues for the CFD`);

// fetching all data
interface IssueWithChangelogs {
issue: Issue,
changelog: any // #thisIsAHack
}
console.log("Fetching changelogs");
const issuesWithChangelogsForCycleTimes: IssueWithChangelogs[] = await getChangelogsForIssues(issuesForCycleTimes, envVar.jiraApiBaseUrl, authHeaderValue)

const issuesWithChangelogs: IssueWithChangelogs[] = await Promise.all(
issuesCycleTimes.map(async issue => {
const changelog = await getIssueChangelog(
issue.key,
envVar.jiraApiBaseUrl,
authHeaderValue)

return <IssueWithChangelogs>{
issue,
changelog
}
})
)
const issuesWithChangelogsForCfd: IssueWithChangelogs[] = await getChangelogsForIssues(issuesForCfd, envVar.jiraApiBaseUrl, authHeaderValue)

const issuesWithStartDate = issuesWithChangelogs.map(issueWithChangelog => {
const issuesWithStartDateForCycleTimes = issuesWithChangelogsForCycleTimes.map(issueWithChangelog => {
const startedDate = getDateForStartingInProgressOfIssue(issueWithChangelog.changelog)
return {
...issueWithChangelog.issue,
Expand All @@ -64,14 +50,14 @@ const issuesWithStartDate = issuesWithChangelogs.map(issueWithChangelog => {
// computing graph data
console.log("Computing graph data");

const cycleTimeHistogramData = getCycleTimeHistogram(issuesWithStartDate)
const cycleTimeHistogramData = getCycleTimeHistogram(issuesWithStartDateForCycleTimes)
const histogramPlotData = createPlotDataFromCycleTimeHistogram(cycleTimeHistogramData)
const percentagesPlotData = createPlotDataForPercentages(cycleTimeHistogramData)

const statesWithDatesArray: StateWithDate[][] = issuesWithChangelogs.map((iwc: IssueWithChangelogs) =>
const statesWithDatesArrayForCfd: StateWithDate[][] = issuesWithChangelogsForCfd.map((iwc: IssueWithChangelogs) =>
getAllStatesWithDates(iwc.issue, iwc.changelog)
)
const cfdPlotData = createPlotDataForCfd(statesWithDatesArray)
const cfdPlotData = createPlotDataForCfd(statesWithDatesArrayForCfd)

// define plots
const histogramPlot: Plot = {
Expand Down Expand Up @@ -122,7 +108,7 @@ const percentagesLayout: Layout = {
title: "Completed within x working days"
},
yaxis: {
title: `% of issues completed (total: ${issuesCycleTimes.length}) `,
title: `% of issues completed (total: ${issuesForCycleTimes.length}) `,
range: [0, 100]
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/core/core-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ export interface Issue {
startedDate?: Date,
resolutionDate: Date,

}
}

export interface IssueWithChangelogs {
issue: Issue,
changelog: any // #thisIsAHack
}
23 changes: 23 additions & 0 deletions src/jira-related/jira-client-functions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Issue, IssueWithChangelogs } from "../core/core-interfaces";

export interface JiraQueryDataForFetchingIssues {
jiraApiBaseUrl: string,
jiraAuthEmail: string,
Expand Down Expand Up @@ -40,4 +42,25 @@ export async function getIssueChangelog(
},
});
return await response.json();
}

export async function getChangelogsForIssues(
issues: Issue[],
jiraApiBaseUrl: string,
authHeaderValue: string
): Promise<IssueWithChangelogs[]> {
const issuesWithChangelogs = await Promise.all(
issues.map(async issue => {
const changelog = await getIssueChangelog(
issue.key,
jiraApiBaseUrl,
authHeaderValue)

return <IssueWithChangelogs>{
issue,
changelog
}
})
)
return issuesWithChangelogs
}
2 changes: 1 addition & 1 deletion src/jira-related/jira-service-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ describe("getAllStatesWithDates()", () => {
expect(result[0].stateName).toEqual(State.created)
expect(result[0].stateReachedDate).toEqual(new Date("2023-11-02T10:02:34.255+0100"))
})
})
})
2 changes: 1 addition & 1 deletion src/jira-related/jira-service-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ export function getAllStatesWithDates(issue: Issue, issueChangelog: any): StateW
...stateChanges
]
return result
}
}

0 comments on commit bba6a0d

Please sign in to comment.