Skip to content

Commit

Permalink
Consolidate [DubDownloads] into one service
Browse files Browse the repository at this point in the history
Same idea as #3318.
  • Loading branch information
paulmelnikow committed Apr 16, 2019
1 parent 483ecf2 commit de9ff46
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 99 deletions.
177 changes: 86 additions & 91 deletions services/dub/dub-download.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const Joi = require('joi')
const { metric } = require('../text-formatters')
const { downloadCount } = require('../color-formatters')
const { downloadCount: downloadCountColor } = require('../color-formatters')
const { BaseJsonService } = require('..')
const { nonNegativeInteger } = require('../validators')

Expand All @@ -15,106 +15,101 @@ const schema = Joi.object({
}).required(),
})

function DownloadsForInterval(interval) {
const { base, messageSuffix, name } = {
daily: {
base: 'dub/dd',
messageSuffix: '/day',
name: 'DubDownloadsDay',
},
weekly: {
base: 'dub/dw',
messageSuffix: '/week',
name: 'DubDownloadsWeek',
},
monthly: {
base: 'dub/dm',
messageSuffix: '/month',
name: 'DubDownloadsMonth',
},
total: {
base: 'dub/dt',
messageSuffix: '',
name: 'DubDownloadsTotal',
},
}[interval]
const intervalMap = {
dd: {
transform: json => json.downloads.daily,
messageSuffix: '/day',
},
dw: {
transform: json => json.downloads.weekly,
messageSuffix: '/week',
},
dm: {
transform: json => json.downloads.monthly,
messageSuffix: '/month',
},
dt: {
transform: json => json.downloads.total,
messageSuffix: '',
},
}

return class DubDownloads extends BaseJsonService {
static get name() {
return name
}
module.exports = class DubDownloads extends BaseJsonService {
static get category() {
return 'downloads'
}

static render({ downloads, version }) {
const label = version ? `downloads@${version}` : 'downloads'
return {
label,
message: `${metric(downloads)}${messageSuffix}`,
color: downloadCount(downloads),
}
static get route() {
return {
base: 'dub',
pattern: ':interval(dd|dw|dm|dt)/:packageName/:version*',
}
}

async fetch({ packageName, version }) {
let url = `https://code.dlang.org/api/packages/${packageName}`
if (version) {
url += `/${version}`
}
url += '/stats'
return this._requestJson({ schema, url })
}
static get examples() {
return [
{
title: 'DUB',
namedParams: { interval: 'dm', packageName: 'vibe-d' },
staticPreview: this.render({ interval: 'dm', downloadCount: 5000 }),
},
{
title: 'DUB (version)',
namedParams: {
interval: 'dm',
packageName: 'vibe-d',
version: '0.8.4',
},
staticPreview: this.render({
interval: 'dm',
version: '0.8.4',
downloadCount: 100,
}),
},
{
title: 'DUB (latest)',
namedParams: {
interval: 'dm',
packageName: 'vibe-d',
version: 'latest',
},
staticPreview: this.render({
interval: 'dm',
version: 'latest',
downloadCount: 100,
}),
},
]
}

async handle({ packageName, version }) {
const data = await this.fetch({ packageName, version })
return this.constructor.render({
downloads: data.downloads[interval],
version,
})
}
static get defaultBadgeData() {
return { label: 'downloads' }
}

static get defaultBadgeData() {
return { label: 'downloads' }
}
static render({ interval, version, downloadCount }) {
const { messageSuffix } = intervalMap[interval]

static get category() {
return 'downloads'
return {
label: version ? `downloads@${version}` : 'downloads',
message: `${metric(downloadCount)}${messageSuffix}`,
color: downloadCountColor(downloadCount),
}
}

static get route() {
return {
base,
pattern: ':packageName/:version*',
}
async fetch({ packageName, version }) {
let url = `https://code.dlang.org/api/packages/${packageName}`
if (version) {
url += `/${version}`
}
url += '/stats'
return this._requestJson({ schema, url })
}

static get examples() {
let examples = [
{
title: 'DUB',
pattern: ':packageName',
namedParams: { packageName: 'vibe-d' },
staticPreview: this.render({ downloads: 5000 }),
},
]
if (interval === 'monthly') {
examples = examples.concat([
{
title: 'DUB (version)',
pattern: ':packageName/:version',
namedParams: { packageName: 'vibe-d', version: '0.8.4' },
staticPreview: this.render({ downloads: 100, version: '0.8.4' }),
},
{
title: 'DUB (latest)',
pattern: ':packageName/:version',
namedParams: { packageName: 'vibe-d', version: 'latest' },
staticPreview: this.render({ downloads: 100, version: 'latest' }),
},
])
}
return examples
}
async handle({ interval, packageName, version }) {
const { transform } = intervalMap[interval]

const json = await this.fetch({ packageName, version })
const downloadCount = transform(json)
return this.constructor.render({ interval, downloadCount, version })
}
}

module.exports = ['daily', 'weekly', 'monthly', 'total'].map(
DownloadsForInterval
)
11 changes: 3 additions & 8 deletions services/dub/dub-download.tester.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const Joi = require('joi')
const { ServiceTester } = require('../tester')
const { isMetric, isMetricOverTimePeriod } = require('../test-validators')
const t = (module.exports = require('../tester').createServiceTester())

const isDownloadsColor = Joi.equal(
'red',
Expand All @@ -12,11 +12,6 @@ const isDownloadsColor = Joi.equal(
'brightgreen'
)

const t = (module.exports = new ServiceTester({
id: 'dub',
title: 'DubDownloads',
}))

t.create('total downloads (valid)')
.get('/dt/vibe-d.json')
.expectBadge({
Expand All @@ -29,7 +24,7 @@ t.create('total downloads, specific version (valid)')
.get('/dt/vibe-d/0.8.4.json')
.expectBadge({
label: '[email protected]',
message: Joi.string().regex(/^[1-9][0-9]*[kMGTPEZY]?$/),
message: isMetric,
color: isDownloadsColor,
})
.timeout(15000)
Expand All @@ -38,7 +33,7 @@ t.create('total downloads, latest version (valid)')
.get('/dt/vibe-d/latest.json')
.expectBadge({
label: 'downloads@latest',
message: Joi.string().regex(/^[1-9][0-9]*[kMGTPEZY]?$/),
message: isMetric,
color: isDownloadsColor,
})

Expand Down

0 comments on commit de9ff46

Please sign in to comment.