2019-06-27 16:21:00 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import Component from '@ember/component';
|
|
|
|
import { computed } from '@ember/object';
|
2019-07-20 19:00:20 +00:00
|
|
|
import { equal } from '@ember/object/computed';
|
2019-06-27 16:21:00 +00:00
|
|
|
import RSVP from 'rsvp';
|
2019-07-02 22:37:17 +00:00
|
|
|
import Log from 'nomad-ui/utils/classes/log';
|
2019-06-27 16:21:00 +00:00
|
|
|
import timeout from 'nomad-ui/utils/timeout';
|
|
|
|
|
2019-07-02 19:59:15 +00:00
|
|
|
export default Component.extend({
|
2019-06-27 16:21:00 +00:00
|
|
|
token: service(),
|
|
|
|
|
|
|
|
classNames: ['boxed-section', 'task-log'],
|
|
|
|
|
|
|
|
allocation: null,
|
|
|
|
task: null,
|
|
|
|
file: null,
|
2019-07-02 20:01:07 +00:00
|
|
|
stat: null, // { Name, IsDir, Size, FileMode, ModTime, ContentType }
|
2019-06-27 16:21:00 +00:00
|
|
|
|
|
|
|
// When true, request logs from the server agent
|
|
|
|
useServer: false,
|
|
|
|
|
|
|
|
// When true, logs cannot be fetched from either the client or the server
|
|
|
|
noConnection: false,
|
|
|
|
|
|
|
|
clientTimeout: 1000,
|
|
|
|
serverTimeout: 5000,
|
|
|
|
|
2019-07-02 20:01:07 +00:00
|
|
|
mode: 'head',
|
|
|
|
|
|
|
|
fileComponent: computed('stat', function() {
|
2019-07-20 19:00:20 +00:00
|
|
|
const contentType = this.stat.ContentType;
|
|
|
|
|
|
|
|
if (contentType.startsWith('image/')) {
|
|
|
|
return 'image';
|
|
|
|
} else if (contentType.startsWith('text/') || contentType.startsWith('application/json')) {
|
|
|
|
return 'stream';
|
|
|
|
} else {
|
|
|
|
return 'unknown';
|
2019-06-27 16:21:00 +00:00
|
|
|
}
|
2019-07-02 20:01:07 +00:00
|
|
|
}),
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2019-07-02 20:01:07 +00:00
|
|
|
isLarge: computed('stat', function() {
|
|
|
|
return this.stat.Size > 50000;
|
|
|
|
}),
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2019-07-25 17:53:45 +00:00
|
|
|
fileTypeIsUnknown: equal('fileComponent', 'unknown'),
|
2019-07-20 19:00:20 +00:00
|
|
|
isStreamable: equal('fileComponent', 'stream'),
|
2019-07-02 20:01:07 +00:00
|
|
|
isStreaming: false,
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2019-07-02 20:01:07 +00:00
|
|
|
catUrl: computed('allocation.id', 'task.name', 'file', function() {
|
2019-06-27 16:21:00 +00:00
|
|
|
return `/v1/client/fs/cat/${this.allocation.id}?path=${this.task.name}/${this.file}`;
|
|
|
|
}),
|
|
|
|
|
2019-07-02 20:01:07 +00:00
|
|
|
fetchMode: computed('isLarge', 'mode', function() {
|
2019-07-20 19:00:20 +00:00
|
|
|
if (this.mode === 'streaming') {
|
|
|
|
return 'stream';
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:01:07 +00:00
|
|
|
if (!this.isLarge) {
|
|
|
|
return 'cat';
|
2019-07-02 22:37:17 +00:00
|
|
|
} else if (this.mode === 'head' || this.mode === 'tail') {
|
2019-07-02 20:01:07 +00:00
|
|
|
return 'readat';
|
|
|
|
}
|
2019-06-27 16:21:00 +00:00
|
|
|
}),
|
|
|
|
|
2019-07-02 20:01:07 +00:00
|
|
|
fileUrl: computed(
|
|
|
|
'allocation.id',
|
|
|
|
'allocation.node.httpAddr',
|
|
|
|
'fetchMode',
|
|
|
|
'useServer',
|
|
|
|
function() {
|
|
|
|
const address = this.get('allocation.node.httpAddr');
|
|
|
|
const url = `/v1/client/fs/${this.fetchMode}/${this.allocation.id}`;
|
|
|
|
return this.useServer ? url : `//${address}${url}`;
|
|
|
|
}
|
|
|
|
),
|
|
|
|
|
|
|
|
fileParams: computed('task.name', 'file', 'mode', function() {
|
|
|
|
const path = `${this.task.name}/${this.file}`;
|
|
|
|
|
|
|
|
switch (this.mode) {
|
|
|
|
case 'head':
|
|
|
|
return { path, offset: 0, limit: 50000 };
|
|
|
|
case 'tail':
|
2019-07-02 22:37:17 +00:00
|
|
|
return { path, offset: this.stat.Size - 50000, limit: 50000 };
|
|
|
|
case 'streaming':
|
2019-07-02 20:01:07 +00:00
|
|
|
return { path, offset: 50000, origin: 'end' };
|
|
|
|
default:
|
|
|
|
return { path };
|
|
|
|
}
|
2019-06-27 16:21:00 +00:00
|
|
|
}),
|
|
|
|
|
2019-07-02 22:37:17 +00:00
|
|
|
logger: computed('fileUrl', 'fileParams', 'mode', function() {
|
|
|
|
// The cat and readat APIs are in plainText while the stream API is always encoded.
|
|
|
|
const plainText = this.mode === 'head' || this.mode === 'tail';
|
|
|
|
|
2019-07-02 20:01:07 +00:00
|
|
|
// If the file request can't settle in one second, the client
|
2019-06-27 16:21:00 +00:00
|
|
|
// must be unavailable and the server should be used instead
|
|
|
|
const timing = this.useServer ? this.serverTimeout : this.clientTimeout;
|
2019-07-02 22:37:17 +00:00
|
|
|
const logFetch = url =>
|
2019-06-27 16:21:00 +00:00
|
|
|
RSVP.race([this.token.authorizedRequest(url), timeout(timing)]).then(
|
|
|
|
response => response,
|
|
|
|
error => {
|
|
|
|
if (this.useServer) {
|
|
|
|
this.set('noConnection', true);
|
|
|
|
} else {
|
|
|
|
this.send('failoverToServer');
|
|
|
|
this.stream.perform();
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
);
|
2019-07-02 22:37:17 +00:00
|
|
|
|
|
|
|
return Log.create({
|
|
|
|
logFetch,
|
|
|
|
plainText,
|
|
|
|
params: this.fileParams,
|
|
|
|
url: this.fileUrl,
|
|
|
|
});
|
2019-06-27 16:21:00 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
toggleStream() {
|
2019-07-02 22:37:17 +00:00
|
|
|
this.set('mode', 'streaming');
|
2019-07-02 20:01:07 +00:00
|
|
|
this.toggleProperty('isStreaming');
|
|
|
|
},
|
|
|
|
gotoHead() {
|
|
|
|
this.set('mode', 'head');
|
|
|
|
this.set('isStreaming', false);
|
|
|
|
},
|
|
|
|
gotoTail() {
|
|
|
|
this.set('mode', 'tail');
|
|
|
|
this.set('isStreaming', false);
|
2019-06-27 16:21:00 +00:00
|
|
|
},
|
|
|
|
failoverToServer() {
|
|
|
|
this.set('useServer', true);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|