<commit_before>// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bufio"
	"cmd/internal/browser"
	"flag"
	"fmt"
	"html/template"
	"internal/trace"
	"io"
	"log"
	"net"
	"net/http"
	"os"
	"runtime"
	"runtime/debug"
	"sync"

	_ "net/http/pprof" // Required to use pprof
)

const usageMessage = "" +
	`Usage of 'go tool trace':
Given a trace file produced by 'go test':
	go test -trace=trace.out pkg

Open a web browser displaying trace:
	go tool trace [flags] [pkg.test] trace.out

Generate a pprof-like profile from the trace:
    go tool trace -pprof=TYPE [pkg.test] trace.out

[pkg.test] argument is required for traces produced by Go 1.6 and below.
Go 1.7 does not require the binary argument.

Supported profile types are:
    - net: network blocking profile
    - sync: synchronization blocking profile
    - syscall: syscall blocking profile
    - sched: scheduler latency profile

Flags:
	-http=addr: HTTP service address (e.g., ':6060')
	-pprof=type: print a pprof-like profile instead
	-d: print debug info such as parsed events

Note that while the various profiles available when launching
'go tool trace' work on every browser, the trace viewer itself
(the 'view trace' page) comes from the Chrome/Chromium project
and is only actively tested on that browser.
`

var (
	httpFlag  = flag.String("http", "localhost:0", "HTTP service address (e.g., ':6060')")
	pprofFlag = flag.String("pprof", "", "print a pprof-like profile instead")
	debugFlag = flag.Bool("d", false, "print debug information such as parsed events list")

	// The binary file name, left here for serveSVGProfile.
	programBinary string
	traceFile     string
)

func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, usageMessage)
		os.Exit(2)
	}
	flag.Parse()

	// Go 1.7 traces embed symbol info and does not require the binary.
	// But we optionally accept binary as first arg for Go 1.5 traces.
	switch flag.NArg() {
	case 1:
		traceFile = flag.Arg(0)
	case 2:
		programBinary = flag.Arg(0)
		traceFile = flag.Arg(1)
	default:
		flag.Usage()
	}

	var pprofFunc func(io.Writer, *http.Request) error
	switch *pprofFlag {
	case "net":
		pprofFunc = pprofByGoroutine(computePprofIO)
	case "sync":
		pprofFunc = pprofByGoroutine(computePprofBlock)
	case "syscall":
		pprofFunc = pprofByGoroutine(computePprofSyscall)
	case "sched":
		pprofFunc = pprofByGoroutine(computePprofSched)
	}
	if pprofFunc != nil {
		if err := pprofFunc(os.Stdout, &http.Request{}); err != nil {
			dief("failed to generate pprof: %v\n", err)
		}
		os.Exit(0)
	}
	if *pprofFlag != "" {
		dief("unknown pprof type %s\n", *pprofFlag)
	}

	ln, err := net.Listen("tcp", *httpFlag)
	if err != nil {
		dief("failed to create server socket: %v\n", err)
	}

	log.Print("Parsing trace...")
	res, err := parseTrace()
	if err != nil {
		dief("%v\n", err)
	}

	if *debugFlag {
		trace.Print(res.Events)
		os.Exit(0)
	}
	reportMemoryUsage("after parsing trace")
	debug.FreeOSMemory()

	log.Print("Splitting trace...")
	ranges = splitTrace(res)
	reportMemoryUsage("after spliting trace")
	debug.FreeOSMemory()

	addr := "http://" + ln.Addr().String()
	log.Printf("Opening browser. Trace viewer is listening on %s", addr)
	browser.Open(addr)

	// Start http server.
	http.HandleFunc("/", httpMain)
	err = http.Serve(ln, nil)
	dief("failed to start http server: %v\n", err)
}

var ranges []Range

var loader struct {
	once sync.Once
	res  trace.ParseResult
	err  error
}

// parseEvents is a compatibility wrapper that returns only
// the Events part of trace.ParseResult returned by parseTrace.
func parseEvents() ([]*trace.Event, error) {
	res, err := parseTrace()
	if err != nil {
		return nil, err
	}
	return res.Events, err
}

func parseTrace() (trace.ParseResult, error) {
	loader.once.Do(func() {
		tracef, err := os.Open(traceFile)
		if err != nil {
			loader.err = fmt.Errorf("failed to open trace file: %v", err)
			return
		}
		defer tracef.Close()

		// Parse and symbolize.
		res, err := trace.Parse(bufio.NewReader(tracef), programBinary)
		if err != nil {
			loader.err = fmt.Errorf("failed to parse trace: %v", err)
			return
		}
		loader.res = res
	})
	return loader.res, loader.err
}

// httpMain serves the starting page.
func httpMain(w http.ResponseWriter, r *http.Request) {
	if err := templMain.Execute(w, ranges); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

var templMain = template.Must(template.New("").Parse(`

cmd/trace: the Go trace event viewer

  This web server provides various visualizations of an event log gathered during
  the execution of a Go program that uses the runtime/trace package.

Event timelines for running goroutines

{{if $}}

  Large traces are split into multiple sections of equal data size
  (not duration) to avoid overwhelming the visualizer.

	{{range $e := $}}

View trace ({{$e.Name}})

	{{end}}

{{else}}

View trace

{{end}}

  This view displays a timeline for each of the GOMAXPROCS logical
  processors, showing which goroutine (if any) was running on that
  logical processor at each moment.

  Each goroutine has an identifying number (e.g. G123), main function,
  and color.

  A colored bar represents an uninterrupted span of execution.

  Execution of a goroutine may migrate from one logical processor to another,
  causing a single colored bar to be horizontally continuous but
  vertically displaced.

  Clicking on a span reveals information about it, such as its
  duration, its causal predecessors and successors, and the stack trace
  at the final moment when it yielded the logical processor, for example
  because it made a system call or tried to acquire a mutex.

  Directly underneath each bar, a smaller bar or more commonly a fine
  vertical line indicates an event occuring during its execution.
  Some of these are related to garbage collection; most indicate that
  a goroutine yielded its logical processor but then immediately resumed execution
  on the same logical processor. Clicking on the event displays the stack trace
  at the moment it occurred.

  The causal relationships between spans of goroutine execution
  can be displayed by clicking the Flow Events button at the top.

  At the top ("STATS"), there are three additional timelines that
  display statistical information.

  "Goroutines" is a time series of the count of existing goroutines;
  clicking on it displays their breakdown by state at that moment:
  running, runnable, or waiting.

  "Heap" is a time series of the amount of heap memory allocated (in orange)
  and (in green) the allocation limit at which the next GC cycle will begin.

  "Threads" shows the number of kernel threads in existence: there is
  always one kernel thread per logical processor, and additional threads
  are created for calls to non-Go code such as a system call or a
  function written in C.

  Above the event trace for the first logical processor are
  traces for various runtime-internal events.

  The "GC" bar shows when the garbage collector is running, and in which stage.
  Garbage collection may temporarily affect all the logical processors
  and the other metrics.

  The "Network", "Timers", and "Syscalls" traces indicate events in
  the runtime that cause goroutines to wake up.

  The visualization allows you to navigate events at scales ranging from several
  seconds to a handful of nanoseconds.

  Consult the documentation for the Chromium Trace Event Profiling Tool
  for help navigating the view.

Goroutine analysis

  This view displays information about each set of goroutines that
  shares the same main function.

  Clicking on a main function shows links to the four types of
  blocking profile (see below) applied to that subset of goroutines.

  It also shows a table of specific goroutine instances, with various
  execution statistics and a link to the event timeline for each one.

  The timeline displays only the selected goroutine and any others it
  interacts with via block/unblock events. (The timeline is
  goroutine-oriented rather than logical processor-oriented.)

Profiles

  Each link below displays a global profile in zoomable graph form as
  produced by pprof's "web" command.

  In addition there is a link to download the profile for offline
  analysis with pprof.

  All four profiles represent causes of delay that prevent a goroutine
  from running on a logical processor: because it was waiting for the network,
  for a synchronization operation on a mutex or channel, for a system call,
  or for a logical processor to become available.

Network blocking profile (⬇)

Synchronization blocking profile (⬇)

Syscall blocking profile (⬇)

Scheduler latency profile (⬇)

User-defined tasks and regions

  The trace API allows a target program to annotate a region of code
  within a goroutine, such as a key function, so that its performance
  can be analyzed.

  Log events may be
  associated with a region to record progress and relevant values.

  The API also allows annotation of higher-level
  tasks,
  which may involve work across many goroutines.

  The links below display, for each region and task, a histogram of its execution times.

  Each histogram bucket contains a sample trace that records the
  sequence of events such as goroutine creations, log events, and
  subregion start/end times.

  For each task, you can click through to a logical-processor or
  goroutine-oriented view showing the tasks and regions on the
  timeline.

  Such information may help uncover which steps in a region are
  unexpectedly slow, or reveal relationships between the data values
  logged in a request and its running time.

User-defined tasks

User-defined regions

Garbage collection metrics

Minimum mutator utilization

  This chart indicates the maximum GC pause time (the largest x value
  for which y is zero), and more generally, the fraction of time that
  the processors are available to application goroutines ("mutators"),
  for any time window of a specified size, in the worst case.

`))

func dief(msg string, args ...any) {
	fmt.Fprintf(os.Stderr, msg, args...)
	os.Exit(1)
}

var debugMemoryUsage bool

func init() {
	v := os.Getenv("DEBUG_MEMORY_USAGE")
	debugMemoryUsage = v != ""
}

func reportMemoryUsage(msg string) {
	if !debugMemoryUsage {
		return
	}
	var s runtime.MemStats
	runtime.ReadMemStats(&s)
	w := os.Stderr
	fmt.Fprintf(w, "%s\n", msg)
	fmt.Fprintf(w, " Alloc:\t%d Bytes\n", s.Alloc)
	fmt.Fprintf(w, " Sys:\t%d Bytes\n", s.Sys)
	fmt.Fprintf(w, " HeapReleased:\t%d Bytes\n", s.HeapReleased)
	fmt.Fprintf(w, " HeapSys:\t%d Bytes\n", s.HeapSys)
	fmt.Fprintf(w, " HeapInUse:\t%d Bytes\n", s.HeapInuse)
	fmt.Fprintf(w, " HeapAlloc:\t%d Bytes\n", s.HeapAlloc)
	var dummy string
	fmt.Printf("Enter to continue...")
	fmt.Scanf("%s", &dummy)
}
<commit_msg>cmd/trace: fix typo in web documentation<commit_after>// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bufio"
	"cmd/internal/browser"
	"flag"
	"fmt"
	"html/template"
	"internal/trace"
	"io"
	"log"
	"net"
	"net/http"
	"os"
	"runtime"
	"runtime/debug"
	"sync"

	_ "net/http/pprof" // Required to use pprof
)

