Skip to content

Commit

Permalink
Merge pull request #995 from input-output-hk/chore/ddw-248-refactor-c…
Browse files Browse the repository at this point in the history
…ompress-download-logs-handling

[DDW-248] Refactor compress/download logs handling
  • Loading branch information
nikolaglumac authored Jul 12, 2018
2 parents 11c959c + d68a724 commit 077a182
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 163 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Changelog
- Added acceptance tests for node update notification with apply and postpone update scenarios ([PR 977](https://github.com/input-output-hk/daedalus/pull/977))
- Added acceptance tests for maximum wallets limit ([PR 979](https://github.com/input-output-hk/daedalus/pull/979))
- Added acceptance tests for the About dialog ([PR 975](https://github.com/input-output-hk/daedalus/pull/975))
- Improved compress/download logs handling ([PR 995](https://github.com/input-output-hk/daedalus/pull/995))

## 0.10.1
=======
Expand Down
13 changes: 13 additions & 0 deletions source/common/fileName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @flow
import moment from 'moment';

export const generateFileNameWithTimestamp = (prefix: string = 'logs', fileType: string = 'zip') =>
`${prefix}-${moment.utc().format('YYYY-MM-DDTHHmmss.0SSS')}Z.${fileType}`;

export const isFileNameWithTimestamp = (prefix: string = 'logs', fileType: string = 'zip') => (fileName: string) =>
fileName.match(RegExp(`(${prefix}-)([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{6}.0[0-9]{3}Z)(.${fileType})`));

export const getPathSlash = (path: string) => ((path.indexOf('/') > -1) ? '/' : '\\');

export const extractFileNameFromPath = (path: string) =>
path.substr(path.lastIndexOf(getPathSlash(path)) + 1);
7 changes: 0 additions & 7 deletions source/common/ipc-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ export const COMPRESS_LOGS = {
ERROR: `${COMPRESS_LOGS_CHANNEL}-error`,
};

const DELETE_COMPRESSED_LOGS_CHANNEL = 'delete-compressed-logs';
export const DELETE_COMPRESSED_LOGS = {
REQUEST: DELETE_COMPRESSED_LOGS_CHANNEL,
SUCCESS: `${DELETE_COMPRESSED_LOGS_CHANNEL}-success`,
ERROR: `${DELETE_COMPRESSED_LOGS_CHANNEL}-error`,
};

const DOWNLOAD_LOGS_CHANNEL = 'download-logs';
export const DOWNLOAD_LOGS = {
REQUEST: DOWNLOAD_LOGS_CHANNEL,
Expand Down
4 changes: 1 addition & 3 deletions source/main/ipc-api/compress-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import { Logger, stringifyError } from '../../common/logging';
import { COMPRESS_LOGS } from '../../common/ipc-api';

export default () => {
ipcMain.on(COMPRESS_LOGS.REQUEST, (event, logs) => {
ipcMain.on(COMPRESS_LOGS.REQUEST, (event, logs, compressedFileName) => {
const sender = event.sender;
const compressedFileName = 'logs.zip';

const outputPath = path.join(appLogsFolderPath, compressedFileName);
const output = fs.createWriteStream(outputPath);
const archive = archiver('zip', {
Expand Down
19 changes: 0 additions & 19 deletions source/main/ipc-api/delete-compressed-logs.js

This file was deleted.

2 changes: 0 additions & 2 deletions source/main/ipc-api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow
import compressLogsApi from './compress-logs';
import deleteCompressedLogsApi from './delete-compressed-logs';
import downloadLogsApi from './download-logs';
import getLogsApi from './get-logs';
import parseRedemptionCodeApi from './parse-redemption-code-from-pdf';
Expand All @@ -10,7 +9,6 @@ import loadAsset from './load-asset';

export default (params: any) => {
compressLogsApi();
deleteCompressedLogsApi();
downloadLogsApi();
getLogsApi();
parseRedemptionCodeApi();
Expand Down
18 changes: 17 additions & 1 deletion source/main/utils/setupLogging.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from 'fs';
import path from 'path';
import log from 'electron-log';
import moment from 'moment';
import ensureDirectoryExists from './ensureDirectoryExists';
import { pubLogsFolderPath, APP_NAME } from '../config';
import { pubLogsFolderPath, appLogsFolderPath, APP_NAME } from '../config';
import { isFileNameWithTimestamp } from '../../common/fileName';

const isTest = process.env.NODE_ENV === 'test';

Expand All @@ -19,4 +21,18 @@ export const setupLogging = () => {
const formattedDate = moment.utc(msg.date).format('YYYY-MM-DDTHH:mm:ss.0SSS');
return `[${formattedDate}Z] [${msg.level}] ${msg.data}`;
};

// Removes existing compressed logs
fs.readdir(appLogsFolderPath, (err, files) => {
files
.filter(isFileNameWithTimestamp())
.forEach((logFileName) => {
const logFile = path.join(appLogsFolderPath, logFileName);
try {
fs.unlinkSync(logFile);
} catch (error) {
console.error(`Compressed log file "${logFile}" deletion failed: ${error}`);
}
});
});
};
9 changes: 4 additions & 5 deletions source/renderer/app/actions/profile-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import Action from './lib/Action';

export default class ProfileActions {
acceptTermsOfUse: Action<any> = new Action();
compressLogs: Action<{ logs: Object }> = new Action();
getLogs: Action<any> = new Action();
downloadLogs: Action<{ destination: string, fresh?: boolean }> = new Action();
deleteCompressedLogs: Action<any> = new Action();
resetBugReportDialog: Action<any> = new Action();
getLogsAndCompress: Action<any> = new Action();
sendBugReport: Action<{
email: string, subject: string, problem: string, compressedLog: ?string,
email: string, subject: string, problem: string, compressedLogsFile: ?string,
}> = new Action();
resetBugReportDialog: Action<any> = new Action();
downloadLogs: Action<{ fileName: string, destination: string, fresh?: boolean }> = new Action();
updateLocale: Action<{ locale: string }> = new Action();
updateTheme: Action<{ theme: string }> = new Action();
}
6 changes: 3 additions & 3 deletions source/renderer/app/api/ada/sendAdaBugReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export type SendAdaBugReportRequestParams = {
email: string,
subject: string,
problem: string,
compressedLog: string,
compressedLogsFile: string,
},
application: string,
};

export const sendAdaBugReport = (
{ requestFormData, application }: SendAdaBugReportRequestParams
): Promise<{}> => {
const { email, subject, problem, compressedLog } = requestFormData;
const { email, subject, problem, compressedLogsFile } = requestFormData;
const reportUrl = url.parse(environment.REPORT_URL);

let platform;
Expand Down Expand Up @@ -45,7 +45,7 @@ export const sendAdaBugReport = (
version: environment.version,
build: environment.build,
os: platform,
compressedLog,
compressedLogsFile,
date: moment().format('YYYY-MM-DDTHH:mm:ss'),
magic: 2000000000,
type: {
Expand Down
6 changes: 3 additions & 3 deletions source/renderer/app/api/etc/sendEtcBugReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export type SendEtcBugReportRequestParams = {
email: string,
subject: string,
problem: string,
compressedLog: string,
compressedLogsFile: string,
},
application: string,
};

export const sendEtcBugReport = (
{ requestFormData, application }: SendEtcBugReportRequestParams
): Promise<{}> => {
const { email, subject, problem, compressedLog } = requestFormData;
const { email, subject, problem, compressedLogsFile } = requestFormData;
const reportUrl = url.parse(environment.REPORT_URL);

let platform;
Expand Down Expand Up @@ -45,7 +45,7 @@ export const sendEtcBugReport = (
version: environment.version,
build: environment.build,
os: platform,
compressedLog,
compressedLogsFile,
date: moment().format('YYYY-MM-DDTHH:mm:ss'),
magic: 2000000000,
type: {
Expand Down
8 changes: 5 additions & 3 deletions source/renderer/app/api/lib/reportRequest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http from 'http';
import FormData from 'form-data/lib/form_data';
import fs from 'fs';
import { extractFileNameFromPath } from '../../../../common/fileName';

export type RequestOptions = {
hostname: string,
Expand Down Expand Up @@ -38,9 +39,10 @@ function typedHttpRequest<Response>(
formData.append('payload', JSON.stringify(payload));

// prepare file stream (attachment)
if (payload.compressedLog) {
const stream = fs.createReadStream(payload.compressedLog);
formData.append('logs.zip', stream);
if (payload.compressedLogsFile) {
const stream = fs.createReadStream(payload.compressedLogsFile);
const fileName = extractFileNameFromPath(payload.compressedLogsFile);
formData.append(fileName, stream);
}

options.headers = formData.getHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,20 @@ const messages = defineMessages({

type Props = {
logFiles: LogFiles,
compressedLog: ?string,
onCancel: Function,
onSubmit: Function,
onSubmitManually: Function,
onDownload: Function,
onGetLogs: Function,
onCompressLogs: Function,
onDeleteCompressedLogs: Function,
isSubmitting: boolean,
isCompressing: boolean,
onGetLogsAndCompress: Function,
isDownloading?: boolean,
isSubmittingBugReport?: boolean,
error: ?LocalizableError,
};

type State = {
showLogs: boolean,
attachLogs: boolean,
compressedLogsFile: ?string,
};

@observer
Expand All @@ -144,19 +142,23 @@ export default class BugReportDialog extends Component<Props, State> {
};

state = {
showLogs: true,
attachLogs: true,
compressedLogsFile: null,
};

componentWillMount() {
this.props.onGetLogs();
this.props.onDeleteCompressedLogs();
}

componentWillReceiveProps(nextProps: Object) {
const commpressionFilesChanged = this.props.compressedLog !== nextProps.compressedLog;
if (nextProps.compressedLog && commpressionFilesChanged && !nextProps.isDownloading) {
// proceed to submit when ipc rendered successfully return compressed files
this.submit(nextProps.compressedLog);
const compressedLogsFileChanged = (
!this.props.compressedLogsFile &&
!!nextProps.compressedLogsFile
);
const { compressedLogsFile } = this.state;
if (compressedLogsFile) return false;
if (nextProps.compressedLogsFile && compressedLogsFileChanged && !nextProps.isDownloading) {
this.setState({ compressedLogsFile: nextProps.compressedLogsFile }, this.submit);
}
}

Expand Down Expand Up @@ -206,40 +208,36 @@ export default class BugReportDialog extends Component<Props, State> {
},
});

submit = (compressedLog: ?string) => {
submit = () => {
this.form.submit({
onSuccess: (form) => {
const { logFiles } = this.props;
const logsExist = get(logFiles, ['files'], []).length > 0;

const { attachLogs, compressedLogsFile } = this.state;
if (attachLogs && !compressedLogsFile) {
this.props.onGetLogsAndCompress();
return false;
}
const { email, subject, problem } = form.values();
const data = {
email, subject, problem, compressedLog
email, subject, problem, compressedLogsFile
};

if (this.state.showLogs && logsExist && !compressedLog) {
// submit request with commpressed logs files
this.props.onCompressLogs(this.props.logFiles);
} else {
// regular submit
this.props.onSubmit(data);
}
this.props.onSubmit(data);
},
onError: () => {},
});
};

handleLogsSwitchToggle = (value: boolean) => {
this.setState({ showLogs: value });
this.setState({ attachLogs: value });
};

onClose = () => !this.props.isSubmittingBugReport && this.props.onCancel();

render() {
const { intl } = this.context;
const { showLogs } = this.state;
const { attachLogs } = this.state;
const { form } = this;
const {
onCancel, isSubmitting, isCompressing,
logFiles, error, onDownload, isDownloading,
logFiles, error, onDownload, isDownloading, isSubmittingBugReport
} = this.props;

const submitManuallyLink = intl.formatMessage(messages.submitManuallyLink);
Expand All @@ -250,12 +248,12 @@ export default class BugReportDialog extends Component<Props, State> {

const attachedLogsClasses = classnames([
styles.attachedLogs,
(showLogs && logFiles) ? styles.show : null,
(attachLogs && logFiles) ? styles.show : null,
]);

const submitButtonClasses = classnames([
'submitButton',
(isSubmitting || isCompressing) ? styles.isSubmitting : null,
isSubmittingBugReport ? styles.isSubmitting : null,
]);

const downloadButtonClasses = classnames([
Expand All @@ -272,8 +270,8 @@ export default class BugReportDialog extends Component<Props, State> {
className: submitButtonClasses,
label: this.context.intl.formatMessage(messages.submitButtonLabel),
primary: true,
disabled: isSubmitting,
onClick: this.submit.bind(this, null),
disabled: isSubmittingBugReport,
onClick: this.submit,
},
];

Expand All @@ -299,8 +297,13 @@ export default class BugReportDialog extends Component<Props, State> {
title={intl.formatMessage(messages.title)}
actions={!error ? actions : alternativeActions}
closeOnOverlayClick
onClose={onCancel}
closeButton={<DialogCloseButton onClose={onCancel} />}
onClose={this.onClose}
closeButton={
<DialogCloseButton
disabled={isSubmittingBugReport}
onClose={this.onClose}
/>
}
>
{error ? (
<div>
Expand Down Expand Up @@ -356,7 +359,7 @@ export default class BugReportDialog extends Component<Props, State> {
<Checkbox
onChange={this.handleLogsSwitchToggle}
label={intl.formatMessage(messages.logsSwitchPlaceholder)}
checked={showLogs}
checked={attachLogs}
skin={<SimpleSwitchSkin />}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

button {
cursor: pointer;
letter-spacing: 1px;
}

p {
Expand Down
Loading

0 comments on commit 077a182

Please sign in to comment.