-
Notifications
You must be signed in to change notification settings - Fork 90
/
ZoweJobNode.ts
325 lines (309 loc) · 12.2 KB
/
ZoweJobNode.ts
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
// @ts-nocheck
/*
* This program and the accompanying materials are made available under the terms of the *
* Eclipse Public License v2.0 which accompanies this distribution, and is available at *
* https://www.eclipse.org/legal/epl-v20.html *
* *
* SPDX-License-Identifier: EPL-2.0 *
* *
* Copyright Contributors to the Zowe Project. *
* *
*/
import * as vscode from "vscode";
import * as zowe from "@zowe/cli";
import * as globals from "../globals";
import { Session, IProfileLoaded } from "@zowe/imperative";
import { IZoweJobTreeNode, ZoweTreeNode } from "@zowe/zowe-explorer-api";
import { ZoweExplorerApiRegister } from "../ZoweExplorerApiRegister";
import { errorHandling, syncSessionNode } from "../utils/ProfilesUtils";
import { getIconByNode } from "../generators/icons";
import * as contextually from "../shared/context";
import * as nls from "vscode-nls";
import { Profiles } from "../Profiles";
// Set up localization
nls.config({
messageFormat: nls.MessageFormat.bundle,
bundleFormat: nls.BundleFormat.standalone,
})();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
// tslint:disable-next-line: max-classes-per-file
export class Job extends ZoweTreeNode implements IZoweJobTreeNode {
public static readonly JobId = "JobId:";
public static readonly Owner = "Owner:";
public static readonly Prefix = "Prefix:";
public children: IZoweJobTreeNode[] = [];
public dirty = true;
// tslint:disable-next-line: variable-name
private _owner: string;
// tslint:disable-next-line: variable-name
private _prefix: string;
// tslint:disable-next-line: variable-name
private _searchId: string;
constructor(
label: string,
collapsibleState: vscode.TreeItemCollapsibleState,
mParent: IZoweJobTreeNode,
session: Session,
public job: zowe.IJob,
profile: IProfileLoaded
) {
super(label, collapsibleState, mParent, session, profile);
if (session) {
if (session.ISession.user) {
this._owner = session.ISession.user;
} else {
this._owner = "*";
}
}
this._prefix = "*";
this._searchId = "";
const icon = getIconByNode(this);
if (icon) {
this.iconPath = icon.path;
}
}
/**
* Retrieves child nodes of this IZoweJobTreeNode
*
* @returns {Promise<IZoweJobTreeNode[]>}
*/
public async getChildren(): Promise<IZoweJobTreeNode[]> {
if (!this._owner && contextually.isSession(this)) {
return [
new Job(
localize("getChildren.search", "Use the search button to display jobs"),
vscode.TreeItemCollapsibleState.None,
this,
null,
null,
null
),
];
}
if (this.dirty) {
let spools: zowe.IJobFile[] = [];
const elementChildren = [];
if (contextually.isJob(this)) {
spools = await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: localize("ZoweJobNode.getJobs.spoolfiles", "Get Job Spool files command submitted."),
},
() => {
return ZoweExplorerApiRegister.getJesApi(this.getProfile()).getSpoolFiles(
this.job.jobname,
this.job.jobid
);
}
);
spools = spools
// filter out all the objects which do not seem to be correct Job File Document types
// see an issue #845 for the details
.filter(
(item) => !(item.id === undefined && item.ddname === undefined && item.stepname === undefined)
);
if (!spools.length) {
const noSpoolNode = new Spool(
localize("getChildren.noSpoolFiles", "There are no JES spool messages to display"),
vscode.TreeItemCollapsibleState.None,
this,
null,
null,
null,
this
);
noSpoolNode.iconPath = null;
return [noSpoolNode];
}
const refreshTimestamp = Date.now();
spools.forEach((spool) => {
let prefix = spool.stepname;
if (prefix === undefined) {
prefix = spool.procstep;
}
const sessionName = this.getProfileName();
const spoolNode = new Spool(
`${spool.stepname}:${spool.ddname}(${spool.id})`,
vscode.TreeItemCollapsibleState.None,
this,
this.session,
spool,
this.job,
this
);
const icon = getIconByNode(spoolNode);
if (icon) {
spoolNode.iconPath = icon.path;
}
spoolNode.command = {
command: "zowe.jobs.zosJobsOpenspool",
title: "",
arguments: [sessionName, spool, refreshTimestamp],
};
elementChildren.push(spoolNode);
});
} else {
const jobs = await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: localize("ZoweJobNode.getJobs.jobs", "Get Jobs command submitted."),
},
() => {
return this.getJobs(this._owner, this._prefix, this._searchId);
}
);
jobs.forEach((job) => {
let nodeTitle: string;
if (job.retcode) {
nodeTitle = `${job.jobname}(${job.jobid}) - ${job.retcode}`;
} else {
nodeTitle = `${job.jobname}(${job.jobid}) - ${job.status}`;
}
const existing = this.children.find((element) => element.label.trim() === nodeTitle);
if (existing) {
elementChildren.push(existing);
} else {
const jobNode = new Job(
nodeTitle,
vscode.TreeItemCollapsibleState.Collapsed,
this,
this.session,
job,
this.getProfile()
);
jobNode.command = { command: "zowe.zosJobsSelectjob", title: "", arguments: [jobNode] };
jobNode.contextValue = globals.JOBS_JOB_CONTEXT;
if (job.retcode) {
jobNode.contextValue += globals.RC_SUFFIX + job.retcode;
}
if (!jobNode.iconPath) {
const icon = getIconByNode(jobNode);
if (icon) {
jobNode.iconPath = icon.path;
}
}
elementChildren.push(jobNode);
}
});
}
elementChildren.sort((a, b) => {
if (a.job.jobid > b.job.jobid) {
return 1;
}
if (a.job.jobid < b.job.jobid) {
return -1;
}
return 0;
});
this.children = elementChildren;
}
this.dirty = false;
return this.children;
}
public getSessionNode(): IZoweJobTreeNode {
return this.getParent() ? this.getParent().getSessionNode() : this;
}
get tooltip(): string {
if (this.job !== null) {
if (this.job.retcode) {
return `${this.job.jobname}(${this.job.jobid}) - ${this.job.retcode}`;
} else {
return `${this.job.jobname}(${this.job.jobid})`;
}
} else if (this.searchId.length > 0) {
return `${this.label} - job id: ${this.searchId}`;
} else {
return `${this.label} - owner: ${this.owner} prefix: ${this.prefix}`;
}
}
set owner(newOwner: string) {
if (newOwner !== undefined) {
if (newOwner.length === 0) {
this._owner = this.session.ISession.user;
} else {
this._owner = newOwner;
}
}
}
get owner() {
return this._owner;
}
set prefix(newPrefix: string) {
if (newPrefix !== undefined) {
if (newPrefix.length === 0) {
this._prefix = "*";
} else {
this._prefix = newPrefix;
}
}
}
get prefix() {
return this._prefix;
}
public set searchId(newId: string) {
if (newId !== undefined) {
this._searchId = newId;
}
}
public get searchId() {
return this._searchId;
}
private async getJobs(owner, prefix, searchId): Promise<zowe.IJob[]> {
let jobsInternal: zowe.IJob[] = [];
const sessNode = this.getSessionNode();
if (this.searchId.length > 0) {
jobsInternal.push(await ZoweExplorerApiRegister.getJesApi(this.getProfile()).getJob(searchId));
} else {
try {
jobsInternal = await ZoweExplorerApiRegister.getJesApi(this.getProfile()).getJobsByOwnerAndPrefix(
owner,
prefix
);
/**
* Note: Temporary fix
* This current fix is necessary since in certain instances the Zowe
* Explorer JES API returns duplicate jobs. The following reduce function
* filters only the unique jobs present by comparing the ids of these returned
* jobs.
*/
jobsInternal = jobsInternal.reduce((acc, current) => {
const duplicateJobExists = acc.find((job) => job.jobid === current.jobid);
if (!duplicateJobExists) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
} catch (error) {
await errorHandling(
error,
this.label,
localize("getChildren.error.response", "Retrieving response from ") + `zowe.GetJobs`
);
await syncSessionNode(Profiles.getInstance())((profileValue) =>
ZoweExplorerApiRegister.getJesApi(profileValue).getSession()
)(sessNode);
}
}
return jobsInternal;
}
}
// tslint:disable-next-line: max-classes-per-file
class Spool extends Job {
constructor(
label: string,
mCollapsibleState: vscode.TreeItemCollapsibleState,
mParent: IZoweJobTreeNode,
session: Session,
spool: zowe.IJobFile,
job: zowe.IJob,
parent: IZoweJobTreeNode
) {
super(label, mCollapsibleState, mParent, session, job, parent.getProfile());
this.contextValue = globals.JOBS_SPOOL_CONTEXT;
const icon = getIconByNode(this);
if (icon) {
this.iconPath = icon.path;
}
}
}