<commit_before>package vfs

import (
	"bytes"
	"crypto/md5" // #nosec
	"encoding/base64"
	"encoding/json"
	"io"
	"net/http"
	"os"
	"path"
	"strconv"
	"time"

	"github.com/cozy/cozy-stack/couchdb"
	"github.com/spf13/afero"
)

// FileDoc is a struct containing all the informations about a file.
// It implements the couchdb.Doc and jsonapi.JSONApier interfaces.
type FileDoc struct {
	// Qualified file identifier
	FID string `json:"_id,omitempty"`
	// File revision
	FRev string `json:"_rev,omitempty"`
	// File name
	Name string `json:"name"`
	// Parent folder identifier
	FolderID string `json:"folderID"`
	// File path on VFS
	Path string `json:"path"`

	CreatedAt  time.Time `json:"created_at"`
	UpdatedAt  time.Time `json:"updated_at"`
	Size       int64     `json:"size,string"`
	Tags       []string  `json:"tags"`
	MD5Sum     []byte    `json:"md5sum"`
	Executable bool      `json:"executable"`
	Class      string    `json:"class"`
	Mime       string    `json:"mime"`
}

// ID returns the file qualified identifier (part of couchdb.Doc
// interface)
func (f *FileDoc) ID() string {
	return f.FID
}

// Rev returns the file revision (part of couchdb.Doc interface)
func (f *FileDoc) Rev() string {
	return f.FRev
}

// DocType returns the file document type (part of couchdb.Doc
// interface)
func (f *FileDoc) DocType() string {
	return string(FileDocType)
}

// SetID is used to change the file qualified identifier (part of
// couchdb.Doc interface)
func (f *FileDoc) SetID(id string) {
	f.FID = id
}

// SetRev is used to change the file revision (part of couchdb.Doc
// interface)
func (f *FileDoc) SetRev(rev string) {
	f.FRev = rev
}

// ToJSONApi implements temporary interface JSONApier to serialize
// the file document
func (f *FileDoc) ToJSONApi() ([]byte, error) {
	id := f.FID
	attrs := map[string]interface{}{
		"name":       f.Name,
		"created_at": f.CreatedAt,
		"updated_at": f.UpdatedAt,
		"size":       strconv.FormatInt(f.Size, 10),
		"tags":       f.Tags,
		"md5sum":     f.MD5Sum,
		"executable": f.Executable,
		"class":      f.Class,
		"mime":       f.Mime,
	}
	data := map[string]interface{}{
		"id":         id,
		"type":       f.DocType(),
		"rev":        f.Rev(),
		"attributes": attrs,
	}
	m := map[string]interface{}{
		"data": data,
	}
	return json.Marshal(m)
}

// NewFileDoc is the FileDoc constructor. The given name is validated.
func NewFileDoc(name, folderID string, size int64, md5Sum []byte, mime, class string, executable bool, tags []string) (doc *FileDoc, err error) {
	if err = checkFileName(name); err != nil {
		return
	}

	createDate := time.Now()
	doc = &FileDoc{
		Name:     name,
		FolderID: folderID,

		CreatedAt:  createDate,
		UpdatedAt:  createDate,
		Size:       size,
		MD5Sum:     md5Sum,
		Mime:       mime,
		Class:      class,
		Executable: executable,
		Tags:       tags,
	}

	return
}

// GetFileDoc is used to fetch file document information form our
// database.
func GetFileDoc(fileID, dbPrefix string) (doc *FileDoc, err error) {
	doc = &FileDoc{}
	err = couchdb.GetDoc(dbPrefix, string(FileDocType), fileID, doc)
	return
}

// ServeFileContent replies to a http request using the content of a
// file given its FileDoc.
//
// It uses internally http.ServeContent and benefits from it by
// offering support to Range, If-Modified-Since and If-None-Match
// requests. It uses the revision of the file as the Etag value for
// non-ranged requests
//
// The content disposition is inlined.
func ServeFileContent(doc *FileDoc, req *http.Request, w http.ResponseWriter, fs afero.Fs) (err error) {
	header := w.Header()
	header.Set("Content-Type", doc.Mime)
	header.Set("Content-Disposition", "inline; filename="+doc.Name)

	if header.Get("Range") == "" {
		eTag := base64.StdEncoding.EncodeToString(doc.MD5Sum)
		header.Set("Etag", eTag)
	}

	return serveContent(req, w, fs, doc.Path, doc.Name, doc.UpdatedAt)
}

// ServeFileContentByPath replies to a http request using the content
// of a file identified by its full path on the VFS. Unlike
// ServeFileContent, this method does not require the full file
// document but only its path.
//
// It also uses internally http.ServeContent but does not provide an
// Etag.
//
// The content disposition is attached
func ServeFileContentByPath(pth string, req *http.Request, w http.ResponseWriter, fs afero.Fs) error {
	fileInfo, err := fs.Stat(pth)
	if err != nil {
		return err
	}

	name := path.Base(pth)
	w.Header().Set("Content-Disposition", "attachment; filename="+name)

	return serveContent(req, w, fs, pth, name, fileInfo.ModTime())
}

func serveContent(req *http.Request, w http.ResponseWriter, fs afero.Fs, pth, name string, modtime time.Time) (err error) {
	content, err := fs.Open(pth)
	if err != nil {
		return
	}

	defer content.Close()
	http.ServeContent(w, req, name, modtime, content)
	return
}

// CreateFileAndUpload is the method for uploading a file onto the filesystem.
func CreateFileAndUpload(doc *FileDoc, fs afero.Fs, dbPrefix string, body io.Reader) (err error) {
	newpath, _, err := createNewFilePath(doc.Name, doc.FolderID, fs, dbPrefix)
	if err != nil {
		return err
	}

	file, err := safeCreateFile(newpath, doc.Executable, fs)
	if err != nil {
		return err
	}

	defer func() {
		if err != nil {
			fs.Remove(newpath)
		}
	}()

	written, md5Sum, err := copyOnFsAndCheckIntegrity(file, doc.MD5Sum, fs, body)
	if err != nil {
		return err
	}

	if doc.Size < 0 {
		doc.Size = written
	}

	if doc.MD5Sum == nil {
		doc.MD5Sum = md5Sum
	}

	if doc.Size != written {
		return ErrContentLengthMismatch
	}

	doc.Path = newpath

	return couchdb.CreateDoc(dbPrefix, doc)
}

// ModifyFileContent overrides the content of a file onto the
// filesystem.
//
// This method should change the file content atomically. If any error
// happens while copying the content, the previous file revision is
// kept undamaged.
func ModifyFileContent(olddoc *FileDoc, newdoc *FileDoc, fs afero.Fs, dbPrefix string, body io.Reader) (err error) {
	mdate := time.Now()

	tmppath := "/" + olddoc.ID() + "_" + olddoc.Rev() + "_" + strconv.FormatInt(mdate.UnixNano(), 10)
	newpath := olddoc.Path
	if err != nil {
		return err
	}

	file, err := safeCreateFile(tmppath, newdoc.Executable, fs)
	if err != nil {
		return err
	}

	defer func() {
		if err != nil {
			fs.Remove(tmppath)
		}
	}()

	written, md5Sum, err := copyOnFsAndCheckIntegrity(file, newdoc.MD5Sum, fs, body)
	if err != nil {
		return err
	}

	if newdoc.Size < 0 {
		newdoc.Size = written
	}

	if newdoc.MD5Sum == nil {
		newdoc.MD5Sum = md5Sum
	}

	if newdoc.Size != written {
		return ErrContentLengthMismatch
	}

	newdoc.Path = newpath
	newdoc.SetID(olddoc.ID())
	newdoc.SetRev(olddoc.Rev())
	newdoc.CreatedAt = olddoc.CreatedAt
	newdoc.UpdatedAt = mdate

	err = couchdb.UpdateDoc(dbPrefix, newdoc)
	if err != nil {
		return err
	}

	return fs.Rename(tmppath, newpath)
}

func safeCreateFile(pth string, executable bool, fs afero.Fs) (afero.File, error) {
	// write only (O_WRONLY), try to create the file and check that it
	// does not already exist (O_CREATE|O_EXCL).
	flag := os.O_WRONLY | os.O_CREATE | os.O_EXCL

	var mode os.FileMode
	if executable {
		mode = 0755 // -rwxr-xr-x
	} else {
		mode = 0644 // -rw-r--r--
	}

	return fs.OpenFile(pth, flag, mode)
}

func copyOnFsAndCheckIntegrity(file afero.File, givenMD5 []byte, fs afero.Fs, r io.Reader) (written int64, md5Sum []byte, err error) {
	defer func() {
		if cerr := file.Close(); cerr != nil && err == nil {
			err = cerr
		}
	}()

	md5H := md5.New() // #nosec

	written, err = io.Copy(file, io.TeeReader(r, md5H))
	if err != nil {
		return
	}

	doCheck := givenMD5 != nil
	md5Sum = md5H.Sum(nil)
	if doCheck && !bytes.Equal(givenMD5, md5Sum) {
		err = ErrInvalidHash
		return
	}

	return
}
<commit_msg>Lint: make file and io.WriteCloser<commit_after>package vfs

import (
	"bytes"
	"crypto/md5" // #nosec
	"encoding/base64"
	"encoding/json"
	"io"
	"net/http"
	"os"
	"path"
	"strconv"
	"time"

	"github.com/cozy/cozy-stack/couchdb"
	"github.com/spf13/afero"
)

// FileDoc is a struct containing all the informations about a file.
// It implements the couchdb.Doc and jsonapi.JSONApier interfaces.
type FileDoc struct {
	// Qualified file identifier
	FID string `json:"_id,omitempty"`
	// File revision
	FRev string `json:"_rev,omitempty"`
	// File name
	Name string `json:"name"`
	// Parent folder identifier
	FolderID string `json:"folderID"`
	// File path on VFS
	Path string `json:"path"`

	CreatedAt  time.Time `json:"created_at"`
	UpdatedAt  time.Time `json:"updated_at"`
	Size       int64     `json:"size,string"`
	Tags       []string  `json:"tags"`
	MD5Sum     []byte    `json:"md5sum"`
	Executable bool      `json:"executable"`
	Class      string    `json:"class"`
	Mime       string    `json:"mime"`
}

// ID returns the file qualified identifier (part of couchdb.Doc
// interface)
func (f *FileDoc) ID() string {
	return f.FID
}

// Rev returns the file revision (part of couchdb.Doc interface)
func (f *FileDoc) Rev() string {
	return f.FRev
}

// DocType returns the file document type (part of couchdb.Doc
// interface)
func (f *FileDoc) DocType() string {
	return string(FileDocType)
}

// SetID is used to change the file qualified identifier (part of
// couchdb.Doc interface)
func (f *FileDoc) SetID(id string) {
	f.FID = id
}

// SetRev is used to change the file revision (part of couchdb.Doc
// interface)
func (f *FileDoc) SetRev(rev string) {
	f.FRev = rev
}