const usageMessage = "" +
	`Usage of 'go tool trace':
Given a trace file produced by 'go test':
	go test -trace=trace.out pkg

Open a web browser displaying trace:
	go tool trace [flags] [pkg.test] trace.out

Generate a pprof-like profile from the trace:
    go tool trace -pprof=TYPE [pkg.test] trace.out

[pkg.test] argument is required for traces produced by Go 1.6 and below.
Go 1.7 does not require the binary argument.

Supported profile types are:
    - net: network blocking profile
    - sync: synchronization blocking profile
    - syscall: syscall blocking profile
    - sched: scheduler latency profile

Flags:
	-http=addr: HTTP service address (e.g., ':6060')
	-pprof=type: print a pprof-like profile instead
	-d: print debug info such as parsed events

Note that while the various profiles available when launching
'go tool trace' work on every browser, the trace viewer itself
(the 'view trace' page) comes from the Chrome/Chromium project
and is only actively tested on that browser.
`

var (
	httpFlag  = flag.String("http", "localhost:0", "HTTP service address (e.g., ':6060')")
	pprofFlag = flag.String("pprof", "", "print a pprof-like profile instead")
	debugFlag = flag.Bool("d", false, "print debug information such as parsed events list")

	// The binary file name, left here for serveSVGProfile.
	programBinary string
	traceFile     string
)

func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, usageMessage)
		os.Exit(2)
	}
	flag.Parse()

	// Go 1.7 traces embed symbol info and does not require the binary.
	// But we optionally accept binary as first arg for Go 1.5 traces.
	switch flag.NArg() {
	case 1:
		traceFile = flag.Arg(0)
	case 2:
		programBinary = flag.Arg(0)
		traceFile = flag.Arg(1)
	default:
		flag.Usage()
	}

	var pprofFunc func(io.Writer, *http.Request) error
	switch *pprofFlag {
	case "net":
		pprofFunc = pprofByGoroutine(computePprofIO)
	case "sync":
		pprofFunc = pprofByGoroutine(computePprofBlock)
	case "syscall":
		pprofFunc = pprofByGoroutine(computePprofSyscall)
	case "sched":
		pprofFunc = pprofByGoroutine(computePprofSched)
	}
	if pprofFunc != nil {
		if err := pprofFunc(os.Stdout, &http.Request{}); err != nil {
			dief("failed to generate pprof: %v\n", err)
		}
		os.Exit(0)
	}
	if *pprofFlag != "" {
		dief("unknown pprof type %s\n", *pprofFlag)
	}

	ln, err := net.Listen("tcp", *httpFlag)
	if err != nil {
		dief("failed to create server socket: %v\n", err)
	}

	log.Print("Parsing trace...")
	res, err := parseTrace()
	if err != nil {
		dief("%v\n", err)
	}

	if *debugFlag {
		trace.Print(res.Events)
		os.Exit(0)
	}
	reportMemoryUsage("after parsing trace")
	debug.FreeOSMemory()

	log.Print("Splitting trace...")
	ranges = splitTrace(res)
	reportMemoryUsage("after spliting trace")
	debug.FreeOSMemory()

	addr := "http://" + ln.Addr().String()
	log.Printf("Opening browser. Trace viewer is listening on %s", addr)
	browser.Open(addr)

	// Start http server.
	http.HandleFunc("/", httpMain)
	err = http.Serve(ln, nil)
	dief("failed to start http server: %v\n", err)
}

var ranges []Range

var loader struct {
	once sync.Once
	res  trace.ParseResult
	err  error
}

// parseEvents is a compatibility wrapper that returns only
// the Events part of trace.ParseResult returned by parseTrace.
func parseEvents() ([]*trace.Event, error) {
	res, err := parseTrace()
	if err != nil {
		return nil, err
	}
	return res.Events, err
}

func parseTrace() (trace.ParseResult, error) {
	loader.once.Do(func() {
		tracef, err := os.Open(traceFile)
		if err != nil {
			loader.err = fmt.Errorf("failed to open trace file: %v", err)
			return
		}
		defer tracef.Close()

		// Parse and symbolize.
		res, err := trace.Parse(bufio.NewReader(tracef), programBinary)
		if err != nil {
			loader.err = fmt.Errorf("failed to parse trace: %v", err)
			return
		}
		loader.res = res
	})
	return loader.res, loader.err
}

