Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Jul 27, 2023
1 parent bbe46da commit 8cd8501
Showing 1 changed file with 53 additions and 37 deletions.
90 changes: 53 additions & 37 deletions packages/shared/sdk-server/src/MigrationOpTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
} from './api/data';
import { LDMigrationOrigin } from './api/LDMigration';

function isPopulated(data: number): boolean {
return !Number.isNaN(data);
}

export default class MigrationOpTracker implements LDMigrationTracker {
private errors = {
old: false,
Expand Down Expand Up @@ -65,43 +69,11 @@ export default class MigrationOpTracker implements LDMigrationTracker {
createEvent(): LDMigrationOpEvent | undefined {
if (this.operation && this.context.valid) {
const measurements: LDMigrationMeasurement[] = [];
if (
// Cannot use a truthy check as 0 is a desired value.
this.consistencyCheck !== undefined &&
this.consistencyCheck !== LDConsistencyCheck.NotChecked
) {
measurements.push({
key: 'consistent',
value: this.consistencyCheck,
// TODO: Needs to come from someplace.
samplingOdds: 0,
});
}
if (
!Number.isNaN(this.latencyMeasurement.new) ||
!Number.isNaN(this.latencyMeasurement.old)
) {
const values: { old?: number; new?: number } = {};
if (!Number.isNaN(this.latencyMeasurement.new)) {
values.new = this.latencyMeasurement.new;
}
if (!Number.isNaN(this.latencyMeasurement.old)) {
values.old = this.latencyMeasurement.old;
}
measurements.push({
key: 'latency',
values,
});
}
if (this.errors.new || this.errors.old) {
measurements.push({
key: 'error',
values: {
old: this.errors.old ? 1 : 0,
new: this.errors.new ? 1 : 0,
},
});
}

this.populateConsistency(measurements);
this.populateLatency(measurements);
this.populateErrors(measurements);

return {
kind: 'migration_op',
operation: this.operation,
Expand All @@ -119,4 +91,48 @@ export default class MigrationOpTracker implements LDMigrationTracker {
}
return undefined;
}

private populateConsistency(measurements: LDMigrationMeasurement[]) {
if (
this.consistencyCheck !== undefined &&
this.consistencyCheck !== LDConsistencyCheck.NotChecked
) {
measurements.push({
key: 'consistent',
value: this.consistencyCheck,
// TODO: Needs to come from someplace.
samplingOdds: 0,
});
}
}

private populateErrors(measurements: LDMigrationMeasurement[]) {
if (this.errors.new || this.errors.old) {
measurements.push({
key: 'error',
values: {
old: this.errors.old ? 1 : 0,
new: this.errors.new ? 1 : 0,
},
});
}
}

private populateLatency(measurements: LDMigrationMeasurement[]) {
const newIsPopulated = isPopulated(this.latencyMeasurement.new);
const oldIsPopulated = isPopulated(this.latencyMeasurement.old);
if (newIsPopulated || oldIsPopulated) {
const values: { old?: number; new?: number } = {};
if (newIsPopulated) {
values.new = this.latencyMeasurement.new;
}
if (oldIsPopulated) {
values.old = this.latencyMeasurement.old;
}
measurements.push({
key: 'latency',
values,
});
}
}
}

0 comments on commit 8cd8501

Please sign in to comment.