Skip to content

Commit

Permalink
Merge pull request #296 from textileio/asutula/pow-2.0
Browse files Browse the repository at this point in the history
Powergate 2.0
  • Loading branch information
asutula authored Jan 21, 2021
2 parents d43237d + 90c8299 commit d131758
Show file tree
Hide file tree
Showing 19 changed files with 595 additions and 331 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async function exampleCode() {

// get information about the latest applied storage configuration,
// current storage state, and all related Powegate storage jobs
const { cidInfosList } = await pow.data.cidInfo(cid)
const { cidInfo } = await pow.data.cidInfo(cid)

// retrieve data stored in the user by cid
const bytes = await pow.data.get(cid)
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
}
},
"dependencies": {
"@textile/grpc-powergate-client": "^1.2.1",
"@textile/grpc-powergate-client": "2.0.0",
"@textile/grpc-transport": "0.0.3",
"ipfs-http-client": "^47.0.1",
"it-block": "^2.0.0"
Expand Down
45 changes: 45 additions & 0 deletions src/admin/data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { grpc } from "@improbable-eng/grpc-web"
import {
GCStagedRequest,
GCStagedResponse,
PinnedCidsRequest,
PinnedCidsResponse,
} from "@textile/grpc-powergate-client/dist/powergate/admin/v1/admin_pb"
import { AdminServiceClient } from "@textile/grpc-powergate-client/dist/powergate/admin/v1/admin_pb_service"
import { Config } from "../../types"
import { promise } from "../../util"

export interface Data {
/**
* Unpins staged data not related to queued or executing jobs.
* @returns An object containing a list of unpinned cids.
*/
gcStaged: () => Promise<GCStagedResponse.AsObject>

/**
* Get pinned cids information of hot-storage.
* @returns Pinned cids information of hot-storage.
*/
pinnedCids: () => Promise<PinnedCidsResponse.AsObject>
}

/**
* @ignore
*/
export const createData = (config: Config, getMeta: () => grpc.Metadata): Data => {
const client = new AdminServiceClient(config.host, config)
return {
gcStaged: () => {
return promise(
(cb) => client.gCStaged(new GCStagedRequest(), getMeta(), cb),
(resp: GCStagedResponse) => resp.toObject(),
)
},

pinnedCids: () =>
promise(
(cb) => client.pinnedCids(new PinnedCidsRequest(), getMeta(), cb),
(resp: PinnedCidsResponse) => resp.toObject(),
),
}
}
16 changes: 15 additions & 1 deletion src/admin/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { grpc } from "@improbable-eng/grpc-web"
import { Config } from "../types"
import { createData, Data } from "./data"
import { createStorageInfo, StorageInfo } from "./storage-info"
import { createStorageJobs, StorageJobs } from "./storage-jobs"
import { createUsers, Users } from "./users"
import { createWallet, Wallet } from "./wallet"
export { Users, StorageJobs, Wallet }
export { Users, StorageJobs, Wallet, StorageInfo }

export interface Admin {
/**
* The admin Data API.
*/
data: Data

/**
* The admin Users API.
*/
Expand All @@ -16,6 +23,11 @@ export interface Admin {
*/
wallet: Wallet

/**
* The admin StorageInfo API.
*/
storageInfo: StorageInfo

/**
* The admin Storage Jobs API.
*/
Expand All @@ -27,8 +39,10 @@ export interface Admin {
*/
export const createAdmin = (config: Config, getMeta: () => grpc.Metadata): Admin => {
return {
data: createData(config, getMeta),
users: createUsers(config, getMeta),
wallet: createWallet(config, getMeta),
storageInfo: createStorageInfo(config, getMeta),
storageJobs: createStorageJobs(config, getMeta),
}
}
59 changes: 59 additions & 0 deletions src/admin/storage-info/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { grpc } from "@improbable-eng/grpc-web"
import {
ListStorageInfoRequest,
ListStorageInfoResponse,
StorageInfoRequest,
StorageInfoResponse,
} from "@textile/grpc-powergate-client/dist/powergate/admin/v1/admin_pb"
import { AdminServiceClient } from "@textile/grpc-powergate-client/dist/powergate/admin/v1/admin_pb_service"
import { Config } from "../../types"
import { promise } from "../../util"

export interface StorageInfo {
/**
* Get the current storage state of a cid.
* @param userId The user id to query.
* @param cid The cid to query.
* @returns The current storage state of the cid.
*/
get: (userId: string, cid: string) => Promise<StorageInfoResponse.AsObject>

/**
* Lists the current storage state for many or all user ids and cids.
* @param cids Optional list of cids to filter the results by.
* @returns An object containing a list of storage info.
*/
list: (userIds?: string[], cids?: string[]) => Promise<ListStorageInfoResponse.AsObject>
}

/**
* @ignore
*/
export const createStorageInfo = (config: Config, getMeta: () => grpc.Metadata): StorageInfo => {
const client = new AdminServiceClient(config.host, config)
return {
get: (userId: string, cid: string) => {
const req = new StorageInfoRequest()
req.setUserId(userId)
req.setCid(cid)
return promise(
(cb) => client.storageInfo(req, getMeta(), cb),
(res: StorageInfoResponse) => res.toObject(),
)
},

list: (userIds?: string[], cids?: string[]) => {
const req = new ListStorageInfoRequest()
if (userIds) {
req.setUserIdsList(userIds)
}
if (cids) {
req.setCidsList(cids)
}
return promise(
(cb) => client.listStorageInfo(req, getMeta(), cb),
(res: ListStorageInfoResponse) => res.toObject(),
)
},
}
}
122 changes: 0 additions & 122 deletions src/admin/storage-jobs.ts

This file was deleted.

91 changes: 91 additions & 0 deletions src/admin/storage-jobs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { grpc } from "@improbable-eng/grpc-web"
import {
ListStorageJobsRequest,
ListStorageJobsResponse,
StorageJobsSummaryRequest,
StorageJobsSummaryResponse,
} from "@textile/grpc-powergate-client/dist/powergate/admin/v1/admin_pb"
import { AdminServiceClient } from "@textile/grpc-powergate-client/dist/powergate/admin/v1/admin_pb_service"
import { StorageJobsSelector } from "@textile/grpc-powergate-client/dist/powergate/user/v1/user_pb"
import { ListSelect } from "../../storage-jobs"
import { Config } from "../../types"
import { promise } from "../../util"
import { AdminListOptions } from "./types"

export interface StorageJobs {
/**
* Lists StorageJobs according to the provided ListOptions.
* @param opts Optional ListOptions to control the behavior of listing jobs.
* @returns An object containing a list of storage jobs.
*/
list: (opts?: AdminListOptions) => Promise<ListStorageJobsResponse.AsObject>

/**
* Get a summary of all jobs.
* @param userId The user id to query or undefined for all users.
* @param cids An optional cid to fileter the results with.
* @returns A summary of all jobs.
*/
summary: (userId?: string, cid?: string) => Promise<StorageJobsSummaryResponse.AsObject>
}

/**
* @ignore
*/
export const createStorageJobs = (config: Config, getMeta: () => grpc.Metadata): StorageJobs => {
const client = new AdminServiceClient(config.host, config)
return {
list: (opts?: AdminListOptions) => {
const req = new ListStorageJobsRequest()
if (opts?.ascending) {
req.setAscending(opts.ascending)
}
if (opts?.cidFilter) {
req.setCidFilter(opts.cidFilter)
}
if (opts?.limit) {
req.setLimit(opts.limit)
}
if (opts?.nextPageToken) {
req.setNextPageToken(opts.nextPageToken)
}
if (opts?.select != undefined) {
switch (opts.select) {
case ListSelect.All:
req.setSelector(StorageJobsSelector.STORAGE_JOBS_SELECTOR_ALL)
break
case ListSelect.Queued:
req.setSelector(StorageJobsSelector.STORAGE_JOBS_SELECTOR_QUEUED)
break
case ListSelect.Executing:
req.setSelector(StorageJobsSelector.STORAGE_JOBS_SELECTOR_EXECUTING)
break
case ListSelect.Final:
req.setSelector(StorageJobsSelector.STORAGE_JOBS_SELECTOR_FINAL)
break
}
}
if (opts?.userId) {
req.setUserIdFilter(opts.userId)
}
return promise(
(cb) => client.listStorageJobs(req, getMeta(), cb),
(resp: ListStorageJobsResponse) => resp.toObject(),
)
},

summary: (userId?: string, cid?: string) => {
const req = new StorageJobsSummaryRequest()
if (userId) {
req.setUserId(userId)
}
if (cid) {
req.setCid(cid)
}
return promise(
(cb) => client.storageJobsSummary(req, getMeta(), cb),
(resp: StorageJobsSummaryResponse) => resp.toObject(),
)
},
}
}
Loading

0 comments on commit d131758

Please sign in to comment.