2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-26 10:47:23 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package consoleql
|
2018-11-26 10:47:23 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-04 15:56:20 +01:00
|
|
|
"time"
|
|
|
|
|
2018-11-26 10:47:23 +00:00
|
|
|
"github.com/graphql-go/graphql"
|
2019-01-08 14:05:14 +00:00
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2018-11-26 10:47:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-01-24 16:26:36 +00:00
|
|
|
// FieldName is a field name for "name"
|
|
|
|
FieldName = "name"
|
|
|
|
// 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"
|
|
|
|
// FieldStorage is a field name for storage total
|
|
|
|
FieldStorage = "storage"
|
|
|
|
// FieldEgress is a field name for egress total
|
|
|
|
FieldEgress = "egress"
|
|
|
|
// FieldObjectsCount is a field name for objects count
|
|
|
|
FieldObjectsCount = "objectsCount"
|
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"
|
2019-04-04 15:56:20 +01:00
|
|
|
// SinceArg marks start of the period
|
|
|
|
SinceArg = "since"
|
|
|
|
// BeforeArg marks end of the period
|
|
|
|
BeforeArg = "before"
|
2018-11-26 10:47:23 +00:00
|
|
|
)
|
|
|
|
|
2018-11-28 16:20:23 +00:00
|
|
|
// 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 {
|
2018-11-26 10:47:23 +00:00
|
|
|
return graphql.NewObject(graphql.ObjectConfig{
|
2019-01-24 16:26:36 +00:00
|
|
|
Name: ProjectType,
|
2018-11-26 10:47:23 +00:00
|
|
|
Fields: graphql.Fields{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldID: &graphql.Field{
|
2018-11-26 10:47:23 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldName: &graphql.Field{
|
2018-11-26 10:47:23 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldDescription: &graphql.Field{
|
2018-11-26 10:47:23 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldCreatedAt: &graphql.Field{
|
2018-11-26 10:47:23 +00:00
|
|
|
Type: graphql.DateTime,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldMembers: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: graphql.NewList(types.projectMember),
|
2018-12-19 13:03:12 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
OffsetArg: &graphql.ArgumentConfig{
|
2018-12-19 13:03:12 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.Int),
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
LimitArg: &graphql.ArgumentConfig{
|
2018-12-19 13:03:12 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.Int),
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
SearchArg: &graphql.ArgumentConfig{
|
2018-12-28 12:07:35 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
OrderArg: &graphql.ArgumentConfig{
|
2018-12-28 12:07:35 +00:00
|
|
|
Type: graphql.Int,
|
|
|
|
},
|
2018-12-19 13:03:12 +00:00
|
|
|
},
|
2018-12-10 11:38:42 +00:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-01-15 13:03:24 +00:00
|
|
|
project, _ := p.Source.(*console.Project)
|
2018-12-10 11:38:42 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
offs, _ := p.Args[OffsetArg].(int)
|
|
|
|
lim, _ := p.Args[LimitArg].(int)
|
|
|
|
search, _ := p.Args[SearchArg].(string)
|
|
|
|
order, _ := p.Args[OrderArg].(int)
|
2018-12-28 12:07:35 +00:00
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
pagination := console.Pagination{
|
2018-12-28 12:07:35 +00:00
|
|
|
Limit: lim,
|
2019-01-10 14:44:15 +00:00
|
|
|
Offset: int64(offs),
|
2018-12-28 12:07:35 +00:00
|
|
|
Search: search,
|
2019-01-15 13:03:24 +00:00
|
|
|
Order: console.ProjectMemberOrder(order),
|
2018-12-28 12:07:35 +00:00
|
|
|
}
|
2018-12-19 13:03:12 +00:00
|
|
|
|
2018-12-28 12:07:35 +00:00
|
|
|
members, err := service.GetProjectMembers(p.Context, project.ID, pagination)
|
2018-12-10 11:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var users []projectMember
|
|
|
|
for _, member := range members {
|
|
|
|
user, err := service.GetUser(p.Context, member.MemberID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
users = append(users, projectMember{
|
|
|
|
User: user,
|
|
|
|
JoinedAt: member.CreatedAt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
},
|
|
|
|
},
|
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) {
|
2019-01-15 13:03:24 +00:00
|
|
|
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)
|
|
|
|
},
|
|
|
|
},
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:20:23 +00:00
|
|
|
// graphqlProjectInput creates graphql.InputObject type needed to create/update satellite.Project
|
2018-11-26 10:47:23 +00:00
|
|
|
func graphqlProjectInput() *graphql.InputObject {
|
|
|
|
return graphql.NewInputObject(graphql.InputObjectConfig{
|
2019-01-24 16:26:36 +00:00
|
|
|
Name: ProjectInputType,
|
2018-11-26 10:47:23 +00:00
|
|
|
Fields: graphql.InputObjectConfigFieldMap{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldName: &graphql.InputObjectFieldConfig{
|
2018-11-26 10:47:23 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldDescription: &graphql.InputObjectFieldConfig{
|
2018-11-26 10:47:23 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
FieldObjectsCount: &graphql.Field{
|
|
|
|
Type: graphql.Float,
|
|
|
|
},
|
|
|
|
SinceArg: &graphql.Field{
|
|
|
|
Type: graphql.DateTime,
|
|
|
|
},
|
|
|
|
BeforeArg: &graphql.Field{
|
|
|
|
Type: graphql.DateTime,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:47:23 +00:00
|
|
|
// fromMapProjectInfo creates satellite.ProjectInfo from input args
|
2019-01-15 13:03:24 +00:00
|
|
|
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)
|
2018-11-26 10:47:23 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|