feat(platform): add managed data product history plane
This commit is contained in:
@@ -9,6 +9,7 @@ import { NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
DATA_PRODUCT_BASE_PATH,
|
||||
DATA_PRODUCT_HISTORY_SUFFIX,
|
||||
DATA_PRODUCT_READER_CATALOG_PATH,
|
||||
DATA_PRODUCT_SNAPSHOT_SUFFIX,
|
||||
DATA_PLANE_DEFAULT_BASE_URL,
|
||||
@@ -16,7 +17,12 @@ import {
|
||||
NDC_DATA_PRODUCT_READER_CREDENTIAL,
|
||||
NDC_NODE_ICON,
|
||||
} from '../shared/constants';
|
||||
import { normalizeSnapshotFacts, snapshotReadParameters } from '../shared/contracts';
|
||||
import {
|
||||
historyReadParameters,
|
||||
normalizeHistoryResponse,
|
||||
normalizeSnapshotFacts,
|
||||
snapshotReadParameters,
|
||||
} from '../shared/contracts';
|
||||
import { safeHttpError, safeInputError } from '../shared/errors';
|
||||
import { loadDataProductOptions, ndcRequest, serviceBaseUrl } from '../shared/http';
|
||||
|
||||
@@ -26,14 +32,25 @@ export class NdcDataProductRead implements INodeType {
|
||||
name: 'ndcDataProductRead',
|
||||
icon: NDC_NODE_ICON,
|
||||
group: ['input'],
|
||||
version: 1,
|
||||
subtitle: '={{$parameter["dataProductId"]}}',
|
||||
description: 'Read a scoped NDC Data Product snapshot',
|
||||
version: [1, 2],
|
||||
subtitle: '={{$parameter["mode"] || "snapshot"}} · {{$parameter["dataProductId"]}}',
|
||||
description: 'Read a scoped NDC Data Product snapshot or bounded history',
|
||||
defaults: { name: 'NDC Data Product Read' },
|
||||
inputs: [NodeConnectionTypes.Main],
|
||||
outputs: [NodeConnectionTypes.Main],
|
||||
credentials: [{ name: NDC_DATA_PRODUCT_READER_CREDENTIAL, required: true }],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Mode',
|
||||
name: 'mode',
|
||||
type: 'options',
|
||||
options: [
|
||||
{ name: 'Current Snapshot', value: 'snapshot' },
|
||||
{ name: 'History', value: 'history' },
|
||||
],
|
||||
default: 'snapshot',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Data Product Name or ID',
|
||||
name: 'dataProductId',
|
||||
@@ -50,6 +67,59 @@ export class NdcDataProductRead implements INodeType {
|
||||
default: 1000,
|
||||
required: true,
|
||||
description: 'Maximum number of snapshot facts to return',
|
||||
displayOptions: { show: { mode: ['snapshot'] } },
|
||||
},
|
||||
{
|
||||
displayName: 'From',
|
||||
name: 'from',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
required: true,
|
||||
displayOptions: { show: { mode: ['history'] } },
|
||||
},
|
||||
{
|
||||
displayName: 'To',
|
||||
name: 'to',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
required: true,
|
||||
displayOptions: { show: { mode: ['history'] } },
|
||||
},
|
||||
{
|
||||
displayName: 'Resolution (MS)',
|
||||
name: 'resolutionMs',
|
||||
type: 'number',
|
||||
typeOptions: { minValue: 1000, maxValue: 86400000, numberStepSize: 1000 },
|
||||
default: 60000,
|
||||
required: true,
|
||||
description: 'Provider-neutral history bucket size; must be a multiple of the Data Product native history interval',
|
||||
displayOptions: { show: { mode: ['history'] } },
|
||||
},
|
||||
{
|
||||
displayName: 'Source IDs',
|
||||
name: 'sourceIds',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'Optional comma-separated entity IDs; empty reads all entities in the scoped Data Product',
|
||||
displayOptions: { show: { mode: ['history'] } },
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
typeOptions: { minValue: 1, maxValue: 5000, numberStepSize: 1 },
|
||||
default: 50,
|
||||
required: true,
|
||||
description: 'Max number of results to return',
|
||||
displayOptions: { show: { mode: ['history'] } },
|
||||
},
|
||||
{
|
||||
displayName: 'Cursor',
|
||||
name: 'cursor',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'Opaque continuation cursor returned by the previous history page',
|
||||
displayOptions: { show: { mode: ['history'] } },
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -69,7 +139,48 @@ export class NdcDataProductRead implements INodeType {
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const mode = this.getNodeParameter('mode', 0, 'snapshot') as string;
|
||||
const dataProductId = this.getNodeParameter('dataProductId', 0) as string;
|
||||
if (mode === 'history') {
|
||||
let parameters;
|
||||
try {
|
||||
parameters = historyReadParameters({
|
||||
dataProductId,
|
||||
from: this.getNodeParameter('from', 0),
|
||||
to: this.getNodeParameter('to', 0),
|
||||
resolutionMs: this.getNodeParameter('resolutionMs', 0, 60000),
|
||||
sourceIds: this.getNodeParameter('sourceIds', 0, ''),
|
||||
limit: this.getNodeParameter('limit', 0, 1000),
|
||||
cursor: this.getNodeParameter('cursor', 0, ''),
|
||||
});
|
||||
} catch (error) {
|
||||
throw new NodeOperationError(this.getNode(), safeInputError(error, 'ndc_data_product_history_input_invalid'));
|
||||
}
|
||||
try {
|
||||
const baseUrl = serviceBaseUrl(DATA_PLANE_BASE_URL_ENV, DATA_PLANE_DEFAULT_BASE_URL);
|
||||
const response = await ndcRequest(this, NDC_DATA_PRODUCT_READER_CREDENTIAL, {
|
||||
method: 'GET',
|
||||
url: `${baseUrl}${DATA_PRODUCT_BASE_PATH}/${parameters.encodedProductId}${DATA_PRODUCT_HISTORY_SUFFIX}`,
|
||||
qs: {
|
||||
from: parameters.from,
|
||||
to: parameters.to,
|
||||
resolutionMs: parameters.resolutionMs,
|
||||
...(parameters.sourceIds.length ? { sourceIds: parameters.sourceIds.join(',') } : {}),
|
||||
limit: parameters.limit,
|
||||
...(parameters.cursor ? { cursor: parameters.cursor } : {}),
|
||||
},
|
||||
});
|
||||
return [[{ json: normalizeHistoryResponse(response), pairedItem: { item: 0 } }]];
|
||||
} catch (error) {
|
||||
const safe = error instanceof Error && error.message === 'data_product_history_invalid'
|
||||
? safeInputError(error, 'ndc_data_product_history_invalid')
|
||||
: safeHttpError(error, 'ndc_data_product_history_read_failed');
|
||||
throw new NodeOperationError(this.getNode(), safe);
|
||||
}
|
||||
}
|
||||
if (mode !== 'snapshot') {
|
||||
throw new NodeOperationError(this.getNode(), safeInputError(new Error('read_mode_invalid'), 'ndc_data_product_read_input_invalid'));
|
||||
}
|
||||
const pageSize = this.getNodeParameter('pageSize', 0, 1000) as number;
|
||||
let parameters;
|
||||
try {
|
||||
|
||||
@@ -5,6 +5,7 @@ export const DATA_PRODUCT_WRITER_CATALOG_PATH = '/internal/data-plane/v1/writer/
|
||||
export const DATA_PRODUCT_READER_CATALOG_PATH = '/internal/data-plane/v1/reader/data-products';
|
||||
export const DATA_PRODUCT_BASE_PATH = '/internal/data-plane/v1/data-products';
|
||||
export const DATA_PRODUCT_SNAPSHOT_SUFFIX = '/snapshot';
|
||||
export const DATA_PRODUCT_HISTORY_SUFFIX = '/history';
|
||||
export const FOUNDRY_BINDING_PATH = '/internal/foundry/v1/data-product-bindings';
|
||||
export const FOUNDRY_CATALOG_PATH = '/internal/foundry/v1/data-products';
|
||||
|
||||
|
||||
@@ -118,6 +118,19 @@ export function normalizeSnapshotFacts(value: unknown): IDataObject[] {
|
||||
});
|
||||
}
|
||||
|
||||
export function normalizeHistoryResponse(value: unknown): IDataObject {
|
||||
if (!isObject(value) || value.schemaVersion !== 'nodedc.data-product.history/v1'
|
||||
|| !isObject(value.dataProduct) || !isObject(value.query) || !Array.isArray(value.facts)) {
|
||||
throw new Error('data_product_history_invalid');
|
||||
}
|
||||
value.facts.forEach((fact, index) => {
|
||||
if (!isObject(fact) || Number.isNaN(Date.parse(String(fact.bucketStart ?? '')))) {
|
||||
throw new Error(`history_fact_${index}_invalid`);
|
||||
}
|
||||
});
|
||||
return value;
|
||||
}
|
||||
|
||||
export function snapshotReadParameters(
|
||||
dataProductId: unknown,
|
||||
pageSize: unknown,
|
||||
@@ -132,6 +145,52 @@ export function snapshotReadParameters(
|
||||
};
|
||||
}
|
||||
|
||||
export function historyReadParameters(value: {
|
||||
dataProductId: unknown;
|
||||
from: unknown;
|
||||
to: unknown;
|
||||
resolutionMs: unknown;
|
||||
sourceIds: unknown;
|
||||
limit: unknown;
|
||||
cursor: unknown;
|
||||
}): {
|
||||
encodedProductId: string;
|
||||
from: string;
|
||||
to: string;
|
||||
resolutionMs: number;
|
||||
sourceIds: string[];
|
||||
limit: number;
|
||||
cursor: string;
|
||||
} {
|
||||
const from = requireIsoTimestamp(value.from, 'from');
|
||||
const to = requireIsoTimestamp(value.to, 'to');
|
||||
if (Date.parse(from) >= Date.parse(to)) throw new Error('history_range_invalid');
|
||||
const resolutionMs = Number(value.resolutionMs);
|
||||
if (!Number.isInteger(resolutionMs) || resolutionMs < 1000 || resolutionMs > 24 * 60 * 60 * 1000) {
|
||||
throw new Error('history_resolution_invalid');
|
||||
}
|
||||
const limit = Number(value.limit);
|
||||
if (!Number.isInteger(limit) || limit < 1 || limit > 5000) throw new Error('history_limit_invalid');
|
||||
const sourceIds = [...new Set((Array.isArray(value.sourceIds)
|
||||
? value.sourceIds
|
||||
: String(value.sourceIds ?? '').split(','))
|
||||
.map((item) => String(item ?? '').trim())
|
||||
.filter(Boolean)
|
||||
.map((item) => requireIdentifier(item, 'sourceIds')))];
|
||||
if (sourceIds.length > 1000) throw new Error('history_source_ids_limit_exceeded');
|
||||
const cursor = String(value.cursor ?? '').trim();
|
||||
if (cursor && (cursor.length > 1024 || !/^[A-Za-z0-9_-]+$/.test(cursor))) throw new Error('history_cursor_invalid');
|
||||
return {
|
||||
encodedProductId: encodeIdentifierPath(value.dataProductId, 'dataProductId'),
|
||||
from,
|
||||
to,
|
||||
resolutionMs,
|
||||
sourceIds,
|
||||
limit,
|
||||
cursor,
|
||||
};
|
||||
}
|
||||
|
||||
export function dataProductOptions(value: unknown): INodeListSearchItems[] {
|
||||
if (!isObject(value)) throw new Error('data_product_catalog_invalid');
|
||||
const rows = Array.isArray(value.dataProducts)
|
||||
|
||||
Reference in New Issue
Block a user