// ToJSONApi implements temporary interface JSONApier to serialize
// the file document
func (f *FileDoc) ToJSONApi() ([]byte, error) {
	id := f.FID
	attrs := map[string]interface{}{
		"name":       f.Name,
		"created_at": f.CreatedAt,
		"updated_at": f.UpdatedAt,
		"size":       strconv.FormatInt(f.Size, 10),
		"tags":       f.Tags,
		"md5sum":     f.MD5Sum,
		"executable": f.Executable,
		"class":      f.Class,
		"mime":       f.Mime,
	}
	data := map[string]interface{}{
		"id":         id,
		"type":       f.DocType(),
		"rev":        f.Rev(),
		"attributes": attrs,
	}
	m := map[string]interface{}{
		"data": data,
	}
	return json.Marshal(m)
}

// NewFileDoc is the FileDoc constructor. The given name is validated.
func NewFileDoc(name, folderID string, size int64, md5Sum []byte, mime, class string, executable bool, tags []string) (doc *FileDoc, err error) {
	if err = checkFileName(name); err != nil {
		return
	}

	createDate := time.Now()
	doc = &FileDoc{
		Name:     name,
		FolderID: folderID,

		CreatedAt:  createDate,
		UpdatedAt:  createDate,
		Size:       size,
		MD5Sum:     md5Sum,
		Mime:       mime,
		Class:      class,
		Executable: executable,
		Tags:       tags,
	}

	return
}

// GetFileDoc is used to fetch file document information form our
// database.
func GetFileDoc(fileID, dbPrefix string) (doc *FileDoc, err error) {
	doc = &FileDoc{}
	err = couchdb.GetDoc(dbPrefix, string(FileDocType), fileID, doc)
	return
}

// ServeFileContent replies to a http request using the content of a
// file given its FileDoc.
//
// It uses internally http.ServeContent and benefits from it by
// offering support to Range, If-Modified-Since and If-None-Match
// requests. It uses the revision of the file as the Etag value for
// non-ranged requests
//
// The content disposition is inlined.
func ServeFileContent(doc *FileDoc, req *http.Request, w http.ResponseWriter, fs afero.Fs) (err error) {
	header := w.Header()
	header.Set("Content-Type", doc.Mime)
	header.Set("Content-Disposition", "inline; filename="+doc.Name)

	if header.Get("Range") == "" {
		eTag := base64.StdEncoding.EncodeToString(doc.MD5Sum)
		header.Set("Etag", eTag)
	}

	return serveContent(req, w, fs, doc.Path, doc.Name, doc.UpdatedAt)
}

// ServeFileContentByPath replies to a http request using the content
// of a file identified by its full path on the VFS. Unlike
// ServeFileContent, this method does not require the full file
// document but only its path.
//
// It also uses internally http.ServeContent but does not provide an
// Etag.
//
// The content disposition is attached
func ServeFileContentByPath(pth string, req *http.Request, w http.ResponseWriter, fs afero.Fs) error {
	fileInfo, err := fs.Stat(pth)
	if err != nil {
		return err
	}

	name := path.Base(pth)
	w.Header().Set("Content-Disposition", "attachment; filename="+name)

	return serveContent(req, w, fs, pth, name, fileInfo.ModTime())
}

func serveContent(req *http.Request, w http.ResponseWriter, fs afero.Fs, pth, name string, modtime time.Time) (err error) {
	content, err := fs.Open(pth)
	if err != nil {
		return
	}

	defer content.Close()
	http.ServeContent(w, req, name, modtime, content)
	return
}

// CreateFileAndUpload is the method for uploading a file onto the filesystem.
func CreateFileAndUpload(doc *FileDoc, fs afero.Fs, dbPrefix string, body io.Reader) (err error) {
	newpath, _, err := createNewFilePath(doc.Name, doc.FolderID, fs, dbPrefix)
	if err != nil {
		return err
	}

	file, err := safeCreateFile(newpath, doc.Executable, fs)
	if err != nil {
		return err
	}

	defer func() {
		if err != nil {
			fs.Remove(newpath)
		}
	}()

	written, md5Sum, err := copyOnFsAndCheckIntegrity(file, doc.MD5Sum, fs, body)
	if err != nil {
		return err
	}

	if doc.Size < 0 {
		doc.Size = written
	}

	if doc.MD5Sum == nil {
		doc.MD5Sum = md5Sum
	}

	if doc.Size != written {
		return ErrContentLengthMismatch
	}

	doc.Path = newpath

	return couchdb.CreateDoc(dbPrefix, doc)
}

// ModifyFileContent overrides the content of a file onto the
// filesystem.
//
// This method should change the file content atomically. If any error
// happens while copying the content, the previous file revision is
// kept undamaged.
func ModifyFileContent(olddoc *FileDoc, newdoc *FileDoc, fs afero.Fs, dbPrefix string, body io.Reader) (err error) {
	mdate := time.Now()

	tmppath := "/" + olddoc.ID() + "_" + olddoc.Rev() + "_" + strconv.FormatInt(mdate.UnixNano(), 10)
	newpath := olddoc.Path
	if err != nil {
		return err
	}

	file, err := safeCreateFile(tmppath, newdoc.Executable, fs)
	if err != nil {
		return err
	}

	defer func() {
		if err != nil {
			fs.Remove(tmppath)
		}
	}()

	written, md5Sum, err := copyOnFsAndCheckIntegrity(file, newdoc.MD5Sum, fs, body)
	if err != nil {
		return err
	}

	if newdoc.Size < 0 {
		newdoc.Size = written
	}

	if newdoc.MD5Sum == nil {
		newdoc.MD5Sum = md5Sum
	}

	if newdoc.Size != written {
		return ErrContentLengthMismatch
	}

	newdoc.Path = newpath
	newdoc.SetID(olddoc.ID())
	newdoc.SetRev(olddoc.Rev())
	newdoc.CreatedAt = olddoc.CreatedAt
	newdoc.UpdatedAt = mdate

	err = couchdb.UpdateDoc(dbPrefix, newdoc)
	if err != nil {
		return err
	}

	return fs.Rename(tmppath, newpath)
}

func safeCreateFile(pth string, executable bool, fs afero.Fs) (afero.File, error) {
	// write only (O_WRONLY), try to create the file and check that it
	// does not already exist (O_CREATE|O_EXCL).
	flag := os.O_WRONLY | os.O_CREATE | os.O_EXCL

	var mode os.FileMode
	if executable {
		mode = 0755 // -rwxr-xr-x
	} else {
		mode = 0644 // -rw-r--r--
	}

	return fs.OpenFile(pth, flag, mode)
}

func copyOnFsAndCheckIntegrity(file io.WriteCloser, givenMD5 []byte, fs afero.Fs, r io.Reader) (written int64, md5Sum []byte, err error) {
	defer func() {
		if cerr := file.Close(); cerr != nil && err == nil {
			err = cerr
		}
	}()

	md5H := md5.New() // #nosec

	written, err = io.Copy(file, io.TeeReader(r, md5H))
	if err != nil {
		return
	}

	doCheck := givenMD5 != nil
	md5Sum = md5H.Sum(nil)
	if doCheck && !bytes.Equal(givenMD5, md5Sum) {
		err = ErrInvalidHash
		return
	}

	return
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"encoding/json"
	"fmt"
	"bytes"
	"log"
	"net/http"
	"io/ioutil"
	"os"
	"strings"
)

var port = flag.String("p", "8888", "Port to listen on")
const template = `<!doctype html>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>viewdocs.io</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">

    <link href="//polyfill.io/normalize.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="//cloud.typography.com/678416/735422/css/fonts.css" />
    <link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:300,600' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="http://static.gist.io/css/screen.css">

viewdocs

            {{CONTENT}}

Inspired by gist.io · documentation for hackers · zero setup · publish in seconds

`

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage:	%v -p <port>\n\n", os.Args[0])
		flag.PrintDefaults()
	}
}

func errorResponse(w http.ResponseWriter, e string) {
	w.WriteHeader(http.StatusInternalServerError)
	w.Write([]byte(e))
}

func main() {
	flag.Parse()

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		hostname := strings.Split(r.Host, ".")
		if len(hostname) < 2 {
			errorResponse(w, "Huh?")
			return
		}
		username := hostname[0]
		path := strings.Split(r.RequestURI, "/")
		if len(path) < 2 {
			errorResponse(w, "Huh?")
			return
		}
		reponame := path[1]
		var docpath string
		if len(path) < 3 {
			docpath = "index"
		} else {
			docpath = strings.Join(path[2:], "/")
		}
		switch r.Method {
		case "GET":
			resp, err := http.Get("https://raw.github.com/"+username+"/"+reponame+"/master/docs/"+docpath+".md")
			if err != nil {
				errorResponse(w, err.Error())
				return
			}
			body, err := ioutil.ReadAll(resp.Body)
			resp.Body.Close()
			if err != nil {
				errorResponse(w, err.Error())
				return
			}
			payload, _ := json.Marshal(map[string]string{"text": string(body)})
			resp, err = http.Post("https://api.github.com/markdown", "application/json", bytes.NewBuffer(payload))
			if err != nil {
				errorResponse(w, err.Error())
				return
			}
			body, err = ioutil.ReadAll(resp.Body)
			resp.Body.Close()
			if err != nil {
				errorResponse(w, err.Error())
				return
			}
			w.Write([]byte(strings.Replace(template, "{{CONTENT}}", string(body), 1)))
		default:
			w.WriteHeader(http.StatusMethodNotAllowed)
		}
	})
	log.Println("Listening on port "+*port)
	log.Fatal(http.ListenAndServe(":"+*port, nil))
}
<commit_msg>works better<commit_after>package main

import (
	"flag"
	"encoding/json"
	"fmt"
	"bytes"
	"log"
	"net/http"
	"io/ioutil"
	"os"
	"strings"
)

var port = flag.String("p", "8888", "Port to listen on")
const template = `<!doctype html>

    <meta charset="utf-8">
    <title>viewdocs.io</title>
    <link rel="stylesheet" type="text/css" href="//cloud.typography.com/678416/735422/css/fonts.css" />
    <link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:300,600' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="http://static.gist.io/css/screen.css">

{{NAME}}

            {{CONTENT}}

`

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage:	%v -p <port>\n\n", os.Args[0])
		flag.PrintDefaults()
	}
}

func errorResponse(w http.ResponseWriter, e string) {
	w.WriteHeader(http.StatusInternalServerError)
	w.Write([]byte(e))
}

func main() {
	flag.Parse()

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		hostname := strings.Split(r.Host, ".")
		if len(hostname) < 2 {
			errorResponse(w, "Huh?")
			return
		}
		username := hostname[0]
		path := strings.Split(r.RequestURI, "/")
		if len(path) < 2 {
			errorResponse(w, "Huh?")
			return
		}
		reponame := path[1]
		var docpath string
		if len(path) < 3 {
			docpath = "index"
		} else {
			docpath = strings.Join(path[2:], "/")
		}
		switch r.Method {
		case "GET":
			resp, err := http.Get("https://raw.github.com/"+username+"/"+reponame+"/master/docs/"+docpath+".md")
			if err != nil {
				errorResponse(w, err.Error())
				return
			}
			body, err := ioutil.ReadAll(resp.Body)
			resp.Body.Close()
			if err != nil {
				errorResponse(w, err.Error())
				return
			}
			payload, _ := json.Marshal(map[string]string{"text": string(body)})
			resp, err = http.Post("https://api.github.com/markdown", "application/json", bytes.NewBuffer(payload))
			if err != nil {
				errorResponse(w, err.Error())
				return
			}
			body, err = ioutil.ReadAll(resp.Body)
			resp.Body.Close()
			if err != nil {
				errorResponse(w, err.Error())
				return
			}
			output := strings.Replace(template, "{{CONTENT}}", string(body), 1)
			output = strings.Replace(output, "{{NAME}}", reponame, -1)
			output = strings.Replace(output, "{{USER}}", username, -1)
			w.Write([]byte(output))
		default:
			w.WriteHeader(http.StatusMethodNotAllowed)
		}
	})
	log.Println("Listening on port "+*port)
	log.Fatal(http.ListenAndServe(":"+*port, nil))
}
<|endoftext|>


