Skip to content

Commit 3662313

Browse files
author
Marco LUCARELLA
committed
Fix log time percentage
1 parent 2f5210e commit 3662313

File tree

6 files changed

+33
-17
lines changed

6 files changed

+33
-17
lines changed

docs-gen/docs/api/api-introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ The responses are cached in order to reduce the network load.
6262
- [`$ji.macro.getWorkLogBySprint(projectKeyOrId: string, sprint: IJiraSprint)`](/docs/api/api-macro#getWorkLogBySprint)
6363
- [`$ji.macro.getWorkLogBySprintId(projectKeyOrId: string, sprintId: number)`](/docs/api/api-macro#getWorkLogBySprintId)
6464
- [`$ji.macro.getWorkLogByDates(projectKeyOrId: string, startDate: string, endDate: string = 'now()')`](/docs/api/api-macro#getWorkLogByDates)
65-
- [`$ji.macro.getWorkLogByUser(projectKeyOrId: string, startDate: string, endDate: string = 'now()')`](/docs/api/api-macro#getWorkLogByUser)
65+
- [`$ji.macro.getWorkLogSeriesByUser(projectKeyOrId: string, startDate: string, endDate: string = 'now()')`](/docs/api/api-macro#getWorkLogSeriesByUser)
6666
- [`$ji.macro.getVelocity(projectKeyOrId: string, sprintId: number, storyPointFieldName: string = 'aggregatetimeoriginalestimate')`](/docs/api/api-macro#getVelocity)
6767

6868
### API Category - [Chart](/docs/api/api-chart)

src/api/api.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getAccountByAlias, getAccountByHost } from "../utils"
22
import ObjectsCache from "../objectsCache"
3-
import { getActiveSprint, getActiveSprintName, getVelocity, getWorkLogByDates, getWorkLogBySprint, getWorkLogBySprintId, getWorkLogByUser } from "./apiMacro"
3+
import { getActiveSprint, getActiveSprintName, getVelocity, getWorkLogByDates, getWorkLogBySprint, getWorkLogBySprintId, getWorkLogSeriesByUser } from "./apiMacro"
44
import { getDefaultedSearchResults, getIssueDefaulted } from "./apiDefaulted"
55
import { getWorklogPerDay, getWorklogPerUser } from "./apiChart"
66
import { getBoards, getDevStatus, getIssue, getLoggedUser, getSearchResults, getSprint, getSprints } from "./apiBase"
@@ -25,7 +25,7 @@ const API = {
2525
getWorkLogBySprint: getWorkLogBySprint,
2626
getWorkLogBySprintId: getWorkLogBySprintId,
2727
getWorkLogByDates: getWorkLogByDates,
28-
getWorkLogByUser: getWorkLogByUser,
28+
getWorkLogSeriesByUser: getWorkLogSeriesByUser,
2929
getVelocity: getVelocity,
3030
},
3131
chart: {

src/api/apiChart.ts

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import moment from "moment"
22
import { IMultiSeries, ISeries } from "../interfaces/issueInterfaces"
3-
import { SECOND_IN_A_DAY } from "../interfaces/settingsInterfaces"
43
import { getRandomRGBColor, resetRandomGenerator } from "../utils"
54
import API from "./api"
5+
const ms = require('ms')
66

77
enum EChartFormat {
8-
SECONDS = 'Seconds',
8+
HOURS = 'Hours',
99
DAYS = 'Days',
1010
PERCENTAGE = 'Percentage',
1111
}
1212

13+
const MS_IN_A_DAY = ms('1d')
14+
const MS_IN_A_HOUR = ms('1h')
15+
1316
export async function getWorklogPerDay(projectKeyOrId: string, startDate: string, endDate: string = 'now()') {
1417
const worklogs = await API.macro.getWorkLogByDates(projectKeyOrId, startDate, endDate)
1518
const labels = []
@@ -22,7 +25,7 @@ export async function getWorklogPerDay(projectKeyOrId: string, startDate: string
2225
}
2326
const usersSeries: IMultiSeries = {}
2427
for (const worklog of worklogs) {
25-
const author = worklog.author.key
28+
const author = worklog.author.name
2629
if (!usersSeries[author]) {
2730
usersSeries[author] = Object.assign({}, emptySeries)
2831
}
@@ -53,22 +56,32 @@ export async function getWorklogPerDay(projectKeyOrId: string, startDate: string
5356
export async function getWorklogPerUser(projectKeyOrId: string, startDate: string, endDate: string = 'now()', options: { format?: EChartFormat, capacity?: ISeries } = {}) {
5457
const opt = {
5558
format: options.format || EChartFormat.PERCENTAGE,
56-
capacity: options.capacity || {},
59+
capacity: options.capacity || null,
5760
}
58-
const series = await API.macro.getWorkLogByUser(projectKeyOrId, startDate, endDate)
61+
const series = await API.macro.getWorkLogSeriesByUser(projectKeyOrId, startDate, endDate)
5962
switch (opt.format) {
60-
case EChartFormat.SECONDS:
63+
case EChartFormat.HOURS:
64+
for (const a in series) {
65+
series[a] = series[a] / MS_IN_A_HOUR
66+
}
6167
break
6268
case EChartFormat.DAYS:
6369
for (const a in series) {
64-
series[a] = series[a] / SECOND_IN_A_DAY
70+
series[a] = series[a] / MS_IN_A_DAY
6571
}
6672
break
6773
case EChartFormat.PERCENTAGE:
68-
const days = moment(moment(endDate).unix() - moment(startDate).unix()).days()
74+
const days = moment.duration(moment(endDate).diff(startDate)).asDays()
6975
for (const author in series) {
70-
const capacity = opt.capacity[author] || days
71-
series[author] = series[author] / capacity / SECOND_IN_A_DAY * 100
76+
if (opt.capacity) {
77+
if (author in opt.capacity) {
78+
series[author] = series[author] / opt.capacity[author] / MS_IN_A_DAY * 100
79+
} else {
80+
delete series[author]
81+
}
82+
} else {
83+
series[author] = series[author] / days / MS_IN_A_DAY * 100
84+
}
7285
}
7386
break
7487
default:

src/api/apiMacro.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ESprintState, IJiraSprint, IJiraWorklog, ISeries } from "../interfaces/issueInterfaces"
22
import API from "./api"
33
import moment from "moment"
4+
const ms = require('ms')
45

56
function dateTimeToDate(dateTime: string): string {
67
if (dateTime.match(/^\d/)) {
@@ -42,21 +43,22 @@ export async function getWorkLogByDates(projectKeyOrId: string, startDate: strin
4243
let worklogs: IJiraWorklog[] = []
4344
for (const issue of searchResults.issues) {
4445
if (issue.fields.worklog && issue.fields.worklog.worklogs) {
46+
issue.fields.worklog.worklogs.forEach(worklog => worklog.issueKey = issue.key)
4547
worklogs = worklogs.concat(issue.fields.worklog.worklogs)
4648
}
4749
}
4850
return worklogs
4951
}
5052

51-
export async function getWorkLogByUser(projectKeyOrId: string, startDate: string, endDate: string = 'now()'): Promise<ISeries> {
53+
export async function getWorkLogSeriesByUser(projectKeyOrId: string, startDate: string, endDate: string = 'now()'): Promise<ISeries> {
5254
const worklogs = await API.macro.getWorkLogByDates(projectKeyOrId, startDate, endDate)
5355
const series: ISeries = {}
5456
for (const worklog of worklogs) {
55-
const author = worklog.author.key
57+
const author = worklog.author.name
5658
if (!(author in series)) {
5759
series[author] = 0
5860
}
59-
series[author] += worklog.timeSpentSeconds
61+
series[author] += worklog.timeSpent.split(' ').map(x => ms(x)).reduce((x, y) => x + y)
6062
}
6163
return series
6264
}

src/interfaces/issueInterfaces.ts

+2
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ export interface IJiraWorklog {
8888
timeSpentSeconds: number
8989
updateAuthor: IJiraUser
9090
updated: string
91+
issueKey?: string
9192
}
9293

9394
export interface IJiraUser {
9495
active: boolean
9596
displayName: string
97+
name: string
9698
key: string
9799
emailAddress: string
98100
self: string

src/interfaces/settingsInterfaces.ts

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export const COLOR_SCHEMA_DESCRIPTION = {
1919

2020
export const COMPACT_SYMBOL = '-'
2121
export const COMMENT_REGEX = /^\s*#/
22-
export const SECOND_IN_A_DAY = 25200
2322

2423
export interface IJiraIssueSettings {
2524
accounts: IJiraIssueAccountSettings[]

0 commit comments

Comments
 (0)