You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by de...@apache.org on 2014/07/23 21:44:17 UTC

svn commit: r1612933 - in /uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server: AlienTextFile.java DuccAbstractHandler.java DuccHandler.java

Author: degenaro
Date: Wed Jul 23 19:44:17 2014
New Revision: 1612933

URL: http://svn.apache.org/r1612933
Log:
UIMA-2723 DUCC Web Server (WS) should handle large data display (e.g. logs) better, perhaps with Top/Bottom/Next/Previous

back-end

Added:
    uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/AlienTextFile.java   (with props)
Modified:
    uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/DuccAbstractHandler.java
    uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/DuccHandler.java

Added: uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/AlienTextFile.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/AlienTextFile.java?rev=1612933&view=auto
==============================================================================
--- uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/AlienTextFile.java (added)
+++ uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/AlienTextFile.java Wed Jul 23 19:44:17 2014
@@ -0,0 +1,271 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+*/
+package org.apache.uima.ducc.ws.server;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.apache.uima.ducc.common.utils.AlienAbstract;
+import org.apache.uima.ducc.common.utils.DuccLogger;
+
+public class AlienTextFile extends AlienAbstract {	
+	
+	private static DuccLogger duccLogger = DuccLogger.getLogger(AlienTextFile.class.getName(), null);
+	
+	private static String command_du = "/usr/bin/du";
+	private static String flag_dash_b = "-b";
+	
+	private static String command_dd = "/bin/dd";
+	private static String arg_if = "if=";
+	private static String arg_skip = "skip=";
+	private static String arg_count = "count=";
+	
+	private static int sizeDu = 4096;
+	private static int sizeDefault = 0;
+	
+	private static int sizeBlockDd = 512;
+	
+	private String file_name;
+	private int page_bytes = 4096;
+	
+	public AlienTextFile(String user, String file_name, String ducc_ling) {
+		set_user(user);
+		set_file_name(file_name);
+		set_ducc_ling(ducc_ling);
+	}
+	
+	public AlienTextFile(String user, String file_name, String ducc_ling, int pageBytes) {
+		set_user(user);
+		set_file_name(file_name);
+		set_ducc_ling(ducc_ling);
+		set_page_bytes(pageBytes);
+	}
+	
+	protected void set_file_name(String value) {
+		file_name = value;
+	}
+	
+	protected String get_file_name() {
+		return file_name;
+	}
+	
+	protected void set_page_bytes(int value) {
+		page_bytes = value;
+	}
+	
+	protected int get_page_bytes() {
+		return page_bytes;
+	}
+	
+	private String[] buildCommandDu() {
+		String[] command_ducc_ling_yes = { ducc_ling, q_parameter, u_parameter, user, command_du, flag_dash_b, file_name };
+		String[] command_ducc_ling_no  = { command_du, flag_dash_b, file_name };
+		String[] command = command_ducc_ling_yes;
+		if(ducc_ling == null) {
+			command = command_ducc_ling_no;
+		}
+		return command;
+	}
+	
+	private String getDu() throws Throwable {
+		String methodName = "getDu";
+		String data = "";
+		try {
+			String[] command = buildCommandDu();
+			echo(command);
+			ProcessBuilder pb = new ProcessBuilder( command );
+			Process p = pb.start();
+			p.waitFor();
+			InputStream pOut = p.getInputStream();
+			InputStreamReader isr = new InputStreamReader(pOut);
+			BufferedReader br = new BufferedReader(isr);
+			char[] cbuf = new char[sizeDu];
+	        int rc = br.read(cbuf);
+	        duccLogger.debug(methodName, duccId, rc);
+	        data = new String(cbuf);
+	        duccLogger.debug(methodName, duccId, data);
+		}
+		catch(Throwable t) {
+			duccLogger.warn(methodName, duccId, t);
+		}
+        return data;
+	}
+	
+	private int convertToInt(String value, int otherwise) {
+		String methodName = "convertToInt";
+		int retVal = otherwise;
+		try {
+			retVal = Integer.parseInt(value);
+		}
+		catch(Throwable t) {
+			duccLogger.debug(methodName, duccId, t);
+		}
+		return retVal;
+	}
+	
+	public int getByteSize() {
+		String methodName = "getByteSize";
+		int retVal = sizeDefault;
+		try {
+			String text = getDu();
+			if(text != null) {
+				text = text.trim();
+				String[] tokens = text.split("\\s+");
+				if(tokens.length > 0) {
+					duccLogger.debug(methodName, duccId, tokens[0]);
+					retVal = convertToInt(tokens[0], sizeDefault);
+				}
+				else {
+					duccLogger.debug(methodName, duccId, "empty");
+				}
+			}
+		}
+		catch(Throwable t) {
+			duccLogger.warn(methodName, duccId, t);
+		}
+		return retVal;
+	}
+	
+	/******/
+	
+	private String[] buildCommandDd(int skip, int count) {
+		String[] command_ducc_ling_yes = { ducc_ling, q_parameter, u_parameter, user, command_dd, arg_if+file_name, arg_skip+skip, arg_count+count };
+		String[] command_ducc_ling_no  = { command_dd, arg_if+file_name, arg_skip+skip, arg_count+count };
+		String[] command = command_ducc_ling_yes;
+		if(ducc_ling == null) {
+			command = command_ducc_ling_no;
+		}
+		return command;
+	}
+	
+	private String getDd(int skip, int count) throws Throwable {
+		String methodName = "getDd";
+		String data = "";
+		try {
+			String[] command = buildCommandDd(skip, count);
+			echo(command);
+			ProcessBuilder pb = new ProcessBuilder( command );
+			Process p = pb.start();
+			p.waitFor();
+			InputStream is = p.getInputStream();
+			int ev = p.exitValue();
+			if(ev > 0) {
+				is = p.getErrorStream();
+			}
+			InputStreamReader isr = new InputStreamReader(is);
+			BufferedReader br = new BufferedReader(isr);
+			int bufSize = count*sizeBlockDd;
+			char[] cbuf = new char[bufSize];
+			int rc = br.read(cbuf);
+			duccLogger.debug(methodName, duccId, rc);
+	        data = new String(cbuf);
+		}
+		catch(Throwable t) {
+			duccLogger.warn(methodName, duccId, t);
+		}
+		return data;
+	}
+	
+	public String getChunk(int byteStart, int byteCount) {
+		String methodName = "getChunk";
+		String retVal = "";
+		try {
+			int skip = (int) Math.ceil(byteStart / (1.0*sizeBlockDd));
+			int count = (int) Math.ceil(byteCount / (1.0*sizeBlockDd));
+			//System.err.println("skip:"+skip+" "+"count:"+count);
+			retVal = getDd(skip, count);
+		}
+		catch(Throwable t) {
+			duccLogger.warn(methodName, duccId, t);
+		}
+		return retVal;
+	}
+	
+	public int getPageCount() {
+		int retVal = 0;
+		int pageSize = get_page_bytes();
+		int fileBytes = getByteSize();
+		retVal = (int) Math.ceil(fileBytes / (1.0 * pageSize));
+		return retVal;
+	}
+	
+	public String getPage(int pageNo) {
+		String retVal = "";
+		int pageSize = get_page_bytes();
+		retVal = getChunk(pageNo*pageSize, pageSize);
+		return retVal;
+	}
+	
+	public String getPageFirst() {
+		String retVal = "";
+		int pageSize = get_page_bytes();
+		retVal = getChunk(0, pageSize);
+		return retVal;
+	}
+	
+	public String getPageLast() {
+		String retVal = "";
+		int pageSize = get_page_bytes();
+		int fileBytes = getByteSize();
+		if(fileBytes > pageSize) {
+			int byteStart = (fileBytes-pageSize)+1;
+			int byteCount = pageSize;
+			//System.err.println("byteStart:"+byteStart+" "+"byteCount:"+byteCount);
+			retVal = getChunk(byteStart, byteCount);
+		}
+		else {
+			retVal = getPageFirst();
+		}
+		return retVal;
+	}
+	
+	/******/
+	
+	public static void main(String[] args) throws Throwable {
+		AlienTextFile alienTextFile;
+		String arg_user = args[0];
+		String arg_file = args[1];
+		if(args.length < 3) {
+			alienTextFile = new AlienTextFile(arg_user, arg_file, null);
+		}
+		else {
+			String arg_ducc_ling = args[2];
+			alienTextFile = new AlienTextFile(arg_user, arg_file, arg_ducc_ling);
+		}
+		int bytes = alienTextFile.getByteSize();
+		System.out.println("--- file bytes ---");
+		System.out.println(bytes);
+		String data;
+		data = alienTextFile.getPageFirst();
+		System.out.println("--- first ---");
+		System.out.println(data);
+		data = alienTextFile.getPageLast();
+		System.out.println("--- last ---");
+		System.out.println(data);
+		int count = alienTextFile.getPageCount();
+		System.out.println("--- page count ---");
+		System.out.println(count);
+		for(int i=0; i<count; i++) {
+			data = alienTextFile.getPage(i);
+			System.out.println("--- page "+i+" ---");
+			System.out.println(data);
+		}
+	}
+}

