Permalink
Browse files

Output timestamp feedback

  • Loading branch information...
Kikobeats committed Apr 30, 2018
1 parent 05c8dd9 commit fa78222c1d0030607f9f989c4fd7cdfc57cef095
Showing with 35 additions and 16 deletions.
  1. +7 −0 bin/view/colorize.js
  2. +9 −6 bin/view/helpers.js
  3. +2 −2 bin/view/index.js
  4. +17 −8 bin/view/render.js
@@ -38,9 +38,16 @@ const byStatusCode = (statusCode, str = statusCode) => {
return statusCodeColor(str)
}
const getRequestColor = timestamp => {
if (timestamp < 500) return 'green'
if (timestamp < 1200) return 'yellow'
return 'red'
}
module.exports = {
...statusCodeColors,
gray: chalk.gray,
byStatusCode,
getRequestColor,
theme: THEME
}
@@ -2,14 +2,17 @@
const { isEqual, includes, keys, size, isNil } = require('lodash')
const setState = (state, { statusCode, data }) => {
const status = state.count[statusCode]
const linkStatus = state.links[statusCode]
const linkItem = [data.statusCode, data.url]
const setState = (state, data) => {
const { statusCodeGroup } = data
const count = { [statusCode]: isNil(status) ? 1 : status + 1 }
const status = state.count[statusCodeGroup]
const linkStatus = state.links[statusCodeGroup]
const linkItem = [data.statusCode, data.url, data.timestamp]
const count = { [statusCodeGroup]: isNil(status) ? 1 : status + 1 }
const links = {
[statusCode]: isNil(linkStatus) ? [linkItem] : linkStatus.concat([linkItem])
[statusCodeGroup]: isNil(linkStatus)
? [linkItem]
: linkStatus.concat([linkItem])
}
return { count, links }
@@ -21,8 +21,8 @@ module.exports = ({ total, emitter, quiet, logspeed, ...opts }) => {
const neat = neatLog(render, { ...opts, logspeed, state })
neat.use((state, bus) => {
emitter.on('status', ({ statusCode, data }) => {
const newState = setState(state, { statusCode, data })
emitter.on('status', data => {
const newState = setState(state, data)
state.count = { ...state.count, ...newState.count }
state.links = { ...state.links, ...newState.links }
})
@@ -5,7 +5,9 @@ const prettyMs = require('pretty-ms')
const { EOL } = require('os')
const spinner = require('ora')({ text: '', color: 'gray' })
const { green, blue, gray, byStatusCode } = require('./colorize')
const colorize = require('./colorize')
const { green, blue, gray, byStatusCode, getRequestColor } = colorize
const resumeCount = (count, statusCode) =>
byStatusCode(statusCode, `${statusCode} ${count}`)
@@ -26,24 +28,31 @@ const renderCount = state => {
const spinnerFrame = spinner.frame()
const timestamp = blue(prettyMs(Date.now() - startTimestamp))
const url = gray(fetchingUrl)
const progress = gray(`${current}/${total}`)
const progress = gray(`${current} of ${total}`)
return `${EOL}${countByStatusCode}${EOL}${EOL}${timestamp} ${spinnerFrame}${progress} ${url}`
}
const renderResume = ({ count, links }) => {
const renderResume = ({ startTimestamp, count, links }) => {
const info = map(sortByStatusCode(count), (count, statusCode) => {
const countByStatusCode = resumeCount(count, statusCode)
const rows = map(
links[statusCode],
([statusCode, url]) => `${byStatusCode(statusCode)} ${url}`
).join(EOL)
const rows = map(links[statusCode], ([statusCode, url, timestamp]) => {
const colorizeStatusCode = byStatusCode(statusCode)
const colorTimestamp = getRequestColor(timestamp)
const colorizeTimestamp = colorize[colorTimestamp](
`+${prettyMs(timestamp)}`
)
return `${colorizeStatusCode} ${url} ${colorizeTimestamp}`
}).join(EOL)
return countByStatusCode + EOL + rows + EOL
}).join(EOL)
const total = reduce(count, (acc, count) => acc + count, 0)
const timestamp = prettyMs(Date.now() - startTimestamp)
return gray(`${EOL}${info}${EOL}Total ${total}`)
return gray(`${EOL}${info}${EOL}${total} links in ${timestamp}`)
}
module.exports = state => {

0 comments on commit fa78222

Please sign in to comment.