storj/satellite/console/consoleweb/consoleql/project.go

440 lines
13 KiB
Go
Raw Normal View History

2019-01-24 16:26:36 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleql
import (
2019-04-04 15:56:20 +01:00
"time"
"github.com/graphql-go/graphql"
2019-01-08 14:05:14 +00:00
"storj.io/storj/satellite/console"
)
const (
2019-01-24 16:26:36 +00:00
// ProjectType is a graphql type name for project
ProjectType = "project"
// ProjectInputType is a graphql type name for project input
ProjectInputType = "projectInput"
2019-04-04 15:56:20 +01:00
// ProjectUsageType is a graphql type name for project usage
ProjectUsageType = "projectUsage"
2019-05-16 11:43:46 +01:00
// BucketUsageCursorInputType is a graphql input
// type name for bucket usage cursor
BucketUsageCursorInputType = "bucketUsageCursor"
// BucketUsageType is a graphql type name for bucket usage
BucketUsageType = "bucketUsage"
// BucketUsagePageType is a field name for bucket usage page
BucketUsagePageType = "bucketUsagePage"
2019-07-10 21:29:26 +01:00
// PaymentMethodType is a field name for payment method
PaymentMethodType = "paymentMethod"
// ProjectMembersPageType is a field name for project members page
ProjectMembersPageType = "projectMembersPage"
// ProjectMembersCursorInputType is a graphql type name for project members
ProjectMembersCursorInputType = "projectMembersCursor"
2019-01-24 16:26:36 +00:00
// FieldName is a field name for "name"
FieldName = "name"
2019-05-16 11:43:46 +01:00
// FieldBucketName is a field name for "bucket name"
FieldBucketName = "bucketName"
2019-01-24 16:26:36 +00:00
// FieldDescription is a field name for description
FieldDescription = "description"
// FieldMembers is field name for members
FieldMembers = "members"
// FieldAPIKeys is a field name for api keys
FieldAPIKeys = "apiKeys"
2019-04-04 15:56:20 +01:00
// FieldUsage is a field name for usage rollup
FieldUsage = "usage"
2019-05-16 11:43:46 +01:00
// FieldBucketUsages is a field name for bucket usages
FieldBucketUsages = "bucketUsages"
2019-07-10 21:29:26 +01:00
// FieldPaymentMethods is a field name for payments methods
FieldPaymentMethods = "paymentMethods"
2019-04-04 15:56:20 +01:00
// FieldStorage is a field name for storage total
FieldStorage = "storage"
// FieldEgress is a field name for egress total
FieldEgress = "egress"
// FieldObjectCount is a field name for objects count
FieldObjectCount = "objectCount"
2019-05-16 11:43:46 +01:00
// FieldPageCount is a field name for total page count
FieldPageCount = "pageCount"
// FieldCurrentPage is a field name for current page number
FieldCurrentPage = "currentPage"
// FieldTotalCount is a field name for bucket usage count total
FieldTotalCount = "totalCount"
// FieldProjectMembers is a field name for project members
FieldProjectMembers = "projectMembers"
2019-07-10 21:29:26 +01:00
// FieldCardBrand is a field name for credit card brand
FieldCardBrand = "brand"
// FieldCardLastFour is a field name for credit card last four digits
FieldCardLastFour = "lastFour"
// FieldCardToken is a field name for credit card token
FieldCardToken = "cardToken"
// FieldIsDefault is a field name for default payment method
FieldIsDefault = "isDefault"
2019-05-16 11:43:46 +01:00
// CursorArg is an argument name for cursor
CursorArg = "cursor"
// PageArg ia an argument name for page number
PageArg = "page"
2019-01-24 16:26:36 +00:00
// LimitArg is argument name for limit
LimitArg = "limit"
// OffsetArg is argument name for offset
OffsetArg = "offset"
// SearchArg is argument name for search
SearchArg = "search"
// OrderArg is argument name for order
OrderArg = "order"
// OrderDirectionArg is argument name for order direction
OrderDirectionArg = "orderDirection"
2019-04-04 15:56:20 +01:00
// SinceArg marks start of the period
SinceArg = "since"
// BeforeArg marks end of the period
BeforeArg = "before"
)
// graphqlProject creates *graphql.Object type representation of satellite.ProjectInfo
2019-04-04 15:56:20 +01:00
func graphqlProject(service *console.Service, types *TypeCreator) *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
2019-01-24 16:26:36 +00:00
Name: ProjectType,
Fields: graphql.Fields{
2019-01-24 16:26:36 +00:00
FieldID: &graphql.Field{
Type: graphql.String,
},
2019-01-24 16:26:36 +00:00
FieldName: &graphql.Field{
Type: graphql.String,
},
2019-01-24 16:26:36 +00:00
FieldDescription: &graphql.Field{
Type: graphql.String,
},
2019-01-24 16:26:36 +00:00
FieldCreatedAt: &graphql.Field{
Type: graphql.DateTime,
},
2019-01-24 16:26:36 +00:00
FieldMembers: &graphql.Field{
Type: types.projectMemberPage,
Args: graphql.FieldConfigArgument{
CursorArg: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(types.projectMembersCursor),
},
},
2018-12-10 11:38:42 +00:00
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
project, _ := p.Source.(*console.Project)
2018-12-10 11:38:42 +00:00
_, err := console.GetAuth(p.Context)
2018-12-10 11:38:42 +00:00
if err != nil {
return nil, err
}
cursor := cursorArgsToProjectMembersCursor(p.Args[CursorArg].(map[string]interface{}))
page, err := service.GetProjectMembers(p.Context, project.ID, cursor)
if err != nil {
return nil, err
}
2018-12-10 11:38:42 +00:00
var users []projectMember
for _, member := range page.ProjectMembers {
2018-12-10 11:38:42 +00:00
user, err := service.GetUser(p.Context, member.MemberID)
if err != nil {
return nil, err
}
users = append(users, projectMember{
User: user,
JoinedAt: member.CreatedAt,
})
}
projectMembersPage := projectMembersPage{
ProjectMembers: users,
TotalCount: page.TotalCount,
Offset: page.Offset,
Limit: page.Limit,
Order: int(page.Order),
OrderDirection: int(page.OrderDirection),
Search: page.Search,
CurrentPage: page.CurrentPage,
PageCount: page.PageCount,
}
return projectMembersPage, nil
2018-12-10 11:38:42 +00:00
},
},
2019-01-24 16:26:36 +00:00
FieldAPIKeys: &graphql.Field{
2019-04-04 15:56:20 +01:00
Type: graphql.NewList(types.apiKeyInfo),
2018-12-27 15:30:15 +00:00
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
project, _ := p.Source.(*console.Project)
2018-12-27 15:30:15 +00:00
return service.GetAPIKeysInfoByProjectID(p.Context, project.ID)
},
},
2019-04-04 15:56:20 +01:00
FieldUsage: &graphql.Field{
Type: types.projectUsage,
Args: graphql.FieldConfigArgument{
SinceArg: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.DateTime),
},
BeforeArg: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.DateTime),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
project, _ := p.Source.(*console.Project)
since := p.Args[SinceArg].(time.Time)
before := p.Args[BeforeArg].(time.Time)
return service.GetProjectUsage(p.Context, project.ID, since, before)
},
},
2019-05-16 11:43:46 +01:00
FieldBucketUsages: &graphql.Field{
Type: types.bucketUsagePage,
Args: graphql.FieldConfigArgument{
BeforeArg: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.DateTime),
},
CursorArg: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(types.bucketUsageCursor),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
project, _ := p.Source.(*console.Project)
before := p.Args[BeforeArg].(time.Time)
cursor := fromMapBucketUsageCursor(p.Args[CursorArg].(map[string]interface{}))
return service.GetBucketTotals(p.Context, project.ID, cursor, before)
},
},
2019-07-10 21:29:26 +01:00
FieldPaymentMethods: &graphql.Field{
Type: graphql.NewList(types.paymentMethod),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
project, _ := p.Source.(*console.Project)
paymentMethods, err := service.GetProjectPaymentMethods(p.Context, project.ID)
if err != nil {
return nil, err
}
var projectPaymentMethods []projectPayment
for _, paymentMethod := range paymentMethods {
projectPaymentMethod := projectPayment{
ID: paymentMethod.ID.String(),
LastFour: paymentMethod.Card.LastFour,
AddedAt: paymentMethod.CreatedAt,
CardBrand: paymentMethod.Card.Brand,
ExpMonth: paymentMethod.Card.ExpirationMonth,
ExpYear: paymentMethod.Card.ExpirationYear,
HolderName: paymentMethod.Card.Name,
IsDefault: paymentMethod.IsDefault,
}
projectPaymentMethods = append(projectPaymentMethods, projectPaymentMethod)
}
return projectPaymentMethods, nil
},
},
},
})
}
// graphqlProjectInput creates graphql.InputObject type needed to create/update satellite.Project
func graphqlProjectInput() *graphql.InputObject {
return graphql.NewInputObject(graphql.InputObjectConfig{
2019-01-24 16:26:36 +00:00
Name: ProjectInputType,
Fields: graphql.InputObjectConfigFieldMap{
2019-01-24 16:26:36 +00:00
FieldName: &graphql.InputObjectFieldConfig{
Type: graphql.String,
},
2019-01-24 16:26:36 +00:00
FieldDescription: &graphql.InputObjectFieldConfig{
Type: graphql.String,
},
},
})
}
2019-05-16 11:43:46 +01:00
// graphqlBucketUsageCursor creates bucket usage cursor graphql input type
func graphqlBucketUsageCursor() *graphql.InputObject {
return graphql.NewInputObject(graphql.InputObjectConfig{
Name: BucketUsageCursorInputType,
Fields: graphql.InputObjectConfigFieldMap{
SearchArg: &graphql.InputObjectFieldConfig{
Type: graphql.NewNonNull(graphql.String),
},
LimitArg: &graphql.InputObjectFieldConfig{
Type: graphql.NewNonNull(graphql.Int),
},
PageArg: &graphql.InputObjectFieldConfig{
Type: graphql.NewNonNull(graphql.Int),
},
},
})
}
// graphqlBucketUsage creates bucket usage grapqhl type
func graphqlBucketUsage() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: BucketUsageType,
Fields: graphql.Fields{
FieldBucketName: &graphql.Field{
Type: graphql.String,
},
FieldStorage: &graphql.Field{
Type: graphql.Float,
},
FieldEgress: &graphql.Field{
Type: graphql.Float,
},
FieldObjectCount: &graphql.Field{
Type: graphql.Float,
},
SinceArg: &graphql.Field{
Type: graphql.DateTime,
},
BeforeArg: &graphql.Field{
Type: graphql.DateTime,
},
},
})
}
// graphqlBucketUsagePage creates bucket usage page graphql object
func graphqlBucketUsagePage(types *TypeCreator) *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: BucketUsagePageType,
Fields: graphql.Fields{
FieldBucketUsages: &graphql.Field{
Type: graphql.NewList(types.bucketUsage),
},
SearchArg: &graphql.Field{
Type: graphql.String,
},
LimitArg: &graphql.Field{
Type: graphql.Int,
},
OffsetArg: &graphql.Field{
Type: graphql.Int,
},
FieldPageCount: &graphql.Field{
Type: graphql.Int,
},
FieldCurrentPage: &graphql.Field{
Type: graphql.Int,
},
FieldTotalCount: &graphql.Field{
Type: graphql.Int,
},
},
})
}
2019-04-04 15:56:20 +01:00
// graphqlProjectUsage creates project usage graphql type
func graphqlProjectUsage() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: ProjectUsageType,
Fields: graphql.Fields{
FieldStorage: &graphql.Field{
Type: graphql.Float,
},
FieldEgress: &graphql.Field{
Type: graphql.Float,
},
FieldObjectCount: &graphql.Field{
2019-04-04 15:56:20 +01:00
Type: graphql.Float,
},
SinceArg: &graphql.Field{
Type: graphql.DateTime,
},
BeforeArg: &graphql.Field{
Type: graphql.DateTime,
},
},
})
}
2019-07-10 21:29:26 +01:00
const (
// FieldExpirationYear is field name for expiration year
FieldExpirationYear = "expYear"
// FieldExpirationMonth is field name for expiration month
FieldExpirationMonth = "expMonth"
// FieldHolderName is field name for holder name
FieldHolderName = "holderName"
// FieldAddedAt is field name for added at date
FieldAddedAt = "addedAt"
)
// graphqlPaymentMethod creates invoice payment method graphql type
func graphqlPaymentMethod() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: PaymentMethodType,
Fields: graphql.Fields{
FieldID: &graphql.Field{
Type: graphql.String,
},
FieldExpirationYear: &graphql.Field{
Type: graphql.Int,
},
FieldExpirationMonth: &graphql.Field{
Type: graphql.Int,
},
FieldCardBrand: &graphql.Field{
Type: graphql.String,
},
FieldCardLastFour: &graphql.Field{
Type: graphql.String,
},
FieldHolderName: &graphql.Field{
Type: graphql.String,
},
FieldAddedAt: &graphql.Field{
Type: graphql.DateTime,
},
FieldIsDefault: &graphql.Field{
Type: graphql.Boolean,
},
},
})
}
type projectPayment struct {
ID string
ExpYear int64
ExpMonth int64
CardBrand string
LastFour string
HolderName string
AddedAt time.Time
IsDefault bool
}
2019-05-16 11:43:46 +01:00
// fromMapProjectInfo creates console.ProjectInfo from input args
func fromMapProjectInfo(args map[string]interface{}) (project console.ProjectInfo) {
2019-01-24 16:26:36 +00:00
project.Name, _ = args[FieldName].(string)
project.Description, _ = args[FieldDescription].(string)
return
}
2019-05-16 11:43:46 +01:00
// fromMapBucketUsageCursor creates console.BucketUsageCursor from input args
func fromMapBucketUsageCursor(args map[string]interface{}) (cursor console.BucketUsageCursor) {
limit, _ := args[LimitArg].(int)
page, _ := args[PageArg].(int)
cursor.Limit = uint(limit)
cursor.Page = uint(page)
cursor.Search, _ = args[SearchArg].(string)
return
}
func cursorArgsToProjectMembersCursor(args map[string]interface{}) console.ProjectMembersCursor {
limit, _ := args[LimitArg].(int)
page, _ := args[PageArg].(int)
order, _ := args[OrderArg].(int)
orderDirection, _ := args[OrderDirectionArg].(int)
var cursor console.ProjectMembersCursor
cursor.Limit = uint(limit)
cursor.Page = uint(page)
cursor.Order = console.ProjectMemberOrder(order)
cursor.OrderDirection = console.ProjectMemberOrderDirection(orderDirection)
cursor.Search, _ = args[SearchArg].(string)
return cursor
}