-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathdiscourse.service.js
120 lines (102 loc) · 2.76 KB
/
discourse.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { nonNegativeInteger, url } from '../validators.js'
import { BaseJsonService, queryParams } from '../index.js'
const schemaSingular = Joi.object({
topic_count: nonNegativeInteger,
user_count: nonNegativeInteger,
post_count: nonNegativeInteger,
like_count: nonNegativeInteger,
}).required()
const schemaPlural = Joi.object({
topics_count: nonNegativeInteger,
users_count: nonNegativeInteger,
posts_count: nonNegativeInteger,
likes_count: nonNegativeInteger,
})
const schema = Joi.alternatives(schemaSingular, schemaPlural)
const queryParamSchema = Joi.object({
server: url,
}).required()
function singular(variant) {
return variant.slice(0, -1)
}
const params = queryParams({
name: 'server',
example: 'https://meta.discourse.org',
required: true,
})
class DiscourseBase extends BaseJsonService {
static category = 'chat'
static defaultBadgeData = { label: 'discourse' }
async fetch({ server }) {
return this._requestJson({
schema,
url: `${server}/site/statistics.json`,
})
}
}
class DiscourseMetric extends DiscourseBase {
static route = {
base: 'discourse',
pattern: ':variant(topics|users|posts|likes)',
queryParamSchema,
}
static openApi = {
'/discourse/topics': {
get: { summary: 'Discourse Topics', parameters: params },
},
'/discourse/users': {
get: { summary: 'Discourse Users', parameters: params },
},
'/discourse/posts': {
get: { summary: 'Discourse Posts', parameters: params },
},
'/discourse/likes': {
get: { summary: 'Discourse Likes', parameters: params },
},
}
static render({ variant, stat }) {
return {
message: `${metric(stat)} ${variant}`,
color: 'brightgreen',
}
}
async handle({ variant }, { server }) {
const data = await this.fetch({ server })
// e.g. variant == 'topics' --> try 'topic_count' then 'topics_count'
let stat = data[`${singular(variant)}_count`]
if (stat === undefined) {
stat = data[`${variant}_count`]
}
return this.constructor.render({ variant, stat })
}
}
class DiscourseStatus extends DiscourseBase {
static route = {
base: 'discourse',
pattern: 'status',
queryParamSchema,
}
static openApi = {
'/discourse/status': {
get: {
summary: 'Discourse Status',
parameters: params,
},
},
}
static render() {
return {
message: 'online',
color: 'brightgreen',
}
}
async handle(_routeParams, { server }) {
await this.fetch({ server })
// if fetch() worked, the server is up
// if it failed, we'll show an error e.g: 'inaccessible'
return this.constructor.render()
}
}
export default [DiscourseMetric, DiscourseStatus]