Skip to content

Commit

Permalink
fix: ensure sites have telemetry (#1430)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjuniper authored Mar 6, 2024
1 parent 1ea5750 commit abc5205
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/common/src/sites/HubSites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const DEFAULT_SITE: Partial<IHubSite> = {
feedback: true,
},
},
telemetry: {},
};

/**
Expand Down
25 changes: 25 additions & 0 deletions packages/common/src/sites/_internal/ensureBaseTelemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getProp } from "../../objects";
import { IDraft, IModel } from "../../types";
import { cloneObject } from "../../util";

/**
* During a period in late 2023 and early 2024, the python api
* was writing a bad confirmation for basemaps as part of the site
* cloning process. This migration will fix that.
* @param {Object} model Site Model
* @private
*/
export function ensureBaseTelemetry<T extends IModel | IDraft>(model: T) {
// Unlike other migrations, this is not based on a version
// rather it checks for a missing telemetry property.

// Early exit if the telemetry is there
if (getProp(model, "data.telemetry")) {
return model;
}

const clone = cloneObject(model);
clone.data.telemetry = {};

return clone;
}
2 changes: 2 additions & 0 deletions packages/common/src/sites/upgrade-site-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { _migrateEventListCardConfigs } from "./_internal/_migrate-event-list-ca
import { migrateLegacyCapabilitiesToFeatures } from "./_internal/capabilities/migrateLegacyCapabilitiesToFeatures";
import { _migrateTelemetryConfig } from "./_internal/_migrate-telemetry-config";
import { migrateBadBasemap } from "./_internal/migrateBadBasemap";
import { ensureBaseTelemetry } from "./_internal/ensureBaseTelemetry";

/**
* Upgrades the schema upgrades
Expand All @@ -37,5 +38,6 @@ export function upgradeSiteSchema(model: IModel) {

// apply versionless migrations
model = migrateBadBasemap(model);
model = ensureBaseTelemetry(model);
return model;
}
23 changes: 23 additions & 0 deletions packages/common/test/sites/_internal/ensureBaseTelemetry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IModel } from "../../../src";
import { ensureBaseTelemetry } from "../../../src/sites/_internal/ensureBaseTelemetry";

describe("ensure base telemtry:", () => {
it("returns model if telemetry is present", () => {
const model: IModel = {
data: {
telemetry: {},
},
} as unknown as IModel;
const chk = ensureBaseTelemetry(model);
expect(chk).toBe(model);
});

it("returns clone with telemetry when telemetry is not present", () => {
const model: IModel = {
data: {},
} as unknown as IModel;
const chk = ensureBaseTelemetry(model);
expect(chk).not.toBe(model);
expect(chk?.data?.telemetry).toBeDefined();
});
});
8 changes: 8 additions & 0 deletions packages/common/test/sites/upgrade-site-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as _migrateEventListCardConfigs from "../../src/sites/_internal/_migrat
import * as migrateLegacyCapabilitiesToFeatures from "../../src/sites/_internal/capabilities/migrateLegacyCapabilitiesToFeatures";
import * as _migrateTelemetryConfig from "../../src/sites/_internal/_migrate-telemetry-config";
import * as migrateBadBasemapModule from "../../src/sites/_internal/migrateBadBasemap";
import * as ensureBaseTelemetry from "../../src/sites/_internal/ensureBaseTelemetry";
import { IModel } from "../../src";
import { SITE_SCHEMA_VERSION } from "../../src/sites/site-schema-version";
import { expectAllCalled, expectAll } from "./test-helpers.test";
Expand All @@ -24,6 +25,7 @@ describe("upgradeSiteSchema", () => {
let migrateLegacyCapabilitiesToFeaturesSpy: jasmine.Spy;
let migrateTelemetryConfigSpy: jasmine.Spy;
let migrateBadBasemapSpy: jasmine.Spy;
let ensureBaseTelemetrySpy: jasmine.Spy;
beforeEach(() => {
applySpy = spyOn(_applySiteSchemaModule, "_applySiteSchema").and.callFake(
(model: IModel) => model
Expand Down Expand Up @@ -64,6 +66,10 @@ describe("upgradeSiteSchema", () => {
migrateBadBasemapModule,
"migrateBadBasemap"
).and.callFake((model: IModel) => model);
ensureBaseTelemetrySpy = spyOn(
ensureBaseTelemetry,
"ensureBaseTelemetry"
).and.callFake((model: IModel) => model);
});

it("runs schema upgrades", async () => {
Expand All @@ -89,6 +95,7 @@ describe("upgradeSiteSchema", () => {
migrateLegacyCapabilitiesToFeaturesSpy,
migrateTelemetryConfigSpy,
migrateBadBasemapSpy,
ensureBaseTelemetrySpy,
],
expect
);
Expand Down Expand Up @@ -122,5 +129,6 @@ describe("upgradeSiteSchema", () => {
);
// Versionless migrations should still run
expectAll([migrateBadBasemapSpy], "toHaveBeenCalled", true, expect);
expectAll([ensureBaseTelemetrySpy], "toHaveBeenCalled", true, expect);
});
});
3 changes: 3 additions & 0 deletions packages/sites/src/ensure-required-site-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export function ensureRequiredSiteProperties(
caps.push("socialSharing");
}
deepSet(model, "data.values.capabilities", caps);
if (!getProp(model, "data.telemetry")) {
deepSet(model, "data.telemetry", {});
}
// return the clone
return model;
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ describe("ensureRequiredSiteProperties", () => {
const model = {
item: {},
data: {
telemetry: {},
values: {
subdomain: "name-org",
defaultHostname: "name-org.hub.arcgis.com",
Expand Down

0 comments on commit abc5205

Please sign in to comment.