Allow users to inform timezones on APIs that have the date parameter (#7032)

Co-authored-by: dahn <daan.hoogland@gmail.com>
This commit is contained in:
João Jandre 2023-01-09 12:50:55 -03:00 committed by GitHub
parent 9009dd1db8
commit cc527523fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 65 deletions

View File

@ -103,7 +103,7 @@ public abstract class BaseCmd {
GET, POST, PUT, DELETE
}
public static enum CommandType {
BOOLEAN, DATE, FLOAT, DOUBLE, INTEGER, SHORT, LIST, LONG, OBJECT, MAP, STRING, TZDATE, UUID
BOOLEAN, DATE, FLOAT, DOUBLE, INTEGER, SHORT, LIST, LONG, OBJECT, MAP, STRING, UUID
}
private Object _responseObject;

View File

@ -33,7 +33,7 @@ public class ListAsyncJobsCmd extends BaseListAccountResourcesCmd {
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name = ApiConstants.START_DATE, type = CommandType.TZDATE, description = "The start date of the async job (use format \"yyyy-MM-dd'T'HH:mm:ss'+'SSSS\")")
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "The start date of the async job (use format \"yyyy-MM-dd'T'HH:mm:ss'+'SSSS\")")
private Date startDate;
/////////////////////////////////////////////////////

View File

@ -63,8 +63,10 @@ import com.cloud.utils.exception.CloudRuntimeException;
public class ParamProcessWorker implements DispatchWorker {
private static final Logger s_logger = Logger.getLogger(ParamProcessWorker.class.getName());
public final DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
public final DateFormat newInputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final String inputFormatString = "yyyy-MM-dd";
private static final String newInputFormatString = "yyyy-MM-dd HH:mm:ss";
public static final DateFormat inputFormat = new SimpleDateFormat(inputFormatString);
public static final DateFormat newInputFormat = new SimpleDateFormat(newInputFormatString);
@Inject
protected AccountManager _accountMgr;
@ -333,26 +335,7 @@ public class ParamProcessWorker implements DispatchWorker {
field.set(cmdObj, Boolean.valueOf(paramObj.toString()));
break;
case DATE:
// This piece of code is for maintaining backward compatibility
// and support both the date formats(Bug 9724)
final boolean isObjInNewDateFormat = isObjInNewDateFormat(paramObj.toString());
if (isObjInNewDateFormat) {
final DateFormat newFormat = newInputFormat;
synchronized (newFormat) {
field.set(cmdObj, newFormat.parse(paramObj.toString()));
}
} else {
final DateFormat format = inputFormat;
synchronized (format) {
Date date = format.parse(paramObj.toString());
if (field.getName().equals("startDate")) {
date = messageDate(date, 0, 0, 0);
} else if (field.getName().equals("endDate")) {
date = messageDate(date, 23, 59, 59);
}
field.set(cmdObj, date);
}
}
parseAndSetDate(field, cmdObj, paramObj);
break;
case FLOAT:
// Assuming that the parameters have been checked for required before now,
@ -428,9 +411,6 @@ public class ParamProcessWorker implements DispatchWorker {
}
}
break;
case TZDATE:
field.set(cmdObj, DateUtil.parseTZDateString(paramObj.toString()));
break;
case MAP:
default:
field.set(cmdObj, paramObj);
@ -442,6 +422,33 @@ public class ParamProcessWorker implements DispatchWorker {
" is not accessible]");
}
}
private void parseAndSetDate(Field field, BaseCmd cmdObj, Object paramObj) throws IllegalAccessException, ParseException {
try {
field.set(cmdObj, DateUtil.parseTZDateString(paramObj.toString()));
return;
} catch (ParseException parseException) {
s_logger.debug(String.format("Could not parse date [%s] with timezone parser, trying to parse without timezone.", paramObj));
}
if (isObjInNewDateFormat(paramObj.toString())) {
s_logger.debug(String.format("Parsing date [%s] using the [%s] format.", paramObj, newInputFormatString));
final DateFormat newFormat = newInputFormat;
synchronized (newFormat) {
field.set(cmdObj, newFormat.parse(paramObj.toString()));
}
} else {
s_logger.debug(String.format("Parsing date [%s] using the [%s] format.", paramObj, inputFormatString));
final DateFormat format = inputFormat;
synchronized (format) {
Date date = format.parse(paramObj.toString());
if (field.getName().equals("startDate")) {
date = messageDate(date, 0, 0, 0);
} else if (field.getName().equals("endDate")) {
date = messageDate(date, 23, 59, 59);
}
field.set(cmdObj, date);
}
}
}
private boolean isObjInNewDateFormat(final String string) {
final Matcher matcher = BaseCmd.newInputDateFormat.matcher(string);

View File

@ -289,26 +289,12 @@ export default {
mounted () {
this.fetchData()
},
computed: {
usebrowsertimezone: function () {
return this.$store.getters.usebrowsertimezone
}
},
watch: {
resource: function (newItem) {
if (!newItem || !newItem.id) {
return
}
this.fetchData()
},
usebrowsertimezone: function () {
if (this.startDate) {
this.startDate = this.onToggleUseBrowserTimezone(new Date(this.startDate))
}
if (this.endDate) {
this.endDate = this.onToggleUseBrowserTimezone(new Date(this.endDate))
}
this.fetchData()
}
},
methods: {
@ -355,32 +341,11 @@ export default {
},
getStartDate () {
var now = new Date()
if (!this.$store.getters.usebrowsertimezone) {
var dateInUTC = new Date(now.getTime() + now.getTimezoneOffset() * 60000)
return dateInUTC.setHours(dateInUTC.getHours() - 1)
}
now.setHours(now.getHours() - 1)
return now
},
getEndDate () {
var now = new Date()
if (this.$store.getters.usebrowsertimezone) {
return now
}
return new Date(now.getTime() + now.getTimezoneOffset() * 60000)
},
onToggleUseBrowserTimezone (date) {
if (this.$store.getters.usebrowsertimezone) {
return this.$toLocalDate(date)
}
return new Date(date.getTime() + date.getTimezoneOffset() * 60000)
},
convertAndFormatDateAppropriately (date) {
if (this.$store.getters.usebrowsertimezone) {
var dateInUTC = new Date(date).toISOString().split('T')
return dateInUTC[0] + ' ' + dateInUTC[1].split('-')[0].split('.')[0]
}
return moment(date).format('YYYY-MM-DD HH:mm:ss')
return new Date()
},
fetchData () {
this.loaded = false
@ -388,10 +353,10 @@ export default {
this.formatPeriod()
var params = { id: this.resource.id }
if (this.startDate) {
params.startDate = this.convertAndFormatDateAppropriately(this.startDate)
params.startDate = moment(this.startDate).format()
}
if (this.endDate) {
params.endDate = this.convertAndFormatDateAppropriately(this.endDate)
params.endDate = moment(this.endDate).format()
}
api('listVirtualMachinesUsageHistory', params).then(response => {
this.handleStatsResponse(response)