Browse Source

Added channel link from log

1.0
Gisle Aune 7 years ago
parent
commit
1a91f4fdd6
  1. 2
      cmd/rpdata-graphiql/main.go
  2. 66
      loader/channel.go
  3. 5
      loader/loader.go
  4. 10
      model/channel/channel.go
  5. 21
      resolver/log.go
  6. 12
      schema/types/log.graphql

2
cmd/rpdata-graphiql/main.go

@ -28,7 +28,7 @@ func main() {
}
log.Println("Updated characters on", n, "logs")
schema, err := graphql.ParseSchema(schema.String(), &resolver.RootResolver{}, graphql.MaxParallelism(4))
schema, err := graphql.ParseSchema(schema.String(), &resolver.RootResolver{}, graphql.MaxParallelism(32))
if err != nil {
log.Fatalln("Failed to parse schema:", err)
}

66
loader/channel.go

@ -0,0 +1,66 @@
package loader
import (
"context"
"errors"
"strings"
"git.aiterp.net/rpdata/api/model/channel"
"github.com/graph-gophers/dataloader"
)
// Channel gets a character by key
func (loader *Loader) Channel(key, value string) (channel.Channel, error) {
if !strings.HasPrefix(key, "Channel.") {
key = "Channel." + key
}
if loader.loaders[key] == nil {
return channel.Channel{}, errors.New("unsupported key")
}
thunk := loader.loaders[key].Load(loader.ctx, dataloader.StringKey(value))
res, err := thunk()
if err != nil {
return channel.Channel{}, err
}
channel, ok := res.(channel.Channel)
if !ok {
return channel, errors.New("incorrect type")
}
return channel, nil
}
func channelNameBatch(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
var results []*dataloader.Result
names := keys.Keys()
channels, err := channel.ListNames(names...)
if err != nil {
for range names {
results = append(results, &dataloader.Result{Data: channel.Channel{}, Error: err})
}
return results
}
for _, name := range names {
found := false
for i := range channels {
if channels[i].Name == name {
results = append(results, &dataloader.Result{Data: channels[i]})
found = true
break
}
}
if !found {
results = append(results, &dataloader.Result{Data: channel.Channel{}, Error: err})
}
}
return results
}

5
loader/loader.go

@ -25,8 +25,9 @@ func New() *Loader {
return &Loader{
ctx: context.Background(),
loaders: map[string]*dataloader.Loader{
"Character.id": dataloader.NewBatchedLoader(characterIDBatch, dataloader.WithWait(time.Millisecond*2)),
"Character.nick": dataloader.NewBatchedLoader(characterNickBatch, dataloader.WithWait(time.Millisecond*2)),
"Character.id": dataloader.NewBatchedLoader(characterIDBatch, dataloader.WithWait(time.Millisecond)),
"Character.nick": dataloader.NewBatchedLoader(characterNickBatch, dataloader.WithWait(time.Millisecond)),
"Channel.name": dataloader.NewBatchedLoader(channelNameBatch, dataloader.WithWait(time.Millisecond)),
},
}
}

10
model/channel/channel.go

@ -121,6 +121,16 @@ func List(logged bool) ([]Channel, error) {
return channels, err
}
// ListNames finds channels by the names provided
func ListNames(names ...string) ([]Channel, error) {
query := bson.M{"_id": bson.M{"$in": names}}
channels := make([]Channel, 0, 32)
err := collection.Find(query).All(&channels)
return channels, err
}
func init() {
store.HandleInit(func(db *mgo.Database) {
collection = db.C("common.channels")

21
resolver/log.go

@ -2,8 +2,10 @@ package resolver
import (
"context"
"errors"
"time"
"git.aiterp.net/rpdata/api/loader"
"git.aiterp.net/rpdata/api/model/change"
"git.aiterp.net/rpdata/api/internal/session"
@ -239,11 +241,26 @@ func (r *LogResolver) Date() string {
return r.L.Date.Format(time.RFC3339Nano)
}
// Channel resolves Log.channel
func (r *LogResolver) Channel() string {
// ChannelName resolves Log.channelName
func (r *LogResolver) ChannelName() string {
return r.L.Channel
}
// Channel resolves Log.channe
func (r *LogResolver) Channel(ctx context.Context) (*ChannelResolver, error) {
loader := loader.FromContext(ctx)
if loader == nil {
return nil, errors.New("no loader")
}
channel, err := loader.Channel("name", r.L.Channel)
if err != nil {
return nil, err
}
return &ChannelResolver{C: channel}, nil
}
// Title resolves Log.title
func (r *LogResolver) Title() string {
return r.L.Title

12
schema/types/log.graphql

@ -11,7 +11,10 @@ type Log {
date: String!
# The channel of a log.
channel: String!
channelName: String!
# Information about the channel this log is in.
channel: Channel!
# The session's title.
title: String!
@ -105,8 +108,11 @@ type LogHeader {
# The date for a log.
date: String!
# The channel of a log.
channel: String!
# The channel name of a log.
channelName: String!
# Information about the channel this log is in.
channel: Channel!
# The session's title.
title: String!

Loading…
Cancel
Save