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
|
import { Catalog, OperationReference, Result, CompileResults } from "../model/idea/idea";
import { computed, observable, action } from "mobx";
export class Gateway {
private static _instance: Gateway;
private constructor() {
}
public static get Instance() {
return this._instance || (this._instance = new this());
}
public async GetCatalog(): Promise<Catalog> {
try {
const json = await this.MakeGetRequest("catalog");
const cat = Catalog.fromJS(json);
return cat;
}
catch (error) {
throw new Error("can not reach northstar's backend");
}
}
public async PostSchema(csvdata: string, schemaname: string): Promise<string> {
try {
const json = await this.MakePostJsonRequest("postSchema", { csv: csvdata, schema: schemaname });
// const cat = Catalog.fromJS(json);
// return cat;
return json;
}
catch (error) {
throw new Error("can not reach northstar's backend");
}
}
public async GetSchema(pathname: string, schemaname: string): Promise<Catalog> {
try {
const json = await this.MakeGetRequest("schema", undefined, { path: pathname, schema: schemaname });
const cat = Catalog.fromJS(json);
return cat;
}
catch (error) {
throw new Error("can not reach northstar's backend");
}
}
public async ClearCatalog(): Promise<void> {
try {
await this.MakePostJsonRequest("Datamart/ClearAllAugmentations", {});
}
catch (error) {
throw new Error("can not reach northstar's backend");
}
}
public async TerminateServer(): Promise<void> {
try {
const url = Gateway.ConstructUrl("terminateServer");
const response = await fetch(url,
{
redirect: "follow",
method: "POST",
credentials: "include"
});
}
catch (error) {
throw new Error("can not reach northstar's backend");
}
}
public async Compile(data: any): Promise<CompileResults | undefined> {
const json = await this.MakePostJsonRequest("compile", data);
if (json !== null) {
const cr = CompileResults.fromJS(json);
return cr;
}
}
public async SubmitResult(data: any): Promise<void> {
try {
console.log(data);
const url = Gateway.ConstructUrl("submitProblem");
const response = await fetch(url,
{
redirect: "follow",
method: "POST",
credentials: "include",
body: JSON.stringify(data)
});
}
catch (error) {
throw new Error("can not reach northstar's backend");
}
}
public async SpecifyProblem(data: any): Promise<void> {
try {
console.log(data);
const url = Gateway.ConstructUrl("specifyProblem");
const response = await fetch(url,
{
redirect: "follow",
method: "POST",
credentials: "include",
body: JSON.stringify(data)
});
}
catch (error) {
throw new Error("can not reach northstar's backend");
}
}
public async ExportToScript(solutionId: string): Promise<string> {
try {
const url = Gateway.ConstructUrl("exportsolution/script/" + solutionId);
const response = await fetch(url,
{
redirect: "follow",
method: "GET",
credentials: "include"
});
return await response.text();
}
catch (error) {
throw new Error("can not reach northstar's backend");
}
}
public async StartOperation(data: any): Promise<OperationReference | undefined> {
const json = await this.MakePostJsonRequest("operation", data);
if (json !== null) {
const or = OperationReference.fromJS(json);
return or;
}
}
public async GetResult(data: any): Promise<Result | undefined> {
const json = await this.MakePostJsonRequest("result", data);
if (json !== null) {
const res = Result.fromJS(json);
return res;
}
}
public async PauseOperation(data: any): Promise<void> {
const url = Gateway.ConstructUrl("pause");
await fetch(url,
{
redirect: "follow",
method: "POST",
credentials: "include",
body: JSON.stringify(data)
});
}
public async MakeGetRequest(endpoint: string, signal?: AbortSignal, params?: any): Promise<any> {
let url = !params ? Gateway.ConstructUrl(endpoint) :
(() => {
let newUrl = new URL(Gateway.ConstructUrl(endpoint));
Object.getOwnPropertyNames(params).map(prop =>
newUrl.searchParams.append(prop, params[prop]));
return Gateway.ConstructUrl(endpoint) + newUrl.search;
})();
const response = await fetch(url,
{
redirect: "follow",
method: "GET",
credentials: "include",
signal
});
const json = await response.json();
return json;
}
public async MakePostJsonRequest(endpoint: string, data: any, signal?: AbortSignal): Promise<any> {
const url = Gateway.ConstructUrl(endpoint);
const response = await fetch(url,
{
redirect: "follow",
method: "POST",
credentials: "include",
body: JSON.stringify(data),
signal
});
const json = await response.json();
return json;
}
public static ConstructUrl(appendix: string): string {
let base = NorthstarSettings.Instance.ServerUrl;
if (base.slice(-1) === "/") {
base = base.slice(0, -1);
}
let url = base + "/" + NorthstarSettings.Instance.ServerApiPath + "/" + appendix;
return url;
}
}
declare var ENV: any;
export class NorthstarSettings {
private _environment: any;
@observable
public ServerUrl: string = document.URL;
@observable
public ServerApiPath?: string;
@observable
public SampleSize?: number;
@observable
public XBins?: number;
@observable
public YBins?: number;
@observable
public SplashTimeInMS?: number;
@observable
public ShowFpsCounter?: boolean;
@observable
public IsMenuFixed?: boolean;
@observable
public ShowShutdownButton?: boolean;
@observable
public IsDarpa?: boolean;
@observable
public IsIGT?: boolean;
@observable
public DegreeOfParallelism?: number;
@observable
public ShowWarnings?: boolean;
@computed
public get IsDev(): boolean {
return ENV.IsDev;
}
@computed
public get TestDataFolderPath(): string {
return this.Origin + "testdata/";
}
@computed
public get Origin(): string {
return window.location.origin + "/";
}
private static _instance: NorthstarSettings;
@action
public UpdateEnvironment(environment: any): void {
/*let serverParam = new URL(document.URL).searchParams.get("serverUrl");
if (serverParam) {
if (serverParam === "debug") {
this.ServerUrl = `http://${window.location.hostname}:1234`;
}
else {
this.ServerUrl = serverParam;
}
}
else {
this.ServerUrl = environment["SERVER_URL"] ? environment["SERVER_URL"] : document.URL;
}*/
this.ServerUrl = environment.SERVER_URL ? environment.SERVER_URL : document.URL;
this.ServerApiPath = environment.SERVER_API_PATH;
this.SampleSize = environment.SAMPLE_SIZE;
this.XBins = environment.X_BINS;
this.YBins = environment.Y_BINS;
this.SplashTimeInMS = environment.SPLASH_TIME_IN_MS;
this.ShowFpsCounter = environment.SHOW_FPS_COUNTER;
this.ShowShutdownButton = environment.SHOW_SHUTDOWN_BUTTON;
this.IsMenuFixed = environment.IS_MENU_FIXED;
this.IsDarpa = environment.IS_DARPA;
this.IsIGT = environment.IS_IGT;
this.DegreeOfParallelism = environment.DEGREE_OF_PARALLISM;
}
public static get Instance(): NorthstarSettings {
if (!this._instance) {
this._instance = new NorthstarSettings();
}
return this._instance;
}
}
|