Skip to content

Commit

Permalink
chore: improve labels
Browse files Browse the repository at this point in the history
(There was a problem where the label got cut off badly in logs)
  • Loading branch information
nytamin committed Oct 19, 2023
1 parent d14ac3e commit 62eefdb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
10 changes: 9 additions & 1 deletion shared/packages/api/src/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deferGets, diff, promiseTimeout, stringifyError, waitTime } from '../lib'
import { deferGets, diff, promiseTimeout, stringMaxLength, stringifyError, waitTime } from '../lib'

describe('lib', () => {
test('diff', () => {
Expand Down Expand Up @@ -218,5 +218,13 @@ describe('lib', () => {
])
expect(i).toBe(5)
})
test('stringMaxLength', () => {
expect(stringMaxLength('abc', 10)).toBe('abc')
expect(stringMaxLength('0123456789abcdefg', 17)).toBe('0123456789abcdefg')
expect(stringMaxLength('0123456789abcdefg', 15)).toBe('012345...bcdefg')
expect(stringMaxLength('0123456789abcdefg', 10)).toBe('012...defg')
expect(stringMaxLength('0123456789abcdefg', 9)).toBe('012...efg')
expect(stringMaxLength('0123456789abcdefg', 8)).toBe('01...efg')
})
})
export {}
14 changes: 14 additions & 0 deletions shared/packages/api/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,17 @@ export function startTimer(): {
},
}
}
/**
* If the string is longer than maxLength, it will be shortened to maxLength, with '...' in the middle
*/
export function stringMaxLength(str: string, maxLength: number): string {
str = `${str}`
if (str.length > maxLength) {
maxLength = maxLength - 3 // make space for '...'
const length0 = Math.floor(maxLength / 2)
const length1 = maxLength - length0

return str.slice(0, length0) + '...' + str.slice(-length1)
}
return str
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line node/no-extraneous-import
import { ExpectedPackageStatusAPI } from '@sofie-automation/shared-lib/dist/package-manager/package'
import { Expectation, ExpectationId, Reason, WorkerAgentId } from '@sofie-package-manager/api'
import { Expectation, ExpectationId, Reason, WorkerAgentId, stringMaxLength } from '@sofie-package-manager/api'
import { ExpectationStateHandlerSession } from '../lib/types'
import { ExpectationTrackerConstants } from './constants'

Expand Down Expand Up @@ -59,12 +59,7 @@ export interface TrackedExpectation {
}

export function expLabel(exp: TrackedExpectation): string {
let id = `${exp.id}`
if (id.length > 16) {
id = id.slice(0, 8) + '...' + id.slice(-8)
}

return `${id} ${exp.exp.statusReport.label.slice(0, 50)}`
return stringMaxLength(exp.id, 16) + ' ' + stringMaxLength(exp.exp.statusReport.label, 80)
}

export function sortTrackedExpectations(
Expand Down

0 comments on commit 62eefdb

Please sign in to comment.