Merge pull request #3169 from vzikratyi-tb/bug/string-to-number-auto-convertion
String to number auto conversion
This commit is contained in:
commit
bf780147b0
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.thingsboard.server.dao.sql.query;
|
package org.thingsboard.server.dao.sql.query;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.math.NumberUtils;
|
||||||
import org.thingsboard.server.common.data.EntityType;
|
import org.thingsboard.server.common.data.EntityType;
|
||||||
import org.thingsboard.server.common.data.UUIDConverter;
|
import org.thingsboard.server.common.data.UUIDConverter;
|
||||||
import org.thingsboard.server.common.data.id.EntityId;
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
@ -82,7 +83,7 @@ public class EntityDataAdapter {
|
|||||||
if (value != null) {
|
if (value != null) {
|
||||||
String strVal = value.toString();
|
String strVal = value.toString();
|
||||||
// check number
|
// check number
|
||||||
if (strVal.length() > 0) {
|
if (strVal.length() > 0 && NumberUtils.isParsable(strVal)) {
|
||||||
try {
|
try {
|
||||||
long longVal = Long.parseLong(strVal);
|
long longVal = Long.parseLong(strVal);
|
||||||
return Long.toString(longVal);
|
return Long.toString(longVal);
|
||||||
|
|||||||
@ -25,6 +25,7 @@ import org.junit.Assert;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.thingsboard.server.common.data.DataConstants;
|
import org.thingsboard.server.common.data.DataConstants;
|
||||||
import org.thingsboard.server.common.data.Device;
|
import org.thingsboard.server.common.data.Device;
|
||||||
import org.thingsboard.server.common.data.EntityType;
|
import org.thingsboard.server.common.data.EntityType;
|
||||||
@ -45,6 +46,8 @@ import org.thingsboard.server.dao.attributes.AttributesService;
|
|||||||
import org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity;
|
import org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity;
|
||||||
import org.thingsboard.server.dao.rule.RuleChainService;
|
import org.thingsboard.server.dao.rule.RuleChainService;
|
||||||
import org.thingsboard.server.dao.timeseries.TimeseriesService;
|
import org.thingsboard.server.dao.timeseries.TimeseriesService;
|
||||||
|
import org.thingsboard.server.dao.util.DaoTestUtil;
|
||||||
|
import org.thingsboard.server.dao.util.SqlDbType;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
@ -60,6 +63,9 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
|
|||||||
|
|
||||||
private TenantId tenantId;
|
private TenantId tenantId;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JdbcTemplate template;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() {
|
||||||
Tenant tenant = new Tenant();
|
Tenant tenant = new Tenant();
|
||||||
@ -821,6 +827,10 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
|
|||||||
.getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue());
|
.getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue());
|
||||||
}
|
}
|
||||||
List<String> deviceTemperatures = temperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList());
|
List<String> deviceTemperatures = temperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList());
|
||||||
|
if (DaoTestUtil.getSqlDbType(template) == SqlDbType.H2) {
|
||||||
|
// in H2 double values are stored with E0 in the end of the string
|
||||||
|
loadedTemperatures = loadedTemperatures.stream().map(s -> s.substring(0, s.length() - 2)).collect(Collectors.toList());
|
||||||
|
}
|
||||||
Assert.assertEquals(deviceTemperatures, loadedTemperatures);
|
Assert.assertEquals(deviceTemperatures, loadedTemperatures);
|
||||||
|
|
||||||
pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
|
pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
|
||||||
@ -848,6 +858,10 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
|
|||||||
entityData.getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue()).collect(Collectors.toList());
|
entityData.getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue()).collect(Collectors.toList());
|
||||||
List<String> deviceHighTemperatures = highTemperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList());
|
List<String> deviceHighTemperatures = highTemperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList());
|
||||||
|
|
||||||
|
if (DaoTestUtil.getSqlDbType(template) == SqlDbType.H2) {
|
||||||
|
// in H2 double values are stored with E0 in the end of the string
|
||||||
|
loadedHighTemperatures = loadedHighTemperatures.stream().map(s -> s.substring(0, s.length() - 2)).collect(Collectors.toList());
|
||||||
|
}
|
||||||
Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures);
|
Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures);
|
||||||
|
|
||||||
deviceService.deleteDevicesByTenantId(tenantId);
|
deviceService.deleteDevicesByTenantId(tenantId);
|
||||||
|
|||||||
@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* Copyright © 2016-2020 The Thingsboard Authors
|
||||||
|
*
|
||||||
|
* Licensed 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.thingsboard.server.dao.util;
|
||||||
|
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
|
||||||
|
public class DaoTestUtil {
|
||||||
|
private static final String POSTGRES_DRIVER_CLASS = "org.postgresql.Driver";
|
||||||
|
private static final String H2_DRIVER_CLASS = "org.hsqldb.jdbc.JDBCDriver";
|
||||||
|
|
||||||
|
|
||||||
|
public static SqlDbType getSqlDbType(JdbcTemplate template){
|
||||||
|
try {
|
||||||
|
String driverName = DriverManager.getDriver(template.getDataSource().getConnection().getMetaData().getURL()).getClass().getName();
|
||||||
|
if (POSTGRES_DRIVER_CLASS.equals(driverName)) {
|
||||||
|
return SqlDbType.POSTGRES;
|
||||||
|
} else if (H2_DRIVER_CLASS.equals(driverName)) {
|
||||||
|
return SqlDbType.H2;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Copyright © 2016-2020 The Thingsboard Authors
|
||||||
|
*
|
||||||
|
* Licensed 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.thingsboard.server.dao.util;
|
||||||
|
|
||||||
|
public enum SqlDbType {
|
||||||
|
POSTGRES, H2;
|
||||||
|
}
|
||||||
@ -624,7 +624,7 @@ export class EntityDataSubscription {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private convertValue(val: string): any {
|
private convertValue(val: string): any {
|
||||||
if (val && this.isNumeric(val)) {
|
if (val && this.isNumeric(val) && Number(val).toString() === val) {
|
||||||
return Number(val);
|
return Number(val);
|
||||||
} else {
|
} else {
|
||||||
return val;
|
return val;
|
||||||
|
|||||||
@ -115,8 +115,7 @@ export function isEmpty(obj: any): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined {
|
export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined {
|
||||||
if (isDefined(value) &&
|
if (isDefinedAndNotNull(value) && isNumeric(value) && (isDefined(dec) || isDefined(units) || Number(value).toString() === value)) {
|
||||||
value !== null && isNumeric(value)) {
|
|
||||||
let formatted: string | number = Number(value);
|
let formatted: string | number = Number(value);
|
||||||
if (isDefined(dec)) {
|
if (isDefined(dec)) {
|
||||||
formatted = formatted.toFixed(dec);
|
formatted = formatted.toFixed(dec);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user