Skip to content

Commit

Permalink
Remove defaults from historic validation (#1006)
Browse files Browse the repository at this point in the history
Remove defaults from historic validation. Allow partial creation of historic parameters.

Previous behavior leveraged the nullability of the checkSchema
mutation. By removing defaults from the flags, we can expect behavior
to be the same as it was before when no historic flags are provided.
  • Loading branch information
trevor-scheer authored Feb 11, 2019
1 parent 52b7352 commit d296e4f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- `apollo`
- Support validation parameters for service:check [#953](https://github.com/apollographql/apollo-tooling/pull/953)
- Leverage nullability of validation parameters [#1006](https://github.com/apollographql/apollo-tooling/pull/1006)
- `apollo-language-server`
- Better error handling in ApolloEngineClient [#953](https://github.com/apollographql/apollo-tooling/pull/953)
- `vscode-apollo`
Expand Down
11 changes: 4 additions & 7 deletions packages/apollo/src/commands/service/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ export default class ServiceCheck extends ProjectCommand {
}),
validationPeriod: flags.string({
description:
"The size of the time window with which to validate the schema against. You may provide a number (in seconds), or an ISO8601 format duration for more granularity (see: https://en.wikipedia.org/wiki/ISO_8601#Durations)",
default: "86400"
"The size of the time window with which to validate the schema against. You may provide a number (in seconds), or an ISO8601 format duration for more granularity (see: https://en.wikipedia.org/wiki/ISO_8601#Durations)"
}),
queryCountThreshold: flags.integer({
description:
"Minimum number of requests within the requested time window for a query to be considered.",
default: 1
"Minimum number of requests within the requested time window for a query to be considered."
}),
queryCountThresholdPercentage: flags.integer({
description:
"Number of requests within the requested time window for a query to be considered, relative to total request count. Expected values are between 0 and 0.05 (minimum 5% of total request volume)",
default: 0
"Number of requests within the requested time window for a query to be considered, relative to total request count. Expected values are between 0 and 0.05 (minimum 5% of total request volume)"
})
};

Expand Down Expand Up @@ -88,7 +85,7 @@ export default class ServiceCheck extends ProjectCommand {
tag: flags.tag,
gitContext: ctx.gitContext,
frontend: flags.frontend || config.engine.frontend,
historicParameters
...(historicParameters && { historicParameters })
});
}
}
Expand Down
29 changes: 29 additions & 0 deletions packages/apollo/src/utils/__tests__/validateHistoricParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,35 @@ describe("validateHistoricParams", () => {
})
).toThrow(/--queryCountThresholdPercentage/);
});

it("handles partial input", () => {
expect(validateHistoricParams({})).toEqual(null);

expect(
validateHistoricParams({
validationPeriod: "P1D"
})
).toEqual({ to: -0, from: -86400 });

expect(
validateHistoricParams({
queryCountThreshold: 1
})
).toEqual({ queryCountThreshold: 1 });

expect(
validateHistoricParams({
queryCountThresholdPercentage: 50
})
).toEqual({ queryCountThresholdPercentage: 0.5 });

expect(
validateHistoricParams({
validationPeriod: "P1D",
queryCountThresholdPercentage: 50
})
).toEqual({ to: -0, from: -86400, queryCountThresholdPercentage: 0.5 });
});
});

function getValidParams(validationPeriod: string) {
Expand Down
65 changes: 40 additions & 25 deletions packages/apollo/src/utils/validateHistoricParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,61 @@ export function validateHistoricParams({
validationPeriod,
queryCountThreshold,
queryCountThresholdPercentage
}: {
}: Partial<{
validationPeriod: string;
queryCountThreshold: number;
queryCountThresholdPercentage: number;
}): HistoricQueryParameters {
// Validation period can be one of two things:
// 1) a number in seconds
// 2) an ISO 8601 formatted duration, i.e. "P1D", "P10DT1H", "PT6H"
const from = isNumeric(validationPeriod)
? -1 * duration(Number(validationPeriod), "seconds").asSeconds()
: -1 * duration(validationPeriod).asSeconds();

if (from >= 0) {
throw new Error(
"Please provide a valid duration for the --validationPeriod flag. Valid durations are represented in ISO 8601, see: https://bit.ly/2DEJ3UN."
);
}>): Partial<HistoricQueryParameters> | null {
if (
!validationPeriod &&
!queryCountThreshold &&
!queryCountThresholdPercentage
) {
return null;
}

if (!Number.isInteger(queryCountThreshold) || queryCountThreshold < 1) {
throw new Error(
"Please provide a valid number for the --queryCountThreshold flag. Valid numbers are integers in the range x >= 1."
);
let from: number | null = null;
if (validationPeriod) {
// Validation period can be one of two things:
// 1) a number in seconds
// 2) an ISO 8601 formatted duration, i.e. "P1D", "P10DT1H", "PT6H"
from = isNumeric(validationPeriod)
? -1 * duration(Number(validationPeriod), "seconds").asSeconds()
: -1 * duration(validationPeriod).asSeconds();

if (from >= 0) {
throw new Error(
"Please provide a valid duration for the --validationPeriod flag. Valid durations are represented in ISO 8601, see: https://bit.ly/2DEJ3UN."
);
}
}

if (
queryCountThresholdPercentage < 0 ||
queryCountThresholdPercentage > 100
queryCountThreshold &&
(!Number.isInteger(queryCountThreshold) || queryCountThreshold < 1)
) {
throw new Error(
"Please provide a valid number for the --queryCountThresholdPercentage flag. Valid numbers are in the range 0 <= x <= 100."
"Please provide a valid number for the --queryCountThreshold flag. Valid numbers are integers in the range x >= 1."
);
}

const asPercentage = queryCountThresholdPercentage / 100;
let asPercentage: number | null = null;
if (queryCountThresholdPercentage) {
if (
queryCountThresholdPercentage < 0 ||
queryCountThresholdPercentage > 100
) {
throw new Error(
"Please provide a valid number for the --queryCountThresholdPercentage flag. Valid numbers are in the range 0 <= x <= 100."
);
}
asPercentage = queryCountThresholdPercentage / 100;
}

return {
to: -0,
from,
queryCountThreshold,
queryCountThresholdPercentage: asPercentage
...(from && { to: -0, from }),
...(queryCountThreshold && { queryCountThreshold }),
...(asPercentage && { queryCountThresholdPercentage: asPercentage })
};
}

Expand Down

0 comments on commit d296e4f

Please sign in to comment.