Add [StackExchange] monthly questions badge service (#2432)

- Badge shows number of questions for a given library for the previous month (not current month because its in progress)
- Service works for not just the StackOverflow site but for other StackExchange sites also

Close #2378
This commit is contained in:
Monica Bui
2018-12-03 18:12:44 -07:00
committed by Paul Melnikow
parent 99e846d2ab
commit ec07f23942
4 changed files with 134 additions and 10 deletions

View File

@@ -0,0 +1,18 @@
'use strict'
const { metric } = require('../../lib/text-formatters')
const { floorCount: floorCountColor } = require('../../lib/color-formatters')
module.exports = function renderQuestionsBadge({
suffix,
stackexchangesite,
query,
numValue,
}) {
const label = `${stackexchangesite} ${query} questions`
return {
label,
message: `${metric(numValue)}${suffix}`,
color: floorCountColor(numValue, 1000, 10000, 20000),
}
}

View File

@@ -0,0 +1,86 @@
'use strict'
const BaseJsonService = require('../base-json')
const renderQuestionsBadge = require('./stackexchange-helpers')
const moment = require('moment')
const Joi = require('joi')
const tagSchema = Joi.object({
total: Joi.number()
.min(1)
.required(),
}).required()
module.exports = class StackExchangeMonthlyQuestions extends BaseJsonService {
static get category() {
return 'other'
}
static get defaultBadgeData() {
return { label: 'stackoverflow' }
}
static get examples() {
return [
{
title: 'Stack Exchange monthly questions',
namedParams: { stackexchangesite: 'stackoverflow', query: 'momentjs' },
staticExample: this.render({
stackexchangesite: 'stackoverflow',
query: 'momentjs',
numValue: 2000,
}),
keywords: ['stackexchange', 'stackoverflow'],
},
]
}
static get route() {
return {
base: 'stackexchange',
pattern: ':stackexchangesite/qm/:query',
}
}
static render(props) {
return renderQuestionsBadge({
suffix: '/month',
...props,
})
}
async handle({ stackexchangesite, query }) {
const today = moment().toDate()
const prevMonthStart = moment(today)
.subtract(1, 'months')
.startOf('month')
.unix()
const prevMonthEnd = moment(today)
.subtract(1, 'months')
.endOf('month')
.unix()
const parsedData = await this._requestJson({
schema: tagSchema,
options: {
gzip: true,
qs: {
site: stackexchangesite,
fromdate: prevMonthStart,
todate: prevMonthEnd,
filter: 'total',
tagged: query,
},
},
url: `https://api.stackexchange.com/2.2/questions`,
})
const numValue = parsedData.total
return this.constructor.render({
stackexchangesite,
query,
numValue,
})
}
}

View File

@@ -0,0 +1,24 @@
'use strict'
const Joi = require('joi')
const { isMetricOverTimePeriod } = require('../test-validators')
const t = require('../create-service-tester')()
module.exports = t
t.create('Monthly Questions for StackOverflow Momentjs')
.get('/stackoverflow/qm/momentjs.json')
.expectJSONTypes(
Joi.object().keys({
name: 'stackoverflow momentjs questions',
value: isMetricOverTimePeriod,
})
)
t.create('Monthly Questions for Tex Spacing')
.get('/tex/qm/spacing.json')
.expectJSONTypes(
Joi.object().keys({
name: 'tex spacing questions',
value: isMetricOverTimePeriod,
})
)

View File

@@ -1,8 +1,7 @@
'use strict'
const BaseJsonService = require('../base-json')
const { metric } = require('../../lib/text-formatters')
const { floorCount: floorCountColor } = require('../../lib/color-formatters')
const renderQuestionsBadge = require('./stackexchange-helpers')
const Joi = require('joi')
const tagSchema = Joi.object({
@@ -49,14 +48,11 @@ module.exports = class StackExchangeQuestions extends BaseJsonService {
}
}
static render({ stackexchangesite, query, numValue }) {
const label = `${stackexchangesite} ${query} questions`
return {
label,
message: metric(numValue),
color: floorCountColor(numValue, 1000, 10000, 20000),
}
static render(props) {
return renderQuestionsBadge({
suffix: '',
...props,
})
}
async handle({ stackexchangesite, query }) {