Browse Source

fix(iot-hub): call the grouped versions endpoint

The Hub answers a sectioned search at /api/versions/published/grouped rather than
through a flag on the flat read. That endpoint takes the filters and a sort key
only - it sizes its own answer - so the popup asks through its own API method and
its own query string, and sends no page, page size or sort order.

MpItemVersionGroupedView mirrors what comes back: the view plus typeTotal, always
set, rather than the flat view with a nullable field and a comment about when it
is null. MpItemVersionQueryOptions loses grouped with it.
pull/16148/head
dshvaika 2 months ago
parent
commit
bb43fffa04
  1. 21
      ui-ngx/src/app/core/http/iot-hub-api.service.ts
  2. 22
      ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-home.component.ts
  3. 49
      ui-ngx/src/app/shared/models/iot-hub/iot-hub-version.models.ts

21
ui-ngx/src/app/core/http/iot-hub-api.service.ts

@ -6,7 +6,7 @@ import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { PageData } from '@shared/models/page/page-data';
import { PageLink } from '@shared/models/page/page-link';
import { MpItemVersionQuery, MpItemVersionView } from '@shared/models/iot-hub/iot-hub-version.models';
import { MpItemVersionGroupedView, MpItemVersionQuery, MpItemVersionView } from '@shared/models/iot-hub/iot-hub-version.models';
import { CreatorView } from '@shared/models/iot-hub/iot-hub-creator.models';
import { IotHubInstalledItem, InstallItemVersionResult, InstallPlan, InstallPlanResult, UpdateItemVersionResult, ItemPublishedVersionInfo } from '@shared/models/iot-hub/iot-hub-installed-item.models';
import { ItemType, ItemTypeFilterInfo, WidgetCategory } from '@shared/models/iot-hub/iot-hub-item.models';
@ -81,6 +81,25 @@ export class IotHubApiService {
);
}
/**
* The sectioned answer: at most four rows of each item type, every row carrying the size of its
* section. Its own endpoint, which is why this is its own method - it takes no page, page size
* or sort direction, and sending them would be sending what the server does not read.
*/
public getPublishedVersionsGrouped(query: MpItemVersionQuery,
config?: IotHubRequestConfig): Observable<PageData<MpItemVersionGroupedView>> {
if (query.options.tbVersion == null) {
query.options.tbVersion = tbVersionToInt(env.tbVersion);
}
if (query.options.peOnly == null) {
query.options.peOnly = false;
}
return this.http.get<PageData<MpItemVersionGroupedView>>(
`${this.baseUrl}/api/versions/published/grouped${query.toGroupedQuery()}`,
{ params: this.buildParams(config) }
);
}
public getFilterInfo(itemType: ItemType, config?: IotHubRequestConfig): Observable<ItemTypeFilterInfo> {
const url = `${this.baseUrl}/api/item-listing/filterInfo/${itemType}`
+ `?peOnly=false&tbVersion=${tbVersionToInt(env.tbVersion)}`;

22
ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-home.component.ts

@ -9,7 +9,7 @@ import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
import { MediaBreakpoints } from '@shared/models/constants';
import { PageLink } from '@shared/models/page/page-link';
import { Direction, SortOrder } from '@shared/models/page/sort-order';
import { MpItemVersionQuery, MpItemVersionView } from '@shared/models/iot-hub/iot-hub-version.models';
import { MpItemVersionGroupedView, MpItemVersionQuery, MpItemVersionView } from '@shared/models/iot-hub/iot-hub-version.models';
import { getItemTypeIcon, ItemType, itemTypeTranslations } from '@shared/models/iot-hub/iot-hub-item.models';
import { IotHubInstalledItem } from '@shared/models/iot-hub/iot-hub-installed-item.models';
import { IotHubApiService } from '@core/http/iot-hub-api.service';
@ -34,7 +34,7 @@ interface HeroTypeConfig {
interface SearchResultGroup {
type: ItemType;
items: MpItemVersionView[];
items: MpItemVersionGroupedView[];
/** Rows of this type behind the answer, from the response's typeTotal. */
total: number;
/** total - items.length, floored at 0. Zero means the header shows no "+N more". */
@ -62,7 +62,7 @@ export class TbIotHubHomeComponent implements OnInit, OnDestroy {
readonly ItemType = ItemType;
searchText = '';
searchResults: MpItemVersionView[] = [];
searchResults: MpItemVersionGroupedView[] = [];
searchResultGroups: SearchResultGroup[] = [];
searchLoaded = false;
searchLoading = false;
@ -171,11 +171,10 @@ export class TbIotHubHomeComponent implements OnInit, OnDestroy {
const sortOrder: SortOrder = trimmed
? { property: RELEVANCE, direction: Direction.DESC }
: { property: 'totalInstallCount', direction: Direction.DESC };
// The server sizes a grouped answer itself - the top rows of every type, one screen - so
// this page size is ignored, and page must stay 0 (a non-zero page is a 400).
// The grouped endpoint sizes its own answer, so the page size here is never sent.
const pageLink = new PageLink(10, 0, trimmed || null, sortOrder);
const query = new MpItemVersionQuery(pageLink, { grouped: true });
return this.iotHubApiService.getPublishedVersions(query, { ignoreLoading: true });
const query = new MpItemVersionQuery(pageLink);
return this.iotHubApiService.getPublishedVersionsGrouped(query, { ignoreLoading: true });
})
).subscribe(result => {
this.searchResults = result.data;
@ -555,8 +554,8 @@ export class TbIotHubHomeComponent implements OnInit, OnDestroy {
* arrives already capped at four per type - so all this does is put the types in the panel's
* fixed order and carry each one's typeTotal onto the header.
*/
private groupSearchResults(items: MpItemVersionView[]): SearchResultGroup[] {
const groupMap = new Map<ItemType, MpItemVersionView[]>();
private groupSearchResults(items: MpItemVersionGroupedView[]): SearchResultGroup[] {
const groupMap = new Map<ItemType, MpItemVersionGroupedView[]>();
for (const item of items) {
if (!SEARCH_GROUP_ORDER.includes(item.type)) {
continue;
@ -572,8 +571,9 @@ export class TbIotHubHomeComponent implements OnInit, OnDestroy {
.filter(type => groupMap.has(type))
.map(type => {
const groupItems = groupMap.get(type);
// Every row of a type carries the same typeTotal. The fallback keeps a non-grouped
// response rendering correctly - which is what a stale backend would send.
// Every row of a type carries the same typeTotal, so the first one answers for the
// section. The fallback covers a backend too old to have the grouped endpoint, whose
// rows arrive without it.
const total = groupItems[0].typeTotal ?? groupItems.length;
return { type, items: groupItems, total, remaining: Math.max(0, total - groupItems.length) };
});

49
ui-ngx/src/app/shared/models/iot-hub/iot-hub-version.models.ts

@ -106,12 +106,18 @@ export interface MpItemVersionView {
resources: MpItemVersionResource[];
relatedItems?: string[];
checksum?: string;
}
/**
* A row of a grouped search: the view plus the size of the section it belongs to. Its own type
* rather than a nullable field on the view, matching what the endpoint returns.
*/
export interface MpItemVersionGroupedView extends MpItemVersionView {
/**
* Rows of this item's type behind a grouped response. A flat read does not omit it — the
* column is projected as NULL and serialised, so the field arrives as `null` there. Read it
* with `??`, never with a truthiness test.
* Rows of this row's item type behind the answer — what a "+N more" header subtracts the
* shown rows from. Always set: the grouped endpoint projects it for every row it returns.
*/
typeTotal?: number | null;
typeTotal: number;
}
// 404 body shapes returned by the public listing item-version endpoint
@ -145,20 +151,36 @@ export interface MpItemVersionQueryOptions {
connectivity?: string[];
vendors?: string[];
scadaFirst?: boolean;
/**
* Ask for the top four of each item type in one response instead of a flat page.
* Every row then carries `typeTotal`, the number of rows of its type behind the answer,
* which is what the section header's "+N more" counts. Grouped responses are one screen:
* the backend rejects a non-zero `page`.
*/
grouped?: boolean;
}
export class MpItemVersionQuery {
constructor(public pageLink: PageLink, public options: MpItemVersionQueryOptions = {}) {}
/**
* The grouped endpoint's query string: the filters, plus a sort key. It sizes its own answer,
* so it declares no page or page size, and each key's direction is fixed by its chain, so it
* declares no sort order either - sending any of the three would be sending something the
* server does not read.
*/
public toGroupedQuery(): string {
const text = this.pageLink.textSearch?.trim();
let query = '?';
if (text?.length) {
query += `textSearch=${encodeURIComponent(text)}&`;
}
if (this.pageLink.sortOrder) {
query += `sortProperty=${this.pageLink.sortOrder.property}&`;
}
return query + this.filtersToQuery();
}
public toQuery(): string {
let query = this.pageLink.toQuery();
return this.pageLink.toQuery() + this.filtersToQuery();
}
/** Every filter, as `&name=value` pairs - shared by both query strings above. */
private filtersToQuery(): string {
let query = '';
const o = this.options;
if (o.type) {
query += `&type=${o.type}`;
@ -202,9 +224,6 @@ export class MpItemVersionQuery {
if (o.scadaFirst != null) {
query += `&scadaFirst=${o.scadaFirst}`;
}
if (o.grouped != null) {
query += `&grouped=${o.grouped}`;
}
return query;
}
}

Loading…
Cancel
Save