Consolidate [NpmDownloads] into one service (#3318)

This moves the four npm download services into a single class, in line with #3174, and other service implementations we've written in the last couple months. 

1. Change the suffixes from **/m** to **/month** for consistency.
2. Add tests of one of the intervals besides total.
3. Rewrite mocked service tests as unit tests.
4. Use (and work with) the `intervalMap` pattern that I think @calebcartwright started us using.
This commit is contained in:
Paul Melnikow
2019-04-16 19:18:16 -04:00
committed by GitHub
parent 483ecf24de
commit b79d1952c0
3 changed files with 126 additions and 124 deletions

View File

@@ -10,95 +10,92 @@ const pointResponseSchema = Joi.object({
downloads: nonNegativeInteger,
}).required()
// https://github.com/npm/registry/blob/master/docs/download-counts.md#output-1
const rangeResponseSchema = Joi.object({
downloads: Joi.array()
.items(pointResponseSchema)
.required(),
}).required()
function DownloadsForInterval(interval) {
const { base, messageSuffix = '', query, isRange = false, name } = {
week: {
base: 'npm/dw',
messageSuffix: '/w',
query: 'point/last-week',
name: 'NpmDownloadsWeek',
},
month: {
base: 'npm/dm',
messageSuffix: '/m',
query: 'point/last-month',
name: 'NpmDownloadsMonth',
},
year: {
base: 'npm/dy',
messageSuffix: '/y',
query: 'point/last-year',
name: 'NpmDownloadsYear',
},
total: {
base: 'npm/dt',
query: 'range/1000-01-01:3000-01-01',
isRange: true,
name: 'NpmDownloadsTotal',
},
}[interval]
const schema = isRange ? rangeResponseSchema : pointResponseSchema
// This hits an entirely different API from the rest of the NPM services, so
// it does not use NpmBase.
return class NpmDownloads extends BaseJsonService {
static get name() {
return name
}
static get category() {
return 'downloads'
}
static get route() {
return {
base,
pattern: ':scope(@.+)?/:packageName',
}
}
static get examples() {
return [
{
title: 'npm',
pattern: ':packageName',
namedParams: { packageName: 'localeval' },
staticPreview: this.render({ downloads: 30000 }),
keywords: ['node'],
},
]
}
static render({ downloads }) {
return {
message: `${metric(downloads)}${messageSuffix}`,
color: downloads > 0 ? 'brightgreen' : 'red',
}
}
async handle({ scope, packageName }) {
const slug = scope ? `${scope}/${packageName}` : packageName
let { downloads } = await this._requestJson({
schema,
url: `https://api.npmjs.org/downloads/${query}/${slug}`,
errorMessages: { 404: 'package not found or too new' },
})
if (isRange) {
downloads = downloads
.map(item => item.downloads)
.reduce((accum, current) => accum + current)
}
return this.constructor.render({ downloads })
}
}
const intervalMap = {
dw: {
query: 'point/last-week',
schema: pointResponseSchema,
transform: json => json.downloads,
messageSuffix: '/week',
},
dm: {
query: 'point/last-month',
schema: pointResponseSchema,
transform: json => json.downloads,
messageSuffix: '/month',
},
dy: {
query: 'point/last-year',
schema: pointResponseSchema,
transform: json => json.downloads,
messageSuffix: '/year',
},
dt: {
query: 'range/1000-01-01:3000-01-01',
// https://github.com/npm/registry/blob/master/docs/download-counts.md#output-1
schema: Joi.object({
downloads: Joi.array()
.items(pointResponseSchema)
.required(),
}).required(),
transform: json =>
json.downloads
.map(item => item.downloads)
.reduce((accum, current) => accum + current),
messageSuffix: '',
},
}
module.exports = ['week', 'month', 'year', 'total'].map(DownloadsForInterval)
// This hits an entirely different API from the rest of the NPM services, so
// it does not use NpmBase.
module.exports = class NpmDownloads extends BaseJsonService {
static get category() {
return 'downloads'
}
static get route() {
return {
base: 'npm',
pattern: ':interval(dw|dm|dy|dt)/:scope(@.+)?/:packageName',
}
}
static get examples() {
return [
{
title: 'npm',
namedParams: { interval: 'dw', packageName: 'localeval' },
staticPreview: this.render({ interval: 'dw', downloadCount: 30000 }),
keywords: ['node'],
},
]
}
// For testing.
static get _intervalMap() {
return intervalMap
}
static render({ interval, downloadCount }) {
const { messageSuffix } = intervalMap[interval]
return {
message: `${metric(downloadCount)}${messageSuffix}`,
color: downloadCount > 0 ? 'brightgreen' : 'red',
}
}
async handle({ interval, scope, packageName }) {
const { query, schema, transform } = intervalMap[interval]
const slug = scope ? `${scope}/${packageName}` : packageName
const json = await this._requestJson({
schema,
url: `https://api.npmjs.org/downloads/${query}/${slug}`,
errorMessages: { 404: 'package not found or too new' },
})
const downloadCount = transform(json)
return this.constructor.render({ interval, downloadCount })
}
}

View File

@@ -0,0 +1,25 @@
'use strict'
const { test, given } = require('sazerac')
const NpmDownloads = require('./npm-downloads.service')
describe('NpmDownloads', function() {
test(NpmDownloads._intervalMap.dt.transform, () => {
given({
downloads: [
{ downloads: 2, day: '2018-01-01' },
{ downloads: 3, day: '2018-01-02' },
],
}).expect(5)
})
test(NpmDownloads.render, () => {
given({
interval: 'dt',
downloadCount: 0,
}).expect({
message: '0',
color: 'red',
})
})
})

View File

@@ -1,14 +1,19 @@
'use strict'
const { ServiceTester } = require('../tester')
const { isMetric } = require('../test-validators')
const { isMetricOverTimePeriod, isMetric } = require('../test-validators')
const t = (module.exports = require('../tester').createServiceTester())
const t = new ServiceTester({
id: 'NpmDownloads',
title: 'NpmDownloads',
pathPrefix: '/npm',
})
module.exports = t
t.create('weekly downloads of left-pad')
.get('/dw/left-pad.json')
.expectBadge({
label: 'downloads',
message: isMetricOverTimePeriod,
color: 'brightgreen',
})
t.create('weekly downloads of @cycle/core')
.get('/dw/@cycle/core.json')
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod })
t.create('total downloads of left-pad')
.get('/dt/left-pad.json')
@@ -22,32 +27,7 @@ t.create('total downloads of @cycle/core')
.get('/dt/@cycle/core.json')
.expectBadge({ label: 'downloads', message: isMetric })
t.create('total downloads of package with zero downloads')
.get('/dt/package-no-downloads.json')
.intercept(nock =>
nock('https://api.npmjs.org')
.get('/downloads/range/1000-01-01:3000-01-01/package-no-downloads')
.reply(200, {
downloads: [{ downloads: 0, day: '2018-01-01' }],
})
)
.expectBadge({ label: 'downloads', message: '0', color: 'red' })
t.create('exact total downloads value')
.get('/dt/exact-value.json')
.intercept(nock =>
nock('https://api.npmjs.org')
.get('/downloads/range/1000-01-01:3000-01-01/exact-value')
.reply(200, {
downloads: [
{ downloads: 2, day: '2018-01-01' },
{ downloads: 3, day: '2018-01-02' },
],
})
)
.expectBadge({ label: 'downloads', message: '5' })
t.create('total downloads of unknown package')
t.create('downloads of unknown package')
.get('/dt/npm-api-does-not-have-this-package.json')
.expectBadge({
label: 'downloads',