| 
									
										
										
										
											2020-08-26 13:49:43 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import fetch from 'node-fetch'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 11:24:15 -07:00
										 |  |  | import {bold, info} from '../../utils/console'; | 
					
						
							|  |  |  | import {BaseModule} from './base'; | 
					
						
							| 
									
										
										
										
											2020-08-26 13:49:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 11:24:15 -07:00
										 |  |  | interface ServiceConfig { | 
					
						
							|  |  |  |   name: string; | 
					
						
							|  |  |  |   url: string; | 
					
						
							| 
									
										
										
										
											2020-08-26 13:49:43 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** The results of checking the status of a service */ | 
					
						
							|  |  |  | interface StatusCheckResult { | 
					
						
							| 
									
										
										
										
											2020-09-14 11:24:15 -07:00
										 |  |  |   name: string; | 
					
						
							|  |  |  |   status: 'passing'|'failing'; | 
					
						
							| 
									
										
										
										
											2020-08-26 13:49:43 -07:00
										 |  |  |   description: string; | 
					
						
							|  |  |  |   lastUpdated: Date; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 11:24:15 -07:00
										 |  |  | /** List of services Angular relies on. */ | 
					
						
							|  |  |  | export const services: ServiceConfig[] = [ | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     url: 'https://status.us-west-1.saucelabs.com/api/v2/status.json', | 
					
						
							|  |  |  |     name: 'Saucelabs', | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     url: 'https://status.npmjs.org/api/v2/status.json', | 
					
						
							|  |  |  |     name: 'Npm', | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     url: 'https://status.circleci.com/api/v2/status.json', | 
					
						
							|  |  |  |     name: 'CircleCi', | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     url: 'https://www.githubstatus.com/api/v2/status.json', | 
					
						
							|  |  |  |     name: 'Github', | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | ]; | 
					
						
							| 
									
										
										
										
											2020-08-26 13:49:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 11:24:15 -07:00
										 |  |  | export class ServicesModule extends BaseModule<StatusCheckResult[]> { | 
					
						
							|  |  |  |   async retrieveData() { | 
					
						
							|  |  |  |     return Promise.all(services.map(service => this.getStatusFromStandardApi(service))); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-26 13:49:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 11:24:15 -07:00
										 |  |  |   async printToTerminal() { | 
					
						
							|  |  |  |     const statuses = await this.data; | 
					
						
							|  |  |  |     const serviceNameMinLength = Math.max(...statuses.map(service => service.name.length)); | 
					
						
							|  |  |  |     info.group(bold('Service Statuses')); | 
					
						
							|  |  |  |     for (const status of statuses) { | 
					
						
							|  |  |  |       const name = status.name.padEnd(serviceNameMinLength); | 
					
						
							|  |  |  |       if (status.status === 'passing') { | 
					
						
							|  |  |  |         info(`${name} ✅`); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         info.group(`${name} ❌ (Updated: ${status.lastUpdated.toLocaleString()})`); | 
					
						
							|  |  |  |         info(`  Details: ${status.description}`); | 
					
						
							|  |  |  |         info.groupEnd(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-26 13:49:43 -07:00
										 |  |  |     info.groupEnd(); | 
					
						
							| 
									
										
										
										
											2020-09-14 11:24:15 -07:00
										 |  |  |     info(); | 
					
						
							| 
									
										
										
										
											2020-08-26 13:49:43 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 11:24:15 -07:00
										 |  |  |   /** Retrieve the status information for a service which uses a standard API response. */ | 
					
						
							|  |  |  |   async getStatusFromStandardApi(service: ServiceConfig): Promise<StatusCheckResult> { | 
					
						
							|  |  |  |     const result = await fetch(service.url).then(result => result.json()); | 
					
						
							|  |  |  |     const status = result.status.indicator === 'none' ? 'passing' : 'failing'; | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       name: service.name, | 
					
						
							|  |  |  |       status, | 
					
						
							|  |  |  |       description: result.status.description, | 
					
						
							|  |  |  |       lastUpdated: new Date(result.page.updated_at) | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-26 13:49:43 -07:00
										 |  |  | } |