1
1
/* eslint-disable camelcase */
2
- import * as queryString from 'query-string' ;
3
2
import * as Bluebird from 'bluebird' ;
4
3
5
4
import * as jsondiffpatch from 'jsondiffpatch' ;
6
5
import { AxiosResponse } from 'axios' ;
7
6
import { ApiEnv , argvToApiEnv } from '../apiEnv' ;
8
- import runQuery from '../run-query' ;
7
+ import runQuery , { AxiosResponseWithDuration } from '../run-query' ;
9
8
import { Change } from './change' ;
10
9
import { CompareFormatter } from './formatters/compare-formatter' ;
11
10
import {
@@ -15,12 +14,16 @@ import getFormatter from './formatters/get-formatter';
15
14
import QueryReader from './query-reader' ;
16
15
import { Query } from './query' ;
17
16
17
+ type CompareArgs = Pick < ParsedArgs , 'concurrency' | 'ignored_fields' | 'extra_params' | 'timeout' > ;
18
+
18
19
/**
19
- * @param root0
20
- * @param root0.oldApiEnv
21
- * @param root0.newApiEnv
22
- * @param root0.query
23
- * @param root0.argv
20
+ * Compare one query against two ApiEnvs, returning a Change object
21
+ *
22
+ * @param {ApiEnv } oldApiEnv old env to run query against
23
+ * @param {ApiEnv } newApiEnv new env to run query against
24
+ * @param {Query } query query to compare
25
+ * @param {CompareArgs } argv global command line args that affect running queries
26
+ * @returns {Promise<Change> } output of running query against both envs
24
27
*/
25
28
async function compareQuery ( {
26
29
oldApiEnv,
@@ -31,23 +34,14 @@ async function compareQuery({
31
34
oldApiEnv : ApiEnv ;
32
35
newApiEnv : ApiEnv ;
33
36
query : Query ;
34
- argv : ParsedArgs ;
37
+ argv : CompareArgs ;
35
38
} ) : Promise < Change > {
36
- const extraParams = queryString . parse ( argv . extra_params . join ( '&' ) ) as Record < string , string > ;
37
- delete extraParams . undefined ;
38
- const params = { ...query . params , ...extraParams } ;
39
- const queryWithExtraParams = {
40
- endpoint : query . endpoint ,
41
- method : query . method ,
42
- params,
43
- } ;
44
-
45
39
// if the query has a baseline response (running from a golden json file), use that
46
40
// otherwise run it against the old server
47
41
const oldResponse = query . baselineResponse
48
- ? ( { data : query . baselineResponse } as AxiosResponse < any > )
49
- : await runQuery ( oldApiEnv , queryWithExtraParams , argv . timeout ) ;
50
- const newResponse = await runQuery ( newApiEnv , queryWithExtraParams , argv . timeout ) ;
42
+ ? ( { data : query . baselineResponse } as AxiosResponse < unknown > )
43
+ : await runQuery ( oldApiEnv , query , argv . timeout ) ;
44
+ const newResponse = await runQuery ( newApiEnv , query , argv . timeout ) ;
51
45
52
46
const differ = jsondiffpatch . create ( {
53
47
propertyFilter ( name ) {
@@ -58,20 +52,23 @@ async function compareQuery({
58
52
const delta = differ . diff ( oldResponse . data , newResponse . data ) ;
59
53
60
54
return {
61
- query : queryWithExtraParams ,
55
+ query,
62
56
delta,
63
57
oldResponse,
64
58
newResponse,
65
59
} ;
66
60
}
67
61
68
62
/**
69
- * @param root0
70
- * @param root0.oldApiEnv
71
- * @param root0.newApiEnv
72
- * @param root0.queries
73
- * @param root0.argv
74
- * @param root0.formatter
63
+ * Compare and output many queries against two ApiEnvs. Passing along
64
+ * the changes for output to a ChangeFormatter.
65
+ *
66
+ * @param {ApiEnv } oldApiEnv old env to run query against
67
+ * @param {ApiEnv } newApiEnv new env to run query against
68
+ * @param {Query[] } queries queries to compare
69
+ * @param {CompareArgs } argv global command line args that affect running queries
70
+ * @param {CompareFormatter } formatter formatter to use for output
71
+ * @returns {Promise<void> } side effect only - outputs to output_file or stdout
75
72
*/
76
73
async function compareQueries ( {
77
74
oldApiEnv,
@@ -83,9 +80,9 @@ async function compareQueries({
83
80
oldApiEnv : ApiEnv ;
84
81
newApiEnv : ApiEnv ;
85
82
queries : Query [ ] ;
86
- argv : ParsedArgs ;
83
+ argv : CompareArgs ;
87
84
formatter : CompareFormatter ;
88
- } ) {
85
+ } ) : Promise < void > {
89
86
const oldResponseTimes : number [ ] = [ ] ;
90
87
const newResponseTimes : number [ ] = [ ] ;
91
88
const oldStatusCodes : Record < number , number > = { } ;
@@ -100,12 +97,11 @@ async function compareQueries({
100
97
query,
101
98
argv,
102
99
} ) ;
103
- formatter . queryRan ( ) ;
104
- if ( change . delta || argv . unchanged ) {
105
- formatter . logChange ( change ) ;
106
- }
107
- oldResponseTimes . push ( ( change . oldResponse as any ) . duration ) ;
108
- newResponseTimes . push ( ( change . newResponse as any ) . duration ) ;
100
+
101
+ formatter . queryCompleted ( change ) ;
102
+
103
+ oldResponseTimes . push ( ( change . oldResponse as AxiosResponseWithDuration ) . duration ) ;
104
+ newResponseTimes . push ( ( change . newResponse as AxiosResponseWithDuration ) . duration ) ;
109
105
110
106
if ( ! oldStatusCodes [ change . oldResponse . status ] ) {
111
107
oldStatusCodes [ change . oldResponse . status ] = 0 ;
@@ -131,24 +127,33 @@ async function compareQueries({
131
127
} ) ;
132
128
}
133
129
134
- const argv = parseArgv ( [ OLD_KEY , NEW_KEY ] ) as ParsedArgs ;
130
+ /** Main logic - parses command line, creates a formatter, runs queries and
131
+ * generates output.
132
+ *
133
+ * @returns {Promise<void> } on completion
134
+ */
135
+ function main ( ) : Promise < void > {
136
+ const argv = parseArgv ( [ OLD_KEY , NEW_KEY ] ) as ParsedArgs ;
135
137
136
- const oldApiEnv = argvToApiEnv ( argv [ OLD_KEY ] ) ;
137
- const newApiEnv = argvToApiEnv ( argv [ NEW_KEY ] ) ;
138
+ const oldApiEnv = argvToApiEnv ( argv [ OLD_KEY ] ) ;
139
+ const newApiEnv = argvToApiEnv ( argv [ NEW_KEY ] ) ;
138
140
139
- const queries = QueryReader ( argv ) ;
141
+ const queries = QueryReader ( argv ) ;
140
142
141
- const formatter = getFormatter ( argv . output_mode , {
142
- oldApiEnv,
143
- newApiEnv,
144
- argv,
145
- totalQueries : queries . length ,
146
- } ) ;
143
+ const formatter = getFormatter ( argv . output_mode , {
144
+ oldApiEnv,
145
+ newApiEnv,
146
+ argv,
147
+ totalQueries : queries . length ,
148
+ } ) ;
147
149
148
- compareQueries ( {
149
- oldApiEnv,
150
- newApiEnv,
151
- queries,
152
- argv,
153
- formatter,
154
- } ) ;
150
+ return compareQueries ( {
151
+ oldApiEnv,
152
+ newApiEnv,
153
+ queries,
154
+ argv,
155
+ formatter,
156
+ } ) ;
157
+ }
158
+
159
+ main ( ) ;
0 commit comments