<commit_before>package web

import (
	"github.com/asteris-llc/gestalt/store"
	"github.com/asteris-llc/gestalt/web/app"
	"github.com/raphael/goa"
)

// TODO: more realistic error handling

// SchemaController implements the schema resource.
type SchemaController struct {
	goa.Controller

	store *store.Store
}

// NewSchemaController creates a schema controller.
func NewSchemaController(service goa.Service, store *store.Store) app.SchemaController {
	return &SchemaController{
		Controller: service.NewController("SchemaController"),
		store:      store,
	}
}

// Create runs the create action.
func (c *SchemaController) Create(ctx *app.CreateSchemaContext) error {
	schema := app.Schema(*ctx.Payload)
	err := c.store.StoreSchema(schema.Name, &schema)
	if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	if ctx.HasSetDefaults && ctx.SetDefaults {
		err = c.store.StoreDefaultValues(schema.Name)
		if err != nil {
			ctx.Logger.Error(err.Error())
			return ctx.InternalServerError()
		}
	}

	return ctx.Created(&schema)
}

// Delete runs the delete action.
func (c *SchemaController) Delete(ctx *app.DeleteSchemaContext) error {
	if ctx.HasDeleteKeys && ctx.DeleteKeys {
		err := c.store.DeleteValues(ctx.Name)
		if err == store.ErrMissingKey {
			return ctx.NotFound()
		} else if err != nil {
			ctx.Logger.Error(err.Error())
			return ctx.InternalServerError()
		}
	}

	err := c.store.DeleteSchema(ctx.Name)
	if err == store.ErrMissingKey {
		return ctx.NotFound()
	} else if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	return ctx.NoContent()
}

// Get runs the get action.
func (c *SchemaController) Get(ctx *app.GetSchemaContext) error {
	schema, err := c.store.RetrieveSchema(ctx.Name)
	if err == store.ErrMissingKey {
		return ctx.NotFound()
	} else if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	return ctx.OK(schema)
}

// List runs the list action.
func (c *SchemaController) List(ctx *app.ListSchemaContext) error {
	schemas, err := c.store.ListSchemas()
	if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	return ctx.OK(schemas)
}

// SetDefaults runs the setDefaults action.
func (c *SchemaController) SetDefaults(ctx *app.SetDefaultsSchemaContext) error {
	err := c.store.StoreDefaultValues(ctx.Name)
	if err == store.ErrMissingKey {
		return ctx.NotFound()
	} else if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	return ctx.NotFound()
}

// Update runs the update action.
func (c *SchemaController) Update(ctx *app.UpdateSchemaContext) error {
	schema := app.Schema(*ctx.Payload)
	err := c.store.StoreSchema(schema.Name, &schema)
	if err == store.ErrMissingKey {
		return ctx.NotFound()
	} else if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	if ctx.HasSetDefaults && ctx.SetDefaults {
		err = c.store.StoreDefaultValues(schema.Name)
		if err == store.ErrMissingKey {
			return ctx.NotFound()
		} else if err != nil {
			ctx.Logger.Error(err.Error())
			return ctx.InternalServerError()
		}
	}

	return ctx.OK(&schema)
}
<commit_msg>web(schema): remove TODO about error handling<commit_after>package web

import (
	"github.com/asteris-llc/gestalt/store"
	"github.com/asteris-llc/gestalt/web/app"
	"github.com/raphael/goa"
)

// SchemaController implements the schema resource.
type SchemaController struct {
	goa.Controller

	store *store.Store
}

// NewSchemaController creates a schema controller.
func NewSchemaController(service goa.Service, store *store.Store) app.SchemaController {
	return &SchemaController{
		Controller: service.NewController("SchemaController"),
		store:      store,
	}
}

// Create runs the create action.
func (c *SchemaController) Create(ctx *app.CreateSchemaContext) error {
	schema := app.Schema(*ctx.Payload)
	err := c.store.StoreSchema(schema.Name, &schema)
	if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	if ctx.HasSetDefaults && ctx.SetDefaults {
		err = c.store.StoreDefaultValues(schema.Name)
		if err != nil {
			ctx.Logger.Error(err.Error())
			return ctx.InternalServerError()
		}
	}

	return ctx.Created(&schema)
}

// Delete runs the delete action.
func (c *SchemaController) Delete(ctx *app.DeleteSchemaContext) error {
	if ctx.HasDeleteKeys && ctx.DeleteKeys {
		err := c.store.DeleteValues(ctx.Name)
		if err == store.ErrMissingKey {
			return ctx.NotFound()
		} else if err != nil {
			ctx.Logger.Error(err.Error())
			return ctx.InternalServerError()
		}
	}

	err := c.store.DeleteSchema(ctx.Name)
	if err == store.ErrMissingKey {
		return ctx.NotFound()
	} else if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	return ctx.NoContent()
}

// Get runs the get action.
func (c *SchemaController) Get(ctx *app.GetSchemaContext) error {
	schema, err := c.store.RetrieveSchema(ctx.Name)
	if err == store.ErrMissingKey {
		return ctx.NotFound()
	} else if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	return ctx.OK(schema)
}

// List runs the list action.
func (c *SchemaController) List(ctx *app.ListSchemaContext) error {
	schemas, err := c.store.ListSchemas()
	if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	return ctx.OK(schemas)
}

// SetDefaults runs the setDefaults action.
func (c *SchemaController) SetDefaults(ctx *app.SetDefaultsSchemaContext) error {
	err := c.store.StoreDefaultValues(ctx.Name)
	if err == store.ErrMissingKey {
		return ctx.NotFound()
	} else if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	return ctx.NotFound()
}

// Update runs the update action.
func (c *SchemaController) Update(ctx *app.UpdateSchemaContext) error {
	schema := app.Schema(*ctx.Payload)
	err := c.store.StoreSchema(schema.Name, &schema)
	if err == store.ErrMissingKey {
		return ctx.NotFound()
	} else if err != nil {
		ctx.Logger.Error(err.Error())
		return ctx.InternalServerError()
	}

	if ctx.HasSetDefaults && ctx.SetDefaults {
		err = c.store.StoreDefaultValues(schema.Name)
		if err == store.ErrMissingKey {
			return ctx.NotFound()
		} else if err != nil {
			ctx.Logger.Error(err.Error())
			return ctx.InternalServerError()
		}
	}

	return ctx.OK(&schema)
}
<|endoftext|>


<commit_before>package web

import (
	"log"
	"math"
	"net"

	"golang.org/x/net/context"

	"github.com/spf13/viper"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"

	pb "github.com/sascha-andres/go-logsink/logsink"
)

// server is used to implement logsink.LogTransferServer.
type server struct {
	hub *hub
}

// SendLine implements logsink.SendLine
func (s *server) SendLine(ctx context.Context, in *pb.LineMessage) (*pb.LineResult, error) {
	breakAt := viper.GetInt("web.break")
	iterations := int(len(in.Line) / breakAt)
	for start := 0; start <= iterations; start++ {
		s.hub.broadcast <- []byte(in.Line[start*breakAt : int32(math.Min(float64((start+1)*breakAt), float64(len(in.Line))))])
	}
	return &pb.LineResult{Result: true}, nil
}

