Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package prerna.sablecc2;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import prerna.algorithm.api.ITableDataFrame;
import prerna.date.SemossDate;
import prerna.ds.shared.CachedIterator;
import prerna.ds.shared.RawCachedWrapper;
import prerna.engine.api.IHeadersDataRow;
import prerna.engine.api.IRawSelectWrapper;
import prerna.engine.api.IStorageEngine;
import prerna.om.Insight;
import prerna.om.InsightPanel;
import prerna.om.InsightSheet;
import prerna.om.Pixel;
import prerna.om.ThreadStore;
import prerna.query.querystruct.AbstractQueryStruct.QUERY_STRUCT_TYPE;
import prerna.query.querystruct.SelectQueryStruct;
import prerna.reactor.export.GraphFormatter;
import prerna.reactor.frame.FrameFactory;
import prerna.sablecc2.comm.PixelJobStatus;
import prerna.sablecc2.comm.PixelJobThread;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.PixelOperationType;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.sablecc2.om.task.AbstractTask;
import prerna.sablecc2.om.task.BasicIteratorTask;
import prerna.sablecc2.om.task.ConstantDataTask;
import prerna.sablecc2.om.task.ITask;
import prerna.util.Constants;
import prerna.util.Utility;
import prerna.util.gson.GsonUtility;
import prerna.util.gson.InsightPanelAdapter;
import prerna.util.gson.InsightSheetAdapter;
import prerna.util.gson.NumberAdapter;
import prerna.util.gson.SemossDateAdapter;
import prerna.util.insight.InsightUtility;
public class PixelStreamUtility {
private static final Logger classLogger = LogManager.getLogger(PixelStreamUtility.class);
private static Gson getPanelGson() {
return new GsonBuilder()
.disableHtmlEscaping()
.excludeFieldsWithModifiers(Modifier.STATIC)
.registerTypeAdapter(Double.class, new NumberAdapter())
.registerTypeAdapter(SemossDate.class, new SemossDateAdapter())
.registerTypeAdapter(InsightPanel.class, new InsightPanelAdapter(true))
.registerTypeAdapter(InsightSheet.class, new InsightSheetAdapter())
.create();
}
/**
* Collect pixel data from the runner
* @param runner
* @param jt
* @return
*/
public static StreamingOutput collectPixelData(PixelRunner runner, PixelJobThread jt) {
// get the default gson object
Gson gson = GsonUtility.getDefaultGson();
// now process everything
try {
return new StreamingOutput() {
PrintStream ps = null;
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
if(jt != null) {
jt.setStatus(PixelJobStatus.STREAMING);
}
try {
ps = new PrintStream(outputStream, true, "UTF-8");
// we want to ignore the first index since it will be a job
classLogger.debug("Starting to generate response");
long start = System.currentTimeMillis();
processPixelRunner(ps, gson, runner);
long end = System.currentTimeMillis();
classLogger.debug("Time to generate json response = " + (end-start) + "ms");
} catch(Exception e) {
classLogger.error(Constants.STACKTRACE, e);
} finally {
if(ps != null) {
ps.close();
}
ThreadStore.remove();
}
if(jt != null) {
jt.setStatus(PixelJobStatus.COMPLETE);
}
}};
} catch (Exception e) {
classLogger.error(Constants.STACKTRACE, e);
if(jt != null) {
jt.setStatus(PixelJobStatus.ERROR);
}
}
return null;
}
/**
* Collect pixel data from the runner
* @param runner
* @return
*/
public static File writePixelData(PixelRunner runner, File fileToWrite, Cipher cipher) {
// get the default gson object
Gson gson = GsonUtility.getDefaultGson();
// now process everything
PrintStream ps = null;
try {
if(cipher != null) {
ps = new PrintStream(new BufferedOutputStream(new CipherOutputStream(new FileOutputStream(fileToWrite), cipher)), false, "UTF-8");
} else {
ps = new PrintStream(new FileOutputStream(fileToWrite), false, "UTF-8");
}
processPixelRunner(ps, gson, runner);
} catch (Exception e) {
classLogger.error("Failed to write object to stream");
} finally {
if (ps != null) {
ps.flush();
ps.close();
}
}
return fileToWrite;
}
/**
*
* @param ps
* @param gson
* @param runner
*/
private static void processPixelRunner(PrintStream ps, Gson gson, PixelRunner runner) {
// get the values we need from the runner
Insight in = runner.getInsight();
List resultList = runner.getResults();
// get the expression which created the return
// this matches with the above by index
List pixelList = runner.getReturnPixelList();
// start of the map
// and the insight id
ps.print("{\"insightID\":\"" + in.getInsightId() + "\",");
ps.flush();
// now flush array of pixel returns
ps.print("\"pixelReturn\":[");
int size = pixelList.size();
// this can be empty when we open an empty insight
// from an insight
if(size > 0) {
// THIS IS BECAUSE WE APPEND THE JOB PIXEL
// BUT FE DOESN'T RESPOND TO IT AND NEED TO REMOVE IT
// HOWEVER, IF THE SIZE IS JUST 1, IT MEANS THAT THERE WAS
// AN ERROR THAT occurred
// but when we run a saved insight within a pixel
// we do not want to shift the index
int startIndex = 0;
boolean firstIsJob = resultList.get(0).getOpType().contains(PixelOperationType.JOB_ID);
if(firstIsJob) {
startIndex = 1;
}
if(size == 1 && !firstIsJob) {
startIndex = 0;
}
for (int i = startIndex; i < size; i++) {
NounMetadata noun = resultList.get(i);
Pixel pixelObj = pixelList.get(i);
processNounMetadata(in, ps, gson, noun, pixelObj);
// add a comma for the next item in the list
if( (i+1) != size) {
ps.print(",");
ps.flush();
}
}
List delayedMessages = in.getDelayedMessages();
for(int i = 0; i < delayedMessages.size(); i++) {
ps.print(",");
ps.flush();
// we want to display these messages
// so meta is always false
Pixel pixelObj = new Pixel("thread_execution_" + Utility.getRandomString(6), "\"delayed message\";");
pixelObj.setMeta(false);
processNounMetadata(in, ps, gson, delayedMessages.get(i), pixelObj);
}
}
// now close of the array and the map
ps.print("]}");
ps.flush();
// help java do garbage cleaning
resultList.clear();
pixelList.clear();
resultList = null;
pixelList = null;
runner = null;
}
/**
* Process the noun metadata for consumption on the FE
* @param in Insight
* @param ps PrintStream to write output json
* @param gson Gson utility instance
* @param noun Nounmetadata to process
* @param pixelObj Pixel object for the step
*/
private static void processNounMetadata(Insight in, PrintStream ps, Gson gson, NounMetadata noun, Pixel pixelObj) {
PixelDataType nounT = noun.getNounType();
// returning a cached insight
if(nounT == PixelDataType.CACHED_PIXEL_RUNNER) {
List