Propchange: uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/AlienTextFile.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/DuccAbstractHandler.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/DuccAbstractHandler.java?rev=1612933&r1=1612932&r2=1612933&view=diff
==============================================================================
--- uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/DuccAbstractHandler.java (original)
+++ uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/DuccAbstractHandler.java Wed Jul 23 19:44:17 2014
@@ -77,6 +77,7 @@ public abstract class DuccAbstractHandle
 	public final String duccContext = "/ducc-servlet";
 	
 	public final String duccLogData			  = duccContext+"/log-data";
+	public final String duccFilePager 		  = "file.pager.html";
 	
 	public final String duccJpInitSummary	  = duccContext+"/uima-initialization-report-summary";
 	public final String duccJpInitData		  = duccContext+"/uima-initialization-report-data";
@@ -816,7 +817,7 @@ public abstract class DuccAbstractHandle
 			}
 			String logsjobdir = job.getUserLogsDir()+job.getDuccId().getFriendly()+File.separator;
 			String logfile = "jd.err.log";
-			String href = "<a href=\""+duccLogData+"?"+"fname="+logsjobdir+logfile+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+name+"</a>";
+			String href = "<a href=\""+duccFilePager+"?"+"fname="+logsjobdir+logfile+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+name+"</a>";
 			retVal = href;
 		}
 		return retVal;

