Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Aug 10, 2019
1 parent c8ba6d4 commit 5c38c5f
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 85 deletions.
39 changes: 5 additions & 34 deletions api/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,20 @@ package api

import (
"github.com/gorilla/mux"
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
gql "github.com/mylxsw/adanos-alert/api/graphql"
lib "github.com/mylxsw/adanos-alert/pkg/graphql"
"github.com/mylxsw/go-toolkit/container"
)

func graphqlRouters(cc *container.Container) func(router *mux.Router) {
var defaultField = graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return "Hello, world", nil
},
}

rootMutation := graphql.NewObject(graphql.ObjectConfig{
Name: "RootMutation",
Fields: graphql.Fields{
"hello": &defaultField,
},
})

rootQuery := graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"hello": &defaultField,
},
})
gqlib := lib.NewBuilder()

gql.NewWelcomeObject().Register(rootQuery, rootMutation)
gql.NewRuleObject(cc).Register(rootQuery, rootMutation)

schemaConfig := graphql.SchemaConfig{Query: rootQuery, Mutation: rootMutation}
schema, err := graphql.NewSchema(schemaConfig)
if err != nil {
panic(err)
}
gql.NewWelcomeObject().Register(gqlib)
gql.NewRuleObject(cc).Register(gqlib)

return func(router *mux.Router) {
// router.Handle("/graphql", graphql.HTTPHandler(schema))
router.Handle("/graphql", handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: true,
}))
router.Handle("/graphql", gqlib.Build())
}
}
4 changes: 2 additions & 2 deletions api/graphql/graphql.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package graphql

import (
"github.com/graphql-go/graphql"
lib "github.com/mylxsw/adanos-alert/pkg/graphql"
)

type Graphql interface {
Register(query *graphql.Object, mutation *graphql.Object)
Register(builder lib.GraphQL)
}
25 changes: 14 additions & 11 deletions api/graphql/rule.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package graphql