// httpMain serves the starting page.
func httpMain(w http.ResponseWriter, r *http.Request) {
	if err := templMain.Execute(w, ranges); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

var templMain = template.Must(template.New("").Parse(`

cmd/trace: the Go trace event viewer

  This web server provides various visualizations of an event log gathered during
  the execution of a Go program that uses the runtime/trace package.

Event timelines for running goroutines

{{if $}}

  Large traces are split into multiple sections of equal data size
  (not duration) to avoid overwhelming the visualizer.

	{{range $e := $}}

View trace ({{$e.Name}})

	{{end}}

{{else}}

View trace

{{end}}

  This view displays a timeline for each of the GOMAXPROCS logical
  processors, showing which goroutine (if any) was running on that
  logical processor at each moment.

  Each goroutine has an identifying number (e.g. G123), main function,
  and color.

  A colored bar represents an uninterrupted span of execution.

  Execution of a goroutine may migrate from one logical processor to another,
  causing a single colored bar to be horizontally continuous but
  vertically displaced.

  Clicking on a span reveals information about it, such as its
  duration, its causal predecessors and successors, and the stack trace
  at the final moment when it yielded the logical processor, for example
  because it made a system call or tried to acquire a mutex.

  Directly underneath each bar, a smaller bar or more commonly a fine
  vertical line indicates an event occurring during its execution.
  Some of these are related to garbage collection; most indicate that
  a goroutine yielded its logical processor but then immediately resumed execution
  on the same logical processor. Clicking on the event displays the stack trace
  at the moment it occurred.

  The causal relationships between spans of goroutine execution
  can be displayed by clicking the Flow Events button at the top.

  At the top ("STATS"), there are three additional timelines that
  display statistical information.

  "Goroutines" is a time series of the count of existing goroutines;
  clicking on it displays their breakdown by state at that moment:
  running, runnable, or waiting.

  "Heap" is a time series of the amount of heap memory allocated (in orange)
  and (in green) the allocation limit at which the next GC cycle will begin.

  "Threads" shows the number of kernel threads in existence: there is
  always one kernel thread per logical processor, and additional threads
  are created for calls to non-Go code such as a system call or a
  function written in C.

  Above the event trace for the first logical processor are
  traces for various runtime-internal events.

  The "GC" bar shows when the garbage collector is running, and in which stage.
  Garbage collection may temporarily affect all the logical processors
  and the other metrics.

  The "Network", "Timers", and "Syscalls" traces indicate events in
  the runtime that cause goroutines to wake up.

  The visualization allows you to navigate events at scales ranging from several
  seconds to a handful of nanoseconds.

  Consult the documentation for the Chromium Trace Event Profiling Tool
  for help navigating the view.

Goroutine analysis

  This view displays information about each set of goroutines that
  shares the same main function.

  Clicking on a main function shows links to the four types of
  blocking profile (see below) applied to that subset of goroutines.

  It also shows a table of specific goroutine instances, with various
  execution statistics and a link to the event timeline for each one.

  The timeline displays only the selected goroutine and any others it
  interacts with via block/unblock events. (The timeline is
  goroutine-oriented rather than logical processor-oriented.)

Profiles

  Each link below displays a global profile in zoomable graph form as
  produced by pprof's "web" command.

  In addition there is a link to download the profile for offline
  analysis with pprof.

  All four profiles represent causes of delay that prevent a goroutine
  from running on a logical processor: because it was waiting for the network,
  for a synchronization operation on a mutex or channel, for a system call,
  or for a logical processor to become available.

Network blocking profile (⬇)

Synchronization blocking profile (⬇)

Syscall blocking profile (⬇)

Scheduler latency profile (⬇)

User-defined tasks and regions

  The trace API allows a target program to annotate a region of code
  within a goroutine, such as a key function, so that its performance
  can be analyzed.

  Log events may be
  associated with a region to record progress and relevant values.

  The API also allows annotation of higher-level
  tasks,
  which may involve work across many goroutines.

  The links below display, for each region and task, a histogram of its execution times.

  Each histogram bucket contains a sample trace that records the
  sequence of events such as goroutine creations, log events, and
  subregion start/end times.

  For each task, you can click through to a logical-processor or
  goroutine-oriented view showing the tasks and regions on the
  timeline.

  Such information may help uncover which steps in a region are
  unexpectedly slow, or reveal relationships between the data values
  logged in a request and its running time.

User-defined tasks

User-defined regions

Garbage collection metrics

Minimum mutator utilization

  This chart indicates the maximum GC pause time (the largest x value
  for which y is zero), and more generally, the fraction of time that
  the processors are available to application goroutines ("mutators"),
  for any time window of a specified size, in the worst case.

`))

func dief(msg string, args ...any) {
	fmt.Fprintf(os.Stderr, msg, args...)
	os.Exit(1)
}

var debugMemoryUsage bool

func init() {
	v := os.Getenv("DEBUG_MEMORY_USAGE")
	debugMemoryUsage = v != ""
}

func reportMemoryUsage(msg string) {
	if !debugMemoryUsage {
		return
	}
	var s runtime.MemStats
	runtime.ReadMemStats(&s)
	w := os.Stderr
	fmt.Fprintf(w, "%s\n", msg)
	fmt.Fprintf(w, " Alloc:\t%d Bytes\n", s.Alloc)
	fmt.Fprintf(w, " Sys:\t%d Bytes\n", s.Sys)
	fmt.Fprintf(w, " HeapReleased:\t%d Bytes\n", s.HeapReleased)
	fmt.Fprintf(w, " HeapSys:\t%d Bytes\n", s.HeapSys)
	fmt.Fprintf(w, " HeapInUse:\t%d Bytes\n", s.HeapInuse)
	fmt.Fprintf(w, " HeapAlloc:\t%d Bytes\n", s.HeapAlloc)
	var dummy string
	fmt.Printf("Enter to continue...")
	fmt.Scanf("%s", &dummy)
}
<|endoftext|>


<commit_before>package core

import (
	"fmt"
	"github.com/MG-RAST/AWE/lib/conf"
	"os"
	"time"
)

const (
	WORK_STAT_QUEUED      = "queued"
	WORK_STAT_CHECKOUT    = "checkout"
	WORK_STAT_SUSPEND     = "suspend"
	WORK_STAT_DONE        = "done"
	WORK_STAT_FAIL        = "fail"
	WORK_STAT_PREPARED    = "prepared"
	WORK_STAT_COMPUTED    = "computed"
	WORK_STAT_DISCARDED   = "discarded"
	WORK_STAT_PROXYQUEUED = "proxyqueued"
)

type Workunit struct {
	Id           string            `bson:"wuid" json:"wuid"`
	Info         *Info             `bson:"info" json:"info"`
	Inputs       IOmap             `bson:"inputs" json:"inputs"`
	Outputs      IOmap             `bson:"outputs" json:"outputs"`
	Predata      IOmap             `bson:"predata" json:"predata"`
	Cmd          *Command          `bson:"cmd" json:"cmd"`
	Rank         int               `bson:"rank" json:"rank"`
	TotalWork    int               `bson:"totalwork" json:"totalwork"`
	Partition    *PartInfo         `bson:"part" json:"part"`
	State        string            `bson:"state" json:"state"`
	Failed       int               `bson:"failed" json:"failed"`
	CheckoutTime time.Time         `bson:"checkout_time" json:"checkout_time"`
	Client       string            `bson:"client" json:"client"`
	ComputeTime  int               `bson:"computetime" json:"computetime"`
	Notes        string            `bson:"-" json:"-"`
	UserAttr     map[string]string `bson:"userattr" json:"userattr"`
}

func NewWorkunit(task *Task, rank int) *Workunit {
	return &Workunit{
		Id:        fmt.Sprintf("%s_%d", task.Id, rank),
		Info:      task.Info,
		Inputs:    task.Inputs,
		Outputs:   task.Outputs,
		Predata:   task.Predata,
		Cmd:       task.Cmd,
		Rank:      rank,
		TotalWork: task.TotalWork, //keep this info in workunit for load balancing
		Partition: task.Partition,
		State:     WORK_STAT_QUEUED,
		Failed:    0,
		UserAttr:  task.UserAttr,
	}
}

func (work *Workunit) Mkdir() (err error) {
	os.RemoveAll(work.Path()) // delete workdir just in case it exists

	err = os.MkdirAll(work.Path(), 0777)
	if err != nil {
		return
	}
	return
}

func (work *Workunit) RemoveDir() (err error) {
	err = os.RemoveAll(work.Path())
	if err != nil {
		return
	}
	return
}

func (work *Workunit) Path() string {
	id := work.Id
	return fmt.Sprintf("%s/%s/%s/%s/%s", conf.WORK_PATH, id[0:2], id[2:4], id[4:6], id)
}

func (work *Workunit) CDworkpath() (err error) {
	return os.Chdir(work.Path())
}

func (work *Workunit) IndexType() (indextype string) {
	return work.Partition.Index
}

//calculate the range of data part
//algorithm: try to evenly distribute indexed parts to workunits
//e.g. totalWork=4, totalParts=10, then each workunits have parts 3,3,2,2
func (work *Workunit) Part() (part string) {
	if work.Rank == 0 {
		return ""
	}
	partsize := work.Partition.TotalIndex / work.TotalWork //floor
	remainder := work.Partition.TotalIndex % work.TotalWork
	var start, end int
	if work.Rank <= remainder {
		start = (partsize+1)*(work.Rank-1) + 1
		end = start + partsize
	} else {
		start = (partsize+1)*remainder + partsize*(work.Rank-remainder-1) + 1
		end = start + partsize - 1
	}
	if start == end {
		part = fmt.Sprintf("%d", start)
	} else {
		part = fmt.Sprintf("%d-%d", start, end)
	}
	return
}
<commit_msg>add comment<commit_after>package core

import (
	"fmt"
	"github.com/MG-RAST/AWE/lib/conf"
	"os"
	"time"
)

const (
	WORK_STAT_QUEUED      = "queued"
	WORK_STAT_CHECKOUT    = "checkout"
	WORK_STAT_SUSPEND     = "suspend"
	WORK_STAT_DONE        = "done"
	WORK_STAT_FAIL        = "fail"
	WORK_STAT_PREPARED    = "prepared"
	WORK_STAT_COMPUTED    = "computed"
	WORK_STAT_DISCARDED   = "discarded"
	WORK_STAT_PROXYQUEUED = "proxyqueued"
)

type Workunit struct {
	Id           string            `bson:"wuid" json:"wuid"`
	Info         *Info             `bson:"info" json:"info"`
	Inputs       IOmap             `bson:"inputs" json:"inputs"`
	Outputs      IOmap             `bson:"outputs" json:"outputs"`
	Predata      IOmap             `bson:"predata" json:"predata"`
	Cmd          *Command          `bson:"cmd" json:"cmd"`
	Rank         int               `bson:"rank" json:"rank"`
	TotalWork    int               `bson:"totalwork" json:"totalwork"`
	Partition    *PartInfo         `bson:"part" json:"part"`
	State        string            `bson:"state" json:"state"`
	Failed       int               `bson:"failed" json:"failed"`
	CheckoutTime time.Time         `bson:"checkout_time" json:"checkout_time"`
	Client       string            `bson:"client" json:"client"`
	ComputeTime  int               `bson:"computetime" json:"computetime"`
	Notes        string            `bson:"-" json:"-"`
	UserAttr     map[string]string `bson:"userattr" json:"userattr"`
}

func NewWorkunit(task *Task, rank int) *Workunit {
	return &Workunit{
		Id:        fmt.Sprintf("%s_%d", task.Id, rank),
		Info:      task.Info,
		Inputs:    task.Inputs,
		Outputs:   task.Outputs,
		Predata:   task.Predata,
		Cmd:       task.Cmd,
		Rank:      rank,
		TotalWork: task.TotalWork, //keep this info in workunit for load balancing
		Partition: task.Partition,
		State:     WORK_STAT_QUEUED,
		Failed:    0,
		UserAttr:  task.UserAttr,
	}
}

func (work *Workunit) Mkdir() (err error) {
	// delete workdir just in case it exists; will not work if awe-client is not in docker container AND tasks are in container
	os.RemoveAll(work.Path())

	err = os.MkdirAll(work.Path(), 0777)
	if err != nil {
		return
	}
	return
}

func (work *Workunit) RemoveDir() (err error) {
	err = os.RemoveAll(work.Path())
	if err != nil {
		return
	}
	return
}

func (work *Workunit) Path() string {
	id := work.Id
	return fmt.Sprintf("%s/%s/%s/%s/%s", conf.WORK_PATH, id[0:2], id[2:4], id[4:6], id)
}

func (work *Workunit) CDworkpath() (err error) {
	return os.Chdir(work.Path())
}

func (work *Workunit) IndexType() (indextype string) {
	return work.Partition.Index
}

//calculate the range of data part
//algorithm: try to evenly distribute indexed parts to workunits
//e.g. totalWork=4, totalParts=10, then each workunits have parts 3,3,2,2
func (work *Workunit) Part() (part string) {
	if work.Rank == 0 {
		return ""
	}
	partsize := work.Partition.TotalIndex / work.TotalWork //floor
	remainder := work.Partition.TotalIndex % work.TotalWork
	var start, end int
	if work.Rank <= remainder {
		start = (partsize+1)*(work.Rank-1) + 1
		end = start + partsize
	} else {
		start = (partsize+1)*remainder + partsize*(work.Rank-remainder-1) + 1
		end = start + partsize - 1
	}
	if start == end {
		part = fmt.Sprintf("%d", start)
	} else {
		part = fmt.Sprintf("%d-%d", start, end)
	}
	return
}
<|endoftext|>


<commit_before>package class

import (
    "strings"
)

type MemberDescriptorParser struct {
    descriptor  string
    offset      int
    md          *MethodDescriptor
}

func newMemberDescriptorParser(descriptor string) (*MemberDescriptorParser) {
    return &MemberDescriptorParser{descriptor: descriptor}
}

func (self *MemberDescriptorParser) parse() (*MethodDescriptor) {
    self.md = &MethodDescriptor{}
    self.startParams()
    self.parseParameterTypes()
    self.endParams()
    self.parseReturnType()
    self.finish()
    return self.md
}

func (self *MemberDescriptorParser) startParams() {
    if self.readUint8() != '(' {
        self.causePanic()
    }
}
func (self *MemberDescriptorParser) endParams() {
    if self.readUint8() != ')' {
        self.causePanic()
    }
}
func (self *MemberDescriptorParser) finish() {
    if self.offset != len(self.descriptor) {
        self.causePanic()
    }
}

func (self *MemberDescriptorParser) readUint8() uint8 {
    b := self.descriptor[self.offset]
    self.offset++
    return b
}
func (self *MemberDescriptorParser) unreadUint8() {
    self.offset--
}

func (self *MemberDescriptorParser) parseParameterTypes() {
    for {
        t := self.readFieldType()
        if t != nil {
            self.md.addParameterType(t)
        } else {
            break
        }
    }
}
func (self *MemberDescriptorParser) parseReturnType() {
    t := self.readFieldType()
    if t != nil {
        self.md.returnType = t
    } else {
        self.causePanic()
    }
}

func (self *MemberDescriptorParser) readFieldType() (*FieldType) {
    switch self.readUint8() {
    case 'B': return baseTypeB
    case 'C': return baseTypeC
    case 'D': return baseTypeD
    case 'F': return baseTypeF
    case 'I': return baseTypeI
    case 'J': return baseTypeJ
    case 'S': return baseTypeS
    case 'Z': return baseTypeZ
    case 'V': return baseTypeV
    case 'L': return self.readObjectType()
    case '[': return self.readArrayType()
    default:
        self.unreadUint8()
        return nil
    }
}
func (self *MemberDescriptorParser) readObjectType() (*FieldType) {
    unread := self.descriptor[self.offset:]
    semicolonIndex := strings.IndexRune(unread, ';')
    if semicolonIndex == -1 {
        self.causePanic()
        return nil
    } else {
        objStart := self.offset - 1
        objEnd := self.offset + semicolonIndex + 1
        self.offset = objEnd
        descriptor := self.descriptor[objStart: objEnd]
        return &FieldType{descriptor}
    }
}
func (self *MemberDescriptorParser) readArrayType() (*FieldType) {
    arrStart := self.offset - 1
    self.readFieldType()
    arrEnd := self.offset
    descriptor := self.descriptor[arrStart: arrEnd]
    return &FieldType{descriptor}
}

func (self *MemberDescriptorParser) causePanic() {
    panic("BAD descriptor: " + self.descriptor)
}
<commit_msg>rename methods<commit_after>package class

import (
    "strings"
)

type MemberDescriptorParser struct {
    descriptor  string
    offset      int
    md          *MethodDescriptor
}

func newMemberDescriptorParser(descriptor string) (*MemberDescriptorParser) {
    return &MemberDescriptorParser{descriptor: descriptor}
}

func (self *MemberDescriptorParser) parse() (*MethodDescriptor) {
    self.md = &MethodDescriptor{}
    self.startParams()
    self.parseParamTypes()
    self.endParams()
    self.parseReturnType()
    self.finish()
    return self.md
}

func (self *MemberDescriptorParser) startParams() {
    if self.readUint8() != '(' {
        self.causePanic()
    }
}
func (self *MemberDescriptorParser) endParams() {
    if self.readUint8() != ')' {
        self.causePanic()
    }
}
func (self *MemberDescriptorParser) finish() {
    if self.offset != len(self.descriptor) {
        self.causePanic()
    }
}

func (self *MemberDescriptorParser) readUint8() uint8 {
    b := self.descriptor[self.offset]
    self.offset++
    return b
}
func (self *MemberDescriptorParser) unreadUint8() {
    self.offset--
}

func (self *MemberDescriptorParser) parseParamTypes() {
    for {
        t := self.parseFieldType()
        if t != nil {
            self.md.addParameterType(t)
        } else {
            break
        }
    }
}
func (self *MemberDescriptorParser) parseReturnType() {
    t := self.parseFieldType()
    if t != nil {
        self.md.returnType = t
    } else {
        self.causePanic()
    }
}

func (self *MemberDescriptorParser) parseFieldType() (*FieldType) {
    switch self.readUint8() {
    case 'B': return baseTypeB
    case 'C': return baseTypeC
    case 'D': return baseTypeD
    case 'F': return baseTypeF
    case 'I': return baseTypeI
    case 'J': return baseTypeJ
    case 'S': return baseTypeS
    case 'Z': return baseTypeZ
    case 'V': return baseTypeV
    case 'L': return self.parseObjectType()
    case '[': return self.parseArrayType()
    default:
        self.unreadUint8()
        return nil
    }
}
func (self *MemberDescriptorParser) parseObjectType() (*FieldType) {
    unread := self.descriptor[self.offset:]
    semicolonIndex := strings.IndexRune(unread, ';')
    if semicolonIndex == -1 {
        self.causePanic()
        return nil
    } else {
        objStart := self.offset - 1
        objEnd := self.offset + semicolonIndex + 1
        self.offset = objEnd
        descriptor := self.descriptor[objStart: objEnd]
        return &FieldType{descriptor}
    }
}
func (self *MemberDescriptorParser) parseArrayType() (*FieldType) {
    arrStart := self.offset - 1
    self.parseFieldType()
    arrEnd := self.offset
    descriptor := self.descriptor[arrStart: arrEnd]
    return &FieldType{descriptor}
}

func (self *MemberDescriptorParser) causePanic() {
    panic("BAD descriptor: " + self.descriptor)
}
<|endoftext|>


<commit_before>package interchange

import (
	"code.google.com/p/go.net/context"
	"fmt"
	"reflect"
	"testing"
	"time"
)

func TestIsValidTopic(t *testing.T) {
	expect_true := func(b bool, msg string) {
		if !b {
			t.Error(msg)
		}
	}

	expect_valid := func(topic []string) {
		expect_true(IsValidTopic(topic), fmt.Sprintf("Should have been valid: %q", topic))
	}
	expect_not_valid := func(topic []string) {
		expect_true(!IsValidTopic(topic), fmt.Sprintf("Should _not_ have been valid: %q", topic))
	}

	expect_valid([]string{"foo"})
	expect_valid([]string{"foo", "bar", "baz", "qux", "quz"})

	expect_not_valid([]string{})
	expect_not_valid([]string{""})
	expect_not_valid([]string{"."})
}

func TestAddSub(t *testing.T) {
	root_ctx, cancel_root := context.WithCancel(context.Background())
	root := newTopicNode(root_ctx, cancel_root, []string{"."})

	topic := []string{"foo", "bar"}
	new_node, _ := root.CreateChild(topic)

	death_notifications := make(chan []string)
	client_messages := make(chan Message)

	new_subscriber := new_node.AddSub(&subscription{
		Topic:    topic,
		Name:     "source",
		Deadline: time.Now().Add(time.Minute * 20),
		Client:   client_messages,
	}, death_notifications)

	for i := 0; i < 10; i += 1 {
		message_sent := Message{Source: fmt.Sprintf("test_%d", i)}
		new_subscriber.Sink <- message_sent

		select {
		case message_received := <-client_messages:
			if !reflect.DeepEqual(message_sent, message_received) {
				t.Error(fmt.Sprintf("(%d): Message received (%+v) did not match message expected (%+v)", i, message_received, message_sent))
			}
		default:
			t.Error(fmt.Sprintf("(%d): Expected message never received from subscriber", i))
		}
	}

	cancel_root()
	select {
	case notified_topic := <-death_notifications:
		if !reflect.DeepEqual(notified_topic, topic) {
			t.Error(fmt.Sprintf("Expected topic (%q) does not equal topic notified (%q)", topic, notified_topic))
		}
	case <-time.After(1 * time.Second):
		t.Error("Timed out waiting for notification of subscriber death.")
		return
	}

	select {
	case <-client_messages:
	default:
		t.Error("Client channel not closed by dying subscriber")
	}
}

func TestMaybeFindTopic(t *testing.T) {
	// Build a sample topicTrie
	ctx, cancel := context.WithCancel(context.Background())
	root := newTopicNode(ctx, cancel, []string{"."})

	add_child := func(parent *topicNode, new_child *topicNode) {
		parent.Children = append(parent.Children, new_child)
	}

	_foo := newTopicNode(ctx, cancel, []string{"foo"})
	_foo_bar := newTopicNode(ctx, cancel, []string{"bar"})
	add_child(_foo, _foo_bar)
	_foo_baz := newTopicNode(ctx, cancel, []string{"baz"})
	add_child(_foo, _foo_baz)
	add_child(root, _foo)

	_qux := newTopicNode(ctx, cancel, []string{"qux"})
	_qux__foo_bar := newTopicNode(ctx, cancel, []string{"foo", "bar"})
	add_child(_qux, _qux__foo_bar)
	add_child(root, _qux)

	child_expectation := func(path []string, node_returned, node_expected *topicNode) {
		if node_expected != node_returned {
			t.Error(fmt.Sprintf("For provided path (%v), MaybeFindTopic returned a node (%+v) other than the one which it should have (%+v).", path, node_returned, node_expected))
		}
	}

	path_expectation := func(path, rest_expected, rest_returned []string) {
		if !reflect.DeepEqual(rest_expected, rest_returned) {
			t.Error(fmt.Sprintf("For provided path (%q), MaybeFindTopic returned a name remainder (%q) when it should have been (%q).", path, rest_returned, rest_expected))
		}
	}

	// Find exact topics
	/******************/
	path := []string{"foo"}
	should_be_foo, rest, overlap := root.MaybeFindTopic(path)
	child_expectation(path, should_be_foo, _foo)
	path_expectation(path, []string{}, rest)
	path_expectation(path, []string{}, overlap)

	path = []string{"foo", "bar"}
	should_be_foo_bar, rest, overlap := root.MaybeFindTopic(path)
	child_expectation(path, should_be_foo_bar, _foo_bar)
	path_expectation(path, []string{}, rest)
	path_expectation(path, []string{}, overlap)

	path = []string{"foo", "baz"}
	should_be_foo_baz, rest, overlap := root.MaybeFindTopic(path)
	child_expectation(path, should_be_foo_baz, _foo_baz)
	path_expectation(path, []string{}, rest)
	path_expectation(path, []string{}, overlap)

	path = []string{"qux"}
	should_be_qux, rest, overlap := root.MaybeFindTopic(path)
	child_expectation(path, should_be_qux, _qux)
	path_expectation(path, []string{}, rest)
	path_expectation(path, []string{}, overlap)

	path = []string{"qux", "foo", "bar"}
	should_be_qux__foo_bar, rest, overlap := root.MaybeFindTopic(path)
	child_expectation(path, should_be_qux__foo_bar, _qux__foo_bar)
	path_expectation(path, []string{}, rest)
	path_expectation(path, []string{}, overlap)

	// Return rest with found parent
	/******************************/
	path = []string{"foo", "bar", "baz"}
	should_be_foo_bar, rest, overlap = root.MaybeFindTopic(path)
	child_expectation(path, should_be_foo_bar, _foo_bar)
	path_expectation(path, []string{"baz"}, rest)
	path_expectation(path, []string{}, overlap)

	// Return rest with overlapping parent
	/************************************/
	path = []string{"qux", "foo", "baz"}
	should_be_qux__foo_bar, rest, overlap = root.MaybeFindTopic(path)
	child_expectation(path, should_be_qux__foo_bar, _qux__foo_bar)
	path_expectation(path, []string{"foo", "baz"}, rest)
	path_expectation(path, []string{"foo"}, overlap)
}

func TestCreateChild(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	root := newTopicNode(ctx, cancel, []string{"."})

	bar_baz_path := []string{"bar", "baz"}
	bar_baz, err := root.CreateChild(bar_baz_path)
	if err != nil {
		t.Error("CreateChild returned error when topicNode creation was expected.")
	}
	if !reflect.DeepEqual(bar_baz.Name, []string{"bar", "baz"}) {
		t.Error(fmt.Sprintf("CreateChild returned child with incorrect name (%q) vs. expected (%q).", bar_baz.Name, bar_baz_path))
	}
	if len(root.Children) != 1 {
		t.Error(fmt.Sprintf("CreateChild created %d children when 1 was expected.", len(root.Children)))
	}
	if child_node := root.Children[0]; bar_baz != child_node {
		t.Error(fmt.Sprintf("CreateChild returned a node (%+v) other than the one which it stored (%+v).", bar_baz, child_node))
	}

	should_be_bar_baz, err := root.CreateChild(bar_baz_path)
	if err != nil {
		t.Error(fmt.Sprintf("CreateChild returned error when no-op was expected: %s", err))
	}
	if len(root.Children) != 1 {
		t.Error(fmt.Sprintf("CreateChild created %d children when 1 was expected.", len(root.Children)))
	}
	if should_be_bar_baz != bar_baz {
		t.Error(fmt.Sprintf("CreateChild returned a new node (%+v) when returning an existing node was expected (%+v)", should_be_bar_baz, bar_baz))
	}
	if should_be_bar_baz != root.Children[0] {
		t.Error("CreateChild returned a node other than the one which it stored.")
	}

	bar_qux_path := []string{"bar", "qux"}
	bar_qux, err := root.CreateChild(bar_qux_path)
	if err != nil {
		t.Error("CreateChild returned error when topicNode creation was expected.")
	}
	if !reflect.DeepEqual(bar_qux.Name, bar_qux_path) {
		t.Error(fmt.Sprintf("CreateChild returned child with incorrect name (%q) vs. expected (%q).", bar_baz.Name, bar_qux_path))
	}
	if len(root.Children) != 1 {
		t.Error(fmt.Sprintf("CreateChild created a node unexpectedly at a higher level. The relative root had 1 child, and now has %d.", len(root.Children)))
	}
	if bar_baz == bar_qux {
		t.Error(fmt.Sprintf("CreateChild returned an existing node (%+v) when returning a new node was expected (%+v)", bar_baz, bar_qux))
	}
	if !reflect.DeepEqual(root.Children[0].Name, []string{"bar"}) {
		t.Error(fmt.Sprintf("CreateChild did not name the trie branch correctly.  Expected [\"bar\"] and got %q", root.Children[0].Name))
		return
	}
	if bar_baz == root.Children[0].Children[0] && bar_qux == root.Children[0].Children[1] {
		children := make([]topicNode, len(root.Children[0].Children))
		for i := range root.Children[0].Children {
			children[i] = *root.Children[0].Children[i]
		}
		t.Error(fmt.Sprintf("CreateChild incorrectly expanded the trie structure. Children of new branch are %+v", children))
	}
}

func TestCollapseSubscribers(t *testing.T) {
	// TODO(akesling)
}

func TestCollapse(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	root := newTopicNode(ctx, cancel, []string{"."})

	root.CreateChild([]string{"foo", "bar", "baz", "qux"})
	root.CreateChild([]string{"foo", "bar", "baz", "quuz"})
	root.CreateChild([]string{"foo", "qux"})

	death_notifications := make(chan []string)

	client1_messages := make(chan Message)
	foo_baz_flibbity_blibbity_bop, _ := root.CreateChild([]string{"foo", "baz", "flibbity", "blibbity", "bop"})
	foo_baz_flibbity_blibbity_bop.AddSub(&subscription{
		Topic:    []string{"foo", "baz", "flibbity", "blibbity", "bop"},
		Name:     "source",
		Deadline: time.Now().Add(time.Minute * 20),
		Client:   client1_messages,
	}, death_notifications)

	client2_messages := make(chan Message)
	foo_quuz, _ := root.CreateChild([]string{"foo", "quuz"})
	foo_quuz.AddSub(&subscription{
		Topic:    []string{"foo", "quuz"},
		Name:     "source",
		Deadline: time.Now().Add(time.Minute * 20),
		Client:   client2_messages,
	}, death_notifications)

	root.Collapse()

	foo, _, _ := root.MaybeFindTopic([]string{"foo"})
	if !(len(foo.Name) == 1 && foo.Name[0] == "foo") {
		t.Error(fmt.Sprintf("Expected topic with name [\"foo\"], received topic with name %q", foo.Name))
	}
	should_be_foo, _, _ := root.MaybeFindTopic([]string{"foo", "bar", "baz", "qux"})
	if foo != should_be_foo {
		t.Error(fmt.Sprintf("Topic found %+v was not the one expected %+v", should_be_foo, foo))
	}
	should_be_foo, _, _ = root.MaybeFindTopic([]string{"foo", "bar", "baz", "quuz"})
	if foo != should_be_foo {
		t.Error(fmt.Sprintf("Topic found %+v was not the one expected %+v", should_be_foo, foo))
	}
	should_be_foo, _, _ = root.MaybeFindTopic([]string{"foo", "qux"})
	if foo != should_be_foo {
		t.Error(fmt.Sprintf("Topic found %+v was not the one expected %+v", should_be_foo, foo))
	}

	should_be_foo_quuz, _, _ := root.MaybeFindTopic([]string{"foo", "quuz"})
	if foo_quuz != should_be_foo_quuz {
		t.Error(fmt.Sprintf("Topic found %+v was not the one expected %+v", should_be_foo_quuz, foo_quuz))
	}

	should_be_foo_baz_flibbity_blibbity_bop, _, _ := root.MaybeFindTopic([]string{"foo", "baz", "flibbity", "blibbity", "bop"})
	if foo_baz_flibbity_blibbity_bop != should_be_foo_baz_flibbity_blibbity_bop {
		t.Error(fmt.Sprintf("Topic found %+v was not the one expected %+v", should_be_foo_baz_flibbity_blibbity_bop, foo_baz_flibbity_blibbity_bop))
	}

	cancel()
	root.Collapse()
	if len(root.Children) > 0 {
		t.Error("Root has children, when the full trie should have collapsed")
	}
}
<commit_msg>Clean up Collapse Test to increase readability.<commit_after>package interchange

import (
	"code.google.com/p/go.net/context"
	"fmt"
	"reflect"
	"testing"
	"time"
)

func TestIsValidTopic(t *testing.T) {
	expect_true := func(b bool, msg string) {
		if !b {
			t.Error(msg)
		}
	}

	expect_valid := func(topic []string) {
		expect_true(IsValidTopic(topic), fmt.Sprintf("Should have been valid: %q", topic))
	}
	expect_not_valid := func(topic []string) {
		expect_true(!IsValidTopic(topic), fmt.Sprintf("Should _not_ have been valid: %q", topic))
	}

	expect_valid([]string{"foo"})
	expect_valid([]string{"foo", "bar", "baz", "qux", "quz"})

	expect_not_valid([]string{})
	expect_not_valid([]string{""})
	expect_not_valid([]string{"."})
}

func TestAddSub(t *testing.T) {
	root_ctx, cancel_root := context.WithCancel(context.Background())
	root := newTopicNode(root_ctx, cancel_root, []string{"."})

	topic := []string{"foo", "bar"}
	new_node, _ := root.CreateChild(topic)

	death_notifications := make(chan []string)
	client_messages := make(chan Message)

	new_subscriber := new_node.AddSub(&subscription{
		Topic:    topic,
		Name:     "source",
		Deadline: time.Now().Add(time.Minute * 20),
		Client:   client_messages,
	}, death_notifications)

	for i := 0; i < 10; i += 1 {
		message_sent := Message{Source: fmt.Sprintf("test_%d", i)}
		new_subscriber.Sink <- message_sent

		select {
		case message_received := <-client_messages:
			if !reflect.DeepEqual(message_sent, message_received) {
				t.Error(fmt.Sprintf("(%d): Message received (%+v) did not match message expected (%+v)", i, message_received, message_sent))
			}
		default:
			t.Error(fmt.Sprintf("(%d): Expected message never received from subscriber", i))
		}
	}

	cancel_root()
	select {
	case notified_topic := <-death_notifications:
		if !reflect.DeepEqual(notified_topic, topic) {
			t.Error(fmt.Sprintf("Expected topic (%q) does not equal topic notified (%q)", topic, notified_topic))
		}
	case <-time.After(1 * time.Second):
		t.Error("Timed out waiting for notification of subscriber death.")
		return
	}

	select {
	case <-client_messages:
	default:
		t.Error("Client channel not closed by dying subscriber")
	}
}

func TestMaybeFindTopic(t *testing.T) {
	// Build a sample topicTrie
	ctx, cancel := context.WithCancel(context.Background())
	root := newTopicNode(ctx, cancel, []string{"."})

	add_child := func(parent *topicNode, new_child *topicNode) {
		parent.Children = append(parent.Children, new_child)
	}

	_foo := newTopicNode(ctx, cancel, []string{"foo"})
	_foo_bar := newTopicNode(ctx, cancel, []string{"bar"})
	add_child(_foo, _foo_bar)
	_foo_baz := newTopicNode(ctx, cancel, []string{"baz"})
	add_child(_foo, _foo_baz)
	add_child(root, _foo)

	_qux := newTopicNode(ctx, cancel, []string{"qux"})
	_qux__foo_bar := newTopicNode(ctx, cancel, []string{"foo", "bar"})
	add_child(_qux, _qux__foo_bar)
	add_child(root, _qux)

	child_expectation := func(path []string, node_returned, node_expected *topicNode) {
		if node_expected != node_returned {
			t.Error(fmt.Sprintf("For provided path (%v), MaybeFindTopic returned a node (%+v) other than the one which it should have (%+v).", path, node_returned, node_expected))
		}
	}

	path_expectation := func(path, rest_expected, rest_returned []string) {
		if !reflect.DeepEqual(rest_expected, rest_returned) {
			t.Error(fmt.Sprintf("For provided path (%q), MaybeFindTopic returned a name remainder (%q) when it should have been (%q).", path, rest_returned, rest_expected))
		}
	}

	// Find exact topics
	/******************/
	path := []string{"foo"}
	should_be_foo, rest, overlap := root.MaybeFindTopic(path)
	child_expectation(path, should_be_foo, _foo)
	path_expectation(path, []string{}, rest)
	path_expectation(path, []string{}, overlap)

	path = []string{"foo", "bar"}
	should_be_foo_bar, rest, overlap := root.MaybeFindTopic(path)
	child_expectation(path, should_be_foo_bar, _foo_bar)
	path_expectation(path, []string{}, rest)
	path_expectation(path, []string{}, overlap)

	path = []string{"foo", "baz"}
	should_be_foo_baz, rest, overlap := root.MaybeFindTopic(path)
	child_expectation(path, should_be_foo_baz, _foo_baz)
	path_expectation(path, []string{}, rest)
	path_expectation(path, []string{}, overlap)

	path = []string{"qux"}
	should_be_qux, rest, overlap := root.MaybeFindTopic(path)
	child_expectation(path, should_be_qux, _qux)
	path_expectation(path, []string{}, rest)
	path_expectation(path, []string{}, overlap)

	path = []string{"qux", "foo", "bar"}
	should_be_qux__foo_bar, rest, overlap := root.MaybeFindTopic(path)
	child_expectation(path, should_be_qux__foo_bar, _qux__foo_bar)
	path_expectation(path, []string{}, rest)
	path_expectation(path, []string{}, overlap)

	// Return rest with found parent
	/******************************/
	path = []string{"foo", "bar", "baz"}
	should_be_foo_bar, rest, overlap = root.MaybeFindTopic(path)
	child_expectation(path, should_be_foo_bar, _foo_bar)
	path_expectation(path, []string{"baz"}, rest)
	path_expectation(path, []string{}, overlap)

	// Return rest with overlapping parent
	/************************************/
	path = []string{"qux", "foo", "baz"}
	should_be_qux__foo_bar, rest, overlap = root.MaybeFindTopic(path)
	child_expectation(path, should_be_qux__foo_bar, _qux__foo_bar)
	path_expectation(path, []string{"foo", "baz"}, rest)
	path_expectation(path, []string{"foo"}, overlap)
}

func TestCreateChild(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	root := newTopicNode(ctx, cancel, []string{"."})

	bar_baz_path := []string{"bar", "baz"}
	bar_baz, err := root.CreateChild(bar_baz_path)
	if err != nil {
		t.Error("CreateChild returned error when topicNode creation was expected.")
	}
	if !reflect.DeepEqual(bar_baz.Name, []string{"bar", "baz"}) {
		t.Error(fmt.Sprintf("CreateChild returned child with incorrect name (%q) vs. expected (%q).", bar_baz.Name, bar_baz_path))
	}
	if len(root.Children) != 1 {
		t.Error(fmt.Sprintf("CreateChild created %d children when 1 was expected.", len(root.Children)))
	}
	if child_node := root.Children[0]; bar_baz != child_node {
		t.Error(fmt.Sprintf("CreateChild returned a node (%+v) other than the one which it stored (%+v).", bar_baz, child_node))
	}

	should_be_bar_baz, err := root.CreateChild(bar_baz_path)
	if err != nil {
		t.Error(fmt.Sprintf("CreateChild returned error when no-op was expected: %s", err))
	}
	if len(root.Children) != 1 {
		t.Error(fmt.Sprintf("CreateChild created %d children when 1 was expected.", len(root.Children)))
	}
	if should_be_bar_baz != bar_baz {
		t.Error(fmt.Sprintf("CreateChild returned a new node (%+v) when returning an existing node was expected (%+v)", should_be_bar_baz, bar_baz))
	}
	if should_be_bar_baz != root.Children[0] {
		t.Error("CreateChild returned a node other than the one which it stored.")
	}

	bar_qux_path := []string{"bar", "qux"}
	bar_qux, err := root.CreateChild(bar_qux_path)
	if err != nil {
		t.Error("CreateChild returned error when topicNode creation was expected.")
	}
	if !reflect.DeepEqual(bar_qux.Name, bar_qux_path) {
		t.Error(fmt.Sprintf("CreateChild returned child with incorrect name (%q) vs. expected (%q).", bar_baz.Name, bar_qux_path))
	}
	if len(root.Children) != 1 {
		t.Error(fmt.Sprintf("CreateChild created a node unexpectedly at a higher level. The relative root had 1 child, and now has %d.", len(root.Children)))
	}
	if bar_baz == bar_qux {
		t.Error(fmt.Sprintf("CreateChild returned an existing node (%+v) when returning a new node was expected (%+v)", bar_baz, bar_qux))
	}
	if !reflect.DeepEqual(root.Children[0].Name, []string{"bar"}) {
		t.Error(fmt.Sprintf("CreateChild did not name the trie branch correctly.  Expected [\"bar\"] and got %q", root.Children[0].Name))
		return
	}
	if bar_baz == root.Children[0].Children[0] && bar_qux == root.Children[0].Children[1] {
		children := make([]topicNode, len(root.Children[0].Children))
		for i := range root.Children[0].Children {
			children[i] = *root.Children[0].Children[i]
		}
		t.Error(fmt.Sprintf("CreateChild incorrectly expanded the trie structure. Children of new branch are %+v", children))
	}
}

func TestCollapseSubscribers(t *testing.T) {
	// TODO(akesling)
}

func TestCollapse(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	root := newTopicNode(ctx, cancel, []string{"."})

	root.CreateChild([]string{"foo", "bar", "baz", "qux"})
	root.CreateChild([]string{"foo", "bar", "baz", "quuz"})
	root.CreateChild([]string{"foo", "qux"})

	death_notifications := make(chan []string)

	client1_messages := make(chan Message)
	foo_baz_flibbity_blibbity_bop, _ := root.CreateChild([]string{"foo", "baz", "flibbity", "blibbity", "bop"})
	foo_baz_flibbity_blibbity_bop.AddSub(&subscription{
		Topic:    []string{"foo", "baz", "flibbity", "blibbity", "bop"},
		Name:     "source",
		Deadline: time.Now().Add(time.Minute * 20),
		Client:   client1_messages,
	}, death_notifications)

	client2_messages := make(chan Message)
	foo_quuz, _ := root.CreateChild([]string{"foo", "quuz"})
	foo_quuz.AddSub(&subscription{
		Topic:    []string{"foo", "quuz"},
		Name:     "source",
		Deadline: time.Now().Add(time.Minute * 20),
		Client:   client2_messages,
	}, death_notifications)

	root.Collapse()

	expect_topic := func(name []string, expected *topicNode) string {
		found, _, _ := root.MaybeFindTopic(name)
		if found != expected {
			return fmt.Sprintf("Topic found %+v was not the one expected %+v", found, expected)
		}
		return ""
	}

	foo, _, _ := root.MaybeFindTopic([]string{"foo"})
	if !(len(foo.Name) == 1 && foo.Name[0] == "foo") {
		t.Error(fmt.Sprintf("Expected topic with name [\"foo\"], received topic with name %q", foo.Name))
	}

	type Expectation struct {
		Name  []string
		Value *topicNode
	}

	topic_expectations := []Expectation{
		{[]string{"foo", "bar", "baz", "qux"}, foo},
		{[]string{"foo", "bar", "baz", "quuz"}, foo},
		{[]string{"foo", "qux"}, foo},
		{[]string{"foo", "qux"}, foo},
		{[]string{"foo", "quuz"}, foo_quuz},
		{[]string{"foo", "baz", "flibbity", "blibbity", "bop"}, foo_baz_flibbity_blibbity_bop},
	}

	for i := range topic_expectations {
		expectation := topic_expectations[i]
		error_string := expect_topic(expectation.Name, expectation.Value)
		if error_string != "" {
			t.Error(error_string)
		}
	}

	cancel()
	root.Collapse()
	if len(root.Children) > 0 {
		t.Error("Root has children, when the full trie should have collapsed")
	}
}
<|endoftext|>


<commit_before>package controllers

import (
    "github.com/orc/db"
    "github.com/orc/sessions"
    "github.com/orc/utils"
    "github.com/orc/mvc/models"
    // "github.com/orc/mailer"
    "strconv"
    "time"
    "net/http"
)

func (this *Handler) HandleLogin(login, pass string) interface{} {
    var id int
    var enabled bool
    var passHash, salt string
    result := make(map[string]interface{}, 1)

    user := this.GetModel("users")
    user.LoadWherePart(map[string]interface{}{"login": login})
    err := db.SelectRow(user, []string{"id", "pass", "salt", "enabled"}).Scan(&id, &passHash, &salt, &enabled)

    if err != nil {
        result["result"] = "invalidCredentials"

    } else if enabled == false {
        result["result"] = "notEnabled"

    } else if passHash != utils.GetMD5Hash(pass+salt) {
        result["result"] = "badPassword"

    } else {
        result["result"] = "ok"

        hash := utils.GetRandSeq(HASH_SIZE)

        user := this.GetModel("users")
        user.LoadModelData(map[string]interface{}{"hash": hash})
        user.GetFields().(*models.User).Enabled = true
        user.LoadWherePart(map[string]interface{}{"id": id})
        db.QueryUpdate_(user).Scan()

        sessions.SetSession(this.Response, map[string]interface{}{"id": id, "hash": hash})
    }

    return result
}

func (this *Handler) HandleLogout() interface{} {
    result := map[string]string{"result": "ok"}
    sessions.ClearSession(this.Response)

    return result
}

func (this *Handler) HandleRegister_(login, password, email, role string) (result string, reg_id int) {
    result = "ok"
    salt := strconv.Itoa(int(time.Now().Unix()))
    pass := utils.GetMD5Hash(password + salt)

    passHasInvalidChars := false
    for i := 0; i < len(password); i++ {
        if strconv.IsPrint(rune(password[i])) == false {
            passHasInvalidChars = true
            break
        }
    }

    if db.IsExists_("users", []string{"login"}, []interface{}{login}) == true {
        result = "loginExists"
    } else if !utils.MatchRegexp("^[a-zA-Z0-9]{2,36}$", login) {
        result = "badLogin"
    } else if !utils.MatchRegexp("^.{6,36}$", password) || passHasInvalidChars {
        result = "badPassword"
    // } else if bad email {
    } else {
        token := utils.GetRandSeq(HASH_SIZE)

        // if !mailer.SendConfirmEmail(login, email, token) {
        //     return "badEmail", -1
        // }

        var user_id int
        user := this.GetModel("users")
        user.LoadModelData(map[string]interface{}{"login": login, "pass": pass, "salt": salt, "role": role, "token": token, "enabled": false})
        user.GetFields().(*models.User).Enabled = false
        db.QueryInsert_(user, "RETURNING id").Scan(&user_id)

        var face_id int
        face := this.GetModel("faces")
        face.LoadModelData(map[string]interface{}{"user_id": user_id})
        db.QueryInsert_(face, "RETURNING id").Scan(&face_id)

        registration := this.GetModel("registrations")
        registration.LoadModelData(map[string]interface{}{"face_id": face_id, "event_id": 1})
        db.QueryInsert_(registration, "RETURNING id").Scan(&reg_id)

        return result, reg_id
    }

    return result, -1
}

func (this *Handler) ConfirmUser(token string) {
    user := this.GetModel("users")
    user.LoadWherePart(map[string]interface{}{"token": token})

    var id int
    err := db.SelectRow(user, []string{"id"}).Scan(&id)
    if utils.HandleErr("[Handle::ConfirmUser]: ", err, this.Response) {
        return
    }

    user = this.GetModel("users")
    user.LoadModelData(map[string]interface{}{"token": " ", "enabled": true})
    user.GetFields().(*models.User).Enabled = true
    user.LoadWherePart(map[string]interface{}{"id": id})
    db.QueryUpdate_(user).Scan()

    if this.Response != nil {
        this.Render([]string{"mvc/views/msg.html"}, "msg", "Регистрация подтверждена.")
    }
}

func (this *Handler) RejectUser(token string) {
    user := this.GetModel("users")
    user.LoadWherePart(map[string]interface{}{"token": token})

    var id string
    err := db.SelectRow(user, []string{"id"}).Scan(&id)
    if err != nil {
        utils.HandleErr("[Handle::RejectUser]: ", err, this.Response)
        return
    }

    db.QueryDeleteByIds("users", id)

    if this.Response != nil {
        this.Render([]string{"mvc/views/msg.html"}, "msg", "Вы успешно отписаны от рассылок Secret Oasis.")
    }
}

func (this *Handler) ResetPassword() {
    user_id := sessions.GetValue("id", this.Request)

    if !sessions.CheackSession(this.Response, this.Request) || user_id == nil {
        http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)
        return
    }

    // if !this.isAdmin() {
    //     http.Redirect(this.Response, this.Request, "/", http.StatusForbidden)
    //     return
    // }

    this.Response.Header().Set("Access-Control-Allow-Origin", "*")
    this.Response.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
    this.Response.Header().Set("Content-type", "application/json")

    request, err := utils.ParseJS(this.Request, this.Response)
    if err != nil {
        utils.SendJSReply(err.Error(), this.Response)
        return
    }

    pass1 := request["pass1"].(string)
    pass2 := request["pass2"].(string)

    if !utils.MatchRegexp("^.{6,36}$", pass1) || !utils.MatchRegexp("^.{6,36}$", pass2) {
        utils.SendJSReply(map[string]interface{}{"result": "badPassword"}, this.Response)
        return
    } else if pass1 != pass2 {
        utils.SendJSReply(map[string]interface{}{"result": "differentPasswords"}, this.Response)
        return
    }

    var id int

    if request["id"] == nil {
        id = user_id.(int)

    } else {
        id, err =  strconv.Atoi(request["id"].(string))
        if utils.HandleErr("[Grid-Handler::ResetPassword] strconv.Atoi: ", err, this.Response) {
            return
        }
    }

    user := this.GetModel("users")
    user.LoadWherePart(map[string]interface{}{"id": id})

    var salt string
    var enabled bool
    db.SelectRow(user, []string{"salt", "enabled"}).Scan(&salt, &enabled)

    user.GetFields().(*models.User).Enabled = enabled

    user.LoadModelData(map[string]interface{}{"pass": utils.GetMD5Hash(pass1 + salt)})
    db.QueryUpdate_(user).Scan()

    utils.SendJSReply(map[string]interface{}{"result": "ok"}, this.Response)
}
<commit_msg>auth.go: add check sid<commit_after>package controllers

