use of org.apache.zeppelin.display.AngularObject in project SSM by Intel-bigdata.
the class Paragraph method extractVariablesFromAngularRegistry.
String extractVariablesFromAngularRegistry(String scriptBody, Map<String, Input> inputs, AngularObjectRegistry angularRegistry) {
final String noteId = this.getNote().getId();
final String paragraphId = this.getId();
final Set<String> keys = new HashSet<>(inputs.keySet());
for (String varName : keys) {
final AngularObject paragraphScoped = angularRegistry.get(varName, noteId, paragraphId);
final AngularObject noteScoped = angularRegistry.get(varName, noteId, null);
final AngularObject angularObject = paragraphScoped != null ? paragraphScoped : noteScoped;
if (angularObject != null) {
inputs.remove(varName);
final String pattern = "[$][{]\\s*" + varName + "\\s*(?:=[^}]+)?[}]";
scriptBody = scriptBody.replaceAll(pattern, angularObject.get().toString());
}
}
return scriptBody;
}
use of org.apache.zeppelin.display.AngularObject in project SSM by Intel-bigdata.
the class Notebook method loadNoteFromRepo.
@SuppressWarnings("rawtypes")
public Note loadNoteFromRepo(String id, AuthenticationInfo subject) {
Note note = null;
try {
note = notebookRepo.get(id, subject);
} catch (IOException e) {
logger.error("Failed to load " + id, e);
}
if (note == null) {
return null;
}
convertFromSingleResultToMultipleResultsFormat(note);
// Manually inject ALL dependencies, as DI constructor was NOT used
note.setIndex(this.noteSearchService);
note.setCredentials(this.credentials);
note.setInterpreterFactory(replFactory);
note.setInterpreterSettingManager(interpreterSettingManager);
note.setJobListenerFactory(jobListenerFactory);
note.setNotebookRepo(notebookRepo);
Map<String, SnapshotAngularObject> angularObjectSnapshot = new HashMap<>();
// restore angular object --------------
Date lastUpdatedDate = new Date(0);
for (Paragraph p : note.getParagraphs()) {
p.setNote(note);
if (p.getDateFinished() != null && lastUpdatedDate.before(p.getDateFinished())) {
lastUpdatedDate = p.getDateFinished();
}
}
Map<String, List<AngularObject>> savedObjects = note.getAngularObjects();
if (savedObjects != null) {
for (String intpGroupName : savedObjects.keySet()) {
List<AngularObject> objectList = savedObjects.get(intpGroupName);
for (AngularObject object : objectList) {
SnapshotAngularObject snapshot = angularObjectSnapshot.get(object.getName());
if (snapshot == null || snapshot.getLastUpdate().before(lastUpdatedDate)) {
angularObjectSnapshot.put(object.getName(), new SnapshotAngularObject(intpGroupName, object, lastUpdatedDate));
}
}
}
}
note.setNoteEventListener(this);
note.setNoteNameListener(folders);
synchronized (notes) {
notes.put(note.getId(), note);
folders.putNote(note);
refreshCron(note.getId());
}
for (String name : angularObjectSnapshot.keySet()) {
SnapshotAngularObject snapshot = angularObjectSnapshot.get(name);
List<InterpreterSetting> settings = interpreterSettingManager.get();
for (InterpreterSetting setting : settings) {
InterpreterGroup intpGroup = setting.getInterpreterGroup(subject.getUser(), note.getId());
if (intpGroup.getId().equals(snapshot.getIntpGroupId())) {
AngularObjectRegistry registry = intpGroup.getAngularObjectRegistry();
String noteId = snapshot.getAngularObject().getNoteId();
String paragraphId = snapshot.getAngularObject().getParagraphId();
// at this point, remote interpreter process is not created.
// so does not make sense add it to the remote.
//
// therefore instead of addAndNotifyRemoteProcess(), need to use add()
// that results add angularObject only in ZeppelinServer side not remoteProcessSide
registry.add(name, snapshot.getAngularObject().get(), noteId, paragraphId);
}
}
}
return note;
}
use of org.apache.zeppelin.display.AngularObject in project zeppelin by apache.
the class ZeppelinContext method angularGlobal.
/**
* Get angular object. Look up global scope
* @param name variable name
* @return value
*/
@Deprecated
public Object angularGlobal(String name) {
AngularObjectRegistry registry = interpreterContext.getAngularObjectRegistry();
AngularObject ao = registry.get(name, null, null);
if (ao == null) {
return null;
} else {
return ao.get();
}
}
use of org.apache.zeppelin.display.AngularObject in project zeppelin by apache.
the class RemoteInterpreterEventPoller method run.
@Override
public void run() {
Client client = null;
AppendOutputRunner runner = new AppendOutputRunner(listener);
ScheduledFuture<?> appendFuture = appendService.scheduleWithFixedDelay(runner, 0, AppendOutputRunner.BUFFER_TIME_MS, TimeUnit.MILLISECONDS);
while (!shutdown) {
// wait and retry
if (!interpreterProcess.isRunning()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// nothing to do
}
continue;
}
try {
client = interpreterProcess.getClient();
} catch (Exception e1) {
logger.error("Can't get RemoteInterpreterEvent", e1);
waitQuietly();
continue;
}
RemoteInterpreterEvent event = null;
boolean broken = false;
try {
event = client.getEvent();
} catch (TException e) {
broken = true;
logger.error("Can't get RemoteInterpreterEvent", e);
waitQuietly();
continue;
} finally {
interpreterProcess.releaseClient(client, broken);
}
AngularObjectRegistry angularObjectRegistry = interpreterGroup.getAngularObjectRegistry();
try {
if (event.getType() == RemoteInterpreterEventType.NO_OP) {
continue;
} else if (event.getType() == RemoteInterpreterEventType.ANGULAR_OBJECT_ADD) {
AngularObject angularObject = gson.fromJson(event.getData(), AngularObject.class);
angularObjectRegistry.add(angularObject.getName(), angularObject.get(), angularObject.getNoteId(), angularObject.getParagraphId());
} else if (event.getType() == RemoteInterpreterEventType.ANGULAR_OBJECT_UPDATE) {
AngularObject angularObject = gson.fromJson(event.getData(), AngularObject.class);
AngularObject localAngularObject = angularObjectRegistry.get(angularObject.getName(), angularObject.getNoteId(), angularObject.getParagraphId());
if (localAngularObject instanceof RemoteAngularObject) {
// to avoid ping-pong loop
((RemoteAngularObject) localAngularObject).set(angularObject.get(), true, false);
} else {
localAngularObject.set(angularObject.get());
}
} else if (event.getType() == RemoteInterpreterEventType.ANGULAR_OBJECT_REMOVE) {
AngularObject angularObject = gson.fromJson(event.getData(), AngularObject.class);
angularObjectRegistry.remove(angularObject.getName(), angularObject.getNoteId(), angularObject.getParagraphId());
} else if (event.getType() == RemoteInterpreterEventType.RUN_INTERPRETER_CONTEXT_RUNNER) {
InterpreterContextRunner runnerFromRemote = gson.fromJson(event.getData(), RemoteInterpreterContextRunner.class);
listener.onRemoteRunParagraph(runnerFromRemote.getNoteId(), runnerFromRemote.getParagraphId());
} else if (event.getType() == RemoteInterpreterEventType.RESOURCE_POOL_GET_ALL) {
ResourceSet resourceSet = getAllResourcePoolExcept();
sendResourcePoolResponseGetAll(resourceSet);
} else if (event.getType() == RemoteInterpreterEventType.RESOURCE_GET) {
String resourceIdString = event.getData();
ResourceId resourceId = gson.fromJson(resourceIdString, ResourceId.class);
logger.debug("RESOURCE_GET {} {}", resourceId.getResourcePoolId(), resourceId.getName());
Object o = getResource(resourceId);
sendResourceResponseGet(resourceId, o);
} else if (event.getType() == RemoteInterpreterEventType.RESOURCE_INVOKE_METHOD) {
String message = event.getData();
InvokeResourceMethodEventMessage invokeMethodMessage = gson.fromJson(message, InvokeResourceMethodEventMessage.class);
Object ret = invokeResourceMethod(invokeMethodMessage);
sendInvokeMethodResult(invokeMethodMessage, ret);
} else if (event.getType() == RemoteInterpreterEventType.OUTPUT_APPEND) {
// on output append
Map<String, String> outputAppend = gson.fromJson(event.getData(), new TypeToken<Map<String, Object>>() {
}.getType());
String noteId = (String) outputAppend.get("noteId");
String paragraphId = (String) outputAppend.get("paragraphId");
int index = Integer.parseInt(outputAppend.get("index"));
String outputToAppend = (String) outputAppend.get("data");
String appId = (String) outputAppend.get("appId");
if (appId == null) {
runner.appendBuffer(noteId, paragraphId, index, outputToAppend);
} else {
appListener.onOutputAppend(noteId, paragraphId, index, appId, outputToAppend);
}
} else if (event.getType() == RemoteInterpreterEventType.OUTPUT_UPDATE_ALL) {
Map<String, Object> outputUpdate = gson.fromJson(event.getData(), new TypeToken<Map<String, Object>>() {
}.getType());
String noteId = (String) outputUpdate.get("noteId");
String paragraphId = (String) outputUpdate.get("paragraphId");
// clear the output
listener.onOutputClear(noteId, paragraphId);
List<Map<String, String>> messages = (List<Map<String, String>>) outputUpdate.get("messages");
if (messages != null) {
for (int i = 0; i < messages.size(); i++) {
Map<String, String> m = messages.get(i);
InterpreterResult.Type type = InterpreterResult.Type.valueOf((String) m.get("type"));
String outputToUpdate = (String) m.get("data");
listener.onOutputUpdated(noteId, paragraphId, i, type, outputToUpdate);
}
}
} else if (event.getType() == RemoteInterpreterEventType.OUTPUT_UPDATE) {
// on output update
Map<String, String> outputAppend = gson.fromJson(event.getData(), new TypeToken<Map<String, Object>>() {
}.getType());
String noteId = (String) outputAppend.get("noteId");
String paragraphId = (String) outputAppend.get("paragraphId");
int index = Integer.parseInt(outputAppend.get("index"));
InterpreterResult.Type type = InterpreterResult.Type.valueOf((String) outputAppend.get("type"));
String outputToUpdate = (String) outputAppend.get("data");
String appId = (String) outputAppend.get("appId");
if (appId == null) {
listener.onOutputUpdated(noteId, paragraphId, index, type, outputToUpdate);
} else {
appListener.onOutputUpdated(noteId, paragraphId, index, appId, type, outputToUpdate);
}
} else if (event.getType() == RemoteInterpreterEventType.APP_STATUS_UPDATE) {
// on output update
Map<String, String> appStatusUpdate = gson.fromJson(event.getData(), new TypeToken<Map<String, String>>() {
}.getType());
String noteId = appStatusUpdate.get("noteId");
String paragraphId = appStatusUpdate.get("paragraphId");
String appId = appStatusUpdate.get("appId");
String status = appStatusUpdate.get("status");
appListener.onStatusChange(noteId, paragraphId, appId, status);
} else if (event.getType() == RemoteInterpreterEventType.REMOTE_ZEPPELIN_SERVER_RESOURCE) {
RemoteZeppelinServerResource reqResourceBody = gson.fromJson(event.getData(), RemoteZeppelinServerResource.class);
progressRemoteZeppelinControlEvent(reqResourceBody.getResourceType(), listener, reqResourceBody);
} else if (event.getType() == RemoteInterpreterEventType.META_INFOS) {
Map<String, String> metaInfos = gson.fromJson(event.getData(), new TypeToken<Map<String, String>>() {
}.getType());
String settingId = RemoteInterpreterUtils.getInterpreterSettingId(interpreterGroup.getId());
listener.onMetaInfosReceived(settingId, metaInfos);
} else if (event.getType() == RemoteInterpreterEventType.PARA_INFOS) {
Map<String, String> paraInfos = gson.fromJson(event.getData(), new TypeToken<Map<String, String>>() {
}.getType());
String noteId = paraInfos.get("noteId");
String paraId = paraInfos.get("paraId");
String settingId = RemoteInterpreterUtils.getInterpreterSettingId(interpreterGroup.getId());
if (noteId != null && paraId != null && settingId != null) {
listener.onParaInfosReceived(noteId, paraId, settingId, paraInfos);
}
}
logger.debug("Event from remote process {}", event.getType());
} catch (Exception e) {
logger.error("Can't handle event " + event, e);
}
}
if (appendFuture != null) {
appendFuture.cancel(true);
}
}
use of org.apache.zeppelin.display.AngularObject in project zeppelin by apache.
the class NotebookServer method angularObjectUpdated.
/**
* When angular object updated from client
*
* @param conn the web socket.
* @param notebook the notebook.
* @param fromMessage the message.
*/
private void angularObjectUpdated(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, Message fromMessage) {
String noteId = (String) fromMessage.get("noteId");
String paragraphId = (String) fromMessage.get("paragraphId");
String interpreterGroupId = (String) fromMessage.get("interpreterGroupId");
String varName = (String) fromMessage.get("name");
Object varValue = fromMessage.get("value");
String user = fromMessage.principal;
AngularObject ao = null;
boolean global = false;
// propagate change to (Remote) AngularObjectRegistry
Note note = notebook.getNote(noteId);
if (note != null) {
List<InterpreterSetting> settings = notebook.getInterpreterSettingManager().getInterpreterSettings(note.getId());
for (InterpreterSetting setting : settings) {
if (setting.getInterpreterGroup(user, note.getId()) == null) {
continue;
}
if (interpreterGroupId.equals(setting.getInterpreterGroup(user, note.getId()).getId())) {
AngularObjectRegistry angularObjectRegistry = setting.getInterpreterGroup(user, note.getId()).getAngularObjectRegistry();
// first trying to get local registry
ao = angularObjectRegistry.get(varName, noteId, paragraphId);
if (ao == null) {
// then try notebook scope registry
ao = angularObjectRegistry.get(varName, noteId, null);
if (ao == null) {
// then try global scope registry
ao = angularObjectRegistry.get(varName, null, null);
if (ao == null) {
LOG.warn("Object {} is not binded", varName);
} else {
// path from client -> server
ao.set(varValue, false);
global = true;
}
} else {
// path from client -> server
ao.set(varValue, false);
global = false;
}
} else {
ao.set(varValue, false);
global = false;
}
break;
}
}
}
if (global) {
// interpreter.
for (Note n : notebook.getAllNotes()) {
List<InterpreterSetting> settings = notebook.getInterpreterSettingManager().getInterpreterSettings(note.getId());
for (InterpreterSetting setting : settings) {
if (setting.getInterpreterGroup(user, n.getId()) == null) {
continue;
}
if (interpreterGroupId.equals(setting.getInterpreterGroup(user, n.getId()).getId())) {
AngularObjectRegistry angularObjectRegistry = setting.getInterpreterGroup(user, n.getId()).getAngularObjectRegistry();
this.broadcastExcept(n.getId(), new Message(OP.ANGULAR_OBJECT_UPDATE).put("angularObject", ao).put("interpreterGroupId", interpreterGroupId).put("noteId", n.getId()).put("paragraphId", ao.getParagraphId()), conn);
}
}
}
} else {
// broadcast to all web session for the note
this.broadcastExcept(note.getId(), new Message(OP.ANGULAR_OBJECT_UPDATE).put("angularObject", ao).put("interpreterGroupId", interpreterGroupId).put("noteId", note.getId()).put("paragraphId", ao.getParagraphId()), conn);
}
}
Aggregations