Modified: uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/DuccHandler.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/DuccHandler.java?rev=1612933&r1=1612932&r2=1612933&view=diff
==============================================================================
--- uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/DuccHandler.java (original)
+++ uima/sandbox/uima-ducc/trunk/uima-ducc-web/src/main/java/org/apache/uima/ducc/ws/server/DuccHandler.java Wed Jul 23 19:44:17 2014
@@ -417,8 +417,17 @@ public class DuccHandler extends DuccAbs
 		}
 		String logsjobdir = job.getUserLogsDir()+job.getDuccId().getFriendly()+File.separator;
 		String logfile = buildLogFileName(job, process, sType);
+		
+		String user = job.getStandardInfo().getUser();
+		String file_name = logsjobdir+logfile;
+		String ducc_ling = null;
+		
+		AlienTextFile atf = new AlienTextFile(user, file_name, ducc_ling);
+		int pages = atf.getPageCount();
+		String parms = "?"+"fname="+file_name+"&"+"pages="+pages;
+		String url=duccFilePager+parms;
 		String errfile = "jd.err.log";
-		String href = "<a href=\""+duccLogData+"?"+"fname="+logsjobdir+logfile+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+logfile+"</a>";
+		String href = "<a href=\""+url+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+logfile+"</a>";
 		String tr = trGet(counter);
 		rb.append(tr);
 		int index = -1;
@@ -964,7 +973,7 @@ public class DuccHandler extends DuccAbs
 				index = 0;
 				// Log
 				index = 1;
-				String href2 = "<a href=\""+duccLogData+"?"+"fname="+logsjobdir+errfile+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+errfile+"</a>";
+				String href2 = "<a href=\""+duccFilePager+"?"+"fname="+logsjobdir+errfile+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+errfile+"</a>";
 				cbList[index] = new StringBuffer();
 				cbList[index].append("<td>");
 				cbList[index].append(href2);
@@ -1743,7 +1752,7 @@ public class DuccHandler extends DuccAbs
 					*/
 					// name
 					row.append("<td>");
-					String href = "<a href=\""+duccFileContents+"?"+"fname="+file.getAbsolutePath()+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+file.getName()+"</a>";
+					String href = "<a href=\""+duccFilePager+"?"+"fname="+file.getAbsolutePath()+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+file.getName()+"</a>";
 					row.append(href);
 					row.append("</td>");
 					// size
@@ -1797,7 +1806,7 @@ public class DuccHandler extends DuccAbs
 					*/
 					// name
 					row.append("<td>");
-					String href = "<a href=\""+duccFileContents+"?"+"fname="+file.getAbsolutePath()+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+file.getName()+"</a>";
+					String href = "<a href=\""+duccFilePager+"?"+"fname="+file.getAbsolutePath()+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+file.getName()+"</a>";
 					row.append(href);
 					row.append("</td>");
 					// size
@@ -1847,7 +1856,7 @@ public class DuccHandler extends DuccAbs
 					row.append("</td>");
 					// name
 					row.append("<td>");
-					String href = "<a href=\""+duccFileContents+"?"+"fname="+file.getAbsolutePath()+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+file.getName()+"</a>";
+					String href = "<a href=\""+duccFilePager+"?"+"fname="+file.getAbsolutePath()+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+file.getName()+"</a>";
 					row.append(href);
 					row.append("</td>");
 					// size
@@ -1965,7 +1974,7 @@ public class DuccHandler extends DuccAbs
 						row.append("</td>");
 						// name
 						row.append("<td>");
-						String href = "<a href=\""+duccFileContents+"?"+"fname="+historyFile.getAbsolutePath()+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+historyFile.getName()+"</a>";
+						String href = "<a href=\""+duccFilePager+"?"+"fname="+historyFile.getAbsolutePath()+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+historyFile.getName()+"</a>";
 						row.append(href);
 						row.append("</td>");
 						// size
