This repository was archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexperiments.js
More file actions
143 lines (130 loc) · 5.48 KB
/
Copy pathexperiments.js
File metadata and controls
143 lines (130 loc) · 5.48 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
import fs from 'node:fs';
import { pipeline } from 'node:stream/promises';
import debugSetup from 'debug';
import { got, defaultHeaders } from './apiutils.js';
import { checkProject } from '../commands/utils.js';
import Content from './content.js';
const debug = debugSetup('cortex:cli');
export default (class Experiments {
constructor(cortexUrl) {
this.cortexUrl = cortexUrl;
this.endpoint = (projectId) => `${cortexUrl}/fabric/v4/projects/${projectId}/experiments`;
}
listExperiments(projectId, modelId, token, filter, limit, skip, sort) {
checkProject(projectId);
const endpoint = `${this.endpoint(projectId)}?${modelId ? `modelId=${modelId}` : ''}`;
debug('listExperiments() => %s', endpoint);
const query = {};
if (filter) query.filter = filter;
if (limit) query.limit = limit;
if (sort) query.sort = sort;
if (skip) query.skip = skip;
return got
.get(endpoint, {
headers: defaultHeaders(token),
searchParams: query,
}).json();
}
describeExperiment(projectId, token, name) {
checkProject(projectId);
const endpoint = `${this.endpoint(projectId)}/${encodeURIComponent(name)}`;
debug('describeExperiment(%s) => %s', name, endpoint);
return got
.get(endpoint, {
headers: defaultHeaders(token),
}).json();
}
deleteExperiment(projectId, token, name) {
checkProject(projectId);
const endpoint = `${this.endpoint(projectId)}/${encodeURIComponent(name)}`;
debug('deleteExperiment(%s) => %s', name, endpoint);
return got
.delete(endpoint, {
headers: defaultHeaders(token),
}).json();
}
listRuns(projectId, token, experimentName, filter, limit, sort, skip) {
checkProject(projectId);
const endpoint = `${this.endpoint(projectId)}/${encodeURIComponent(experimentName)}/runs`;
debug('listRuns(%s) => %s', experimentName, endpoint);
const query = {};
if (filter) query.filter = filter;
if (limit) query.limit = limit;
if (sort) query.sort = sort;
if (skip) query.skip = skip;
return got
.get(endpoint, {
headers: defaultHeaders(token),
searchParams: query,
}).json();
}
describeRun(projectId, token, experimentName, runId) {
checkProject(projectId);
const endpoint = `${this.endpoint(projectId)}/${encodeURIComponent(experimentName)}/runs/${runId}`;
debug('describeRun(%s) => %s', runId, endpoint);
return got
.get(endpoint, {
headers: defaultHeaders(token),
}).json();
}
deleteRun(projectId, token, experimentName, runId) {
checkProject(projectId);
const endpoint = `${this.endpoint(projectId)}/${encodeURIComponent(experimentName)}/runs/${runId}`;
debug('deleteRun(%s, %s) => %s', experimentName, runId, endpoint);
return got
.delete(endpoint, {
headers: defaultHeaders(token),
}).json();
}
_artifactKey(experimentName, runId, artifact) {
return `experiments/${experimentName}/${runId}/artifacts/${artifact}`;
}
// eslint-disable-next-line no-unused-vars
async downloadArtifact(projectId, token, experimentName, runId, artifactName, showProgress = false) {
checkProject(projectId);
// Check if run exists..
await this.describeRun(projectId, token, experimentName, runId);
// just generate the key and avoid hop to managed content..
const key = this._artifactKey(experimentName, runId, artifactName);
const cont = new Content(this.cortexUrl);
return cont.downloadContent(projectId, token, key, showProgress);
}
saveExperiment(projectId, token, experimentObj) {
checkProject(projectId);
const endpoint = `${this.endpoint(projectId)}`;
debug('saveExperiment(%s) => %s', experimentObj.name, endpoint);
return got
.post(endpoint, {
headers: defaultHeaders(token),
json: experimentObj,
}).json();
}
createRun(projectId, token, runObj) {
checkProject(projectId);
const endpoint = `${this.endpoint(projectId)}/${encodeURIComponent(runObj.experimentName)}/runs`;
debug('createRun(%s) => %s', runObj.experimentName, endpoint);
return got
.post(endpoint, {
headers: defaultHeaders(token),
json: runObj,
}).json();
}
updateRun(projectId, token, runObj) {
checkProject(projectId);
const endpoint = `${this.endpoint(projectId)}/${encodeURIComponent(runObj.experimentName)}/runs/${runObj.runId}`;
debug('updateRun(%s) => %s', runObj.experimentName, endpoint);
return got
.post(endpoint, {
headers: defaultHeaders(token),
json: runObj,
}).json();
}
uploadArtifact(projectId, token, experimentName, runId, content, artifact, contentType = 'application/octet-stream') {
checkProject(projectId);
const endpoint = `${this.endpoint(projectId)}/${encodeURIComponent(experimentName)}/runs/${encodeURIComponent(runId)}/artifacts/${artifact}`;
debug('uploadArtifact(%s, %s) => %s', artifact, content, endpoint);
return pipeline(fs.createReadStream(content), got.stream.put(endpoint, {
headers: defaultHeaders(token, { 'Content-Type': contentType }),
}));
}
});