Commit bf780147b0b454c5819ce45d36c833ecd7ec7912
Committed by
GitHub
Merge pull request #3169 from vzikratyi-tb/bug/string-to-number-auto-convertion
String to number auto conversion
Showing
6 changed files
with
78 additions
and
4 deletions
@@ -15,6 +15,7 @@ | @@ -15,6 +15,7 @@ | ||
15 | */ | 15 | */ |
16 | package org.thingsboard.server.dao.sql.query; | 16 | package org.thingsboard.server.dao.sql.query; |
17 | 17 | ||
18 | +import org.apache.commons.lang3.math.NumberUtils; | ||
18 | import org.thingsboard.server.common.data.EntityType; | 19 | import org.thingsboard.server.common.data.EntityType; |
19 | import org.thingsboard.server.common.data.UUIDConverter; | 20 | import org.thingsboard.server.common.data.UUIDConverter; |
20 | import org.thingsboard.server.common.data.id.EntityId; | 21 | import org.thingsboard.server.common.data.id.EntityId; |
@@ -82,7 +83,7 @@ public class EntityDataAdapter { | @@ -82,7 +83,7 @@ public class EntityDataAdapter { | ||
82 | if (value != null) { | 83 | if (value != null) { |
83 | String strVal = value.toString(); | 84 | String strVal = value.toString(); |
84 | // check number | 85 | // check number |
85 | - if (strVal.length() > 0) { | 86 | + if (strVal.length() > 0 && NumberUtils.isParsable(strVal)) { |
86 | try { | 87 | try { |
87 | long longVal = Long.parseLong(strVal); | 88 | long longVal = Long.parseLong(strVal); |
88 | return Long.toString(longVal); | 89 | return Long.toString(longVal); |
@@ -25,6 +25,7 @@ import org.junit.Assert; | @@ -25,6 +25,7 @@ import org.junit.Assert; | ||
25 | import org.junit.Before; | 25 | import org.junit.Before; |
26 | import org.junit.Test; | 26 | import org.junit.Test; |
27 | import org.springframework.beans.factory.annotation.Autowired; | 27 | import org.springframework.beans.factory.annotation.Autowired; |
28 | +import org.springframework.jdbc.core.JdbcTemplate; | ||
28 | import org.thingsboard.server.common.data.DataConstants; | 29 | import org.thingsboard.server.common.data.DataConstants; |
29 | import org.thingsboard.server.common.data.Device; | 30 | import org.thingsboard.server.common.data.Device; |
30 | import org.thingsboard.server.common.data.EntityType; | 31 | import org.thingsboard.server.common.data.EntityType; |
@@ -45,6 +46,8 @@ import org.thingsboard.server.dao.attributes.AttributesService; | @@ -45,6 +46,8 @@ import org.thingsboard.server.dao.attributes.AttributesService; | ||
45 | import org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity; | 46 | import org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity; |
46 | import org.thingsboard.server.dao.rule.RuleChainService; | 47 | import org.thingsboard.server.dao.rule.RuleChainService; |
47 | import org.thingsboard.server.dao.timeseries.TimeseriesService; | 48 | import org.thingsboard.server.dao.timeseries.TimeseriesService; |
49 | +import org.thingsboard.server.dao.util.DaoTestUtil; | ||
50 | +import org.thingsboard.server.dao.util.SqlDbType; | ||
48 | 51 | ||
49 | import java.util.*; | 52 | import java.util.*; |
50 | import java.util.concurrent.ExecutionException; | 53 | import java.util.concurrent.ExecutionException; |
@@ -60,6 +63,9 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { | @@ -60,6 +63,9 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { | ||
60 | 63 | ||
61 | private TenantId tenantId; | 64 | private TenantId tenantId; |
62 | 65 | ||
66 | + @Autowired | ||
67 | + private JdbcTemplate template; | ||
68 | + | ||
63 | @Before | 69 | @Before |
64 | public void before() { | 70 | public void before() { |
65 | Tenant tenant = new Tenant(); | 71 | Tenant tenant = new Tenant(); |
@@ -821,6 +827,10 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { | @@ -821,6 +827,10 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { | ||
821 | .getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue()); | 827 | .getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue()); |
822 | } | 828 | } |
823 | List<String> deviceTemperatures = temperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList()); | 829 | List<String> deviceTemperatures = temperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList()); |
830 | + if (DaoTestUtil.getSqlDbType(template) == SqlDbType.H2) { | ||
831 | + // in H2 double values are stored with E0 in the end of the string | ||
832 | + loadedTemperatures = loadedTemperatures.stream().map(s -> s.substring(0, s.length() - 2)).collect(Collectors.toList()); | ||
833 | + } | ||
824 | Assert.assertEquals(deviceTemperatures, loadedTemperatures); | 834 | Assert.assertEquals(deviceTemperatures, loadedTemperatures); |
825 | 835 | ||
826 | pageLink = new EntityDataPageLink(10, 0, null, sortOrder); | 836 | pageLink = new EntityDataPageLink(10, 0, null, sortOrder); |
@@ -848,6 +858,10 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { | @@ -848,6 +858,10 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest { | ||
848 | entityData.getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue()).collect(Collectors.toList()); | 858 | entityData.getLatest().get(EntityKeyType.TIME_SERIES).get("temperature").getValue()).collect(Collectors.toList()); |
849 | List<String> deviceHighTemperatures = highTemperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList()); | 859 | List<String> deviceHighTemperatures = highTemperatures.stream().map(aDouble -> Double.toString(aDouble)).collect(Collectors.toList()); |
850 | 860 | ||
861 | + if (DaoTestUtil.getSqlDbType(template) == SqlDbType.H2) { | ||
862 | + // in H2 double values are stored with E0 in the end of the string | ||
863 | + loadedHighTemperatures = loadedHighTemperatures.stream().map(s -> s.substring(0, s.length() - 2)).collect(Collectors.toList()); | ||
864 | + } | ||
851 | Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures); | 865 | Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures); |
852 | 866 | ||
853 | deviceService.deleteDevicesByTenantId(tenantId); | 867 | deviceService.deleteDevicesByTenantId(tenantId); |
1 | +/** | ||
2 | + * Copyright © 2016-2020 The Thingsboard Authors | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.thingsboard.server.dao.util; | ||
17 | + | ||
18 | +import org.springframework.jdbc.core.JdbcTemplate; | ||
19 | + | ||
20 | +import java.sql.DriverManager; | ||
21 | + | ||
22 | +public class DaoTestUtil { | ||
23 | + private static final String POSTGRES_DRIVER_CLASS = "org.postgresql.Driver"; | ||
24 | + private static final String H2_DRIVER_CLASS = "org.hsqldb.jdbc.JDBCDriver"; | ||
25 | + | ||
26 | + | ||
27 | + public static SqlDbType getSqlDbType(JdbcTemplate template){ | ||
28 | + try { | ||
29 | + String driverName = DriverManager.getDriver(template.getDataSource().getConnection().getMetaData().getURL()).getClass().getName(); | ||
30 | + if (POSTGRES_DRIVER_CLASS.equals(driverName)) { | ||
31 | + return SqlDbType.POSTGRES; | ||
32 | + } else if (H2_DRIVER_CLASS.equals(driverName)) { | ||
33 | + return SqlDbType.H2; | ||
34 | + } | ||
35 | + } catch (Exception e) { | ||
36 | + e.printStackTrace(); | ||
37 | + } | ||
38 | + return null; | ||
39 | + } | ||
40 | +} |
1 | +/** | ||
2 | + * Copyright © 2016-2020 The Thingsboard Authors | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.thingsboard.server.dao.util; | ||
17 | + | ||
18 | +public enum SqlDbType { | ||
19 | + POSTGRES, H2; | ||
20 | +} |
@@ -624,7 +624,7 @@ export class EntityDataSubscription { | @@ -624,7 +624,7 @@ export class EntityDataSubscription { | ||
624 | } | 624 | } |
625 | 625 | ||
626 | private convertValue(val: string): any { | 626 | private convertValue(val: string): any { |
627 | - if (val && this.isNumeric(val)) { | 627 | + if (val && this.isNumeric(val) && Number(val).toString() === val) { |
628 | return Number(val); | 628 | return Number(val); |
629 | } else { | 629 | } else { |
630 | return val; | 630 | return val; |
@@ -115,8 +115,7 @@ export function isEmpty(obj: any): boolean { | @@ -115,8 +115,7 @@ export function isEmpty(obj: any): boolean { | ||
115 | } | 115 | } |
116 | 116 | ||
117 | export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined { | 117 | export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined { |
118 | - if (isDefined(value) && | ||
119 | - value !== null && isNumeric(value)) { | 118 | + if (isDefinedAndNotNull(value) && isNumeric(value) && (isDefined(dec) || isDefined(units) || Number(value).toString() === value)) { |
120 | let formatted: string | number = Number(value); | 119 | let formatted: string | number = Number(value); |
121 | if (isDefined(dec)) { | 120 | if (isDefined(dec)) { |
122 | formatted = formatted.toFixed(dec); | 121 | formatted = formatted.toFixed(dec); |