74 changed files with 2004 additions and 335 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,144 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.service.apiusage; |
|||
|
|||
import org.junit.Before; |
|||
import org.junit.Test; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.test.context.TestPropertySource; |
|||
import org.thingsboard.server.common.data.ApiUsageStateValue; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.SaveDeviceWithCredentialsRequest; |
|||
import org.thingsboard.server.common.data.Tenant; |
|||
import org.thingsboard.server.common.data.TenantProfile; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.security.Authority; |
|||
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|||
import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
|||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; |
|||
import org.thingsboard.server.common.data.tenant.profile.TenantProfileData; |
|||
import org.thingsboard.server.controller.AbstractControllerTest; |
|||
import org.thingsboard.server.controller.TbUrlConstants; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; |
|||
|
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
import static org.awaitility.Awaitility.await; |
|||
import static org.junit.Assert.assertEquals; |
|||
import static org.junit.Assert.assertNotNull; |
|||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|||
|
|||
@DaoSqlTest |
|||
@TestPropertySource(properties = { |
|||
"usage.stats.report.enabled=true", |
|||
"usage.stats.report.interval=2", |
|||
"usage.stats.gauge_report_interval=1", |
|||
}) |
|||
public class ApiUsageTest extends AbstractControllerTest { |
|||
|
|||
private Tenant savedTenant; |
|||
private User tenantAdmin; |
|||
|
|||
private static final int MAX_DP_ENABLE_VALUE = 12; |
|||
private static final double WARN_THRESHOLD_VALUE = 0.5; |
|||
@Autowired |
|||
private ApiUsageStateService apiUsageStateService; |
|||
|
|||
@Before |
|||
public void beforeTest() throws Exception { |
|||
loginSysAdmin(); |
|||
|
|||
TenantProfile tenantProfile = createTenantProfile(); |
|||
TenantProfile savedTenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class); |
|||
assertNotNull(savedTenantProfile); |
|||
|
|||
Tenant tenant = new Tenant(); |
|||
tenant.setTitle("My tenant"); |
|||
tenant.setTenantProfileId(savedTenantProfile.getId()); |
|||
savedTenant = saveTenant(tenant); |
|||
tenantId = savedTenant.getId(); |
|||
assertNotNull(savedTenant); |
|||
|
|||
tenantAdmin = new User(); |
|||
tenantAdmin.setAuthority(Authority.TENANT_ADMIN); |
|||
tenantAdmin.setTenantId(savedTenant.getId()); |
|||
tenantAdmin.setEmail("tenant2@thingsboard.org"); |
|||
|
|||
tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1"); |
|||
} |
|||
|
|||
@Test |
|||
public void testTelemetryApiCall() throws Exception { |
|||
Device device = createDevice(); |
|||
assertNotNull(device); |
|||
String telemetryPayload = "{\"temperature\":25, \"humidity\":60}"; |
|||
String url = TbUrlConstants.TELEMETRY_URL_PREFIX + "/DEVICE/" + device.getId() + "/timeseries/ANY"; |
|||
|
|||
long VALUE_WARNING = (long) (MAX_DP_ENABLE_VALUE * WARN_THRESHOLD_VALUE) / 2; |
|||
|
|||
for (int i = 0; i < VALUE_WARNING; i++) { |
|||
doPostAsync(url, telemetryPayload, String.class, status().isOk()); |
|||
} |
|||
|
|||
await().atMost(TIMEOUT, TimeUnit.SECONDS).untilAsserted(() -> assertEquals(ApiUsageStateValue.WARNING, apiUsageStateService.findTenantApiUsageState(tenantId).getDbStorageState())); |
|||
|
|||
long VALUE_DISABLE = (long) (MAX_DP_ENABLE_VALUE - (MAX_DP_ENABLE_VALUE * WARN_THRESHOLD_VALUE)) / 2; |
|||
|
|||
for (int i = 0; i < VALUE_DISABLE; i++) { |
|||
doPostAsync(url, telemetryPayload, String.class, status().isOk()); |
|||
} |
|||
|
|||
await().atMost(TIMEOUT, TimeUnit.SECONDS) |
|||
.untilAsserted(() -> { |
|||
assertEquals(ApiUsageStateValue.DISABLED, apiUsageStateService.findTenantApiUsageState(tenantId).getDbStorageState()); |
|||
}); |
|||
} |
|||
|
|||
private TenantProfile createTenantProfile() { |
|||
TenantProfile tenantProfile = new TenantProfile(); |
|||
tenantProfile.setName("Tenant Profile"); |
|||
tenantProfile.setDescription("Tenant Profile" + " Test"); |
|||
|
|||
TenantProfileData tenantProfileData = new TenantProfileData(); |
|||
DefaultTenantProfileConfiguration config = DefaultTenantProfileConfiguration.builder() |
|||
.maxDPStorageDays(MAX_DP_ENABLE_VALUE) |
|||
.warnThreshold(WARN_THRESHOLD_VALUE) |
|||
.build(); |
|||
|
|||
tenantProfileData.setConfiguration(config); |
|||
tenantProfile.setProfileData(tenantProfileData); |
|||
return tenantProfile; |
|||
} |
|||
|
|||
private Device createDevice() throws Exception { |
|||
String testToken = "TEST_TOKEN"; |
|||
|
|||
Device device = new Device(); |
|||
device.setName("My device"); |
|||
device.setType("default"); |
|||
device.setTenantId(tenantId); |
|||
|
|||
DeviceCredentials deviceCredentials = new DeviceCredentials(); |
|||
deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN); |
|||
deviceCredentials.setCredentialsId(testToken); |
|||
|
|||
SaveDeviceWithCredentialsRequest saveRequest = new SaveDeviceWithCredentialsRequest(device, deviceCredentials); |
|||
|
|||
return readResponse(doPost("/api/device-with-credentials", saveRequest).andExpect(status().isOk()), Device.class); |
|||
} |
|||
|
|||
} |
|||
File diff suppressed because one or more lines are too long
@ -0,0 +1,32 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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.common.data; |
|||
|
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.id.HasId; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
@Data |
|||
@Builder |
|||
public class TbResourceDeleteResult { |
|||
|
|||
private boolean success; |
|||
private Map<String, List<? extends HasId<?>>> references; |
|||
|
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
/** |
|||
* Copyright © 2016-2025 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; |
|||
|
|||
import org.thingsboard.server.common.data.id.HasId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
|
|||
import java.util.List; |
|||
|
|||
public interface ResourceContainerDao<T extends HasId<?>> { |
|||
|
|||
List<T> findByTenantIdAndResourceLink(TenantId tenantId, String link, int limit); |
|||
|
|||
List<T> findByResourceLink(String link, int limit); |
|||
|
|||
} |
|||
@ -0,0 +1,130 @@ |
|||
///
|
|||
/// Copyright © 2016-2025 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.
|
|||
///
|
|||
|
|||
import { CollectionViewer, DataSource, SelectionModel } from '@angular/cdk/collections'; |
|||
import { ResourceInfo, ResourceSubType, ResourceType } from '@shared/models/resource.models'; |
|||
import { BehaviorSubject, Observable, of, ReplaySubject, Subject } from 'rxjs'; |
|||
import { emptyPageData, PageData } from '@shared/models/page/page-data'; |
|||
import { EntityBooleanFunction } from '@home/models/entity/entities-table-config.models'; |
|||
import { PageLink } from '@shared/models/page/page-link'; |
|||
import { catchError, map, take, tap } from 'rxjs/operators'; |
|||
import { ResourceService } from "@core/http/resource.service"; |
|||
|
|||
export class ResourcesDatasource implements DataSource<ResourceInfo> { |
|||
private entitiesSubject: Subject<ResourceInfo[]>; |
|||
private readonly pageDataSubject: Subject<PageData<ResourceInfo>>; |
|||
|
|||
public pageData$: Observable<PageData<ResourceInfo>>; |
|||
|
|||
public selection = new SelectionModel<ResourceInfo>(true, []); |
|||
|
|||
public dataLoading = true; |
|||
|
|||
constructor(private resourceService: ResourceService, |
|||
private resources: ResourceInfo[], |
|||
private selectionEnabledFunction: EntityBooleanFunction<ResourceInfo>) { |
|||
if (this.resources && this.resources.length) { |
|||
this.entitiesSubject = new BehaviorSubject<ResourceInfo[]>(this.resources); |
|||
} else { |
|||
this.entitiesSubject = new BehaviorSubject<ResourceInfo[]>([]); |
|||
this.pageDataSubject = new BehaviorSubject<PageData<ResourceInfo>>(emptyPageData<ResourceInfo>()); |
|||
this.pageData$ = this.pageDataSubject.asObservable(); |
|||
} |
|||
} |
|||
|
|||
connect(collectionViewer: CollectionViewer): |
|||
Observable<ResourceInfo[] | ReadonlyArray<ResourceInfo>> { |
|||
return this.entitiesSubject.asObservable(); |
|||
} |
|||
|
|||
disconnect(collectionViewer: CollectionViewer): void { |
|||
this.entitiesSubject.complete(); |
|||
if (this.pageDataSubject) { |
|||
this.pageDataSubject.complete(); |
|||
} |
|||
} |
|||
|
|||
reset() { |
|||
this.entitiesSubject.next([]); |
|||
if (this.pageDataSubject) { |
|||
this.pageDataSubject.next(emptyPageData<ResourceInfo>()); |
|||
} |
|||
} |
|||
|
|||
loadEntities(pageLink: PageLink, resourceType: ResourceType, subType: ResourceSubType): Observable<PageData<ResourceInfo>> { |
|||
this.dataLoading = true; |
|||
const result = new ReplaySubject<PageData<ResourceInfo>>(); |
|||
this.fetchEntities(pageLink, resourceType, subType).pipe( |
|||
tap(() => { |
|||
this.selection.clear(); |
|||
}), |
|||
catchError(() => of(emptyPageData<ResourceInfo>())), |
|||
).subscribe( |
|||
(pageData) => { |
|||
this.entitiesSubject.next(pageData.data); |
|||
this.pageDataSubject.next(pageData); |
|||
result.next(pageData); |
|||
this.dataLoading = false; |
|||
} |
|||
); |
|||
return result; |
|||
} |
|||
|
|||
fetchEntities(pageLink: PageLink, resourceType: ResourceType, subType: ResourceSubType): Observable<PageData<ResourceInfo>> { |
|||
return this.resourceService.getResources(pageLink, resourceType, subType); |
|||
} |
|||
|
|||
isAllSelected(): Observable<boolean> { |
|||
const numSelected = this.selection.selected.length; |
|||
return this.entitiesSubject.pipe( |
|||
map((entities) => numSelected === entities.length) |
|||
); |
|||
} |
|||
|
|||
isEmpty(): Observable<boolean> { |
|||
return this.entitiesSubject.pipe( |
|||
map((entities) => !entities.length) |
|||
); |
|||
} |
|||
|
|||
total(): Observable<number> { |
|||
return this.pageDataSubject.pipe( |
|||
map((pageData) => pageData.totalElements) |
|||
); |
|||
} |
|||
|
|||
masterToggle() { |
|||
this.entitiesSubject.pipe( |
|||
tap((entities) => { |
|||
const numSelected = this.selection.selected.length; |
|||
if (numSelected === this.selectableEntitiesCount(entities)) { |
|||
this.selection.clear(); |
|||
} else { |
|||
entities.forEach(row => { |
|||
if (this.selectionEnabledFunction(row)) { |
|||
this.selection.select(row); |
|||
} |
|||
}); |
|||
} |
|||
}), |
|||
take(1) |
|||
).subscribe(); |
|||
} |
|||
|
|||
private selectableEntitiesCount(entities: Array<ResourceInfo>): number { |
|||
return entities.filter((entity) => this.selectionEnabledFunction(entity)).length; |
|||
} |
|||
} |
|||
@ -1 +0,0 @@ |
|||
<!-- [TODO]: [Calculated Fields] add content --> |
|||
Loading…
Reference in new issue