Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion docs/rest-api/rest-notebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ If you work with Apache Zeppelin and find a need for an additional REST API, ple
<col width="200">
<tr>
<td>Description</td>
<td> This ```POST``` method runs the paragraph synchronously by given note and paragraph id. This API can return SUCCESS or ERROR depending on the outcome of the paragraph execution
<td>This ```POST``` method runs the paragraph synchronously by given note and paragraph id. This API can return SUCCESS or ERROR depending on the outcome of the paragraph execution
</td>
</tr>
<tr>
Expand Down Expand Up @@ -972,3 +972,31 @@ If you work with Apache Zeppelin and find a need for an additional REST API, ple
</tr>
</tr>
</table>

<br />
### Clear all paragraph result
<table class="table-configuration">
<col width="200">
<tr>
<td>Description</td>
<td>This ```PUT``` method clear all paragraph results from note of given id.
</td>
</tr>
<tr>
<td>URL</td>
<td>```http://[zeppelin-server]:[zeppelin-port]/api/notebook/[noteId]/clear```</td>
</tr>
<tr>
<td>Success code</td>
<td>200</td>
</tr>
<tr>
<td> Fail code</td>
<td> 500 </td>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we also add 404 (not found) and 401(unauthorised)? as a possible response

</tr>
<tr>
<td>sample JSON response</td>
<td><pre>{"status": "OK"}</pre></td>
</tr>
</tr>
</table>
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,27 @@ public Response deleteParagraph(@PathParam("noteId") String noteId,
return new JsonResponse(Status.OK, "").build();
}

/**
* Clear result of all paragraphs REST API
*
* @param noteId ID of Note
* @return JSON with status.ok
*/
@PUT
@Path("{noteId}/clear")
@ZeppelinApi
public Response clearAllParagraphOutput(@PathParam("noteId") String noteId)
throws IOException {
LOG.info("clear all paragraph output of note {}", noteId);
checkIfUserCanWrite(noteId, "Insufficient privileges you cannot clear this note");

Note note = notebook.getNote(noteId);
checkIfNoteIsNotNull(note);
note.clearAllParagraphOutput();

return new JsonResponse(Status.OK, "").build();
}

/**
* Run note jobs REST API
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ public void onMessage(NotebookSocket conn, String msg) {
case PARAGRAPH_CLEAR_OUTPUT:
clearParagraphOutput(conn, userAndRoles, notebook, messagereceived);
break;
case PARAGRAPH_CLEAR_ALL_OUTPUT:
clearAllParagraphOutput(conn, userAndRoles, notebook, messagereceived);
break;
case NOTE_UPDATE:
updateNote(conn, userAndRoles, notebook, messagereceived);
break;
Expand Down Expand Up @@ -812,6 +815,25 @@ private void cloneNote(NotebookSocket conn, HashSet<String> userAndRoles,
broadcastNoteList(subject, userAndRoles);
}

private void clearAllParagraphOutput(NotebookSocket conn, HashSet<String> userAndRoles,
Notebook notebook, Message fromMessage)
throws IOException {
final String noteId = (String) fromMessage.get("id");
if (StringUtils.isBlank(noteId)) {
return;
}
Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "clear output", fromMessage.principal,
userAndRoles, notebookAuthorization.getOwners(noteId));
return;
}

note.clearAllParagraphOutput();
broadcastNote(note);
}

protected Note importNote(NotebookSocket conn, HashSet<String> userAndRoles,
Notebook notebook, Message fromMessage)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
package org.apache.zeppelin.rest;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.notebook.NotebookAuthorization;
import org.apache.zeppelin.notebook.NotebookAuthorizationInfoSaving;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.server.ZeppelinServer;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.junit.AfterClass;
Expand All @@ -37,12 +36,11 @@
import org.junit.runners.MethodSorters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

/**
Expand Down Expand Up @@ -179,6 +177,43 @@ public void testCloneNote() throws IOException {
ZeppelinServer.notebook.removeNote(clonedNoteId, anonymous);

}

@Test
public void testClearAllParagraphOutput() throws IOException {
// Create note and set result explicitly
Note note = ZeppelinServer.notebook.createNote(anonymous);
Paragraph p1 = note.addParagraph();
InterpreterResult result = new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TEXT, "result");
p1.setResult(result);

Paragraph p2 = note.addParagraph();
p2.setReturn(result, new Throwable());

// clear paragraph result
PutMethod put = httpPut("/notebook/" + note.getId() + "/clear", "");
LOG.info("test clear paragraph output response\n" + put.getResponseBodyAsString());
assertThat(put, isAllowed());
put.releaseConnection();

// check if paragraph results are cleared
GetMethod get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p1.getId());
assertThat(get, isAllowed());
Map<String, Object> resp1 = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
}.getType());
Map<String, Object> resp1Body = (Map<String, Object>) resp1.get("body");
assertNull(resp1Body.get("result"));

get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p2.getId());
assertThat(get, isAllowed());
Map<String, Object> resp2 = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
}.getType());
Map<String, Object> resp2Body = (Map<String, Object>) resp2.get("body");
assertNull(resp2Body.get("result"));
get.releaseConnection();

//cleanup
ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
}


15 changes: 12 additions & 3 deletions zeppelin-web/src/app/home/home.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
'websocketMsgSrv',
'$rootScope',
'arrayOrderingSrv',
'ngToast'
'ngToast',
'noteActionSrv'
];

function HomeCtrl($scope, noteListDataFactory, websocketMsgSrv, $rootScope, arrayOrderingSrv, ngToast) {
function HomeCtrl($scope, noteListDataFactory, websocketMsgSrv, $rootScope, arrayOrderingSrv,
ngToast, noteActionSrv) {
ngToast.dismiss();
var vm = this;
vm.notes = noteListDataFactory;
Expand Down Expand Up @@ -85,6 +87,13 @@
vm.notebookHome = false;
}
});
}

$scope.removeNote = function(noteId) {
noteActionSrv.removeNote(noteId, false);
};

$scope.clearAllParagraphOutput = function(noteId) {
noteActionSrv.clearAllParagraphOutput(noteId);
};
}
})();
16 changes: 15 additions & 1 deletion zeppelin-web/src/app/home/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@
-->

<script type="text/ng-template" id="notebook_folder_renderer.html">
<div ng-if="node.children == null">
<div ng-if="node.children == null"
ng-mouseenter="showButton=true"
ng-mouseleave="showButton=false">
<a style="text-decoration: none;" href="#/notebook/{{node.id}}">
<i style="font-size: 10px;" class="icon-doc"/> {{noteName(node)}}
</a>
<a style="text-decoration: none;">
<i style="font-size: 13px; margin-left: 10px; cursor: pointer; text-decoration: none;"
class="fa fa-eraser" ng-show="showButton" ng-click="clearAllParagraphOutput(node.id)"
tooltip-placement="bottom" tooltip="Clear output">
</i>
</a>
<a style="text-decoration: none;">
<i style="font-size: 13px; margin-left: 2px; cursor: pointer; text-decoration: none;"
class="fa fa-trash-o" ng-show="showButton" ng-click="removeNote(node.id)"
tooltip-placement="bottom" tooltip="Remove note">
</i>
</a>
</div>
<div ng-if="node.children != null">
<a style="text-decoration: none; cursor: pointer;" ng-click="toggleFolderNode(node)">
Expand Down
2 changes: 1 addition & 1 deletion zeppelin-web/src/app/notebook/notebook-actionBar.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ <h3>
</button>
<button type="button"
class="btn btn-default btn-xs"
ng-click="clearAllParagraphOutput()"
ng-click="clearAllParagraphOutput(note.id)"
ng-hide="viewOnly"
ng-class="{'disabled':isNoteRunning()}"
tooltip-placement="bottom" tooltip="Clear output">
Expand Down
35 changes: 7 additions & 28 deletions zeppelin-web/src/app/notebook/notebook.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
'baseUrlSrv',
'$timeout',
'saveAsService',
'ngToast'
'ngToast',
'noteActionSrv'
];

function NotebookCtrl($scope, $route, $routeParams, $location, $rootScope,
$http, websocketMsgSrv, baseUrlSrv, $timeout, saveAsService,
ngToast) {
ngToast, noteActionSrv) {

ngToast.dismiss();

Expand Down Expand Up @@ -143,20 +144,9 @@
$scope.$broadcast('doubleClickParagraph', paragraphId);
};

/** Remove the note and go back tot he main page */
/** TODO(anthony): In the nearly future, go back to the main page and telle to the dude that the note have been remove */
// Remove the note and go back to the main page
$scope.removeNote = function(noteId) {
BootstrapDialog.confirm({
closable: true,
title: '',
message: 'Do you want to delete this note?',
callback: function(result) {
if (result) {
websocketMsgSrv.deleteNote(noteId);
$location.path('/');
}
}
});
noteActionSrv.removeNote(noteId, true);
};

