Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Main][Task]27923018: Post Channel getOfflineSupport should set correct headers and url based on payload data #2338

Merged
merged 5 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions channels/1ds-post-js/src/HttpManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
_IInternalXhrOverride, _ISendPostMgrConfig, _ISenderOnComplete, _eExtendedInternalMessageId, _eInternalMessageId, _getAllResponseHeaders,
_throwInternal, _warnToConsole, arrForEach, dateNow, doPerf, dumpObj, eLoggingSeverity, extend, getCommonSchemaMetaData, getNavigator,
getResponseText, getTime, hasOwnProperty, isBeaconsSupported, isFetchSupported, isNullOrUndefined, isReactNative, isUndefined,
isValueAssigned, objForEachKey, objKeys, onConfigChange, prependTransports, strUndefined
isValueAssigned, objForEachKey, objKeys, onConfigChange, optimizeObject, prependTransports, strUndefined
} from "@microsoft/1ds-core-js";
import { arrAppend } from "@nevware21/ts-utils";
import { BatchNotificationAction, BatchNotificationActions } from "./BatchNotificationActions";
Expand Down Expand Up @@ -331,9 +331,55 @@
}

_self.getOfflineRequestDetails = () => {
return null;
}

_self.createOneDSPayload = (evts: ITelemetryItem[], optimize?: boolean) => {
try {
let payload = _serializer && _serializer.createPayload(0, false, false, false, SendRequestReason.NormalSchedule, EventSendType.Batched);
return _buildRequestDetails(payload, _useHeaders);
// TODO: optimize
let theBatches: EventBatch[] = [];
// create a eventBatch for each event
arrForEach(evts, (evt) => {
if (optimize) {
evt = optimizeObject(evt)
}
let batch = EventBatch.create(evt.iKey, [evt]);
theBatches.push(batch);
})

let thePayload: ISerializedPayload = null;

while (theBatches && theBatches.length && _serializer) {
let theBatch = theBatches.shift();
if (theBatch && theBatch.count() > 0) {
thePayload = thePayload || _serializer.createPayload(0, false, false, false, SendRequestReason.NormalSchedule, EventSendType.Batched);
_serializer.appendPayload(thePayload, theBatch, maxEventsPerBatch)
}
}

let requestDetails = _buildRequestDetails(thePayload, _useHeaders);

let payloadData: IPayloadData = {
data: thePayload.payloadBlob,
urlString: requestDetails.url,
headers: requestDetails.hdrs,
timeout: _xhrTimeout,
disableXhrSync: _disableXhrSync,
disableFetchKeepAlive: _disableFetchKeepAlive
};

// Only automatically add the following headers if already sending headers and we are not attempting to avoid an options call
if (_useHeaders) {
if (!_hasHeader(payloadData.headers, STR_CACHE_CONTROL)) {
payloadData.headers[STR_CACHE_CONTROL] = DEFAULT_CACHE_CONTROL;
}

if (!_hasHeader(payloadData.headers, STR_CONTENT_TYPE_HEADER)) {
payloadData.headers[STR_CONTENT_TYPE_HEADER] = DEFAULT_CONTENT_TYPE;
}
}

return payloadData;

} catch (e) {
// eslint-disable-next-line no-empty
Expand All @@ -342,6 +388,8 @@
return null;
}



// Special internal method to allow the DebugPlugin to hook embedded objects
function _getSenderInterface(transports: TransportType[], syncSupport: boolean): _IInternalXhrOverride {
try {
Expand Down Expand Up @@ -1355,4 +1403,14 @@
// @DynamicProtoStub - DO NOT add any code as this will be removed during packaging
return null;
}

/**
* Create payload data
* @param evts telemetry events
* @returns payload
*/
public createOneDSPayload(evts?: ITelemetryItem[], optimize?: boolean): IPayloadData {
// @DynamicProtoStub - DO NOT add any code as this will be removed during packaging
return null;
}
}
23 changes: 16 additions & 7 deletions channels/1ds-post-js/src/PostChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export class PostChannel extends BaseTelemetryPlugin implements IChannelControls
_self.getOfflineSupport = () => {
try {
let details = _httpManager && _httpManager.getOfflineRequestDetails();
if (details) {
if (_httpManager) {
return {
getUrl: () => {
return details.url
Expand All @@ -246,12 +246,21 @@ export class PostChannel extends BaseTelemetryPlugin implements IChannelControls
shouldProcess: (evt) => {
return !_disableTelemetry;
},
createPayload: (evt) => {
return {
urlString: details.url,
headers: details.hdrs,
data: evt
} as IPayloadData;
createPayload: (evt, ikeys?: string[]) => {
return null;
// should get new url headers based on payload directly
// let curDetails = _httpManager && _httpManager.getOfflineRequestDetails();
// return {
// urlString: curDetails.url,
// headers: curDetails.hdrs,
// data: evt
// } as IPayloadData;
},
createOneDSPayload: (evts: ITelemetryItem[]) => {
if (_httpManager.createOneDSPayload) {
return _httpManager.createOneDSPayload(evts, _optimizeObject);
}

}
} as IInternalOfflineSupport;

Expand Down
10 changes: 9 additions & 1 deletion channels/offline-channel-js/src/OfflineChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,16 @@
let sentItems = evts.slice(0, idx + 1);

_inMemoBatch = _inMemoBatch.createNew(_endpoint, inMemo.getItems().slice(idx + 1), _evtsLimitInMemo);

let payloadData: IStorageTelemetryItem = null;
if (_offineSupport && _offineSupport.createOneDSPayload) {
payloadData = _offineSupport.createOneDSPayload(sentItems);
payloadData.criticalCnt = criticalCnt
} else {
payloadData = _constructPayloadData(payloadArr, criticalCnt);
}

let payloadData = _constructPayloadData(payloadArr, criticalCnt);

let callback: OfflineBatchStoreCallback = (res) => {
if (!res || !res.state) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export interface IInternalOfflineSupport {
*/
shouldProcess?: (evt: ITelemetryItem) => boolean;

/**
* Create 1ds payload data
* @param evts ITelemetryItems
* @returns IPayloadData
*/
createOneDSPayload?: (evts: ITelemetryItem[]) => IPayloadData;

}

/**
Expand Down
Loading