Skip to content

Commit

Permalink
fixing time function
Browse files Browse the repository at this point in the history
  • Loading branch information
suhothayan committed Sep 22, 2015
1 parent 8c86f54 commit db8049d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
* timestampInMilliseconds - date value in milliseconds.(from the epoch) eg: 1415712224000L
* dateTargetFormat - Date format which need to be converted to. eg: yyyy/MM/dd HH:mm:ss
* Accept Type(s) for dateFormat(dateValue,dateTargetFormat,dateSourceFormat):
* dateValue : STRING
* dateTargetFormat : STRING
* dateSourceFormat : STRING
* dateValue : STRING
* dateTargetFormat : STRING
* dateSourceFormat : STRING
* Accept Type(s) for dateFormat(timestampInMilliseconds,dateTargetFormat):
* timestampInMilliseconds : LONG
* dateTargetFormat : STRING
* timestampInMilliseconds : LONG
* dateTargetFormat : STRING
* Return Type(s): STRING
*/
public class DateFormatFunctionExtension extends FunctionExecutor {
Expand All @@ -56,10 +56,10 @@ public class DateFormatFunctionExtension extends FunctionExecutor {

@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors,
ExecutionPlanContext executionPlanContext) {
ExecutionPlanContext executionPlanContext) {

if(attributeExpressionExecutors[0].getReturnType() != Attribute.Type.LONG && attributeExpressionExecutors
.length == 2){
if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.LONG && attributeExpressionExecutors
.length == 2) {
useDefaultDateFormat = true;
sourceDateFormat = TimeExtensionConstants.EXTENSION_TIME_DEFAULT_DATE_FORMAT;
}
Expand All @@ -84,7 +84,7 @@ protected void init(ExpressionExecutor[] attributeExpressionExecutors,
" but found " + attributeExpressionExecutors[2].getReturnType().toString());
}
} else if (attributeExpressionExecutors.length == 2) {
if(useDefaultDateFormat){
if (useDefaultDateFormat) {
if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException("Invalid parameter type found for the first argument of " +
"time:dateFormat(dateValue,dateTargetFormat,dateSourceFormat) function, " + "required "
Expand All @@ -97,7 +97,7 @@ protected void init(ExpressionExecutor[] attributeExpressionExecutors,
+ Attribute.Type.STRING +
" but found " + attributeExpressionExecutors[1].getReturnType().toString());
}
} else{
} else {
if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.LONG) {
throw new ExecutionPlanValidationException("Invalid parameter type found for the first argument of " +
"time:dateFormat(timestampInMilliseconds,dateTargetFormat) function, " +
Expand Down Expand Up @@ -139,7 +139,7 @@ protected Object execute(Object[] data) {
throw new ExecutionPlanRuntimeException("Invalid input given to time:dateFormat(dateValue," +
"dateTargetFormat,dateSourceFormat) function" + ". Second " + "argument cannot be null");
}
if(!useDefaultDateFormat){
if (!useDefaultDateFormat) {
if (data[2] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to time:dateFormat(dateValue," +
"dateTargetFormat,dateSourceFormat) function" + ". Third " + "argument cannot be null");
Expand All @@ -156,13 +156,13 @@ protected Object execute(Object[] data) {
return targetFormat.format(userSpecifiedSourceDate);
} catch (ParseException e) {
String errorMsg = "Provided format " + sourceDateFormat + " does not match with the timestamp " +
sourceDate +" "+ e.getMessage();
throw new ExecutionPlanRuntimeException(errorMsg,e);
} catch (ClassCastException e){
String errorMsg ="Provided Data type cannot be cast to desired format. " + e.getMessage();
throw new ExecutionPlanRuntimeException(errorMsg,e);
sourceDate + " " + e.getMessage();
throw new ExecutionPlanRuntimeException(errorMsg, e);
} catch (ClassCastException e) {
String errorMsg = "Provided Data type cannot be cast to desired format. " + e.getMessage();
throw new ExecutionPlanRuntimeException(errorMsg, e);
}
} else if(data.length == 2){
} else if (data.length == 2) {

if (data[0] == null) {
throw new ExecutionPlanRuntimeException("Invalid input given to dateFormat(timestampInMilliseconds," +
Expand Down Expand Up @@ -190,15 +190,10 @@ protected Object execute(Object[] data) {
calInstance.setTimeInMillis(dateInMills);
userSpecifiedSourceDate = calInstance.getTime();
formattedNewDateValue = targetFormat.format(userSpecifiedSourceDate);
long newFormattedDateInUnix = targetFormat.parse(formattedNewDateValue).getTime();
return String.valueOf(newFormattedDateInUnix);
} catch (ParseException e) {
String errorMsg = "Provided format " + targetDataFormat + " does not match with the timestamp " +
formattedNewDateValue +" "+ e.getMessage();
throw new ExecutionPlanRuntimeException(errorMsg,e);
} catch (ClassCastException e){
String errorMsg ="Provided Data type cannot be cast to desired format. " + e.getMessage();
throw new ExecutionPlanRuntimeException(errorMsg,e);
return formattedNewDateValue;
} catch (ClassCastException e) {
String errorMsg = "Provided Data type cannot be cast to desired format. " + e.getMessage();
throw new ExecutionPlanRuntimeException(errorMsg, e);
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ protected Object execute(Object[] data) {
returnValue = cal.get(Calendar.MINUTE);

} else if (unit.equals(TimeExtensionConstants.EXTENSION_TIME_UNIT_HOUR)) {
returnValue = cal.get(Calendar.HOUR);
returnValue = cal.get(Calendar.HOUR_OF_DAY);

} else if (unit.equals(TimeExtensionConstants.EXTENSION_TIME_UNIT_DAY)) {
returnValue = cal.get(Calendar.DAY_OF_MONTH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void extractAttributesFunctionExtension() throws InterruptedException {
"define stream inputStream (symbol string,dateValue string,dateFormat string,timestampInMilliseconds long);";
String query = ("@info(name = 'query1') " +
"from inputStream " +
"select symbol , time:extract('YEAR',dateValue,dateFormat) as YEAR,time:extract('HOUR',dateValue," +
"dateFormat) as MONTH,time:extract(timestampInMilliseconds,'HOUR') as QUARTER "+
"select symbol , time:extract('YEAR',dateValue,dateFormat) as YEAR,time:extract('MONTH',dateValue," +
"dateFormat) as MONTH,time:extract(timestampInMilliseconds,'HOUR') as HOUR "+
"insert into outputStream;");
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(inStreamDefinition + query);

Expand All @@ -62,7 +62,7 @@ public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
for(int cnt=0;cnt<inEvents.length;cnt++){
count++;
log.info("Event : " + count + ",YEAR : " + inEvents[cnt].getData(1) +"," +
"MONTH : "+inEvents[cnt].getData(2) + ",QUARTER : "+inEvents[cnt].getData(3));
"MONTH : "+inEvents[cnt].getData(2) + ",HOUR : "+inEvents[cnt].getData(3));

}
}
Expand All @@ -72,7 +72,7 @@ public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
executionPlanRuntime.start();
inputHandler.send(new Object[]{"IBM", "2014-3-11 02:23:44", "yyyy-MM-dd hh:mm:ss",1394484824000L});
inputHandler.send(new Object[]{"IBM", "2014-3-11 02:23:44", "yyyy-MM-dd hh:mm:ss",1394484824000L});
inputHandler.send(new Object[]{"IBM", "2014-3-11 02:23:44", "yyyy-MM-dd hh:mm:ss",1394484824000L});
inputHandler.send(new Object[]{"IBM", "2014-3-11 22:23:44", "yyyy-MM-dd hh:mm:ss",1394556804000L});
Thread.sleep(100);
Assert.assertEquals(3, count);
Assert.assertTrue(eventArrived);
Expand Down

0 comments on commit db8049d

Please sign in to comment.