func (s *server) run() {
	s.hub = newHub()
	go s.hub.run()
	lis, err := net.Listen("tcp", viper.GetString("web.bind"))
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	srv := grpc.NewServer()
	pb.RegisterLogTransferServer(srv, s)
	// Register reflection service on gRPC server.
	reflection.Register(srv)
	if err := srv.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
<commit_msg>Fixing division by zero for break at default<commit_after>package web

import (
	"log"
	"math"
	"net"

	"golang.org/x/net/context"

	"github.com/spf13/viper"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"

	pb "github.com/sascha-andres/go-logsink/logsink"
)

// server is used to implement logsink.LogTransferServer.
type server struct {
	hub *hub
}

// SendLine implements logsink.SendLine
func (s *server) SendLine(ctx context.Context, in *pb.LineMessage) (*pb.LineResult, error) {
	breakAt := viper.GetInt("web.break")
	if breakAt == 0 {
		s.hub.broadcast <- []byte(in.Line)
	} else {
		iterations := int(len(in.Line) / breakAt)
		for start := 0; start <= iterations; start++ {
			s.hub.broadcast <- []byte(in.Line[start*breakAt : int32(math.Min(float64((start+1)*breakAt), float64(len(in.Line))))])
		}
	}
	return &pb.LineResult{Result: true}, nil
}

func (s *server) run() {
	s.hub = newHub()
	go s.hub.run()
	lis, err := net.Listen("tcp", viper.GetString("web.bind"))
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	srv := grpc.NewServer()
	pb.RegisterLogTransferServer(srv, s)
	// Register reflection service on gRPC server.
	reflection.Register(srv)
	if err := srv.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
<|endoftext|>


<commit_before>// Copyright 2016 Google Inc. All Rights Reserved.
// This file is available under the Apache license.

package vm

import (
	"fmt"
	"reflect"
	"regexp/syntax"
	"strings"
	"sync"

	"github.com/golang/glog"
)

type Type interface {
	Root() Type
	String() string
}

func Equals(t1, t2 Type) bool {
	return reflect.DeepEqual(t1, t2)
}

var (
	nextVariableId   int
	nextVariableIdMu sync.Mutex
)

type TypeVariable struct {
	Id       int
	Instance *Type
}

func NewTypeVariable() Type {
	nextVariableIdMu.Lock()
	id := nextVariableId
	nextVariableId += 1
	nextVariableIdMu.Unlock()
	return &TypeVariable{Id: id}
}

func (t *TypeVariable) Root() Type {
	if t.Instance == nil {
		return t
	} else {
		r := (*t.Instance).Root()
		t.Instance = &r
		return r
	}
}

func (t *TypeVariable) String() string {
	if t.Instance != nil {
		return (*t.Instance).String()
	}
	return fmt.Sprintf("typeVar%d", t.Id)

}

type TypeOperator struct {
	Name string
}

func (t *TypeOperator) Root() Type {
	return t
}

func (t *TypeOperator) String() string {
	return t.Name
}

// Builtin types
var (
	Undef  = &TypeOperator{"Undef"}
	None   = &TypeOperator{"None"}
	Int    = &TypeOperator{"Int"}
	Float  = &TypeOperator{"Float"}
	String = &TypeOperator{"String"}
)

// Unify performs type unification of both parameter Types.  It returns the
// least upper bound of both types, the smallest type that is capable of
// representing both parameters.  If either type is a type variable, then that
// variable is unified with the LUB.
func Unify(a, b Type) Type {
	a1, b1 := a.Root(), b.Root()
	switch a2 := a1.(type) {
	case *TypeVariable:
		switch b2 := b1.(type) {
		case *TypeVariable:
			if a2.Id == b2.Id {
				return a2
			} else {
				glog.V(2).Infof("Making %q type %q", a2, b1)
				a2.Instance = &b1
				return a2
			}
		case *TypeOperator:
			glog.V(2).Infof("Making %q type %q", a2, b1)
			a2.Instance = &b1
			return b1
		}
	case *TypeOperator:
		switch b2 := b1.(type) {
		case *TypeVariable:
			return Unify(b, a)
		case *TypeOperator:
			if Equals(a2, b2) {
				return a2
			}
			// least upper bound
			if (Equals(a2, Float) && Equals(b2, Int)) ||
				(Equals(b2, Float) && Equals(a2, Int)) {
				return Float
			}
			if (Equals(a2, String) && Equals(b2, Int)) ||
				(Equals(b2, String) && Equals(a2, Int)) ||
				(Equals(a2, String) && Equals(b2, Float)) ||
				(Equals(b2, String) && Equals(a2, Float)) {
				return String
			}
			return None
		}
	}
	return None
}

// inferCaprefType determines a type for a capturing group, based on contents
// of that capture group.
func inferCaprefType(re *syntax.Regexp, cap int) Type {
	group := getCaptureGroup(re, cap)
	if group == nil {
		return None
	}
	switch {
	case groupOnlyMatches(group, "+-0123456789"):
		return Int
	case groupOnlyMatches(group, "+-0123456789.eE"):
		return Float
	}
	// TODO: String.  Current behaviour of mtail before types is assume all
	// matches are usable in arithmetic expressions.
	return Int
}

// getCaptureGroup returns the Regexp node of the capturing group numbered cap
// in re.
func getCaptureGroup(re *syntax.Regexp, cap int) *syntax.Regexp {
	if re.Op == syntax.OpCapture && re.Cap == cap {
		return re
	}
	for _, sub := range re.Sub {
		r := getCaptureGroup(sub, cap)
		if r != nil {
			return r
		}
	}
	return nil
}

// groupOnlyMatches returns true iff re only matches for runes in the s.
func groupOnlyMatches(re *syntax.Regexp, s string) bool {
	switch re.Op {
	case syntax.OpLiteral:
		for _, r := range re.Rune {
			if !strings.ContainsRune(s, r) {
				return false
			}
		}
		return true

	case syntax.OpCharClass:
		for i := 0; i < len(re.Rune); i += 2 {
			lo, hi := re.Rune[i], re.Rune[i+1]
			for r := lo; r <= hi; r++ {
				if !strings.ContainsRune(s, r) {
					return false
				}
			}
		}
		return true

	case syntax.OpStar, syntax.OpPlus, syntax.OpRepeat, syntax.OpQuest, syntax.OpCapture:
		return groupOnlyMatches(re.Sub[0], s)

	case syntax.OpConcat, syntax.OpAlternate:
		for _, sub := range re.Sub {
			if !groupOnlyMatches(sub, s) {
				return false
			}
		}

	default:
		return false
	}
	return true
}
<commit_msg>Fixes a data race when setting the concrete type instance of a type variable.<commit_after>// Copyright 2016 Google Inc. All Rights Reserved.
// This file is available under the Apache license.

package vm

import (
	"fmt"
	"reflect"
	"regexp/syntax"
	"strings"
	"sync"

	"github.com/golang/glog"
)

type Type interface {
	Root() Type
	String() string
}

func Equals(t1, t2 Type) bool {
	return reflect.DeepEqual(t1, t2)
}

var (
	nextVariableId   int
	nextVariableIdMu sync.Mutex
)

type TypeVariable struct {
	Id         int
	Instance   *Type
	instanceMu sync.RWMutex
}

func NewTypeVariable() Type {
	nextVariableIdMu.Lock()
	id := nextVariableId
	nextVariableId += 1
	nextVariableIdMu.Unlock()
	return &TypeVariable{Id: id}
}

func (t *TypeVariable) Root() Type {
	t.instanceMu.Lock()
	defer t.instanceMu.Unlock()
	if t.Instance == nil {
		return t
	} else {
		r := (*t.Instance).Root()
		t.Instance = &r
		return r
	}
}

func (t *TypeVariable) String() string {
	t.instanceMu.RLock()
	defer t.instanceMu.RUnlock()
	if t.Instance != nil {
		return (*t.Instance).String()
	}
	return fmt.Sprintf("typeVar%d", t.Id)

}

func (t *TypeVariable) SetInstance(t1 *Type) {
	t.instanceMu.Lock()
	defer t.instanceMu.Unlock()
	t.Instance = t1
}

type TypeOperator struct {
	Name string
}

func (t *TypeOperator) Root() Type {
	return t
}

func (t *TypeOperator) String() string {
	return t.Name
}

// Builtin types
var (
	Undef  = &TypeOperator{"Undef"}
	None   = &TypeOperator{"None"}
	Int    = &TypeOperator{"Int"}
	Float  = &TypeOperator{"Float"}
	String = &TypeOperator{"String"}
)

// Unify performs type unification of both parameter Types.  It returns the
// least upper bound of both types, the smallest type that is capable of
// representing both parameters.  If either type is a type variable, then that
// variable is unified with the LUB.
func Unify(a, b Type) Type {
	a1, b1 := a.Root(), b.Root()
	switch a2 := a1.(type) {
	case *TypeVariable:
		switch b2 := b1.(type) {
		case *TypeVariable:
			if a2.Id == b2.Id {
				return a2
			} else {
				glog.V(2).Infof("Making %q type %q", a2, b1)
				a2.SetInstance(&b1)
				return a2
			}
		case *TypeOperator:
			glog.V(2).Infof("Making %q type %q", a2, b1)
			a2.SetInstance(&b1)
			return b1
		}
	case *TypeOperator:
		switch b2 := b1.(type) {
		case *TypeVariable:
			return Unify(b, a)
		case *TypeOperator:
			if Equals(a2, b2) {
				return a2
			}
			// least upper bound
			if (Equals(a2, Float) && Equals(b2, Int)) ||
				(Equals(b2, Float) && Equals(a2, Int)) {
				return Float
			}
			if (Equals(a2, String) && Equals(b2, Int)) ||
				(Equals(b2, String) && Equals(a2, Int)) ||
				(Equals(a2, String) && Equals(b2, Float)) ||
				(Equals(b2, String) && Equals(a2, Float)) {
				return String
			}
			return None
		}
	}
	return None
}

// inferCaprefType determines a type for a capturing group, based on contents
// of that capture group.
func inferCaprefType(re *syntax.Regexp, cap int) Type {
	group := getCaptureGroup(re, cap)
	if group == nil {
		return None
	}
	switch {
	case groupOnlyMatches(group, "+-0123456789"):
		return Int
	case groupOnlyMatches(group, "+-0123456789.eE"):
		return Float
	}
	// TODO: String.  Current behaviour of mtail before types is assume all
	// matches are usable in arithmetic expressions.
	return Int
}

// getCaptureGroup returns the Regexp node of the capturing group numbered cap
// in re.
func getCaptureGroup(re *syntax.Regexp, cap int) *syntax.Regexp {
	if re.Op == syntax.OpCapture && re.Cap == cap {
		return re
	}
	for _, sub := range re.Sub {
		r := getCaptureGroup(sub, cap)
		if r != nil {
			return r
		}
	}
	return nil
}

// groupOnlyMatches returns true iff re only matches for runes in the s.
func groupOnlyMatches(re *syntax.Regexp, s string) bool {
	switch re.Op {
	case syntax.OpLiteral:
		for _, r := range re.Rune {
			if !strings.ContainsRune(s, r) {
				return false
			}
		}
		return true

	case syntax.OpCharClass:
		for i := 0; i < len(re.Rune); i += 2 {
			lo, hi := re.Rune[i], re.Rune[i+1]
			for r := lo; r <= hi; r++ {
				if !strings.ContainsRune(s, r) {
					return false
				}
			}
		}
		return true

	case syntax.OpStar, syntax.OpPlus, syntax.OpRepeat, syntax.OpQuest, syntax.OpCapture:
		return groupOnlyMatches(re.Sub[0], s)

	case syntax.OpConcat, syntax.OpAlternate:
		for _, sub := range re.Sub {
			if !groupOnlyMatches(sub, s) {
				return false
			}
		}

	default:
		return false
	}
	return true
}
<|endoftext|>


<commit_before>// Copyright 2015 The Vanadium 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 test

import (
	"path/filepath"
	"time"

	"v.io/x/devtools/internal/collect"
	"v.io/x/devtools/internal/project"
	"v.io/x/devtools/internal/runutil"
	"v.io/x/devtools/internal/test"
	"v.io/x/devtools/internal/tool"
	"v.io/x/devtools/internal/xunit"
)

const (
	defaultJSTestTimeout = 15 * time.Minute
)

// runJSTest is a harness for executing javascript tests.
func runJSTest(ctx *tool.Context, testName, testDir, target string, cleanFn func() error, env map[string]string, extraDeps []string) (_ *test.Result, e error) {
	// Initialize the test.
	deps := append(extraDeps, "nodejs")
	cleanup, err := initTest(ctx, testName, deps)
	if err != nil {
		return nil, internalTestError{err, "Init"}
	}
	defer collect.Error(func() error { return cleanup() }, &e)

	// Navigate to the target directory.
	if err := ctx.Run().Chdir(testDir); err != nil {
		return nil, err
	}

	// Clean up after previous instances of the test.
	opts := ctx.Run().Opts()
	for key, value := range env {
		opts.Env[key] = value
	}
	if err := ctx.Run().CommandWithOpts(opts, "make", "clean"); err != nil {
		return nil, err
	}
	if cleanFn != nil {
		if err := cleanFn(); err != nil {
			return nil, err
		}
	}

	// Run the test target.
	if err := ctx.Run().TimedCommandWithOpts(defaultJSTestTimeout, opts, "make", target); err != nil {
		if err == runutil.CommandTimedOutErr {
			return &test.Result{
				Status:       test.TimedOut,
				TimeoutValue: defaultJSTestTimeout,
			}, nil
		} else {
			return nil, internalTestError{err, "Make " + target}
		}
	}

	return &test.Result{Status: test.Passed}, nil
}

// vanadiumJSBuildExtension tests the vanadium javascript build extension.
func vanadiumJSBuildExtension(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "extension/vanadium.zip"
	return runJSTest(ctx, testName, testDir, target, nil, nil, []string{"nacl"})
}

// vanadiumJSDoc (re)generates the content of the vanadium javascript
// documentation server.
func vanadiumJSDoc(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "docs"

	result, err := runJSTest(ctx, testName, testDir, target, nil, nil, nil)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// vanadiumJSBrowserIntegration runs the vanadium javascript integration test in a browser environment using nacl plugin.
func vanadiumJSBrowserIntegration(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-integration-browser"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["BROWSER_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"nacl"})
}

// vanadiumJSNodeIntegration runs the vanadium javascript integration test in NodeJS environment using wspr.
func vanadiumJSNodeIntegration(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-integration-node"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, nil)
}

// vanadiumJSUnit runs the vanadium javascript unit test.
func vanadiumJSUnit(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-unit"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"nacl"})
}

// vanadiumJSVdl runs the vanadium javascript vdl test.
func vanadiumJSVdl(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-vdl"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"nacl"})
}

// vanadiumJSVDLAudit checks that all VDL-based JS source files are up-to-date.
func vanadiumJSVdlAudit(ctx *tool.Context, testName string, _ ...Opt) (_ *test.Result, e error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-vdl-audit"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, nil)
}

// vanadiumJSVom runs the vanadium javascript vom test.
func vanadiumJSVom(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-vom"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"nacl"})
}

// vanadiumJSSyncbaseBrowser runs the vanadium javascript syncbase test in a browser.
func vanadiumJSSyncbaseBrowser(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "roadmap", "javascript", "syncbase")
	target := "test-integration-browser"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["BROWSER_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"nacl", "syncbase"})
}

// vanadiumJSSyncbaseNode runs the vanadium javascript syncbase test in nodejs.
func vanadiumJSSyncbaseNode(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "roadmap", "javascript", "syncbase")
	target := "test-integration-node"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"syncbase"})
}

