forked from dipseth/dataproc-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield-analyzer.ts
More file actions
363 lines (314 loc) · 10.5 KB
/
field-analyzer.ts
File metadata and controls
363 lines (314 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Field Analyzer Service
* Automatic field detection and analysis for generic type conversion
*/
import { FieldAnalysisResult } from '../types/generic-converter.js';
import { logger } from '../utils/logger.js';
/**
* Service for analyzing object fields and determining conversion strategies
*/
export class FieldAnalyzer {
private readonly DEFAULT_SIZE_THRESHOLD = 10240; // 10KB
private readonly ARRAY_SIZE_MULTIPLIER = 100; // Estimate array overhead
/**
* Analyze fields in a source object to determine conversion strategy
*/
async analyzeFields<T extends Record<string, any>>(source: T): Promise<FieldAnalysisResult<T>> {
const result: FieldAnalysisResult<T> = {
compressibleFields: [],
primitiveFields: [],
objectFields: [],
arrayFields: [],
estimatedSizes: {} as Record<keyof T, number>,
};
for (const [key, value] of Object.entries(source)) {
const typedKey = key as keyof T;
const fieldType = this.getFieldType(value);
const estimatedSize = this.estimateFieldSize(value);
result.estimatedSizes[typedKey] = estimatedSize;
switch (fieldType) {
case 'primitive':
result.primitiveFields.push(typedKey);
break;
case 'array':
result.arrayFields.push(typedKey);
if (estimatedSize > this.DEFAULT_SIZE_THRESHOLD) {
result.compressibleFields.push(typedKey);
}
break;
case 'object':
result.objectFields.push(typedKey);
if (estimatedSize > this.DEFAULT_SIZE_THRESHOLD) {
result.compressibleFields.push(typedKey);
}
break;
}
}
logger.debug(`Field analysis completed for object with ${Object.keys(source).length} fields`, {
compressibleFields: result.compressibleFields.length,
primitiveFields: result.primitiveFields.length,
objectFields: result.objectFields.length,
arrayFields: result.arrayFields.length,
});
return result;
}
/**
* Determine the type category of a field value
*/
private getFieldType(value: unknown): 'primitive' | 'array' | 'object' {
if (value === null || value === undefined) {
return 'primitive';
}
if (Array.isArray(value)) {
return 'array';
}
if (typeof value === 'object') {
return 'object';
}
return 'primitive';
}
/**
* Estimate the serialized size of a field value
*/
private estimateFieldSize(value: unknown): number {
try {
if (value === null || value === undefined) {
return 4; // "null" or "undefined"
}
if (typeof value === 'string') {
return value.length * 2; // UTF-16 encoding estimate
}
if (typeof value === 'number' || typeof value === 'boolean') {
return 8; // Rough estimate for JSON representation
}
if (Array.isArray(value)) {
// Estimate array size with overhead
const elementSizes = value.map((item) => this.estimateFieldSize(item));
const totalElementSize = elementSizes.reduce((sum, size) => sum + size, 0);
return totalElementSize + value.length * this.ARRAY_SIZE_MULTIPLIER;
}
if (typeof value === 'object') {
// Estimate object size by JSON stringification
const jsonString = JSON.stringify(value);
return jsonString.length * 2; // UTF-16 encoding estimate
}
return 0;
} catch (error) {
logger.warn('Failed to estimate field size, using default', {
error: error instanceof Error ? error.message : String(error),
});
return this.DEFAULT_SIZE_THRESHOLD; // Conservative estimate
}
}
/**
* Analyze nested object structure for deep field mapping
*/
async analyzeNestedStructure<T extends Record<string, any>>(
source: T,
maxDepth: number = 3
): Promise<Map<string, FieldAnalysisResult<any>>> {
const results = new Map<string, FieldAnalysisResult<any>>();
await this.analyzeNestedRecursive(source, '', maxDepth, results);
return results;
}
/**
* Recursive helper for nested structure analysis
*/
private async analyzeNestedRecursive(
obj: any,
path: string,
depth: number,
results: Map<string, FieldAnalysisResult<any>>
): Promise<void> {
if (depth <= 0 || obj === null || typeof obj !== 'object' || Array.isArray(obj)) {
return;
}
const analysis = await this.analyzeFields(obj);
results.set(path || 'root', analysis);
// Analyze nested objects
for (const [key, value] of Object.entries(obj)) {
if (value && typeof value === 'object' && !Array.isArray(value)) {
const nestedPath = path ? `${path}.${key}` : key;
await this.analyzeNestedRecursive(value, nestedPath, depth - 1, results);
}
}
}
/**
* Generate compression recommendations based on field analysis
*/
generateCompressionRecommendations<T extends Record<string, any>>(
analysis: FieldAnalysisResult<T>,
customThreshold?: number
): {
recommended: (keyof T)[];
optional: (keyof T)[];
reasons: Record<keyof T, string>;
} {
const threshold = customThreshold || this.DEFAULT_SIZE_THRESHOLD;
const recommended: (keyof T)[] = [];
const optional: (keyof T)[] = [];
const reasons: Record<keyof T, string> = {} as Record<keyof T, string>;
for (const field of analysis.compressibleFields) {
const size = analysis.estimatedSizes[field];
if (size > threshold * 2) {
recommended.push(field);
reasons[field] =
`Large field (${this.formatSize(size)}) - high compression benefit expected`;
} else if (size > threshold) {
optional.push(field);
reasons[field] = `Medium field (${this.formatSize(size)}) - moderate compression benefit`;
}
}
// Additional recommendations based on field types
for (const field of analysis.arrayFields) {
if (!recommended.includes(field) && !optional.includes(field)) {
const size = analysis.estimatedSizes[field];
if (size > threshold * 0.5) {
optional.push(field);
reasons[field] = `Array field (${this.formatSize(size)}) - arrays often compress well`;
}
}
}
return { recommended, optional, reasons };
}
/**
* Detect field patterns for automatic mapping
*/
detectFieldPatterns<T extends Record<string, any>>(
source: T
): {
idFields: (keyof T)[];
timestampFields: (keyof T)[];
configFields: (keyof T)[];
dataFields: (keyof T)[];
} {
const patterns = {
idFields: [] as (keyof T)[],
timestampFields: [] as (keyof T)[],
configFields: [] as (keyof T)[],
dataFields: [] as (keyof T)[],
};
for (const [key, value] of Object.entries(source)) {
const typedKey = key as keyof T;
const keyLower = key.toLowerCase();
// ID field patterns
if (keyLower.includes('id') || keyLower.includes('uuid') || keyLower.includes('guid')) {
patterns.idFields.push(typedKey);
}
// Timestamp field patterns
if (
keyLower.includes('time') ||
keyLower.includes('date') ||
keyLower.includes('timestamp')
) {
patterns.timestampFields.push(typedKey);
}
// Configuration field patterns
if (
keyLower.includes('config') ||
keyLower.includes('setting') ||
keyLower.includes('option')
) {
patterns.configFields.push(typedKey);
}
// Data field patterns (large objects/arrays)
if (typeof value === 'object' && value !== null) {
if (
keyLower.includes('data') ||
keyLower.includes('result') ||
keyLower.includes('content')
) {
patterns.dataFields.push(typedKey);
}
}
}
return patterns;
}
/**
* Validate field compatibility with Qdrant payload structure
*/
validateQdrantCompatibility<T extends Record<string, any>>(
source: T
): {
compatible: boolean;
issues: string[];
suggestions: string[];
} {
const issues: string[] = [];
const suggestions: string[] = [];
// Check for reserved Qdrant field names
const reservedFields = ['id', 'vector', 'payload'];
for (const field of reservedFields) {
if (field in source) {
issues.push(`Field '${field}' conflicts with Qdrant reserved field`);
suggestions.push(`Consider renaming '${field}' to '${field}_data' or similar`);
}
}
// Check for circular references
try {
JSON.stringify(source);
} catch (error) {
if (error instanceof Error && error.message.includes('circular')) {
issues.push('Object contains circular references');
suggestions.push('Remove circular references or implement custom serialization');
}
}
// Check for unsupported data types
this.checkUnsupportedTypes(source, '', issues, suggestions);
return {
compatible: issues.length === 0,
issues,
suggestions,
};
}
/**
* Recursively check for unsupported data types
*/
private checkUnsupportedTypes(
obj: any,
path: string,
issues: string[],
suggestions: string[]
): void {
if (obj === null || obj === undefined) {
return;
}
if (typeof obj === 'function') {
issues.push(`Function found at ${path || 'root'} - functions cannot be serialized`);
suggestions.push(`Remove function at ${path || 'root'} or convert to string representation`);
return;
}
if (typeof obj === 'symbol') {
issues.push(`Symbol found at ${path || 'root'} - symbols cannot be serialized`);
suggestions.push(`Convert symbol at ${path || 'root'} to string representation`);
return;
}
if (obj instanceof Date) {
suggestions.push(`Date object at ${path || 'root'} - consider converting to ISO string`);
}
if (typeof obj === 'object' && !Array.isArray(obj)) {
for (const [key, value] of Object.entries(obj)) {
const nestedPath = path ? `${path}.${key}` : key;
this.checkUnsupportedTypes(value, nestedPath, issues, suggestions);
}
} else if (Array.isArray(obj)) {
obj.forEach((item, index) => {
const nestedPath = path ? `${path}[${index}]` : `[${index}]`;
this.checkUnsupportedTypes(item, nestedPath, issues, suggestions);
});
}
}
/**
* Format byte size for human readability
*/
private formatSize(bytes: number): string {
const units = ['B', 'KB', 'MB', 'GB'];
let size = bytes;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(1)}${units[unitIndex]}`;
}
}