@miso.ai/server-sdk 0.6.6-beta.1 → 0.6.6-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/index.js +2 -0
- package/cli/merge-local.js +118 -0
- package/package.json +2 -2
- package/src/version.js +1 -1
package/cli/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import upload from './upload.js';
|
|
|
7
7
|
import del from './delete.js';
|
|
8
8
|
import ids from './ids.js';
|
|
9
9
|
import transform from './transform.js';
|
|
10
|
+
import mergeLocal from './merge-local.js';
|
|
10
11
|
import status from './status.js';
|
|
11
12
|
import get from './get.js';
|
|
12
13
|
import search from './search.js';
|
|
@@ -70,6 +71,7 @@ yargs.build(yargs => {
|
|
|
70
71
|
.command(users)
|
|
71
72
|
.command(experiments)
|
|
72
73
|
.command(transform)
|
|
74
|
+
.command(mergeLocal)
|
|
73
75
|
.command(search)
|
|
74
76
|
.command(hybridSearch)
|
|
75
77
|
.version(MisoClient.version);
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import 'dotenv/config';
|
|
3
|
+
import { promisify } from 'util';
|
|
4
|
+
import { readFile } from 'fs/promises';
|
|
5
|
+
import { gunzip } from 'zlib';
|
|
6
|
+
import { Transform } from 'stream';
|
|
7
|
+
import { pipeline } from 'stream/promises';
|
|
8
|
+
import { stream, trimObj } from '@miso.ai/server-commons';
|
|
9
|
+
import split2 from 'split2';
|
|
10
|
+
|
|
11
|
+
process.stdout.on('error', err => err.code == 'EPIPE' && process.exit(0));
|
|
12
|
+
|
|
13
|
+
function build(yargs) {
|
|
14
|
+
return yargs
|
|
15
|
+
.positional('file', {
|
|
16
|
+
describe: 'Patch record file',
|
|
17
|
+
type: 'string',
|
|
18
|
+
demandOption: true,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function run({ file }) {
|
|
23
|
+
const patches = await readRecordsIntoMap(file);
|
|
24
|
+
await pipeline(
|
|
25
|
+
process.stdin,
|
|
26
|
+
split2(JSON.parse),
|
|
27
|
+
new MergeLocalStream({ patches }),
|
|
28
|
+
new stream.OutputStream(),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function readRecordsIntoMap(file) {
|
|
33
|
+
const map = new Map();
|
|
34
|
+
const content = await readContent(file);
|
|
35
|
+
for (let line of content.split('\n')) {
|
|
36
|
+
line = line.trim();
|
|
37
|
+
if (!line) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const record = JSON.parse(line);
|
|
42
|
+
const { product_id } = record;
|
|
43
|
+
if (!product_id) {
|
|
44
|
+
console.error(`Missing product_id: ${line}`);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
map.set(product_id, record);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error(`Error parsing JSON line: ${line}`, error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return map;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function readContent(file) {
|
|
56
|
+
let fileBuffer = await readFile(file);
|
|
57
|
+
if (file.endsWith('.gz')) {
|
|
58
|
+
fileBuffer = await promisify(gunzip)(fileBuffer);
|
|
59
|
+
}
|
|
60
|
+
return fileBuffer.toString('utf-8');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
class MergeLocalStream extends Transform {
|
|
64
|
+
|
|
65
|
+
constructor({
|
|
66
|
+
patches,
|
|
67
|
+
...options
|
|
68
|
+
} = {}) {
|
|
69
|
+
super({
|
|
70
|
+
objectMode: true,
|
|
71
|
+
...options,
|
|
72
|
+
});
|
|
73
|
+
this._patches = patches;
|
|
74
|
+
this.on('drain', () => this._handleDrain());
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
_transform(record, _, next) {
|
|
78
|
+
const patch = this._patches.get(record.product_id);
|
|
79
|
+
if (patch) {
|
|
80
|
+
record = this._patch(record, patch);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const pause = !this.push(record);
|
|
84
|
+
if (pause) {
|
|
85
|
+
this._next = next;
|
|
86
|
+
} else {
|
|
87
|
+
next();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
_patch(record, patch) {
|
|
92
|
+
patch = trimObj(patch);
|
|
93
|
+
return {
|
|
94
|
+
...record,
|
|
95
|
+
...patch,
|
|
96
|
+
custom_attributes: trimObj({
|
|
97
|
+
...(record && record.custom_attributes),
|
|
98
|
+
...(patch && patch.custom_attributes),
|
|
99
|
+
}),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
_handleDrain() {
|
|
104
|
+
if (this._next) {
|
|
105
|
+
this._next();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
export default {
|
|
113
|
+
command: 'merge-local <file>',
|
|
114
|
+
alias: ['patch-local'],
|
|
115
|
+
description: 'Apply patch records from file onto input record stream',
|
|
116
|
+
builder: build,
|
|
117
|
+
handler: run,
|
|
118
|
+
};
|
package/package.json
CHANGED
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
"simonpai <simon.pai@askmiso.com>"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@miso.ai/server-commons": "0.6.6-beta.
|
|
19
|
+
"@miso.ai/server-commons": "0.6.6-beta.3",
|
|
20
20
|
"axios": "^1.6.2",
|
|
21
21
|
"axios-retry": "^4.5.0",
|
|
22
22
|
"dotenv": "^16.0.1",
|
|
23
23
|
"split2": "^4.1.0",
|
|
24
24
|
"yargs": "^17.5.1"
|
|
25
25
|
},
|
|
26
|
-
"version": "0.6.6-beta.
|
|
26
|
+
"version": "0.6.6-beta.3"
|
|
27
27
|
}
|
package/src/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '0.6.6-beta.
|
|
1
|
+
export default '0.6.6-beta.3';
|