func setCommonJSEnv(env map[string]string) {
	env["XUNIT"] = "true"
}
<commit_msg>TBR: devtools: update syncbase-js tests to use the new locations.<commit_after>// Copyright 2015 The Vanadium 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 test

import (
	"path/filepath"
	"time"

	"v.io/x/devtools/internal/collect"
	"v.io/x/devtools/internal/project"
	"v.io/x/devtools/internal/runutil"
	"v.io/x/devtools/internal/test"
	"v.io/x/devtools/internal/tool"
	"v.io/x/devtools/internal/xunit"
)

const (
	defaultJSTestTimeout = 15 * time.Minute
)

// runJSTest is a harness for executing javascript tests.
func runJSTest(ctx *tool.Context, testName, testDir, target string, cleanFn func() error, env map[string]string, extraDeps []string) (_ *test.Result, e error) {
	// Initialize the test.
	deps := append(extraDeps, "nodejs")
	cleanup, err := initTest(ctx, testName, deps)
	if err != nil {
		return nil, internalTestError{err, "Init"}
	}
	defer collect.Error(func() error { return cleanup() }, &e)

	// Navigate to the target directory.
	if err := ctx.Run().Chdir(testDir); err != nil {
		return nil, err
	}

	// Clean up after previous instances of the test.
	opts := ctx.Run().Opts()
	for key, value := range env {
		opts.Env[key] = value
	}
	if err := ctx.Run().CommandWithOpts(opts, "make", "clean"); err != nil {
		return nil, err
	}
	if cleanFn != nil {
		if err := cleanFn(); err != nil {
			return nil, err
		}
	}

	// Run the test target.
	if err := ctx.Run().TimedCommandWithOpts(defaultJSTestTimeout, opts, "make", target); err != nil {
		if err == runutil.CommandTimedOutErr {
			return &test.Result{
				Status:       test.TimedOut,
				TimeoutValue: defaultJSTestTimeout,
			}, nil
		} else {
			return nil, internalTestError{err, "Make " + target}
		}
	}

	return &test.Result{Status: test.Passed}, nil
}

// vanadiumJSBuildExtension tests the vanadium javascript build extension.
func vanadiumJSBuildExtension(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "extension/vanadium.zip"
	return runJSTest(ctx, testName, testDir, target, nil, nil, []string{"nacl"})
}

// vanadiumJSDoc (re)generates the content of the vanadium javascript
// documentation server.
func vanadiumJSDoc(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "docs"

	result, err := runJSTest(ctx, testName, testDir, target, nil, nil, nil)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// vanadiumJSBrowserIntegration runs the vanadium javascript integration test in a browser environment using nacl plugin.
func vanadiumJSBrowserIntegration(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-integration-browser"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["BROWSER_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"nacl"})
}

// vanadiumJSNodeIntegration runs the vanadium javascript integration test in NodeJS environment using wspr.
func vanadiumJSNodeIntegration(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-integration-node"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, nil)
}

// vanadiumJSUnit runs the vanadium javascript unit test.
func vanadiumJSUnit(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-unit"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"nacl"})
}

// vanadiumJSVdl runs the vanadium javascript vdl test.
func vanadiumJSVdl(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-vdl"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"nacl"})
}

// vanadiumJSVDLAudit checks that all VDL-based JS source files are up-to-date.
func vanadiumJSVdlAudit(ctx *tool.Context, testName string, _ ...Opt) (_ *test.Result, e error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-vdl-audit"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, nil)
}

// vanadiumJSVom runs the vanadium javascript vom test.
func vanadiumJSVom(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "core")
	target := "test-vom"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"nacl"})
}

// vanadiumJSSyncbaseBrowser runs the vanadium javascript syncbase test in a browser.
func vanadiumJSSyncbaseBrowser(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "syncbase")
	target := "test-integration-browser"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["BROWSER_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"nacl", "syncbase"})
}

// vanadiumJSSyncbaseNode runs the vanadium javascript syncbase test in nodejs.
func vanadiumJSSyncbaseNode(ctx *tool.Context, testName string, _ ...Opt) (*test.Result, error) {
	root, err := project.V23Root()
	if err != nil {
		return nil, err
	}
	testDir := filepath.Join(root, "release", "javascript", "syncbase")
	target := "test-integration-node"
	env := map[string]string{}
	setCommonJSEnv(env)
	env["NODE_OUTPUT"] = xunit.ReportPath(testName)
	return runJSTest(ctx, testName, testDir, target, nil, env, []string{"syncbase"})
}

func setCommonJSEnv(env map[string]string) {
	env["XUNIT"] = "true"
}
<|endoftext|>


<commit_before>package driver

import (
	"fmt"

	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"

	"github.com/ory/hydra/metrics/prometheus"
	"github.com/ory/x/cmdx"
	"github.com/ory/x/tracing"

	"github.com/ory/hydra/client"
	"github.com/ory/hydra/consent"
	"github.com/ory/hydra/driver/configuration"
	"github.com/ory/hydra/jwk"
	"github.com/ory/hydra/oauth2"
	"github.com/ory/hydra/x"
	"github.com/ory/x/dbal"
	"github.com/ory/x/healthx"
)

type Registry interface {
	dbal.Driver

	Init() error

	WithConfig(c configuration.Provider) Registry
	WithLogger(l logrus.FieldLogger) Registry

	WithBuildInfo(version, hash, date string) Registry
	BuildVersion() string
	BuildDate() string
	BuildHash() string

	x.RegistryLogger
	x.RegistryWriter
	x.RegistryCookieStore
	client.Registry
	consent.Registry
	jwk.Registry
	oauth2.Registry
	PrometheusManager() *prometheus.MetricsManager
	Tracer() *tracing.Tracer

	RegisterRoutes(admin *x.RouterAdmin, public *x.RouterPublic)
	ClientHandler() *client.Handler
	KeyHandler() *jwk.Handler
	ConsentHandler() *consent.Handler
	OAuth2Handler() *oauth2.Handler
	HealthHandler() *healthx.Handler
}

func MustNewRegistry(c configuration.Provider) Registry {
	r, err := NewRegistry(c)
	cmdx.Must(err, "unable to initialize services: %s", err)
	return r
}

func NewRegistry(c configuration.Provider) (Registry, error) {
	fmt.Print("Got dsn: ", c.DSN())
	driver, err := dbal.GetDriverFor(c.DSN())
	if err != nil {
		return nil, errors.WithStack(err)
	}

	registry, ok := driver.(Registry)
	if !ok {
		return nil, errors.Errorf("driver of type %T does not implement interface Registry", driver)
	}

	registry = registry.WithConfig(c)

	if err := registry.Init(); err != nil {
		return nil, err
	}

	return registry, nil
}

func CallRegistry(r Registry) {
	r.ClientValidator()
	r.ClientManager()
	r.ClientHasher()
	r.ConsentManager()
	r.ConsentStrategy()
	r.SubjectIdentifierAlgorithm()
	r.KeyManager()
	r.KeyGenerators()
	r.KeyCipher()
	r.OAuth2Storage()
	r.OAuth2Provider()
	r.AudienceStrategy()
	r.ScopeStrategy()
	r.AccessTokenJWTStrategy()
	r.OpenIDJWTStrategy()
	r.OpenIDConnectRequestValidator()
	r.PrometheusManager()
	r.Tracer()
}
<commit_msg>driver: don't log DSN (#1593)<commit_after>package driver

import (
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"

	"github.com/ory/hydra/metrics/prometheus"
	"github.com/ory/x/cmdx"
	"github.com/ory/x/tracing"

	"github.com/ory/hydra/client"
	"github.com/ory/hydra/consent"
	"github.com/ory/hydra/driver/configuration"
	"github.com/ory/hydra/jwk"
	"github.com/ory/hydra/oauth2"
	"github.com/ory/hydra/x"
	"github.com/ory/x/dbal"
	"github.com/ory/x/healthx"
)

type Registry interface {
	dbal.Driver

	Init() error

	WithConfig(c configuration.Provider) Registry
	WithLogger(l logrus.FieldLogger) Registry

	WithBuildInfo(version, hash, date string) Registry
	BuildVersion() string
	BuildDate() string
	BuildHash() string

	x.RegistryLogger
	x.RegistryWriter
	x.RegistryCookieStore
	client.Registry
	consent.Registry
	jwk.Registry
	oauth2.Registry
	PrometheusManager() *prometheus.MetricsManager
	Tracer() *tracing.Tracer

	RegisterRoutes(admin *x.RouterAdmin, public *x.RouterPublic)
	ClientHandler() *client.Handler
	KeyHandler() *jwk.Handler
	ConsentHandler() *consent.Handler
	OAuth2Handler() *oauth2.Handler
	HealthHandler() *healthx.Handler
}

func MustNewRegistry(c configuration.Provider) Registry {
	r, err := NewRegistry(c)
	cmdx.Must(err, "unable to initialize services: %s", err)
	return r
}

func NewRegistry(c configuration.Provider) (Registry, error) {
	driver, err := dbal.GetDriverFor(c.DSN())
	if err != nil {
		return nil, errors.WithStack(err)
	}

	registry, ok := driver.(Registry)
	if !ok {
		return nil, errors.Errorf("driver of type %T does not implement interface Registry", driver)
	}

	registry = registry.WithConfig(c)

	if err := registry.Init(); err != nil {
		return nil, err
	}

	return registry, nil
}

func CallRegistry(r Registry) {
	r.ClientValidator()
	r.ClientManager()
	r.ClientHasher()
	r.ConsentManager()
	r.ConsentStrategy()
	r.SubjectIdentifierAlgorithm()
	r.KeyManager()
	r.KeyGenerators()
	r.KeyCipher()
	r.OAuth2Storage()
	r.OAuth2Provider()
	r.AudienceStrategy()
	r.ScopeStrategy()
	r.AccessTokenJWTStrategy()
	r.OpenIDJWTStrategy()
	r.OpenIDConnectRequestValidator()
	r.PrometheusManager()
	r.Tracer()
}
<|endoftext|>


<commit_before>package generatorControllers

