Files
shields/services/sourceforge/sourceforge-downloads.service.js
chris48s 14892e3943 Implement a pattern for dealing with upstream APIs which are slow on the first hit; affects [endpoint] (#9233)
* allow serviceData to override cacheSeconds with a longer value

* prevent [endpoint] json cacheSeconds property exceeding service default

* allow ShieldsRuntimeError to specify a cacheSeconds property

By default error responses use the cacheLength of
the service class throwing the error.

This allows error to tell the handling layer the maxAge
that should be set on the error badge response.

* add customExceptions param

This

1. allows us to specify custom properties to pass to the exception
   constructor if we throw any of the standard got errors
   e.g: `ETIMEDOUT`, `ECONNRESET`, etc
2. uses a custom `cacheSeconds` property (if set on the exception)
   to set the response maxAge

* customExceptions --> systemErrors

* errorMessages --> httpErrors
2023-06-13 21:08:43 +01:00

105 lines
2.5 KiB
JavaScript

import Joi from 'joi'
import dayjs from 'dayjs'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'
const schema = Joi.object({
total: nonNegativeInteger,
}).required()
const intervalMap = {
dd: {
startDate: endDate => endDate,
interval: 'day',
},
dw: {
// 6 days, since date range is inclusive,
startDate: endDate => dayjs(endDate).subtract(6, 'days'),
interval: 'week',
},
dm: {
startDate: endDate => dayjs(endDate).subtract(30, 'days'),
interval: 'month',
},
dt: {
startDate: () => dayjs(0),
},
}
export default class SourceforgeDownloads extends BaseJsonService {
static category = 'downloads'
static route = {
base: 'sourceforge',
pattern: ':interval(dt|dm|dw|dd)/:project/:folder*',
}
static examples = [
{
title: 'SourceForge Downloads',
pattern: ':interval(dt|dm|dw|dd)/:project',
namedParams: {
interval: 'dm',
project: 'sevenzip',
},
staticPreview: this.render({
downloads: 215990,
interval: 'dm',
}),
},
{
title: 'SourceForge Downloads (folder)',
pattern: ':interval(dt|dm|dw|dd)/:project/:folder',
namedParams: {
interval: 'dm',
project: 'arianne',
folder: 'stendhal',
},
staticPreview: this.render({
downloads: 550,
interval: 'dm',
}),
},
]
static defaultBadgeData = { label: 'sourceforge' }
static render({ downloads, interval }) {
return renderDownloadsBadge({
downloads,
labelOverride: 'downloads',
interval: intervalMap[interval].interval,
})
}
async fetch({ interval, project, folder }) {
const url = `https://sourceforge.net/projects/${project}/files/${
folder ? `${folder}/` : ''
}stats/json`
// get yesterday since today is incomplete
const endDate = dayjs().subtract(24, 'hours')
const startDate = intervalMap[interval].startDate(endDate)
const options = {
searchParams: {
start_date: startDate.format('YYYY-MM-DD'),
end_date: endDate.format('YYYY-MM-DD'),
},
}
return this._requestJson({
schema,
url,
options,
httpErrors: {
404: 'project not found',
},
})
}
async handle({ interval, project, folder }) {
const { total: downloads } = await this.fetch({ interval, project, folder })
return this.constructor.render({ interval, downloads })
}
}