@@ -2049,7 +2058,7 @@ public class DuccHandler extends DuccAbs
 								link = logfile+":"+reason;
 							}
 						}
-						String href = "<a href=\""+duccLogData+"?"+"loc=bot&"+"fname="+logsjobdir+logfile+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+link+"</a>";
+						String href = "<a href=\""+duccFilePager+"?"+"fname="+logsjobdir+logfile+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+link+"</a>";
 						data.append(href);
 					}
 					data.append("</table>");
@@ -2097,7 +2106,7 @@ public class DuccHandler extends DuccAbs
 								link = logfile+":"+reason;
 							}
 						}
-						String href = "<a href=\""+duccLogData+"?"+"loc=bot&"+"fname="+logsjobdir+logfile+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+link+"</a>";
+						String href = "<a href=\""+duccFilePager+"?"+"fname="+logsjobdir+logfile+"\" onclick=\"var newWin = window.open(this.href,'child','height=800,width=1200,scrollbars');  newWin.focus(); return false;\">"+link+"</a>";
 						data.append(href);
 					}
 					data.append("</table>");
@@ -3293,39 +3302,82 @@ public class DuccHandler extends DuccAbs
 		String methodName = "handleDuccServletFileContents";
 		duccLogger.trace(methodName, null, messages.fetch("enter"));
 		String fname = request.getParameter("fname");
+		String page = request.getParameter("page");
 		StringBuffer sb = new StringBuffer();
-		InputStreamReader isr = null;
-		BufferedReader br = null;
+		String userId = duccWebSessionManager.getUserId(request);	
+		String newline = "\n";
+		String colon = ":";
 		try {
-			String userId = duccWebSessionManager.getUserId(request);
-			isr = DuccFile.getInputStreamReader(fname, userId);
-			br = new BufferedReader(isr);
-			String logLine;
-			while ((logLine = br.readLine()) != null)   {
-				if(fname.endsWith(".xml")) {
-					logLine = logLine.replace("<", "&lt");
-					logLine = logLine.replace(">", "&gt");
-				}
-				sb.append(logLine+"<br>");
-			}
-		}
-		catch(FileNotFoundException e) {
-			sb.append("File not found");
-		}
-		catch(Throwable t) {
-			sb.append("Error accessing file");
-		}
-		finally {
+			String user = userId;
+			String file_name = fname;
+			String ducc_ling = null;
+			AlienTextFile atf = new AlienTextFile(user, file_name, ducc_ling);
+			int pageCount = atf.getPageCount();
+			int pageNo = 0;
 			try {
-				br.close();
+				pageNo = Integer.parseInt(page);
 			}
-			catch(Throwable t) {
+			catch(Exception e) {
 			}
-			try {
-				isr.close();
+			if(pageNo == 0) {
+				pageNo = pageCount;
 			}
-			catch(Throwable t) {
+			pageNo = pageNo - 1;
+			if(pageNo < 0) {
+				pageNo = 0;
+			}
+			String prepend = "";
+			String chunk = atf.getPage(pageNo);
+			String postpend = "";
+			if(pageNo > 0) {
+				String previous = atf.getPage(pageNo-1);
+				if(previous.contains(newline)) {
+					String[] lines = previous.split(newline);
+					int index = lines.length - 1;
+					prepend = lines[index];
+				}
+				else if(previous.contains(colon)) {
+					String[] lines = previous.split(colon);
+					int index = lines.length - 1;
+					prepend = lines[index];
+				}
+			}
+			if(pageNo < (pageCount - 1)) {
+				String next = atf.getPage(pageNo+1);
+				if(next.contains(newline)) {
+					String[] lines = next.split(newline);
+					int index = 0;
+					postpend = lines[index];
+				}
+				if(next.contains(colon)) {
+					String[] lines = next.split(colon);
+					int index = 0;
+					postpend = lines[index];
+				}
 			}
+			String aggregate = prepend + chunk + postpend;
+			
+			if(fname.endsWith(".xml")) {
+				aggregate = aggregate.replace("<", "&lt");
+				aggregate = aggregate.replace(">", "&gt");
+			}
+			
+			//if(!aggregate.trim().contains("\n")) {
+			//	if(aggregate.trim().contains(":")) {
+			//		String[] lines = aggregate.trim().split(":");
+			//		aggregate = "";
+			//		for(String line : lines) {
+			//			aggregate += line+"\n";
+			//		}
+			//	}
+			//}
+			
+			sb.append(aggregate);
+		}
+		catch(Exception e) {
+			duccLogger.error(methodName, jobid, e);
+			sb = new StringBuffer();
+			sb.append("Error processing file");
 		}
 		response.getWriter().println(sb);
 		duccLogger.trace(methodName, null, messages.fetch("exit"));