Skip to content

Commit

Permalink
fix: support for creating multiple ibpPeers(#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
dayuy committed Mar 2, 2023
1 parent 3dcc889 commit bd9a312
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/epolicy/epolicy.gql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ query getEpolicies($network: String) {
description
channel
value
creationTimestamp
lastHeartbeatTime
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/epolicy/epolicy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ export class EpolicyService {
) {}

format(epolicy: CRD.EndorsePolicy): Epolicy {
const creationTimestamp = new Date(
epolicy.metadata?.creationTimestamp,
).toISOString();
const lastHeartbeatTime = epolicy.status?.lastHeartbeatTime
? new Date(epolicy.status?.lastHeartbeatTime).toISOString()
: creationTimestamp;
return {
name: epolicy.metadata?.name,
creationTimestamp: new Date(
epolicy.metadata?.creationTimestamp,
).toISOString(),
creationTimestamp,
lastHeartbeatTime,
channel: epolicy.spec?.channel,
description: epolicy.spec?.description,
value: epolicy.spec?.value,
Expand Down
3 changes: 3 additions & 0 deletions src/epolicy/models/epolicy.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ export class Epolicy {

/** 创建时间 */
creationTimestamp?: string;

/** 更新时间 */
lastHeartbeatTime?: string;
}
4 changes: 2 additions & 2 deletions src/ibppeer/ibppeer.gql
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ query getIbppeers($organization: String!) {
}

# 创建节点
mutation createIbppeer($organization: String!) {
ibppeerCreate(organization: $organization) {
mutation createIbppeer($org: String!, $count: Float) {
ibppeerCreate(organization: $org, count: $count) {
name
creationTimestamp
status
Expand Down
7 changes: 4 additions & 3 deletions src/ibppeer/ibppeer.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ export class IbppeerResolver {
return this.ibppeerService.getIbppeers(auth, org);
}

@Mutation(() => Ibppeer, { description: '创建IBPPeer节点' })
@Mutation(() => [Ibppeer], { description: '创建IBPPeer节点' })
async ibppeerCreate(
@Auth() auth: JwtAuth,
@Args('organization', { description: '所在组织' }) org: string,
): Promise<Ibppeer> {
return this.ibppeerService.createIbppeer(auth, org);
@Args('count', { description: '新增节点数', nullable: true }) count: number,
): Promise<Ibppeer[]> {
return this.ibppeerService.createIbppeers(auth, org, count);
}

@ResolveField(() => [String], {
Expand Down
10 changes: 10 additions & 0 deletions src/ibppeer/ibppeer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ export class IbppeerService {
return this.format(body);
}

async createIbppeers(
auth: JwtAuth,
org: string,
count = 1,
): Promise<Ibppeer[]> {
return Promise.all(
Array.from({ length: count }).map(() => this.createIbppeer(auth, org)),
);
}

async createIbppeer(auth: JwtAuth, org: string): Promise<Ibppeer> {
const { token, preferred_username } = auth;
const { binaryData } = await this.cmService.getConfigmap(
Expand Down
7 changes: 1 addition & 6 deletions src/organization/organization.gql
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,10 @@ query getOrganization($name: String!) {
users {
name
isOrganizationAdmin
joinedAt
}
ibppeers {
name
creationTimestamp # TODO:去除,查询太多数据,分两个接口
status
limits {
cpu
memory
}
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/organization/organization.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@ export class OrganizationResolver {
@Parent() org: Organization,
@Loader(UsersLoader) usersLoader: DataLoader<User['name'], User>,
): Promise<User[]> {
const { clients, admin } = org;
const { clients, admin, name } = org;
const names = [...new Set([...(clients || []), ...(admin ? [admin] : [])])];
const users = await usersLoader.loadMany(names);
return (users as User[]).map((u) => ({
...u,
isOrganizationAdmin: admin === u.name,
}));
return (users as User[]).map((u) => {
let joinedAt = null;
try {
const bestchains = JSON.parse(u?.annotations?.bestchains);
joinedAt = bestchains?.list[name]?.creationTimestamp;
} catch (e) {}
return {
...u,
isOrganizationAdmin: admin === u.name,
joinedAt,
};
});
}

@ResolveField(() => [Network], {
Expand Down
11 changes: 10 additions & 1 deletion src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ type Epolicy {
"""描述"""
description: String

"""更新时间"""
lastHeartbeatTime: String

"""name"""
name: ID!

Expand Down Expand Up @@ -221,9 +224,12 @@ type Mutation {

"""创建IBPPeer节点"""
ibppeerCreate(
"""新增节点数"""
count: Float

"""所在组织"""
organization: String!
): Ibppeer!
): [Ibppeer!]!

"""创建网络"""
networkCreate(network: NewNetworkInput!): Network!
Expand Down Expand Up @@ -720,6 +726,9 @@ type User {
"""是否为组织管理员(组织列表中)"""
isOrganizationAdmin: Boolean

"""加入组织时间(组织列表中)"""
joinedAt: String

"""用户名"""
name: ID!

Expand Down
7 changes: 7 additions & 0 deletions src/users/models/user.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Field, HideField, ID, ObjectType } from '@nestjs/graphql';
import { AnyObj } from 'src/types';

@ObjectType({ description: '用户' })
export class User {
Expand All @@ -25,4 +26,10 @@ export class User {

/** 是否为组织管理员(组织列表中) */
isOrganizationAdmin?: boolean;

/** 加入组织时间(组织列表中) */
joinedAt?: string;

@HideField()
annotations?: AnyObj;
}

0 comments on commit bd9a312

Please sign in to comment.