Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add [NpmStatDownloads] Badge #9783

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions services/npm-stat/npm-stat-downloads.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import Joi from 'joi'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'
import { renderDownloadsBadge } from '../downloads.js'

const schema = Joi.object()
.pattern(Joi.string(), Joi.object().pattern(Joi.string(), nonNegativeInteger))
.required()

const NPM_INITIAL_RELEASE_DATE = '2010-01-12'

const MILLISECONDS_IN_A_DAY = 1000 * 60 * 60 * 24
const MILLISECONDS_IN_A_WEEK = MILLISECONDS_IN_A_DAY * 7
const MILLISECONDS_IN_A_MONTH = MILLISECONDS_IN_A_DAY * 30
const MILLISECONDS_IN_A_YEAR = MILLISECONDS_IN_A_DAY * 365

const intervalMap = {
dw: { interval: 'week', difference: MILLISECONDS_IN_A_WEEK },
dm: { interval: 'month', difference: MILLISECONDS_IN_A_MONTH },
dy: { interval: 'year', difference: MILLISECONDS_IN_A_YEAR },
dt: { difference: -1 },
}

export default class NpmStatDownloads extends BaseJsonService {
static category = 'downloads'

static route = {
base: 'npm-stat',
pattern: ':interval(dw|dm|dy|dt)/:author',
}

static examples = [
{
title: 'npm (by author)',
documentation:
'The total number of downloads of npm packages published by the specified author.',
dukeluo marked this conversation as resolved.
Show resolved Hide resolved
namedParams: { interval: 'dy', author: 'dukeluo' },
staticPreview: this.render({ interval: 'dy', downloadCount: 30000 }),
keywords: ['node'],
},
]

static _cacheLength = 21600

static defaultBadgeData = { label: 'downloads' }

static getTotalDownloads(data) {
dukeluo marked this conversation as resolved.
Show resolved Hide resolved
const add = (x, y) => x + y
const sum = nums => nums.reduce(add, 0)

return Object.values(data).reduce(
(count, packageDownloads) => count + sum(Object.values(packageDownloads)),
0,
)
}

static render({ interval, downloads }) {
return renderDownloadsBadge({
downloads,
interval: intervalMap[interval].interval,
colorOverride: downloads > 0 ? 'brightgreen' : 'red',
})
}

async handle({ interval, author }) {
const format = date => date.toISOString().split('T')[0]
dukeluo marked this conversation as resolved.
Show resolved Hide resolved

const { difference } = intervalMap[interval]
const today = new Date()
const until = format(today)
const from =
difference > 0
? format(new Date(today.getTime() - difference))
: NPM_INITIAL_RELEASE_DATE
const data = await this._requestJson({
url: `https://npm-stat.com/api/download-counts?author=${author}&from=${from}&until=${until}`,
schema,
})
const downloads = this.constructor.getTotalDownloads(data)

return this.constructor.render({ interval, downloads })
}
}
43 changes: 43 additions & 0 deletions services/npm-stat/npm-stat-downloads.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { isMetricOverTimePeriod, isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('weekly downloads of npm author dukeluo')
.get('/dw/dukeluo.json')
.expectBadge({
label: 'downloads',
message: isMetricOverTimePeriod,
color: 'brightgreen',
})

t.create('monthly downloads of npm author dukeluo')
.get('/dm/dukeluo.json')
.expectBadge({
label: 'downloads',
message: isMetricOverTimePeriod,
color: 'brightgreen',
})

t.create('yearly downloads of npm author dukeluo')
.get('/dy/dukeluo.json')
.expectBadge({
label: 'downloads',
message: isMetricOverTimePeriod,
color: 'brightgreen',
})

t.create('total downloads of npm author dukeluo')
.get('/dt/dukeluo.json')
.expectBadge({
label: 'downloads',
message: isMetric,
color: 'brightgreen',
})

t.create('downloads of unknown npm package author')
.get('/dt/npm-api-does-not-have-this-package-author.json')
.expectBadge({
label: 'downloads',
message: '0',
color: 'red',
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this API allow us to differentiate between a user who does exist with 0 downloads and a non-existant user?
If not, we can roll with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this API cant differentiate between a user who does exist with 0 downloads and a non-existant user. After my testing, this API always return an empty object with a staus code 200 for a non-existant user.

Loading