import (
	. "eaciit/wfdemo-git/library/helper"
	. "eaciit/wfdemo-git/library/models"
	. "eaciit/wfdemo-git/processapp/summaryGenerator/controllers"
	"eaciit/wfdemo-git/web/helper"
	_ "fmt"
	"log"
	"os"
	"strconv"
	_ "strings"
	"time"

	"strings"

	"github.com/eaciit/dbox"
	_ "github.com/eaciit/dbox/dbc/mongo"
	tk "github.com/eaciit/toolkit"
)

type GenScadaLast24 struct {
	*BaseController
}

func (d *GenScadaLast24) Generate(base *BaseController) {
	if base != nil {
		d.BaseController = base
		ctx, e := PrepareConnection()
		if e != nil {
			ErrorHandler(e, "Scada Summary")
			os.Exit(0)
		}

		d.BaseController.Ctx.DeleteMany(new(ScadaLastUpdate), dbox.And(dbox.Ne("_id", "")))

		budgetMonths := []float64{
			5911.8744,
			6023.419200000001,
			7027.3224,
			8588.9496,
			14389.2792,
			16954.8096,
			15727.8168,
			12046.8384,
			9704.3976,
			5688.784799999999,
			3569.4336,
			5911.8744}

		projectList := []ProjectOut{}
		projectList = append(projectList, ProjectOut{})
		projects, _ := helper.GetProjectList()
		projectList = append(projectList, projects...)

		for _, proj := range projectList {
			projectName := proj.Value
			turbineList := []TurbineOut{}

			if projectName != "" {
				turbineList, _ = helper.GetTurbineList([]interface{}{projectName})
			} else {
				turbineList, _ = helper.GetTurbineList(nil)
			}

			totalTurbine := len(turbineList)

			filter := []*dbox.Filter{}

			filter = append(filter, dbox.Gte("power", -200))
			if projectName != "" {
				filter = append(filter, dbox.Eq("projectname", projectName))
			}

			/*for _, v := range filter {
				log.Printf(">> %#v \n", v)
			}*/

			csr, e := ctx.NewQuery().
				From(new(ScadaData).TableName()).
				Where(dbox.And(filter...)).
				Aggr(dbox.AggrMax, "$timestamp", "timestamp").
				Aggr(dbox.AggrMax, "$dateinfo.dateid", "dateid").
				Group("").
				Cursor(nil)
			defer csr.Close()

			if e != nil {
				log.Printf("Error: %v \n", e.Error())
			} else {
				datas := []tk.M{}
				e = csr.Fetch(&datas, 0, false)

				tk.Printf(">> %#v \n", datas)

				if len(datas) > 0 {
					dateId := datas[0]["dateid"].(time.Time).UTC()
					dtInfo := GetDateInfo(dateId)
					maxTimeStamp := datas[0]["timestamp"].(time.Time).UTC()
					//startTime := maxTimeStamp.Add(-24 * time.Hour)
					var budgetCurrMonthDaily float64

					if len(budgetMonths)-1 >= int(dateId.Month()) {
						budgetCurrMonths := budgetMonths[int(dateId.Month())-1] * 1000.0
						noOfDay := float64(daysIn(dateId.Month(), dateId.Year()))
						budgetCurrMonthDaily = tk.Div(budgetCurrMonths, noOfDay)
					}

					mdl := new(ScadaLastUpdate).New()

					if projectName != "" {
						mdl.ID = "SCADALASTUPDATE_" + strings.ToUpper(projectName)
						mdl.ProjectName = projectName
						mdl.NoOfProjects = 1
					} else {
						mdl.ID = "SCADALASTUPDATE_FLEET"
						mdl.ProjectName = "Fleet"
						mdl.NoOfProjects = len(projectList)
					}

					for _, t := range turbineList {
						mdl.TotalMaxCapacity += t.Capacity
					}

					mdl.TotalMaxCapacity = tk.ToFloat64(mdl.TotalMaxCapacity*1000.0, 2, tk.RoundingAuto)
					mdl.LastUpdate = maxTimeStamp
					mdl.DateInfo = dtInfo
					mdl.NoOfTurbines = totalTurbine

					items := make([]LastData24Hours, 0)
					for i := 0; i < 24; i++ {
						year := strconv.Itoa(dateId.Year())
						month := dateId.Month().String()
						day := strconv.Itoa(dateId.Day())
						strTime := year + "-" + month + "-" + day + " " + strconv.Itoa(i) + ":00:00"
						timeHr, _ := time.Parse("2006-January-2 15:04:05", strTime)

						timeHrStart := timeHr.Add(-1 * time.Hour)

						filterSub := []*dbox.Filter{}
						filterSub = append(filterSub, dbox.Gt("timestamp", timeHrStart))
						filterSub = append(filterSub, dbox.Lte("timestamp", timeHr))
						filterSub = append(filterSub, dbox.Gte("power", -200))

						if projectName != "" {
							filterSub = append(filterSub, dbox.Eq("projectname", projectName))
						}

						csr, e = ctx.NewQuery().From(new(ScadaData).TableName()).
							Where(dbox.And(filterSub...)).
							Aggr(dbox.AggrSum, "$power", "totalpower").
							Aggr(dbox.AggrSum, "$powerlost", "totalpowerlost").
							Aggr(dbox.AggrSum, "$energylost", "energylost").
							Aggr(dbox.AggrSum, "$denpower", "denpower").
							Aggr(dbox.AggrSum, "$oktime", "totaloktime").
							Aggr(dbox.AggrSum, "$griddowntime", "totalgriddowntime").
							Aggr(dbox.AggrAvr, "$windspeed", "avgwindspeed").
							Group("projectname").
							Cursor(nil)
						defer csr.Close()

						scadas := []tk.M{}
						e = csr.Fetch(&scadas, 0, false)

						var last LastData24Hours
						if len(scadas) > 0 {
							data := scadas[0]
							trueAvail := 0.0
							gridAvail := 0.0

							ipower := data["totalpower"]
							power := 0.0
							if ipower != nil {
								power = ipower.(float64)
							}

							ipotentialpower := data["denpower"]
							potentialpower := 0.0
							if ipotentialpower != nil {
								potentialpower = ipotentialpower.(float64)
							}

							iwindspeed := data["avgwindspeed"]
							windspeed := 0.0
							if iwindspeed != nil {
								windspeed = iwindspeed.(float64)
							}
							last.Hour = i
							last.TimeHour = timeHr
							last.AvgWindSpeed = windspeed
							last.PowerKw = power
							last.EnergyKwh = power / 6
							last.Potential = potentialpower
							last.PotentialKwh = potentialpower / 6
							last.TrueAvail = trueAvail
							last.GridAvail = gridAvail
						} else {
							last.Hour = i
							last.TimeHour = timeHr
							last.AvgWindSpeed = 0.0
							last.PowerKw = 0.0
							last.EnergyKwh = 0.0
							last.Potential = 0.0
							last.PotentialKwh = 0.0
							last.TrueAvail = 0.0
							last.GridAvail = 0.0
						}

						items = append(items, last)
					}

					match := tk.M{}

					match.Set("dateinfo.monthid", tk.M{}.Set("$eq", dtInfo.MonthId)).Set("power", tk.M{}.Set("$gte", -200))

					if projectName != "" {
						match.Set("projectname", projectName)
					}

					pipe := []tk.M{tk.M{}.Set("$match", match), tk.M{}.Set("$group", tk.M{}.Set("_id", "$dateinfo.dateid").Set("totalpower", tk.M{}.Set("$sum", "$power"))), tk.M{}.Set("$sort", tk.M{}.Set("_id", 1))}

					csr, _ := ctx.NewQuery().
						Command("pipe", pipe).
						From(new(ScadaData).TableName()).
						Cursor(nil)
					defer csr.Close()

					scadas := []tk.M{}
					e = csr.Fetch(&scadas, 0, false)

					item30s := make([]Last30Days, 0)
					dateData := dateId
					cummProd := 0.0
					cummBudget := 0.0
					for _, data := range scadas {
						dateData = data["_id"].(time.Time)
						var last30 Last30Days
						last30.DateId = dateData
						last30.DayNo = dateData.Day()

						currProd := 0.0
						currBudget := budgetCurrMonthDaily // 565160.32
						if data != nil {
							ipower := data["totalpower"]
							power := 0.0
							if ipower != nil {
								power = ipower.(float64)
							}
							currProd = power / 6
						}
						cummProd = cummProd + currProd
						cummBudget = cummBudget + currBudget

						last30.CurrBudget = currBudget
						last30.CurrProduction = currProd
						last30.CumBudget = cummBudget / 1000000
						last30.CumProduction = cummProd / 1000000

						item30s = append(item30s, last30)

						dateData = dateId.Add(-1)
					}

					mdl.Productions = items
					mdl.CummulativeProductions = item30s

					d.BaseController.Ctx.Insert(mdl)
				}
			}
		}
	}
}
<commit_msg>change get project list on scada last update<commit_after>package generatorControllers

import (
	. "eaciit/wfdemo-git/library/helper"
	. "eaciit/wfdemo-git/library/models"
	. "eaciit/wfdemo-git/processapp/summaryGenerator/controllers"
	"eaciit/wfdemo-git/web/helper"
	_ "fmt"
	"log"
	"os"
	"strconv"
	_ "strings"
	"time"

	"strings"

	"github.com/eaciit/dbox"
	_ "github.com/eaciit/dbox/dbc/mongo"
	tk "github.com/eaciit/toolkit"
)

type GenScadaLast24 struct {
	*BaseController
}

func (d *GenScadaLast24) Generate(base *BaseController) {
	if base != nil {
		d.BaseController = base
		ctx, e := PrepareConnection()
		if e != nil {
			ErrorHandler(e, "Scada Summary")
			os.Exit(0)
		}

		d.BaseController.Ctx.DeleteMany(new(ScadaLastUpdate), dbox.And(dbox.Ne("_id", "")))

		budgetMonths := []float64{
			5911.8744,
			6023.419200000001,
			7027.3224,
			8588.9496,
			14389.2792,
			16954.8096,
			15727.8168,
			12046.8384,
			9704.3976,
			5688.784799999999,
			3569.4336,
			5911.8744}

		for _, proj := range d.BaseController.ProjectList {
			projectName := proj.Value
			turbineList := []TurbineOut{}

			if projectName != "Fleet" {
				turbineList, _ = helper.GetTurbineList([]interface{}{projectName})
			} else {
				turbineList, _ = helper.GetTurbineList(nil)
			}

			totalTurbine := len(turbineList)

			filter := []*dbox.Filter{}

			filter = append(filter, dbox.Gte("power", -200))
			if projectName != "Fleet" {
				filter = append(filter, dbox.Eq("projectname", projectName))
			}

			/*for _, v := range filter {
				log.Printf(">> %#v \n", v)
			}*/

			csr, e := ctx.NewQuery().
				From(new(ScadaData).TableName()).
				Where(dbox.And(filter...)).
				Aggr(dbox.AggrMax, "$timestamp", "timestamp").
				Aggr(dbox.AggrMax, "$dateinfo.dateid", "dateid").
				Group("").
				Cursor(nil)
			defer csr.Close()

			if e != nil {
				log.Printf("Error: %v \n", e.Error())
			} else {
				datas := []tk.M{}
				e = csr.Fetch(&datas, 0, false)

				tk.Printf(">> %#v \n", datas)

				if len(datas) > 0 {
					dateId := datas[0]["dateid"].(time.Time).UTC()
					dtInfo := GetDateInfo(dateId)
					maxTimeStamp := datas[0]["timestamp"].(time.Time).UTC()
					//startTime := maxTimeStamp.Add(-24 * time.Hour)
					var budgetCurrMonthDaily float64

					if len(budgetMonths)-1 >= int(dateId.Month()) {
						budgetCurrMonths := budgetMonths[int(dateId.Month())-1] * 1000.0
						noOfDay := float64(daysIn(dateId.Month(), dateId.Year()))
						budgetCurrMonthDaily = tk.Div(budgetCurrMonths, noOfDay)
					}

					mdl := new(ScadaLastUpdate).New()

					if projectName != "Fleet" {
						mdl.ID = "SCADALASTUPDATE_" + strings.ToUpper(projectName)
						mdl.ProjectName = projectName
						mdl.NoOfProjects = 1
					} else {
						mdl.ID = "SCADALASTUPDATE_FLEET"
						mdl.ProjectName = "Fleet"
						mdl.NoOfProjects = len(d.BaseController.ProjectList) - 1
					}

					for _, t := range turbineList {
						mdl.TotalMaxCapacity += t.Capacity
					}

					mdl.TotalMaxCapacity = tk.ToFloat64(mdl.TotalMaxCapacity*1000.0, 2, tk.RoundingAuto)
					mdl.LastUpdate = maxTimeStamp
					mdl.DateInfo = dtInfo
					mdl.NoOfTurbines = totalTurbine

					items := make([]LastData24Hours, 0)
					for i := 0; i < 24; i++ {
						year := strconv.Itoa(dateId.Year())
						month := dateId.Month().String()
						day := strconv.Itoa(dateId.Day())
						strTime := year + "-" + month + "-" + day + " " + strconv.Itoa(i) + ":00:00"
						timeHr, _ := time.Parse("2006-January-2 15:04:05", strTime)

						timeHrStart := timeHr.Add(-1 * time.Hour)

						filterSub := []*dbox.Filter{}
						filterSub = append(filterSub, dbox.Gt("timestamp", timeHrStart))
						filterSub = append(filterSub, dbox.Lte("timestamp", timeHr))
						filterSub = append(filterSub, dbox.Gte("power", -200))

						if projectName != "Fleet" {
							filterSub = append(filterSub, dbox.Eq("projectname", projectName))
						}

						csr, e = ctx.NewQuery().From(new(ScadaData).TableName()).
							Where(dbox.And(filterSub...)).
							Aggr(dbox.AggrSum, "$power", "totalpower").
							Aggr(dbox.AggrSum, "$powerlost", "totalpowerlost").
							Aggr(dbox.AggrSum, "$energylost", "energylost").
							Aggr(dbox.AggrSum, "$denpower", "denpower").
							Aggr(dbox.AggrSum, "$oktime", "totaloktime").
							Aggr(dbox.AggrSum, "$griddowntime", "totalgriddowntime").
							Aggr(dbox.AggrAvr, "$windspeed", "avgwindspeed").
							Group("projectname").
							Cursor(nil)
						defer csr.Close()

						scadas := []tk.M{}
						e = csr.Fetch(&scadas, 0, false)

						var last LastData24Hours
						if len(scadas) > 0 {
							data := scadas[0]
							trueAvail := 0.0
							gridAvail := 0.0

							ipower := data["totalpower"]
							power := 0.0
							if ipower != nil {
								power = ipower.(float64)
							}

							ipotentialpower := data["denpower"]
							potentialpower := 0.0
							if ipotentialpower != nil {
								potentialpower = ipotentialpower.(float64)
							}

							iwindspeed := data["avgwindspeed"]
							windspeed := 0.0
							if iwindspeed != nil {
								windspeed = iwindspeed.(float64)
							}
							last.Hour = i
							last.TimeHour = timeHr
							last.AvgWindSpeed = windspeed
							last.PowerKw = power
							last.EnergyKwh = power / 6
							last.Potential = potentialpower
							last.PotentialKwh = potentialpower / 6
							last.TrueAvail = trueAvail
							last.GridAvail = gridAvail
						} else {
							last.Hour = i
							last.TimeHour = timeHr
							last.AvgWindSpeed = 0.0
							last.PowerKw = 0.0
							last.EnergyKwh = 0.0
							last.Potential = 0.0
							last.PotentialKwh = 0.0
							last.TrueAvail = 0.0
							last.GridAvail = 0.0
						}

						items = append(items, last)
					}

					match := tk.M{}

					match.Set("dateinfo.monthid", tk.M{}.Set("$eq", dtInfo.MonthId)).Set("power", tk.M{}.Set("$gte", -200))

					if projectName != "Fleet" {
						match.Set("projectname", projectName)
					}

					pipe := []tk.M{tk.M{}.Set("$match", match), tk.M{}.Set("$group", tk.M{}.Set("_id", "$dateinfo.dateid").Set("totalpower", tk.M{}.Set("$sum", "$power"))), tk.M{}.Set("$sort", tk.M{}.Set("_id", 1))}

					csr, _ := ctx.NewQuery().
						Command("pipe", pipe).
						From(new(ScadaData).TableName()).
						Cursor(nil)
					defer csr.Close()

					scadas := []tk.M{}
					e = csr.Fetch(&scadas, 0, false)

					item30s := make([]Last30Days, 0)
					dateData := dateId
					cummProd := 0.0
					cummBudget := 0.0
					for _, data := range scadas {
						dateData = data["_id"].(time.Time)
						var last30 Last30Days
						last30.DateId = dateData
						last30.DayNo = dateData.Day()

						currProd := 0.0
						currBudget := budgetCurrMonthDaily // 565160.32
						if data != nil {
							ipower := data["totalpower"]
							power := 0.0
							if ipower != nil {
								power = ipower.(float64)
							}
							currProd = power / 6
						}
						cummProd = cummProd + currProd
						cummBudget = cummBudget + currBudget

						last30.CurrBudget = currBudget
						last30.CurrProduction = currProd
						last30.CumBudget = cummBudget / 1000000
						last30.CumProduction = cummProd / 1000000

						item30s = append(item30s, last30)

						dateData = dateId.Add(-1)
					}

					mdl.Productions = items
					mdl.CummulativeProductions = item30s

					d.BaseController.Ctx.Insert(mdl)
				}
			}
		}
	}
}
<|endoftext|>


<commit_before>package middleware

import (
	"context"
	"net/http"

	"github.com/nats-io/nuid"
)

type ctxRequestIDKeyType int

const CtxRequestIDKey int = 0

const (
	headerXRequestID = "X-Request-ID"
)

type RequestID struct {
	SetHeader bool
	n         *nuid.NUID
}

func NewRequestID() *RequestID {
	return &RequestID{
		SetHeader: true,
		n:         nuid.New(),
	}
}

func (rid *RequestID) ServeNext(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		requestid := rid.n.Next()
		if rid.SetHeader {
			w.Header().Set(headerXRequestID, requestid)
		}
		ctx := context.WithValue(r.Context(), CtxRequestIDKey, requestid)
		next.ServeHTTP(w, r.WithContext(ctx))
	}
	return http.HandlerFunc(fn)
}
<commit_msg>Fix request id ctx key<commit_after>package middleware

import (
	"context"
	"net/http"

	"github.com/nats-io/nuid"
)

type ctxRequestIDKeyType int

const CtxRequestIDKey ctxRequestIDKeyType = 0

const (
	headerXRequestID = "X-Request-ID"
)

type RequestID struct {
	SetHeader bool
	n         *nuid.NUID
}

func NewRequestID() *RequestID {
	return &RequestID{
		SetHeader: true,
		n:         nuid.New(),
	}
}

func (rid *RequestID) ServeNext(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		requestid := rid.n.Next()
		if rid.SetHeader {
			w.Header().Set(headerXRequestID, requestid)
		}
		ctx := context.WithValue(r.Context(), CtxRequestIDKey, requestid)
		next.ServeHTTP(w, r.WithContext(ctx))
	}
	return http.HandlerFunc(fn)
}
<|endoftext|>


<commit_before>package main

import (
	"mabetle/govendors/uuid"
)

func main() {
	for i := 0; i < 10; i++ {
		println(uuid.New())
	}
}
<commit_msg>fix lib'<commit_after>package main

import (
	"github.com/mabetle/govendors/uuid"
)

func main() {
	for i := 0; i < 10; i++ {
		println(uuid.New())
	}
}
<|endoftext|>


<commit_before>package docker

import (
	"errors"
	"strings"
	"time"

	dkc "github.com/fsouza/go-dockerclient"
	"github.com/op/go-logging"
)

// An Image is a Docker container which this library supports running.
type Image string

const (
	// Etcd consnesus provider.
	Etcd Image = "quay.io/coreos/etcd:v2.1.3"

	// Kubelet implements the kubernetes cluster scheduler.
	Kubelet Image = "quay.io/netsys/kubelet"

	// Ovnnorthd is the central controller for OVN network virtualization.
	Ovnnorthd Image = "quay.io/netsys/ovn-northd"

	// Ovncontroller is the per host controller for OVN network virtualization.
	Ovncontroller Image = "quay.io/netsys/ovn-controller"

	// Ovnoverlay is the ovn networking driver for docker
	Ovnoverlay Image = "quay.io/netsys/ovn-overlay"

	// Ovsvswitchd is the Open vSwitch control plane.
	Ovsvswitchd Image = "quay.io/netsys/ovs-vswitchd"

	// Ovsdb is teh database for Open vSwitch and OVN.
	Ovsdb Image = "quay.io/netsys/ovsdb-server"
)

const dockerSock = "unix:///var/run/docker.sock"

var log = logging.MustGetLogger("docker")

var errNoSuchContainer = errors.New("container does not exist")

// A Client to the local docker daemon.
type Client interface {
	Run(image Image, args []string) error
	Exec(image Image, cmd []string) error
	Remove(image Image) error
	RemoveAll()
	CreateLSwitch(name string) error
}

type docker struct {
	*dkc.Client
	running map[Image]struct{}
}

// New creates client to the docker daemon.
func New() Client {
	var client *dkc.Client
	for {
		var err error
		client, err = dkc.NewClient(dockerSock)
		if err != nil {
			log.Warning("Failed to create docker client: %s", err)
			time.Sleep(10 * time.Second)
			continue
		}
		break
	}

	dk := docker{client, make(map[Image]struct{})}
	images := []Image{Etcd, Kubelet, Ovnnorthd, Ovncontroller, Ovsvswitchd, Ovsdb}
	for _, image := range images {
		go dk.pull(image)
	}

	return dk
}

func (dk docker) Run(image Image, args []string) error {
	name := image.String()
	_, err := dk.get(name)
	if err == errNoSuchContainer {
		// Only log the first time we attempt to boot.
		log.Info("Start Container: %s", name)
	} else if err != nil {
		log.Warning("Failed to start container: %s", err.Error())
	}

	id, err := dk.create(name, image, args)
	if err != nil {
		log.Warning("Failed to create container %s: %s", name, err.Error())
		return err
	}

	if err = dk.StartContainer(id, hc(image)); err != nil {
		if _, ok := err.(*dkc.ContainerAlreadyRunning); !ok {
			log.Warning("Failed to start container %s: %s", name, err)
		}
		return err
	}

	dk.running[image] = struct{}{}
	return nil
}

