From 0ceca4244b81df2899bea8e10c62d819a09fc220 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Thu, 23 Jan 2020 18:37:45 +0000 Subject: [PATCH] [swagger-stats] Add new definition (#41818) * Allow undefined bind parameters * Upgrading oracledbto 4.1 * Update version number * Fix tsconfig typo * Initial pass. Needs linting * Fix lint errors for swagger-stats * Remove express from package.json --- types/swagger-stats/index.d.ts | 210 +++++++++++++++++++++ types/swagger-stats/package.json | 7 + types/swagger-stats/swagger-stats-tests.ts | 91 +++++++++ types/swagger-stats/tsconfig.json | 49 +++++ types/swagger-stats/tslint.json | 1 + 5 files changed, 358 insertions(+) create mode 100644 types/swagger-stats/index.d.ts create mode 100644 types/swagger-stats/package.json create mode 100644 types/swagger-stats/swagger-stats-tests.ts create mode 100644 types/swagger-stats/tsconfig.json create mode 100644 types/swagger-stats/tslint.json diff --git a/types/swagger-stats/index.d.ts b/types/swagger-stats/index.d.ts new file mode 100644 index 0000000000..5dfa75e04c --- /dev/null +++ b/types/swagger-stats/index.d.ts @@ -0,0 +1,210 @@ +// Type definitions for swagger-stats 0.95 +// Project: http://swaggerstats.io +// Definitions by: Connor Fitzgerald +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +// TypeScript Version: 2.9 + +/// + +import { Server } from '@hapi/hapi'; +import { RequestHandler } from 'express'; +import { FastifyInstance } from 'fastify'; +import * as PromClient from 'prom-client'; + +export interface SWStats { + hostname: string; + name: string; + version: string; + ip: string; + swaggerSpec: null | Record; + uriPath: string; + timelineBucketDuration: number; + durationBuckets: number[]; + requestSizeBuckets: number[]; + responseSizeBuckets: number[]; + apdexThreshold: number; + onResponseFinish: null | (() => void); + authentication: boolean; + onAuthenticate: null | (() => void); + sessionMaxAge: number; + elasticsearch: null | string; + elasticsearchIndexPrefix: string; + elasticsearchUsername: string | null; + elasticsearchPassword: string | null; + swaggerOnly: boolean; + metricsPrefix: string; + enableEgress: boolean; + pathUI: string; + pathDist: string; + pathStats: string; + pathMetrics: string; + pathLogout: string; +} + +export namespace getHapiPlugin { + const name: string; + const version: string; + function register(server: Server, opts?: Partial): Promise; +} + +export function getFastifyPlugin( + fastify: FastifyInstance, + opts: Partial, + done: () => void, +): void; + +export function getMiddleware(opts: Partial): RequestHandler; + +export interface ReqResStats { + requests: number; + responses: number; + errors: number; + info: number; + success: number; + redirect: number; + client_error: number; + server_error: number; + total_time: number; + max_time: number; + avg_time: number; + total_req_clength: number; + max_req_clength: number; + avg_req_clength: number; + total_res_clength: number; + max_res_clength: number; + avg_res_clength: number; + req_rate: number; + err_rate: number; + apdex_threshold: number; + apdex_satisfied: number; + apdex_tolerated: number; + apdex_score: number; +} + +export interface SysStats { + rss: number; + heapTotal: number; + heapUsed: number; + external: number; + cpu: number; +} + +export interface TimelineStatsData { + stats: ReqResStats; + sys: SysStats; +} + +export interface TimelineStats { + settings: { + bucket_duration: number; + bucket_current: number; + length: number; + }; + data: Record; +} + +type HTTPMethodSubset = 'GET' | 'POST' | 'PUT' | 'DELETE'; +export type HTTPMethod = + | HTTPMethodSubset + | 'HEAD' + | 'OPTIONS' + | 'TRACE' + | 'PATCH'; + +export interface RequestResponseRecord { + path: string; + method: string; + query: string; + startts: number; + endts: number; + responsetime: number; + node: { + name: string; + version: string; + hostname: string; + ip: string; + }; + http: { + request: { + url: string; + headers?: Record; + clength?: number; + route_path?: string; + params?: Record; + query?: Record; + body?: any; + }; + response: { + code: string; + class: string; + phrase: string; + headers?: Record; + clength?: number; + }; + }; + ip: string; + real_ip: string; + port: string; + '@timestamp': string; + api: { + path: string; + query: string; + swagger?: string; + deprecated?: string; + operationId?: string; + tags?: string; + params?: string; + }; +} + +export interface APIOperationDefinition { + swagger: boolean; + deprecated: boolean; + description?: string; + operationId?: string; + summary?: string; + tags?: any; +} + +export interface ErrorsStats { + statuscode: Record; + topnotfound: Record; + topservererror: Record; +} + +export interface APIOperationStats { + defs?: APIOperationDefinition; + stats?: APIOperationDefinition; + details?: APIOperationDefinition; +} + +export interface CoreStats { + startts: number; + all: ReqResStats; + egress: ReqResStats; + sys: SysStats; + name: string; + version: string; + hostname: string; + ip: string; + apdexThreshold: number; + method?: Record; + timeline?: TimelineStats; + lasterrors?: RequestResponseRecord[]; + longestreq?: RequestResponseRecord[]; + apidefs?: Record>; + apistats?: Record>; + errors?: ErrorsStats; + apiop?: Record>; +} + +export function getCoreStats(): CoreStats; + +export function getPromStats(): string; + +export function getPromClient(): typeof PromClient; + +export function stop(): void; + +export {}; diff --git a/types/swagger-stats/package.json b/types/swagger-stats/package.json new file mode 100644 index 0000000000..2db1f1b764 --- /dev/null +++ b/types/swagger-stats/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "dependencies": { + "fastify": ">=2.11.0", + "prom-client": ">=11.5.3" + } +} diff --git a/types/swagger-stats/swagger-stats-tests.ts b/types/swagger-stats/swagger-stats-tests.ts new file mode 100644 index 0000000000..b6453e70ea --- /dev/null +++ b/types/swagger-stats/swagger-stats-tests.ts @@ -0,0 +1,91 @@ +import { + getHapiPlugin, + getFastifyPlugin, + getMiddleware, + getCoreStats, + getPromStats, + getPromClient, + stop, +} from 'swagger-stats'; +import { Server } from '@hapi/hapi'; +import * as fastify from 'fastify'; +import * as express from 'express'; + +const isDefined = (input: any) => { + if (input === undefined) throw new Error('Expected value to be defined'); + + return; +}; + +const testHapi = async () => { + isDefined(getHapiPlugin.name); + isDefined(getHapiPlugin.version); + + const hapiServer = new Server(); + + await getHapiPlugin.register(hapiServer); +}; + +// testHapi(); + +const testFastify = () => { + isDefined(getFastifyPlugin); + getFastifyPlugin(fastify(), {}, () => console.log('Fastify loaded')); +}; + +// testFastify(); + +const testExpress = () => { + isDefined(getMiddleware); + + const app = express(); + + const middleware = getMiddleware({ ip: '1.1.1.1' }); + + app.use(middleware); +}; + +// testExpress(); + +const testCoreStats = () => { + isDefined(getCoreStats); + setTimeout(() => { + const stats = getCoreStats(); + + console.log(stats); + }, 3000); +}; + +testCoreStats(); + +const testPromStats = () => { + isDefined(getPromStats); + setTimeout(() => { + const stats = getPromStats(); + + console.log(stats); + }, 3000); +}; + +testPromStats(); + +const testPromClient = () => { + isDefined(getPromClient); + setTimeout(() => { + const client = getPromClient(); + + isDefined(client); + isDefined(client.Counter); + + new client.Counter({ + name: 'test', + help: 'test', + }); + }, 3000); +}; + +testPromClient(); + +isDefined(stop); + +stop(); diff --git a/types/swagger-stats/tsconfig.json b/types/swagger-stats/tsconfig.json new file mode 100644 index 0000000000..691a443dca --- /dev/null +++ b/types/swagger-stats/tsconfig.json @@ -0,0 +1,49 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "paths": { + "@hapi/hapi": [ + "hapi__hapi" + ], + "@hapi/boom": [ + "hapi__boom" + ], + "@hapi/shot": [ + "hapi__shot" + ], + "@hapi/mimos": [ + "hapi__mimos" + ], + "@hapi/iron": [ + "hapi__iron" + ], + "@hapi/joi": [ + "hapi__joi" + ], + "@hapi/podium": [ + "hapi__podium" + ], + "@hapi/catbox": [ + "hapi__catbox" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "swagger-stats-tests.ts" + ] +} diff --git a/types/swagger-stats/tslint.json b/types/swagger-stats/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/swagger-stats/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }