Skip to content

Commit

Permalink
Merge pull request #26 from franky47/feat/cachet
Browse files Browse the repository at this point in the history
Add Cachet-based services
  • Loading branch information
alisalahio committed Apr 10, 2021
2 parents da428a9 + f8661bd commit 94fb3ef
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/services/base/cachet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import axios, { AxiosResponse } from 'axios'
import { SettingsState } from '../../types'
import Service from '../service'
import Status from '../status'

// https://docs.cachethq.io/docs/component-statuses
enum CachetComponentStatus {
OPERATIONAL = 1, // The component is working.
PERFORMANCE_ISSUES = 2, // The component is experiencing some slowness.
PARTIAL_OUTAGE = 3, // The component may not be working for everybody. This could be a geographical issue for example.
MAJOR_OUTAGE = 4, // The component is not working for anybody.
}

interface CachetComponent {
enabled: boolean
status: CachetComponentStatus
}

type CachetComponentsResponse = AxiosResponse<{
// todo: Handle pagination.
// By default only the first 20 components are monitored.
meta: any
data: CachetComponent[]
}>

// --

class CachetService extends Service {
constructor(name: string, domain: string) {
super(name, domain)
}

async updateStatus(settings: SettingsState) {
const components = await axios.get<any, CachetComponentsResponse>(
`${this.domain}/api/v1/components`
)

const status = components?.data?.data?.reduce<CachetComponentStatus>(
(highestStatus, component) =>
component.enabled
? Math.max(highestStatus, component.status)
: highestStatus, // skip disabled components
CachetComponentStatus.OPERATIONAL
)

switch (status) {
case CachetComponentStatus.OPERATIONAL:
this.status = Status.OPERATIONAL
break
case CachetComponentStatus.PERFORMANCE_ISSUES:
this.status = Status.MINOR
break
case CachetComponentStatus.PARTIAL_OUTAGE:
this.status = Status.PARTIAL
break
case CachetComponentStatus.MAJOR_OUTAGE:
this.status = Status.MAJOR
break
default:
this.status = Status.OPERATIONAL
break
}

this.triggerNotification(settings)
this.prevStatus = this.status
}
}

export default CachetService
11 changes: 11 additions & 0 deletions src/services/cachet/clevercloud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import CachetService from '../base/cachet'

class CleverCloud extends CachetService {
constructor() {
const name = 'Clever Cloud'
const url = 'https://www.clevercloudstatus.com'
super(name, url)
}
}

export default CleverCloud
6 changes: 6 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ import Letsencrypt from './statusio/letsencrypt'
import Sendinblue from './statusio/sendinblue'
import Wasabi from './statusio/wasabi'

// Cachet services
import CleverCloud from './cachet/clevercloud'

// Custom services
import Algolia from './algolia'
import Stripe from './stripe'
Expand Down Expand Up @@ -277,6 +280,9 @@ const allServices = new Map<string, Service>([
['Wasabi', new Wasabi()],
[`Letsencrypt`, new Letsencrypt()],

// Cachet pages
['Clever Cloud', new CleverCloud()],

// Custom pages
['Algolia', new Algolia()],
['Stripe', new Stripe()],
Expand Down

0 comments on commit 94fb3ef

Please sign in to comment.