//Export notebook
Expand Down Expand Up @@ -230,19 +220,8 @@
}
};

$scope.clearAllParagraphOutput = function() {
BootstrapDialog.confirm({
closable: true,
title: '',
message: 'Do you want to clear all output?',
callback: function(result) {
if (result) {
_.forEach($scope.note.paragraphs, function(n, key) {
angular.element('#' + n.id + '_paragraphColumn_main').scope().clearParagraphOutput();
});
}
}
});
$scope.clearAllParagraphOutput = function(noteId) {
noteActionSrv.clearAllParagraphOutput(noteId);
};

$scope.toggleAllEditor = function() {
Expand Down
51 changes: 51 additions & 0 deletions zeppelin-web/src/components/noteAction/noteAction.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
(function() {

angular.module('zeppelinWebApp').service('noteActionSrv', noteActionSrv);

noteActionSrv.$inject = ['websocketMsgSrv', '$location'];

function noteActionSrv(websocketMsgSrv, $location) {
this.removeNote = function(noteId, redirectToHome) {
BootstrapDialog.confirm({
closable: true,
title: '',
message: 'Do you want to delete this note?',
callback: function(result) {
if (result) {
websocketMsgSrv.deleteNote(noteId);
if (redirectToHome) {
$location.path('/');
}
}
}
});
};

this.clearAllParagraphOutput = function(noteId) {
BootstrapDialog.confirm({
closable: true,
title: '',
message: 'Do you want to clear all output?',
callback: function(result) {
if (result) {
websocketMsgSrv.clearAllParagraphOutput(noteId);
}
}
});
};
}
})();
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@
websocketEvents.sendNewEvent({op: 'PARAGRAPH_CLEAR_OUTPUT', data: {id: paragraphId}});
},

clearAllParagraphOutput: function(noteId) {
websocketEvents.sendNewEvent({op: 'PARAGRAPH_CLEAR_ALL_OUTPUT', data: {id: noteId}});
},

completion: function(paragraphId, buf, cursor) {
websocketEvents.sendNewEvent({
op: 'COMPLETION',
Expand Down
1 change: 1 addition & 0 deletions zeppelin-web/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
<script src="components/searchService/search.service.js"></script>
<script src="components/login/login.controller.js"></script>
<script src="components/elasticInputCtrl/elasticInput.controller.js"></script>
<script src="components/noteAction/noteAction.service.js"></script>
<!-- endbuild -->
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,17 @@ public Paragraph clearParagraphOutput(String paragraphId) {
return null;
}

/**
* Clear all paragraph output of note
*/
public void clearAllParagraphOutput() {
synchronized (paragraphs) {
for (Paragraph p : paragraphs) {
p.setReturn(null, null);
}
}
}

/**
* Move paragraph into the new index (order from 0 ~ n-1).
*
Expand Down
Loading