func (dk docker) Exec(image Image, cmd []string) error {
	name := image.String()

	id, err := dk.get(name)
	if err != nil {
		return err
	}

	log.Info("Exec in %s: %s", name, strings.Join(cmd, " "))
	exec, err := dk.CreateExec(dkc.CreateExecOptions{Container: id, Cmd: cmd})
	if err != nil {
		log.Warning("Failed to exec %s %s: %s", name, cmd, err.Error())
		return err
	}

	err = dk.StartExec(exec.ID, dkc.StartExecOptions{})
	if err != nil {
		log.Warning("Failed to exec %s %s: %s", name, cmd, err.Error())
		return err
	}

	return nil
}

func (dk docker) Remove(image Image) error {
	name := image.String()

	id, err := dk.get(name)
	if err != nil {
		return err
	}

	log.Info("Remove Container: %s", name)
	err = dk.RemoveContainer(dkc.RemoveContainerOptions{ID: id, Force: true})
	if err != nil {
		log.Warning("Failed to remove container %s: %s", name, err)
		return err
	}

	return nil
}

func (dk docker) RemoveAll() {
	for name := range dk.running {
		dk.Remove(name)
		delete(dk.running, name)
	}
}

func (dk docker) CreateLSwitch(name string) error {
	_, err := dk.CreateNetwork(dkc.CreateNetworkOptions{
		Name:           name,
		CheckDuplicate: true,
		Driver:         "openvswitch"})
	if err != nil {
		log.Warning("Failed to create di lswitch: %s", err)
	}
	return err
}

func (dk docker) create(name string, image Image, args []string) (string, error) {
	dk.pull(image)

	id, err := dk.get(name)
	if err == nil {
		return id, nil
	}

	container, err := dk.CreateContainer(dkc.CreateContainerOptions{
		Name:   name,
		Config: &dkc.Config{Image: string(image), Cmd: args},
	})
	if err != nil {
		log.Warning("Failed to create container: %s", err)
		return "", err
	}

	return container.ID, nil
}

func (dk docker) get(name string) (string, error) {
	containers, err := dk.ListContainers(dkc.ListContainersOptions{All: true})
	if err != nil {
		return "", err
	}

	name = "/" + name
	for _, c := range containers {
		for _, cname := range c.Names {
			if name == cname {
				return c.ID, nil
			}
		}
	}

	return "", errNoSuchContainer
}

func (dk docker) pull(image Image) {
	err := dk.PullImage(dkc.PullImageOptions{Repository: string(image)},
		dkc.AuthConfiguration{})
	if err != nil {
		log.Warning("Failed to pull image: %s", err)
	}
}

func hc(image Image) *dkc.HostConfig {
	hc := dkc.HostConfig{
		NetworkMode: "host",
		Binds:       []string{"/usr/share/ca-certificates:/etc/ssl/certs"}}

	switch image {
	case Ovnoverlay:
		hc.Binds = []string{"/etc/docker:/etc/docker:rw"}
		fallthrough
	case Ovsvswitchd:
		hc.Privileged = true
		fallthrough
	case Ovnnorthd:
		fallthrough
	case Ovncontroller:
		hc.VolumesFrom = []string{Ovsdb.String()}
	case Kubelet:
		hc.Binds = []string{
			"/proc:/hostproc:ro",
			"/var/run:/var/run:rw",
			"/var/lib/docker:/var/lib/docker:rw",
			"/etc/docker:/etc/docker:rw",
			"/dev:/dev:rw",
			"/sys:/sys:ro",
		}
		hc.Privileged = true
		hc.PidMode = "host" // Use PID=host per the kubernetes documentation.
	default:
	}

	return &hc
}

func (image Image) String() string {
	switch image {
	case Etcd:
		return "etcd"
	case Kubelet:
		return "kubelet"
	case Ovnnorthd:
		return "ovn-northd"
	case Ovncontroller:
		return "ovn-controller"
	case Ovnoverlay:
		return "ovn-overlay"
	case Ovsvswitchd:
		return "ovs-vswitchd"
	case Ovsdb:
		return "ovsdb-server"
	default:
		panic("Unimplemented")
	}
}
<commit_msg>docker: Upgrade to the latest etcd image<commit_after>package docker

import (
	"errors"
	"strings"
	"time"

	dkc "github.com/fsouza/go-dockerclient"
	"github.com/op/go-logging"
)

// An Image is a Docker container which this library supports running.
type Image string

const (
	// Etcd consnesus provider.
	Etcd Image = "quay.io/coreos/etcd:v2.2.4"

	// Kubelet implements the kubernetes cluster scheduler.
	Kubelet Image = "quay.io/netsys/kubelet"

	// Ovnnorthd is the central controller for OVN network virtualization.
	Ovnnorthd Image = "quay.io/netsys/ovn-northd"

	// Ovncontroller is the per host controller for OVN network virtualization.
	Ovncontroller Image = "quay.io/netsys/ovn-controller"

	// Ovnoverlay is the ovn networking driver for docker
	Ovnoverlay Image = "quay.io/netsys/ovn-overlay"

	// Ovsvswitchd is the Open vSwitch control plane.
	Ovsvswitchd Image = "quay.io/netsys/ovs-vswitchd"

	// Ovsdb is teh database for Open vSwitch and OVN.
	Ovsdb Image = "quay.io/netsys/ovsdb-server"
)

const dockerSock = "unix:///var/run/docker.sock"

var log = logging.MustGetLogger("docker")

var errNoSuchContainer = errors.New("container does not exist")

// A Client to the local docker daemon.
type Client interface {
	Run(image Image, args []string) error
	Exec(image Image, cmd []string) error
	Remove(image Image) error
	RemoveAll()
	CreateLSwitch(name string) error
}

type docker struct {
	*dkc.Client
	running map[Image]struct{}
}

// New creates client to the docker daemon.
func New() Client {
	var client *dkc.Client
	for {
		var err error
		client, err = dkc.NewClient(dockerSock)
		if err != nil {
			log.Warning("Failed to create docker client: %s", err)
			time.Sleep(10 * time.Second)
			continue
		}
		break
	}

	dk := docker{client, make(map[Image]struct{})}
	images := []Image{Etcd, Kubelet, Ovnnorthd, Ovncontroller, Ovsvswitchd, Ovsdb}
	for _, image := range images {
		go dk.pull(image)
	}

	return dk
}

func (dk docker) Run(image Image, args []string) error {
	name := image.String()
	_, err := dk.get(name)
	if err == errNoSuchContainer {
		// Only log the first time we attempt to boot.
		log.Info("Start Container: %s", name)
	} else if err != nil {
		log.Warning("Failed to start container: %s", err.Error())
	}

	id, err := dk.create(name, image, args)
	if err != nil {
		log.Warning("Failed to create container %s: %s", name, err.Error())
		return err
	}

	if err = dk.StartContainer(id, hc(image)); err != nil {
		if _, ok := err.(*dkc.ContainerAlreadyRunning); !ok {
			log.Warning("Failed to start container %s: %s", name, err)
		}
		return err
	}

	dk.running[image] = struct{}{}
	return nil
}

func (dk docker) Exec(image Image, cmd []string) error {
	name := image.String()

	id, err := dk.get(name)
	if err != nil {
		return err
	}

	log.Info("Exec in %s: %s", name, strings.Join(cmd, " "))
	exec, err := dk.CreateExec(dkc.CreateExecOptions{Container: id, Cmd: cmd})
	if err != nil {
		log.Warning("Failed to exec %s %s: %s", name, cmd, err.Error())
		return err
	}

	err = dk.StartExec(exec.ID, dkc.StartExecOptions{})
	if err != nil {
		log.Warning("Failed to exec %s %s: %s", name, cmd, err.Error())
		return err
	}

	return nil
}

func (dk docker) Remove(image Image) error {
	name := image.String()

	id, err := dk.get(name)
	if err != nil {
		return err
	}

	log.Info("Remove Container: %s", name)
	err = dk.RemoveContainer(dkc.RemoveContainerOptions{ID: id, Force: true})
	if err != nil {
		log.Warning("Failed to remove container %s: %s", name, err)
		return err
	}

	return nil
}

func (dk docker) RemoveAll() {
	for name := range dk.running {
		dk.Remove(name)
		delete(dk.running, name)
	}
}

func (dk docker) CreateLSwitch(name string) error {
	_, err := dk.CreateNetwork(dkc.CreateNetworkOptions{
		Name:           name,
		CheckDuplicate: true,
		Driver:         "openvswitch"})
	if err != nil {
		log.Warning("Failed to create di lswitch: %s", err)
	}
	return err
}

func (dk docker) create(name string, image Image, args []string) (string, error) {
	dk.pull(image)

	id, err := dk.get(name)
	if err == nil {
		return id, nil
	}

	container, err := dk.CreateContainer(dkc.CreateContainerOptions{
		Name:   name,
		Config: &dkc.Config{Image: string(image), Cmd: args},
	})
	if err != nil {
		log.Warning("Failed to create container: %s", err)
		return "", err
	}

	return container.ID, nil
}

func (dk docker) get(name string) (string, error) {
	containers, err := dk.ListContainers(dkc.ListContainersOptions{All: true})
	if err != nil {
		return "", err
	}

	name = "/" + name
	for _, c := range containers {
		for _, cname := range c.Names {
			if name == cname {
				return c.ID, nil
			}
		}
	}

	return "", errNoSuchContainer
}

func (dk docker) pull(image Image) {
	err := dk.PullImage(dkc.PullImageOptions{Repository: string(image)},
		dkc.AuthConfiguration{})
	if err != nil {
		log.Warning("Failed to pull image: %s", err)
	}
}

func hc(image Image) *dkc.HostConfig {
	hc := dkc.HostConfig{
		NetworkMode: "host",
		Binds:       []string{"/usr/share/ca-certificates:/etc/ssl/certs"}}

	switch image {
	case Ovnoverlay:
		hc.Binds = []string{"/etc/docker:/etc/docker:rw"}
		fallthrough
	case Ovsvswitchd:
		hc.Privileged = true
		fallthrough
	case Ovnnorthd:
		fallthrough
	case Ovncontroller:
		hc.VolumesFrom = []string{Ovsdb.String()}
	case Kubelet:
		hc.Binds = []string{
			"/proc:/hostproc:ro",
			"/var/run:/var/run:rw",
			"/var/lib/docker:/var/lib/docker:rw",
			"/etc/docker:/etc/docker:rw",
			"/dev:/dev:rw",
			"/sys:/sys:ro",
		}
		hc.Privileged = true
		hc.PidMode = "host" // Use PID=host per the kubernetes documentation.
	default:
	}

	return &hc
}

func (image Image) String() string {
	switch image {
	case Etcd:
		return "etcd"
	case Kubelet:
		return "kubelet"
	case Ovnnorthd:
		return "ovn-northd"
	case Ovncontroller:
		return "ovn-controller"
	case Ovnoverlay:
		return "ovn-overlay"
	case Ovsvswitchd:
		return "ovs-vswitchd"
	case Ovsdb:
		return "ovsdb-server"
	default:
		panic("Unimplemented")
	}
}
<|endoftext|>