import (
    "github.com/orc/db"
    "github.com/orc/sessions"
    "github.com/orc/utils"
    "github.com/orc/mvc/models"
    // "github.com/orc/mailer"
    "strconv"
    "time"
    "net/http"
)

func (this *Handler) HandleLogin(login, pass string) interface{} {
    var id int
    var enabled bool
    var passHash, salt string
    result := make(map[string]interface{}, 1)

    user := this.GetModel("users")
    user.LoadWherePart(map[string]interface{}{"login": login})
    err := db.SelectRow(user, []string{"id", "pass", "salt", "enabled"}).Scan(&id, &passHash, &salt, &enabled)

    if err != nil {
        result["result"] = "invalidCredentials"

    } else if enabled == false {
        result["result"] = "notEnabled"

    } else if passHash != utils.GetMD5Hash(pass+salt) {
        result["result"] = "badPassword"

    } else {
        result["result"] = "ok"

        hash := utils.GetRandSeq(HASH_SIZE)

        user := this.GetModel("users")
        user.LoadModelData(map[string]interface{}{"hash": hash})
        user.GetFields().(*models.User).Enabled = true
        user.LoadWherePart(map[string]interface{}{"id": id})
        db.QueryUpdate_(user).Scan()

        sessions.SetSession(this.Response, map[string]interface{}{"id": id, "hash": hash})
    }

    return result
}

func (this *Handler) HandleLogout() interface{} {
    user_id := sessions.GetValue("id", this.Request)
    user_hash := sessions.GetValue("hash", this.Request)

    if !sessions.CheackSession(this.Response, this.Request) || user_id == nil || user_hash == nil || user_hash == " " {
        http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)
        return map[string]string{"result": "badSid"}
    }

    user := this.GetModel("users")
    user.LoadWherePart(map[string]interface{}{"id": user_id, "hash": user_hash})

    var id string
    var enabled bool
    err := db.SelectRow(user, []string{"id", "enabled"}).Scan(&id, &enabled)
    if err != nil {
        utils.HandleErr("[Handle::HandleLogout]: ", err, this.Response)
        return map[string]string{"result": "badSid"}
    }

    user = this.GetModel("users")
    user.LoadModelData(map[string]interface{}{"hash": " "})
    user.GetFields().(*models.User).Enabled = enabled
    user.LoadWherePart(map[string]interface{}{"id": id})
    db.QueryUpdate_(user).Scan()

    sessions.ClearSession(this.Response)

    return map[string]string{"result": "ok"}
}

func (this *Handler) HandleRegister_(login, password, email, role string) (result string, reg_id int) {
    result = "ok"
    salt := strconv.Itoa(int(time.Now().Unix()))
    pass := utils.GetMD5Hash(password + salt)

    passHasInvalidChars := false
    for i := 0; i < len(password); i++ {
        if strconv.IsPrint(rune(password[i])) == false {
            passHasInvalidChars = true
            break
        }
    }

    if db.IsExists_("users", []string{"login"}, []interface{}{login}) == true {
        result = "loginExists"
    } else if !utils.MatchRegexp("^[a-zA-Z0-9]{2,36}$", login) {
        result = "badLogin"
    } else if !utils.MatchRegexp("^.{6,36}$", password) || passHasInvalidChars {
        result = "badPassword"
    // } else if bad email {
    } else {
        token := utils.GetRandSeq(HASH_SIZE)

        // if !mailer.SendConfirmEmail(login, email, token) {
        //     return "badEmail", -1
        // }

        var user_id int
        user := this.GetModel("users")
        user.LoadModelData(map[string]interface{}{"login": login, "pass": pass, "salt": salt, "role": role, "token": token, "enabled": false})
        user.GetFields().(*models.User).Enabled = false
        db.QueryInsert_(user, "RETURNING id").Scan(&user_id)

        var face_id int
        face := this.GetModel("faces")
        face.LoadModelData(map[string]interface{}{"user_id": user_id})
        db.QueryInsert_(face, "RETURNING id").Scan(&face_id)

        registration := this.GetModel("registrations")
        registration.LoadModelData(map[string]interface{}{"face_id": face_id, "event_id": 1})
        db.QueryInsert_(registration, "RETURNING id").Scan(&reg_id)

        return result, reg_id
    }

    return result, -1
}

func (this *Handler) ConfirmUser(token string) {
    user := this.GetModel("users")
    user.LoadWherePart(map[string]interface{}{"token": token})

    var id int
    err := db.SelectRow(user, []string{"id"}).Scan(&id)
    if utils.HandleErr("[Handle::ConfirmUser]: ", err, this.Response) {
        return
    }

    user = this.GetModel("users")
    user.LoadModelData(map[string]interface{}{"token": " ", "enabled": true})
    user.GetFields().(*models.User).Enabled = true
    user.LoadWherePart(map[string]interface{}{"id": id})
    db.QueryUpdate_(user).Scan()

    if this.Response != nil {
        this.Render([]string{"mvc/views/msg.html"}, "msg", "Регистрация подтверждена.")
    }
}

func (this *Handler) RejectUser(token string) {
    user := this.GetModel("users")
    user.LoadWherePart(map[string]interface{}{"token": token})

    var id string
    err := db.SelectRow(user, []string{"id"}).Scan(&id)
    if err != nil {
        utils.HandleErr("[Handle::RejectUser]: ", err, this.Response)
        return
    }

    db.QueryDeleteByIds("users", id)

    if this.Response != nil {
        this.Render([]string{"mvc/views/msg.html"}, "msg", "Вы успешно отписаны от рассылок Secret Oasis.")
    }
}

