All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prerna.reactor.frame.py.DropRowsReactor Maven / Gradle / Ivy

The newest version!
package prerna.reactor.frame.py;

import prerna.ds.py.PandasFrame;
import prerna.query.interpreters.PandasInterpreter;
import prerna.query.querystruct.SelectQueryStruct;
import prerna.query.querystruct.filters.GenRowFilters;
import prerna.sablecc2.om.GenRowStruct;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.PixelOperationType;
import prerna.sablecc2.om.ReactorKeysEnum;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.util.usertracking.AnalyticsTrackerHelper;
import prerna.util.usertracking.UserTrackerFactory;

public class DropRowsReactor extends AbstractPyFrameReactor {

	/**
	 * This reactor drops rows based on a comparison The inputs to the reactor
	 * are: 1) the filter comparison for dropping rows
	 */

	public DropRowsReactor() {
		this.keysToGet = new String[] { ReactorKeysEnum.QUERY_STRUCT.getKey() };
	}

	@Override
	public NounMetadata execute() {
		// get frame
		PandasFrame frame = (PandasFrame) getFrame();
		String frameName = frame.getName();
		String wrapperFrameName = frame.getWrapperName();

		// the first noun will be a query struct - the filter
		SelectQueryStruct qs = getQueryStruct();
		// get the filters from the query struct
		// and iterate through each filtered column
		GenRowFilters grf = qs.getExplicitFilters();

		// use RInterpreter to create filter syntax
		StringBuilder pyFilterBuilder = new StringBuilder();
		PandasInterpreter pi = new PandasInterpreter();
		pi.setDataTableName(frameName, wrapperFrameName + ".cache['data']");
		pi.setDataTypeMap(frame.getMetaData().getHeaderToTypeMap());
		pi.addFilters(grf.getFilters(), wrapperFrameName, pyFilterBuilder, true);

		// execute the r script
		// FRAME <- FRAME[!( FRAME$Director == "value"),]
		String script = wrapperFrameName + ".cache['data'] =  " + wrapperFrameName + ".cache['data'][~" + pyFilterBuilder.toString() + "]";
		frame.runScript(script);
		this.addExecutedCode(script);

		// NEW TRACKING
		UserTrackerFactory.getInstance().trackAnalyticsWidget(this.insight, frame, "DropRows",
				AnalyticsTrackerHelper.getHashInputs(this.store, this.keysToGet));

		return new NounMetadata(frame, PixelDataType.FRAME, PixelOperationType.FRAME_DATA_CHANGE);
	}

	private SelectQueryStruct getQueryStruct() {
		GenRowStruct inputsGRS = this.store.getNoun(this.keysToGet[0]);
		if (inputsGRS != null) {
			NounMetadata filterNoun = inputsGRS.getNoun(0);
			// filter is query struct pksl type
			// the qs is the value of the filterNoun
			SelectQueryStruct qs = (SelectQueryStruct) filterNoun.getValue();
			if (qs == null) {
				throw new IllegalArgumentException("Need to define filter condition");
			}
			return qs;
		}

		inputsGRS = this.getCurRow();
		NounMetadata filterNoun = inputsGRS.getNoun(0);
		// filter is query struct pksl type
		// the qs is the value of the filterNoun
		SelectQueryStruct qs = (SelectQueryStruct) filterNoun.getValue();
		if (qs == null) {
			throw new IllegalArgumentException("Need to define filter condition");
		}
		return qs;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy