diff --git a/lib/redis.js b/lib/redis.js index 0234875..b4dc5c6 100644 --- a/lib/redis.js +++ b/lib/redis.js @@ -152,9 +152,9 @@ async function call(query, body) { const results = []; for (let i = 0; i < lines.length; i++) { try { - results.push(await redis.call(...lines[i])); + results.push({error: false, result: await redis.call(...lines[i])}); } catch (e) { - results.push(`${e.message}`); + results.push({error: true, message: `${e.message}`}); } } return results; diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 43242f4..5dac248 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -219,16 +219,20 @@ export class AppComponent implements OnInit { */ onNewValue(newValue) { this.redisService.call(newValue.id, [newValue.rawLine]).subscribe(ret => { - if (newValue.from === 'root') { - this.onRefresh(); - } - if (newValue.onSuccess) { - newValue.onSuccess(newValue); + if (!ret[0].error) { + if (newValue.from === 'root') { + this.onRefresh(); + } + if (newValue.onSuccess) { + newValue.onSuccess(newValue); + } + this.util.showMessage(newValue.edit ? 'Updated successfully.' : 'Added successfully.'); + } else { + this.util.showMessage('Failed to add the value: ' + ret[0].message); } - this.util.showMessage(newValue.edit ? 'Updated successfully.' : 'Added successfully.'); }, e => { - console.error(e.error.message); - this.util.showMessage('Fail to add the value: ' + this.util.getErrorMessage(e)); + this.util.showMessage('Failed to add the value: ' + this.util.getErrorMessage(e)); + this._store.dispatch(new RedisConnectFailed({id: this.pageData.id})) }); } diff --git a/src/app/components/add-value-dialog/add-value-dialog.component.ts b/src/app/components/add-value-dialog/add-value-dialog.component.ts index c3e0ccb..dfadd8c 100644 --- a/src/app/components/add-value-dialog/add-value-dialog.component.ts +++ b/src/app/components/add-value-dialog/add-value-dialog.component.ts @@ -173,18 +173,6 @@ export class AddValueDialogComponent implements OnInit { } } - /** - * remove exist key - * @param ret the ret include id and key - * @param cb the callback - */ - removeExistKey(ret, cb) { - this.redisService.call(ret.id, [['DEL', ret.key]]).subscribe(() => { - cb(); - }, err => { - this.util.showMessage('Delete is failed: ' + this.util.getErrorMessage(err)); - }); - } /** * check is exist @@ -193,7 +181,7 @@ export class AddValueDialogComponent implements OnInit { */ checkIsExist(ret, cb) { this.redisService.call(ret.id, [['EXISTS', ret.key]]).subscribe((r) => { - if (r && r.length > 0 && r[0] > 0) { // exist + if (!r[0].error && r[0].result > 0) { // exist this.dialogService.open(ConfirmDialogComponent, { width: '360px', data: { title: `Key "${ret.key}" Already Exists`, @@ -201,7 +189,7 @@ export class AddValueDialogComponent implements OnInit { } }).afterClosed().subscribe(cr => { if (cr) { - this.removeExistKey(ret, cb); + cb(); } }); } else { diff --git a/src/app/components/data-viewer/data-viewer.component.ts b/src/app/components/data-viewer/data-viewer.component.ts index 7033f54..5ac54bb 100644 --- a/src/app/components/data-viewer/data-viewer.component.ts +++ b/src/app/components/data-viewer/data-viewer.component.ts @@ -7,7 +7,7 @@ import _ from 'lodash'; import {UtilService} from '../../services/util.service'; import {Observable} from 'rxjs'; import {Store} from '@ngrx/store'; -import {ReqFetchTree} from '../../ngrx/actions/redis-actions'; +import {ReqFetchTree, RedisConnectFailed} from '../../ngrx/actions/redis-actions'; /** * the backend type to frontend type map @@ -124,7 +124,10 @@ export class DataViewerComponent implements OnInit, OnChanges { this.fetchData(); this.util.showMessage('Deleted successfully.'); if (cb) { cb(); } - }, () => this.util.showMessage('Delete is failed.')); + }, () => { + this.util.showMessage('Deleted Failed.'); + this._store.dispatch(new RedisConnectFailed({id: this.pageData.id})) + } } }); } @@ -180,7 +183,7 @@ export class DataViewerComponent implements OnInit, OnChanges { this.loadingPageData = true; this.redisService.call(instanceId, [['LRANGE', key, start, end], ['LLEN', key]]).subscribe(ret => { this.page.totalSize = ret[1]; - this.data = injectValuesToArray(ret[0]); + this.data = injectValuesToArray(ret[0].result); this.showPagination = true; this.loadingPageData = false; } @@ -189,12 +192,13 @@ export class DataViewerComponent implements OnInit, OnChanges { this.loadingPageData = true; this.redisService.call(instanceId, [['ZRANGE', key, start, end, 'withscores'], ['ZCARD', key]]).subscribe(ret => { this.page.totalSize = ret[1]; + const { result } = ret[0]; this.data = []; - for (let i = 0; i < ret[0].length;) { + for (let i = 0; i < result.length;) { this.data.push({ index: this.page.pageIndex * this.page.pageSize + (i / 2), - score: parseFloat(ret[0][i + 1]), - value: ret[0][i], + score: parseFloat(result[i + 1]), + value: result[i], }); i += 2; } @@ -206,7 +210,7 @@ export class DataViewerComponent implements OnInit, OnChanges { if (!this.setCachedData) { this.loadingPageData = true; this.redisService.call(instanceId, [['SMEMBERS', key]]).subscribe(ret => { - this.setCachedData = injectValuesToArray(ret[0]); + this.setCachedData = injectValuesToArray(ret[0].result); this.page.totalSize = this.setCachedData.length; this.data = this.setCachedData.slice(start, end + 1); this.loadingPageData = false; @@ -222,10 +226,11 @@ export class DataViewerComponent implements OnInit, OnChanges { this.loadingPageData = true; this.redisService.call(instanceId, [['HGETALL', key]]).subscribe(ret => { this.hashCachedData = []; - for (let i = 0; i < ret[0].length;) { + const { result } = ret[0]; + for (let i = 0; i < result.length;) { this.hashCachedData.push({ - key: ret[0][i], - value: ret[0][i + 1], + key: result[i], + value: result[i + 1], }); i += 2; } @@ -244,7 +249,7 @@ export class DataViewerComponent implements OnInit, OnChanges { this.loadingPageData = true; this.redisService.call(instanceId, [['GET', key]]).subscribe(ret => { this.loadingPageData = false; - this.stringValue = ret[0]; + this.stringValue = ret[0].result; }); } } @@ -358,8 +363,8 @@ export class DataViewerComponent implements OnInit, OnChanges { this.onDeleteValue.emit(); this._store.dispatch(new ReqFetchTree({id: this.pageData.id})); }, e => { - this.util.showMessage('Delete is failed.'); - console.error(e); + this.util.showMessage('Delete Failed.'); + this._store.dispatch(new RedisConnectFailed({id: this.pageData.id})) }); } }); diff --git a/src/app/components/import-data-dialog/import-data-dialog.component.ts b/src/app/components/import-data-dialog/import-data-dialog.component.ts index a92c53b..0dd87ed 100644 --- a/src/app/components/import-data-dialog/import-data-dialog.component.ts +++ b/src/app/components/import-data-dialog/import-data-dialog.component.ts @@ -113,10 +113,10 @@ export class ImportDataDialogComponent implements OnInit { }); const totalRow = this.flushDB ? commands.length - 1 : commands.length; - this.redisService.call(this.instanceId, commands).subscribe((rsp) => { + this.redisService.call(this.instanceId, commands).subscribe((results) => { let numberOfSucceed = 0; - _.each(rsp, v => { - numberOfSucceed += (!!v && v.toString().toLowerCase().indexOf('err') < 0) ? 1 : 0; + _.each(results, v => { + numberOfSucceed += v.error ? 0 : 1; }); numberOfSucceed -= this.flushDB ? 1 : 0; const numberOfFailed = totalRow - numberOfSucceed; diff --git a/src/app/ngrx/effects/cli-effect.ts b/src/app/ngrx/effects/cli-effect.ts index 56217f0..26a858b 100644 --- a/src/app/ngrx/effects/cli-effect.ts +++ b/src/app/ngrx/effects/cli-effect.ts @@ -31,13 +31,17 @@ export class CliEffect { action['payload'].redisId, [action['payload'].command]).pipe( map(ret => { + const { error } = ret[0]; + const result = error ? ret[0].message : ret[0].result; + if (action['payload'].cb) { - action['payload'].cb(false); + action['payload'].cb(error); } + return new CommandRunFinished({ - result: ret[0], + result, id: action['payload'].id, - error: !(ret[0] && ret[0].toString() && ret[0].toString().toLowerCase().indexOf('err') < 0), + error, }); }), catchError((e) => { diff --git a/src/app/ngrx/effects/page-effect.ts b/src/app/ngrx/effects/page-effect.ts index fc948c1..8e25208 100644 --- a/src/app/ngrx/effects/page-effect.ts +++ b/src/app/ngrx/effects/page-effect.ts @@ -34,26 +34,30 @@ export class PageEffect { action['payload'].id, [['info']]).pipe( map(ret => { - const rawInfo = ret[0]; - const result = []; - rawInfo.split('\n').forEach(line => { - if (line.indexOf('#') === 0) { - return; - } - if (line.trim() === '') { - return; - } - const parts = line.split(':'); - result.push({ - key: parts[0].split('_').join(' '), - value: parts[1], + if (!ret[0].error) { + const rawInfo = ret[0].result; + const result = []; + rawInfo.split('\n').forEach(line => { + if (line.indexOf('#') === 0) { + return; + } + if (line.trim() === '') { + return; + } + const parts = line.split(':'); + result.push({ + key: parts[0].split('_').join(' '), + value: parts[1], + }); }); - }); - return new LoadedPage({ - item: result, - requestId: action['payload'].requestId, - id: action['payload'].id - }); + return new LoadedPage({ + item: result, + requestId: action['payload'].requestId, + id: action['payload'].id + }); + } else { + this.util.showMessage('Failed to load instance.'); + } }), catchError(() => { const id = action['payload'].id;