func (this *Handler) ResetPassword() {
    user_id := sessions.GetValue("id", this.Request)

    if !sessions.CheackSession(this.Response, this.Request) || user_id == nil {
        http.Redirect(this.Response, this.Request, "/", http.StatusUnauthorized)
        return
    }

    // if !this.isAdmin() {
    //     http.Redirect(this.Response, this.Request, "/", http.StatusForbidden)
    //     return
    // }

    this.Response.Header().Set("Access-Control-Allow-Origin", "*")
    this.Response.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
    this.Response.Header().Set("Content-type", "application/json")

    request, err := utils.ParseJS(this.Request, this.Response)
    if err != nil {
        utils.SendJSReply(err.Error(), this.Response)
        return
    }

    pass1 := request["pass1"].(string)
    pass2 := request["pass2"].(string)

    if !utils.MatchRegexp("^.{6,36}$", pass1) || !utils.MatchRegexp("^.{6,36}$", pass2) {
        utils.SendJSReply(map[string]interface{}{"result": "badPassword"}, this.Response)
        return
    } else if pass1 != pass2 {
        utils.SendJSReply(map[string]interface{}{"result": "differentPasswords"}, this.Response)
        return
    }

    var id int

    if request["id"] == nil {
        id = user_id.(int)

    } else {
        id, err =  strconv.Atoi(request["id"].(string))
        if utils.HandleErr("[Grid-Handler::ResetPassword] strconv.Atoi: ", err, this.Response) {
            return
        }
    }

    user := this.GetModel("users")
    user.LoadWherePart(map[string]interface{}{"id": id})

    var salt string
    var enabled bool
    db.SelectRow(user, []string{"salt", "enabled"}).Scan(&salt, &enabled)

    user.GetFields().(*models.User).Enabled = enabled

    user.LoadModelData(map[string]interface{}{"pass": utils.GetMD5Hash(pass1 + salt)})
    db.QueryUpdate_(user).Scan()

    utils.SendJSReply(map[string]interface{}{"result": "ok"}, this.Response)
}
<|endoftext|>


<commit_before>package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/dockercn/docker-bucket/models"
	"net/http"
)

type AuthWebController struct {
	beego.Controller
}

func (this *AuthWebController) Prepare() {
	beego.Debug(fmt.Sprintf("[%s] %s | %s", this.Ctx.Input.Host(), this.Ctx.Input.Request.Method, this.Ctx.Input.Request.RequestURI))

	beego.Debug("[Headers]")
	beego.Debug(this.Ctx.Input.Request.Header)

	//设置 Response 的 Header 信息，在处理函数中可以覆盖
	this.Ctx.Output.Context.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8")
}

func (this *AuthWebController) Signin() {
	//获得用户提交的登陆(注册)信息
	var u map[string]interface{}
	if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &u); err != nil {
		beego.Error(fmt.Sprintf("[API 用户] 解码用户注册发送的 JSON 数据失败: %s", err.Error()))
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"错误\":\"解码用户发送的 JSON 数据失败\"}"))
		this.StopRun()
	}

	beego.Debug(fmt.Sprintf("[Web 用户] 用户登陆: %s", string(this.Ctx.Input.CopyBody())))
	beego.Debug(fmt.Sprintf("[Web 用户] 用户登陆: %s", u["username"].(string)))
	//验证用户登陆
	user := new(models.User)
	if has, err := user.Get(fmt.Sprint(u["username"]), fmt.Sprint(u["passwd"])); err != nil {
		beego.Error(fmt.Sprintf("[WEB 用户] 登录查询错误: %s", err.Error()))
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"用户登陆失败\"}"))
		return
	} else if !has {
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"用户名或密码不存在\"}"))
		return
	}

	//写入session中
	this.SetSession("username", fmt.Sprint(u["username"]))

	this.Ctx.Output.Context.Output.SetStatus(http.StatusOK)
	this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"登录成功\"}"))
	return
}

func (this *AuthWebController) ResetPasswd() {
	var u map[string]interface{}
	if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &u); err != nil {
		beego.Error(fmt.Sprintf("[API 用户] 解码用户重置密码发送的 JSON 数据失败: %s", err.Error()))
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"错误\":\"解码用户发送的 JSON 数据失败\"}"))
		this.StopRun()
	}

	beego.Debug(fmt.Sprintf("[Web 用户] 用户重置密码: %s", string(this.Ctx.Input.CopyBody())))

	this.Ctx.Output.Context.Output.SetStatus(http.StatusOK)
	this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"发送重置密码邮件成功\"}"))
	return
}

func (this *AuthWebController) Signup() {
	var u map[string]interface{}
	if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &u); err != nil {
		beego.Error(fmt.Sprintf("[API 用户] 解码用户注册发送的 JSON 数据失败: %s", err.Error()))
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"错误\":\"解码用户发送的 JSON 数据失败\"}"))
		this.StopRun()
	}
	beego.Debug(fmt.Sprintf("[Web 用户] 用户注册: %s", string(this.Ctx.Input.CopyBody())))
	//判断用户是否存在，存在返回错误；不存在创建用户数据
	user := new(models.User)
	if err := user.Put(fmt.Sprint(u["username"]), fmt.Sprint(u["password"]), fmt.Sprint(u["email"])); err != nil {
		beego.Error(fmt.Sprintf("[WEB 用户] 解码用户注册发送的 JSON 数据失败: %s", err.Error()))
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"用户注册失败\"}"))
		return
	}
	this.Ctx.Output.Context.Output.SetStatus(http.StatusOK)
	this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"用户注册成功\"}"))
	return
}
<commit_msg>规范日志输出<commit_after>package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/dockercn/docker-bucket/models"
	"net/http"
)

type AuthWebController struct {
	beego.Controller
}

func (this *AuthWebController) Prepare() {
	beego.Debug(fmt.Sprintf("[%s] %s | %s", this.Ctx.Input.Host(), this.Ctx.Input.Request.Method, this.Ctx.Input.Request.RequestURI))

	beego.Debug("[Headers]")
	beego.Debug(this.Ctx.Input.Request.Header)

	//设置 Response 的 Header 信息，在处理函数中可以覆盖
	this.Ctx.Output.Context.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8")
}

func (this *AuthWebController) Signin() {
	//获得用户提交的登陆(注册)信息
	var u map[string]interface{}
	if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &u); err != nil {
		beego.Error(fmt.Sprintf("[API 用户] 解码用户注册发送的 JSON 数据失败: %s", err.Error()))
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"错误\":\"解码用户发送的 JSON 数据失败\"}"))
		this.StopRun()
	}

	beego.Debug(fmt.Sprintf("[Web 用户] 用户登陆: %s", string(this.Ctx.Input.CopyBody())))
	beego.Debug(fmt.Sprintf("[Web 用户] 用户登陆: %s", u["username"].(string)))
	//验证用户登陆
	user := new(models.User)
	if has, err := user.Get(fmt.Sprint(u["username"]), fmt.Sprint(u["passwd"])); err != nil {
		beego.Error(fmt.Sprintf("[WEB 用户] 登录查询错误: %s", err.Error()))
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"用户登陆失败\"}"))
		return
	} else if !has {
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"用户名或密码不存在\"}"))
		return
	}

	//写入session中
	this.SetSession("username", fmt.Sprint(u["username"]))

	this.Ctx.Output.Context.Output.SetStatus(http.StatusOK)
	this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"登录成功\"}"))
	return
}

func (this *AuthWebController) ResetPasswd() {
	var u map[string]interface{}
	if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &u); err != nil {
		beego.Error(fmt.Sprintf("[API 用户] 解码用户重置密码发送的 JSON 数据失败: %s", err.Error()))
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"错误\":\"解码用户发送的 JSON 数据失败\"}"))
		this.StopRun()
	}

	beego.Debug(fmt.Sprintf("[Web 用户] 用户重置密码: %s", string(this.Ctx.Input.CopyBody())))

	this.Ctx.Output.Context.Output.SetStatus(http.StatusOK)
	this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"发送重置密码邮件成功\"}"))
	return
}

func (this *AuthWebController) Signup() {
	var u map[string]interface{}
	if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &u); err != nil {
		beego.Error(fmt.Sprintf("[API 用户] 解码用户注册发送的 JSON 数据失败: %s", err.Error()))
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"错误\":\"解码用户发送的 JSON 数据失败\"}"))
		this.StopRun()
	}
	beego.Debug(fmt.Sprintf("[Web 用户] 用户注册: %s", string(this.Ctx.Input.CopyBody())))
	//判断用户是否存在，存在返回错误；不存在创建用户数据
	user := new(models.User)
	if err := user.Put(fmt.Sprint(u["username"]), fmt.Sprint(u["password"]), fmt.Sprint(u["email"])); err != nil {
		beego.Error(fmt.Sprintf("[WEB 用户] 登录查询错误: %s", err.Error()))
		this.Ctx.Output.Context.Output.SetStatus(http.StatusBadRequest)
		this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"用户注册失败\"}"))
		return
	}
	this.Ctx.Output.Context.Output.SetStatus(http.StatusOK)
	this.Ctx.Output.Context.Output.Body([]byte("{\"message\":\"用户注册成功\"}"))
	return
}
<|endoftext|>


<commit_before>package controllers

import (
	"encoding/json"

	"github.com/lfq7413/tomato/orm"
	"github.com/lfq7413/tomato/utils"
)

import "gopkg.in/mgo.v2/bson"

// SchemasController ...
type SchemasController struct {
	ObjectsController
}

// HandleFind ...
// @router / [get]
func (s *SchemasController) HandleFind() {
	result, err := orm.SchemaCollection().GetAllSchemas()
	if err != nil && result == nil {
		s.Data["json"] = bson.M{
			"results": []interface{}{},
		}
		s.ServeJSON()
		return
	}
	for i, v := range result {
		result[i] = orm.MongoSchemaToSchemaAPIResponse(v)
	}
	s.Data["json"] = bson.M{
		"results": result,
	}
	s.ServeJSON()
}

// HandleGet ...
// @router /:className [get]
func (s *SchemasController) HandleGet() {
	className := s.Ctx.Input.Param(":className")
	result, err := orm.SchemaCollection().FindSchema(className)
	if err != nil && result == nil {
		// TODO 类不存在
		return
	}
	s.Data["json"] = result
	s.ServeJSON()
}

// HandleCreate ...
// @router /:className [post]
func (s *SchemasController) HandleCreate() {
	className := s.Ctx.Input.Param(":className")
	if s.Ctx.Input.RequestBody == nil {
		// TODO 数据为空
		return
	}
	var data bson.M
	err := json.Unmarshal(s.Ctx.Input.RequestBody, &data)
	if err != nil {
		// TODO 解析错误
		return
	}
	bodyClassName := ""
	if data["className"] != nil && utils.String(data["className"]) != "" {
		bodyClassName = utils.String(data["className"])
	}
	if className != bodyClassName {
		// TODO 类名不一致
		return
	}
	if className == "" && bodyClassName == "" {
		// TODO 类名不能为空
		return
	}
	if className == "" {
		className = bodyClassName
	}

	schema := orm.LoadSchema(nil)
	result := schema.AddClassIfNotExists(className, utils.MapInterface(data["fields"]), utils.MapInterface(data["classLevelPermissions"]))

	s.Data["json"] = orm.MongoSchemaToSchemaAPIResponse(result)
	s.ServeJSON()
}

// HandleUpdate ...
// @router /:className [put]
func (s *SchemasController) HandleUpdate() {
	s.ObjectsController.Put()
}

// HandleDelete ...
// @router /:className [delete]
func (s *SchemasController) HandleDelete() {
	s.ObjectsController.Delete()
}

// Delete ...
// @router / [delete]
func (s *SchemasController) Delete() {
	s.ObjectsController.Delete()
}

// Put ...
// @router / [put]
func (s *SchemasController) Put() {
	s.ObjectsController.Put()
}
<commit_msg>bug fix<commit_after>package controllers

import (
	"encoding/json"

	"github.com/lfq7413/tomato/orm"
	"github.com/lfq7413/tomato/utils"
)

import "gopkg.in/mgo.v2/bson"

// SchemasController ...
type SchemasController struct {
	ObjectsController
}

// HandleFind ...
// @router / [get]
func (s *SchemasController) HandleFind() {
	result, err := orm.SchemaCollection().GetAllSchemas()
	if err != nil && result == nil {
		s.Data["json"] = bson.M{
			"results": []interface{}{},
		}
		s.ServeJSON()
		return
	}
	for i, v := range result {
		result[i] = orm.MongoSchemaToSchemaAPIResponse(v)
	}
	s.Data["json"] = bson.M{
		"results": result,
	}
	s.ServeJSON()
}

// HandleGet ...
// @router /:className [get]
func (s *SchemasController) HandleGet() {
	className := s.Ctx.Input.Param(":className")
	result, err := orm.SchemaCollection().FindSchema(className)
	if err != nil && result == nil {
		// TODO 类不存在
		return
	}
	s.Data["json"] = result
	s.ServeJSON()
}

// HandleCreate ...
// @router /:className [post]
func (s *SchemasController) HandleCreate() {
	className := s.Ctx.Input.Param(":className")
	if s.Ctx.Input.RequestBody == nil {
		// TODO 数据为空
		return
	}
	var data bson.M
	err := json.Unmarshal(s.Ctx.Input.RequestBody, &data)
	if err != nil {
		// TODO 解析错误
		return
	}
	bodyClassName := ""
	if data["className"] != nil && utils.String(data["className"]) != "" {
		bodyClassName = utils.String(data["className"])
	}
	if className != "" && bodyClassName != "" {
		if className != bodyClassName {
			// TODO 类名不一致
			return
		}
	}
	if className == "" {
		className = bodyClassName
	}
	if className == "" {
		// TODO 类名不能为空
		return
	}

	schema := orm.LoadSchema(nil)
	result := schema.AddClassIfNotExists(className, utils.MapInterface(data["fields"]), utils.MapInterface(data["classLevelPermissions"]))

	s.Data["json"] = orm.MongoSchemaToSchemaAPIResponse(result)
	s.ServeJSON()
}

// HandleUpdate ...
// @router /:className [put]
func (s *SchemasController) HandleUpdate() {
	s.ObjectsController.Put()
}

// HandleDelete ...
// @router /:className [delete]
func (s *SchemasController) HandleDelete() {
	s.ObjectsController.Delete()
}

// Delete ...
// @router / [delete]
func (s *SchemasController) Delete() {
	s.ObjectsController.Delete()
}

// Put ...
// @router / [put]
func (s *SchemasController) Put() {
	s.ObjectsController.Put()
}
<|endoftext|>