import (
"fmt"

"github.com/graphql-go/graphql"
"github.com/mylxsw/adanos-alert/api/view"
"github.com/mylxsw/adanos-alert/internal/matcher"
Expand All @@ -11,20 +13,17 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

var ruleType = graphql.NewObject(graphql.ObjectConfig{
Name: "Rule",
Fields: graphql.BindFields(view.Rule{}),
})
var ruleType = gql.Object(view.Rule{})

type RuleObject struct {
cc *container.Container
}

func (r *RuleObject) Register(query *graphql.Object, mutation *graphql.Object) {
func (r *RuleObject) Register(builder gql.GraphQL) {
r.cc.MustResolve(func(ruleRepo repository.RuleRepo) {
query.AddFieldConfig("rules", r.GetAllRules(ruleRepo))
mutation.AddFieldConfig("addRule", r.AddRule(ruleRepo))
mutation.AddFieldConfig("deleteRule", r.DeleteRule(ruleRepo))
builder.Query("rules", r.GetAllRules(ruleRepo))
builder.Mutation("addRule", r.AddRule(ruleRepo))
builder.Mutation("deleteRule", r.DeleteRule(ruleRepo))
})
}

Expand All @@ -35,7 +34,7 @@ func NewRuleObject(cc *container.Container) *RuleObject {
func (r *RuleObject) GetAllRules(ruleRepo repository.RuleRepo) *graphql.Field {
return gql.CreateField(
graphql.NewList(ruleType),
gql.BindArgs(struct{ ID string `json:"id"` }{}, "id"),
gql.BindArgs(struct{ ID string `json:"id"` }{}),
func(arg struct{ ID string `json:"id"` }) (interface{}, error) {
filter := bson.M{}

Expand Down Expand Up @@ -88,9 +87,13 @@ func (r *RuleObject) GetAllRules(ruleRepo repository.RuleRepo) *graphql.Field {
// }

func (r *RuleObject) AddRule(repo repository.RuleRepo) *graphql.Field {
args := gql.BindArgs(view.Rule{}, "name", "description", "interval", "threshold", "priority", "triggers", "rule", "template", "summary_template", "status")
for name, arg := range args {
fmt.Println(name, arg.Type, arg.Type.Name())
}
return gql.CreateField(
ruleType,
gql.BindArgs(view.Rule{}, "name", "description", "interval", "threshold", "priority", "rule", "template", "summary_template", "status"),
gql.BindArgs(view.Rule{}, "name", "description", "interval", "threshold", "priority", "triggers", "rule", "template", "summary_template", "status"),
func(rule view.Rule) (interface{}, error) {
value := view.RuleToRepo(rule)

Expand Down Expand Up @@ -168,7 +171,7 @@ func (r *RuleObject) AddRule(repo repository.RuleRepo) *graphql.Field {
func (r *RuleObject) DeleteRule(repo repository.RuleRepo) *graphql.Field {
return gql.CreateField(
ruleType,
gql.BindArgs(struct{ ID string `json:"id"` }{}, "id"),
gql.BindArgs(struct{ ID string `json:"id"` }{}),
func(arg struct{ ID string `json:"id"` }) (interface{}, error) {
id, err := primitive.ObjectIDFromHex(arg.ID)
if err != nil {
Expand Down
28 changes: 11 additions & 17 deletions api/graphql/welcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphql

import (
"github.com/graphql-go/graphql"
lib "github.com/mylxsw/adanos-alert/pkg/graphql"
)

type WelcomeObject struct {
Expand All @@ -12,23 +13,16 @@ func NewWelcomeObject() *WelcomeObject {
return &WelcomeObject{}
}

func (w *WelcomeObject) Register(query *graphql.Object, mutation *graphql.Object) {
query.AddFieldConfig("welcome", &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "WelcomeObject",
Fields: graphql.BindFields(WelcomeObject{}),
}),
Args: graphql.FieldConfigArgument{
"name": &graphql.ArgumentConfig{
Type: graphql.String,
DefaultValue: "Graphql",
},
},
Resolve: w.Hello,
})
func (w *WelcomeObject) Register(builder lib.GraphQL) {
builder.Query("welcome", w.Hello())
}

func (w *WelcomeObject) Hello(p graphql.ResolveParams) (interface{}, error) {
name := p.Args["name"].(string)
return WelcomeObject{Message: name}, nil
func (w *WelcomeObject) Hello() *graphql.Field {
return lib.CreateField(
lib.Object(w),
lib.BindArgs(struct {Name string `json:"name"`}{}),
func(arg struct{ Name string `json:"name"` }) (interface{}, error) {
return WelcomeObject{Message: arg.Name}, nil
},
)
}
55 changes: 39 additions & 16 deletions api/view/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/mylxsw/adanos-alert/internal/repository"
"github.com/mylxsw/asteria/log"
"go.mongodb.org/mongo-driver/bson/primitive"
)

Expand All @@ -28,15 +29,34 @@ type Rule struct {
Threshold int64 `json:"threshold"`
Priority int64 `json:"priority"`

Rule string `json:"rule"`
Template string `json:"template"`
SummaryTemplate string `json:"summary_template"`
Triggers []Trigger `json:"triggers"`
Rule string `json:"rule"`
Template string `json:"template"`
SummaryTemplate string `json:"summary_template"`
Triggers []*Trigger `json:"triggers"`

Status repository.RuleStatus `json:"status"`

CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
CreatedTime time.Time `json:"created_time"`
}

func (r Rule) TriggersResolver() ([]Trigger, error) {
return []Trigger{
{
ID: "000001",
PreCondition: "a == b",
},
{
ID: "000002",
PreCondition: "a == b and c == d",
},
}, nil
}

func (r Rule) CreatedTimeResolver() (string, error) {
log.Info("query created_time")
return r.CreatedTime.Format(time.RFC3339), nil
}

func RuleFromRepo(r repository.Rule) Rule {
Expand All @@ -52,8 +72,9 @@ func RuleFromRepo(r repository.Rule) Rule {
SummaryTemplate: r.SummaryTemplate,
Triggers: TriggersFromRepos(r.Triggers),
Status: r.Status,
CreatedAt: r.CreatedAt,
UpdatedAt: r.UpdatedAt,
CreatedAt: r.CreatedAt.Format(time.RFC3339),
UpdatedAt: r.UpdatedAt.Format(time.RFC3339),
CreatedTime: r.CreatedAt,
}
}

Expand All @@ -68,6 +89,8 @@ func RulesFromRepos(rs []repository.Rule) []Rule {

func RuleToRepo(r Rule) repository.Rule {
id, _ := primitive.ObjectIDFromHex(r.ID)
createdAt, _ := time.Parse(time.RFC3339, r.CreatedAt)
updatedAt, _ := time.Parse(time.RFC3339, r.UpdatedAt)
return repository.Rule{
ID: id,
Name: r.Name,
Expand All @@ -80,13 +103,13 @@ func RuleToRepo(r Rule) repository.Rule {
SummaryTemplate: r.SummaryTemplate,
Triggers: TriggersToRepo(r.Triggers),
Status: r.Status,
CreatedAt: r.CreatedAt,
UpdatedAt: r.UpdatedAt,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}
}

func TriggerFromRepo(tr repository.Trigger) Trigger {
return Trigger{
func TriggerFromRepo(tr repository.Trigger) *Trigger {
return &Trigger{
ID: tr.ID.Hex(),
PreCondition: tr.PreCondition,
Action: tr.Action,
Expand All @@ -97,8 +120,8 @@ func TriggerFromRepo(tr repository.Trigger) Trigger {
}
}

func TriggersFromRepos(trs []repository.Trigger) []Trigger {
triggers := make([]Trigger, len(trs))
func TriggersFromRepos(trs []repository.Trigger) []*Trigger {
triggers := make([]*Trigger, len(trs))
for i, tr := range trs {
triggers[i] = TriggerFromRepo(tr)
}
Expand All @@ -119,10 +142,10 @@ func TriggerToRepo(tr Trigger) repository.Trigger {
}
}

func TriggersToRepo(trs []Trigger) []repository.Trigger {
func TriggersToRepo(trs []*Trigger) []repository.Trigger {
var triggers = make([]repository.Trigger, len(trs))
for i, t := range trs {
triggers[i] = TriggerToRepo(t)
triggers[i] = TriggerToRepo(*t)
}

return triggers
Expand Down

0 comments on commit 5c38c5f

Please sign in to comment.