Skip to content

Commit

Permalink
UI: Allow sharing of queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawan Rawal committed Apr 12, 2017
1 parent 3f2eca1 commit cea0622
Show file tree
Hide file tree
Showing 22 changed files with 586 additions and 93 deletions.
76 changes: 76 additions & 0 deletions cmd/dgraph/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,24 @@ package main
import (
"encoding/json"
"net/http"
"regexp"

"github.com/dgraph-io/dgraph/protos/graphp"
"github.com/dgraph-io/dgraph/x"
)

func homeHandler(h http.Handler, reg *regexp.Regexp) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// If path is '/hexValue', lets return the index.html.
if reg.MatchString(r.URL.Path) {
http.ServeFile(w, r, uiDir+"/index.html")
return
}

h.ServeHTTP(w, r)
})
}

type keyword struct {
// Type could be a predicate, function etc.
Type string `json:"type"`
Expand All @@ -34,6 +50,11 @@ type keywords struct {
// Used to return a list of keywords, so that UI can show them for autocompletion.
func keywordHandler(w http.ResponseWriter, r *http.Request) {
addCorsHeaders(w)
if r.Method != "GET" {
http.Error(w, x.ErrorInvalidMethod, http.StatusBadRequest)
return
}

var kws keywords
predefined := []string{
"@facets",
Expand Down Expand Up @@ -86,3 +107,58 @@ func keywordHandler(w http.ResponseWriter, r *http.Request) {
}
w.Write(js)
}

func hasOnlySharePred(mutation *graphp.Mutation) bool {
for _, nq := range mutation.Set {
if nq.Predicate != INTERNAL_SHARE {
return false
}
}

for _, nq := range mutation.Del {
if nq.Predicate != INTERNAL_SHARE {
return false
}
}
return true
}

func hasSharePred(mutation *graphp.Mutation) bool {
for _, nq := range mutation.Set {
if nq.Predicate == INTERNAL_SHARE {
return true
}
}

for _, nq := range mutation.Del {
if nq.Predicate == INTERNAL_SHARE {
return true
}
}
return false
}

type dashboardState struct {
Share bool `json:"share"`
SharePred string `json:"share_pred"`
}

func initialState(w http.ResponseWriter, r *http.Request) {
addCorsHeaders(w)
if r.Method != "GET" {
http.Error(w, x.ErrorInvalidMethod, http.StatusBadRequest)
return
}

ds := dashboardState{
Share: !*noshare,
SharePred: INTERNAL_SHARE,
}

js, err := json.Marshal(ds)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(js)
}
37 changes: 34 additions & 3 deletions cmd/dgraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"os"
"os/signal"
"path"
"regexp"
"runtime"
"runtime/pprof"
"strconv"
Expand Down Expand Up @@ -70,6 +71,7 @@ var (
bindall = flag.Bool("bindall", false,
"Use 0.0.0.0 instead of localhost to bind to all addresses on local machine.")
nomutations = flag.Bool("nomutations", false, "Don't allow mutations on this server.")
noshare = flag.Bool("noshare", false, "Don't allow sharing queries through the UI.")
tracing = flag.Float64("trace", 0.0, "The ratio of queries to trace.")
cpuprofile = flag.String("cpu", "", "write cpu profile to file")
memprofile = flag.String("mem", "", "write memory profile to file")
Expand Down Expand Up @@ -229,14 +231,39 @@ func applyMutations(ctx context.Context, m *taskp.Mutations) error {
return nil
}

const INTERNAL_SHARE = "_share_"

func ismutationAllowed(mutation *graphp.Mutation) error {
if *nomutations {
if *noshare {
return x.Errorf("Mutations are forbidden on this server.")
}

// Sharing is allowed, lets check that mutation should have only internal
// share predicate.
if !hasOnlySharePred(mutation) {
return x.Errorf("Only mutations with: %v as predicate are allowed ",
INTERNAL_SHARE)
}
}
// Mutations are allowed but sharing isn't allowed.
if *noshare {
if hasSharePred(mutation) {
return x.Errorf("Mutations with: %v as predicate are not allowed ",
INTERNAL_SHARE)
}
}
return nil
}

func convertAndApply(ctx context.Context, mutation *graphp.Mutation) (map[string]uint64, error) {
var allocIds map[string]uint64
var m taskp.Mutations
var err error
var mr mutationResult

if *nomutations {
return nil, fmt.Errorf("Mutations are forbidden on this server.")
if err := ismutationAllowed(mutation); err != nil {
return nil, err
}

if mr, err = convertToEdges(ctx, mutation.Set); err != nil {
Expand Down Expand Up @@ -801,8 +828,12 @@ func setupServer(che chan error) {
http.HandleFunc("/admin/backup", backupHandler)

// UI related API's.
http.Handle("/", http.FileServer(http.Dir(uiDir)))
// Share urls have a hex string as the shareId. So if
// our url path matches it, we wan't to serve index.html.
reg := regexp.MustCompile(`\/0[xX][0-9a-fA-F]+`)
http.Handle("/", homeHandler(http.FileServer(http.Dir(uiDir)), reg))
http.HandleFunc("/ui/keywords", keywordHandler)
http.HandleFunc("/ui/init", initialState)

// Initilize the servers.
go serveGRPC(grpcl)
Expand Down
8 changes: 4 additions & 4 deletions dashboard/build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"main.css": "static/css/main.d27e6f36.css",
"main.css.map": "static/css/main.d27e6f36.css.map",
"main.js": "static/js/main.5f944870.js",
"main.js.map": "static/js/main.5f944870.js.map",
"main.css": "static/css/main.31d8e6df.css",
"main.css.map": "static/css/main.31d8e6df.css.map",
"main.js": "static/js/main.7e044d7e.js",
"main.js.map": "static/js/main.7e044d7e.js.map",
"static/media/addNodeIcon.png": "static/media/addNodeIcon.a1a2d01b.png",
"static/media/backIcon.png": "static/media/backIcon.dd0baa69.png",
"static/media/connectIcon.png": "static/media/connectIcon.d5267b8d.png",
Expand Down
2 changes: 1 addition & 1 deletion dashboard/build/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="shortcut icon" href="/favicon.ico"><script>window.SERVER_URL=location.protocol+"//"+location.hostname+(location.port?":"+location.port:"")</script><title>Dgraph UI</title><link href="/static/css/main.d27e6f36.css" rel="stylesheet"></head><body><div id="root"></div><script type="text/javascript" src="/static/js/main.5f944870.js"></script></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="shortcut icon" href="/favicon.ico"><script>window.SERVER_URL=location.protocol+"//"+location.hostname+(location.port?":"+location.port:"")</script><script src="https://use.fontawesome.com/bd4789f5b8.js"></script><title>Dgraph UI</title><link href="/static/css/main.31d8e6df.css" rel="stylesheet"></head><body><div id="root"></div><script type="text/javascript" src="/static/js/main.7e044d7e.js"></script></body></html>

Large diffs are not rendered by default.

52 changes: 0 additions & 52 deletions dashboard/build/static/js/main.5f944870.js

This file was deleted.

53 changes: 53 additions & 0 deletions dashboard/build/static/js/main.7e044d7e.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
"randomcolor": "^0.4.4",
"react": "^15.4.2",
"react-bootstrap": "^0.30.7",
"react-copy-to-clipboard": "^4.2.3",
"react-dom": "^15.4.2",
"react-redux": "^5.0.3",
"react-router-dom": "^4.0.0",
"redux": "^3.6.0",
"redux-persist": "^4.4.2",
"redux-thunk": "^2.2.0",
Expand Down
1 change: 1 addition & 0 deletions dashboard/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<script>
window.SERVER_URL = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '');
</script>
<script src="https://use.fontawesome.com/bd4789f5b8.js"></script>
<title>Dgraph UI</title>
</head>

Expand Down
Loading

0 comments on commit cea0622

Please sign in to comment.