packageDocumentation module import { AccessToken , GuidString , Logger } from "@itwin/core-bentley" ; import { ECJsonTypeMap , WsgInstance } from "../wsg/ECJsonTypeMap" ; import { WsgQuery } from "../wsg/WsgQuery" ; import { IModelHubClientLoggerCategory } from "../IModelHubClientLoggerCategories" ; import { IModelBaseHandler } from "./BaseHandler" ; import { ArgumentCheck } from "./Errors" ; const loggerCategory = IModelHubClientLoggerCategory . IModelHub ; internal @ ECJsonTypeMap . classToJson ( "wsg" , "iModelScope.UserInfo" , { schemaPropertyName : "schemaName" , classPropertyName : "className" } ) export class HubUserInfo extends WsgInstance { /** Id of the user. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.Id" ) public id ? : string ; /** First name of the user. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.Name" ) public firstName ? : string ; /** Last name of the user. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.Surname" ) public lastName ? : string ; /** Email address of the user. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.Email" ) public email ? : string ; } internal @ ECJsonTypeMap . classToJson ( "wsg" , "iModelScope.UserInfo" , { schemaPropertyName : "schemaName" , classPropertyName : "className" } ) export class UserStatistics extends HubUserInfo { /** Number of [[Briefcase]]s the user currently owns. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "relationshipInstances[HasStatistics].relatedInstance[Statistics].properties.BriefcasesCount" ) public briefcasesCount ? : number ; /** Number of [[Lock]]s the user currently owns. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "relationshipInstances[HasStatistics].relatedInstance[Statistics].properties.OwnedLocksCount" ) public ownedLocksCount ? : number ; /** Number of [[ChangeSet]]s the user has pushed. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "relationshipInstances[HasStatistics].relatedInstance[Statistics].properties.PushedChangeSetsCount" ) public pushedChangeSetsCount ? : number ; /** Date of the last [[ChangeSet]] the user has pushed to this iModel. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "relationshipInstances[HasStatistics].relatedInstance[Statistics].properties.LastChangeSetPushDate" ) public lastChangeSetPushDate ? : string ; } internal export class UserStatisticsQuery extends WsgQuery { internal protected _byId ? : string ; private _statisticsPrefix = "HasStatistics-forward-Statistics" ; private _queriedByIds = false ; /**
     * Constructor for UserStatisticsQuery.
     */ constructor ( ) { super ( ) ; this . select ( `*` ) ; } param id returns throws public byId ( id ) { ArgumentCheck . defined ( "id" , id ) ; this . _byId = id ; return this ; } internal public getId ( ) { return this . _byId ; } param ids returns throws public byIds ( ids ) { ArgumentCheck . nonEmptyArray ( "ids" , ids ) ; let filter = "$id+in+[" ; ids . forEach ( ( id , index ) => { ArgumentCheck . defined ( `ids[${ index }]` , id ) ; if ( index > 0 ) filter += "," ; filter += `'${ id }'` ; } ) ; filter += "]" ; this . addFilter ( filter ) ; this . _queriedByIds = true ; return this ; } /** Select all statistics. */ public selectAll ( ) { return this . addSelect ( `${ this . _statisticsPrefix }.*` ) ; } /** Select currently owned [[Briefcase]]s count. */ public selectBriefcasesCount ( ) { return this . addSelect ( `${ this . _statisticsPrefix }.BriefcasesCount` ) ; } /** Select total pushed [[ChangeSet]]s count. */ public selectPushedChangeSetsCount ( ) { return this . addSelect ( `${ this . _statisticsPrefix }.PushedChangeSetsCount` ) ; } /** Select currently owned [[Lock]]s count. */ public selectOwnedLocksCount ( ) { return this . addSelect ( `${ this . _statisticsPrefix }.OwnedLocksCount` ) ; } /** Select the last [[ChangeSet]] push date. */ public selectLastChangeSetPushDate ( ) { return this . addSelect ( `${ this . _statisticsPrefix }.LastChangeSetPushDate` ) ; } internal public get isQueriedByIds ( ) { return this . _queriedByIds ; } } internal export class UserStatisticsHandler { private _handler : IModelBaseHandler ; param handler internal constructor ( handler ) { this . _handler = handler ; } param iModelId param userId private getRelativeUrl ( iModelId , userId ? ) { return `/Repositories/iModel--${ iModelId }/iModelScope/UserInfo/${ userId ? userId : "" }` ; } param iModelId param query returns throws public async get ( accessToken , iModelId , query = new UserStatisticsQuery ( ) ) { Logger . logInfo ( loggerCategory , "Querying user statistics for iModel" , ( ) => ( { iModelId } ) ) ; ArgumentCheck . validGuid ( "iModelId" , iModelId ) ; if ( query . getQueryOptions ( ) . $select === "*" ) { query . selectAll ( ) ; } let userStatistics ; if ( query . isQueriedByIds ) { userStatistics = await this . _handler . postQuery < UserStatistics > ( accessToken , UserStatistics , this . getRelativeUrl ( iModelId ) , query . getQueryOptions ( ) ) ; } else { userStatistics = await this . _handler . getInstances < UserStatistics > ( accessToken , UserStatistics , this . getRelativeUrl ( iModelId , query . getId ( ) ) , query . getQueryOptions ( ) ) ; } Logger . logTrace ( loggerCategory , `Queried ${ userStatistics . length } user statistics for iModel` , ( ) => ( { iModelId } ) ) ; return userStatistics ; } } internal export class UserInfoQuery extends WsgQuery { private _queriedByIds = false ; internal protected _byId ? : string ; param ids returns throws public byIds ( ids ) { ArgumentCheck . nonEmptyArray ( "ids" , ids ) ; let filter = "$id+in+[" ; ids . forEach ( ( id , index ) => { ArgumentCheck . defined ( `id[${ index }]` , id ) ; if ( index > 0 ) filter += "," ; filter += `'${ id }'` ; } ) ; filter += "]" ; this . addFilter ( filter ) ; this . _queriedByIds = true ; return this ; } internal public get isQueriedByIds ( ) { return this . _queriedByIds ; } param id returns throws public byId ( id ) { ArgumentCheck . defined ( "id" , id ) ; this . _byId = id ; return this ; } internal public getId ( ) { return this . _byId ; } } internal export class UserInfoHandler { private _handler : IModelBaseHandler ; param handler internal constructor ( handler ) { this . _handler = handler ; } /** Get the handler for querying [[UserStatistics]]. */ public get statistics ( ) : UserStatisticsHandler { return new UserStatisticsHandler ( this . _handler ) ; } param iModelId param userId private getRelativeUrl ( iModelId , userId ? ) { return `/Repositories/iModel--${ iModelId }/iModelScope/UserInfo/${ userId || "" }` ; } param iModelId param query throws public async get ( accessToken , iModelId , query = new UserInfoQuery ( ) ) { Logger . logInfo ( loggerCategory , "Querying users for iModel" , ( ) => ( { iModelId } ) ) ; ArgumentCheck . validGuid ( "iModelId" , iModelId ) ; let users ; if ( query . isQueriedByIds ) { users = await this . _handler . postQuery < HubUserInfo > ( accessToken , HubUserInfo , this . getRelativeUrl ( iModelId , query . getId ( ) ) , query . getQueryOptions ( ) ) ; } else { users = await this . _handler . getInstances < HubUserInfo > ( accessToken , HubUserInfo , this . getRelativeUrl ( iModelId , query . getId ( ) ) , query . getQueryOptions ( ) ) ; } Logger . logTrace ( loggerCategory , "Queried users for iModel" , ( ) => ( { iModelId } ) ) ; return users ; } }