<commit_before>// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package announce

import (
	"github.com/bitmark-inc/bitmarkd/fault"
	"github.com/bitmark-inc/bitmarkd/messagebus"
	"github.com/bitmark-inc/logger"
	"time"
)

const (
	announceInitial     = 2 * time.Minute  // startup delay be for first send
	announceRebroadcast = 7 * time.Minute  // to prevent too frequent rebroadcasts
	announceInterval    = 11 * time.Minute // regular polling time
	announceExpiry      = 70 * time.Minute // if no responses received within this time, delete the entry
)

type announcer struct {
	log *logger.L
}

// initialise the announcer
func (ann *announcer) initialise() error {

	log := logger.New("announcer")
	if nil == log {
		return fault.ErrInvalidLoggerChannel
	}
	ann.log = log

	log.Info("initialising…")

	return nil
}

// wait for incoming requests, process them and reply
func (ann *announcer) Run(args interface{}, shutdown <-chan struct{}) {

	log := ann.log

	log.Info("starting…")

	delay := time.After(announceInitial)
loop:
	for {
		log.Info("waiting…")
		select {
		case <-shutdown:
			break loop
		case <-delay:
			delay = time.After(announceInterval)
			ann.process()
		}
	}
}

// process the ann and return response to client
func (ann *announcer) process() {

	log := ann.log

	log.Info("process starting…")

	globalData.Lock()
	defer globalData.Unlock()

	// announce this nodes IP and ports to other peers
	if globalData.rpcsSet {
		messagebus.Bus.Broadcast.Send("rpc", globalData.fingerprint[:], globalData.rpcs)
	}
	if globalData.peerSet {
		messagebus.Bus.Broadcast.Send("peer", globalData.publicKey, globalData.broadcasts, globalData.listeners)
	}

	if globalData.change {
		determineConnections(log)
		globalData.change = false
	}
	expireRPC()
}

func determineConnections(log *logger.L) {
	if nil == globalData.thisNode {
		log.Errorf("determineConnections called to early")
		return // called to early
	}

	// N1
	node := globalData.thisNode.Next()
	if nil == node {
		node = globalData.peerTree.First()
	}
	if nil == node || node == globalData.thisNode {
		log.Errorf("determineConnections tree too small")
		return // tree still too small
	}
	if globalData.n1 != node {
		globalData.n1 = node
		peer := node.Value().(*peerEntry)
		log.Infof("N1: this: %x", globalData.publicKey)
		log.Infof("N1: peer: %x", peer)
		messagebus.Bus.Subscriber.Send("N1", peer.publicKey, peer.broadcasts)
		messagebus.Bus.Connector.Send("N1", peer.publicKey, peer.listeners)
	}

	// N2
	node = node.Next()
	if nil == node {
		node = globalData.peerTree.First()
	}
	if nil == node || node == globalData.thisNode {
		return // tree still too small
	}

	// N3
	node = node.Next()
	if nil == node {
		node = globalData.peerTree.First()
	}
	if nil == node || node == globalData.thisNode {
		return // tree still too small
	}
	if globalData.n3 != node {
		globalData.n3 = node
		peer := node.Value().(*peerEntry)
		log.Infof("N3: this: %x", globalData.publicKey)
		log.Infof("N3: peer: %x", peer)
		messagebus.Bus.Subscriber.Send("N3", peer.publicKey, peer.broadcasts)
		messagebus.Bus.Connector.Send("N3", peer.publicKey, peer.listeners)
	}

	// ***** FIX THIS: more code to determine X25, X50 and X75 the cross ¼,½ and ¾ positions
	// ***** FIX THIS:   possible treat key as a number and compute; assuming uniformly distributed keys
	// ***** FIX THIS:   but would need the tree search to be able to find the "next highest/lowest key" for this to work
	// ***** FIX THIS: more code to determine some random positions
}
<commit_msg>[announce] find the X25, X50 and X75 nodes from a subtree and connect to them<commit_after>// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package announce

import (
	"fmt"
	"math/rand"

	"github.com/bitmark-inc/bitmarkd/avl"
	"github.com/bitmark-inc/bitmarkd/fault"
	"github.com/bitmark-inc/bitmarkd/messagebus"
	"github.com/bitmark-inc/logger"
	"time"
)

const (
	announceInitial     = 2 * time.Minute  // startup delay be for first send
	announceRebroadcast = 7 * time.Minute  // to prevent too frequent rebroadcasts
	announceInterval    = 11 * time.Minute // regular polling time
	announceExpiry      = 70 * time.Minute // if no responses received within this time, delete the entry
)

type announcer struct {
	log *logger.L
}

// initialise the announcer
func (ann *announcer) initialise() error {

	log := logger.New("announcer")
	if nil == log {
		return fault.ErrInvalidLoggerChannel
	}
	ann.log = log

	log.Info("initialising…")

	return nil
}

// wait for incoming requests, process them and reply
func (ann *announcer) Run(args interface{}, shutdown <-chan struct{}) {

	log := ann.log

	log.Info("starting…")

	delay := time.After(announceInitial)
loop:
	for {
		log.Info("waiting…")
		select {
		case <-shutdown:
			break loop
		case <-delay:
			delay = time.After(announceInterval)
			ann.process()
		}
	}
}

// process the ann and return response to client
func (ann *announcer) process() {

	log := ann.log

	log.Info("process starting…")

	globalData.Lock()
	defer globalData.Unlock()

	// announce this nodes IP and ports to other peers
	if globalData.rpcsSet {
		messagebus.Bus.Broadcast.Send("rpc", globalData.fingerprint[:], globalData.rpcs)
	}
	if globalData.peerSet {
		messagebus.Bus.Broadcast.Send("peer", globalData.publicKey, globalData.broadcasts, globalData.listeners)
	}

	if globalData.change {
		determineConnections(log)
		globalData.change = false
	}
	expireRPC()
}

func determineConnections(log *logger.L) {
	if nil == globalData.thisNode {
		log.Errorf("determineConnections called to early")
		return // called to early
	}

	// N1
	node := globalData.thisNode.Next()
	if nil == node {
		node = globalData.peerTree.First()
	}
	if nil == node || node == globalData.thisNode {
		log.Errorf("determineConnections tree too small")
		return // tree still too small
	}
	if globalData.n1 != node {
		globalData.n1 = node
		peer := node.Value().(*peerEntry)
		log.Infof("N1: this: %x", globalData.publicKey)
		log.Infof("N1: peer: %x", peer)
		messagebus.Bus.Subscriber.Send("N1", peer.publicKey, peer.broadcasts)
		messagebus.Bus.Connector.Send("N1", peer.publicKey, peer.listeners)
	}

	// N2
	node = node.Next()
	if nil == node {
		node = globalData.peerTree.First()
	}
	if nil == node || node == globalData.thisNode {
		return // tree still too small
	}

	// N3
	node = node.Next()
	if nil == node {
		node = globalData.peerTree.First()
	}
	if nil == node || node == globalData.thisNode {
		return // tree still too small
	}
	if globalData.n3 != node {
		globalData.n3 = node
		peer := node.Value().(*peerEntry)
		log.Infof("N3: this: %x", globalData.publicKey)
		log.Infof("N3: peer: %x", peer)
		messagebus.Bus.Subscriber.Send("N3", peer.publicKey, peer.broadcasts)
		messagebus.Bus.Connector.Send("N3", peer.publicKey, peer.listeners)
	}

	// ***** FIX THIS: more code to determine X25, X50 and X75 the cross ¼,½ and ¾ positions
	thisNode := globalData.thisNode
	nodeDepth := thisNode.Depth()
	treeRoot := globalData.peerTree.Root()
	lv2NodeChildren := treeRoot.GetChildrenByDepth(2)

	toConnectTree := make([]*avl.Node, 0, 3)
	toConnectNode := make([]*avl.Node, 0, 3)
	var connectOrder uint

	if nodeDepth < 2 {
		if len(lv2NodeChildren) > 3 {
			switch thisNode.Key().Compare(treeRoot.Key()) {
			case -1:
				toConnectTree = lv2NodeChildren[:3]
			case 1:
				fallthrough
			case 0:
				toConnectTree = lv2NodeChildren[1:]
			}
		} else {
			toConnectTree = lv2NodeChildren
		}
		connectOrder = uint(rand.Uint32())
	} else if nodeDepth >= 2 {
		depth2Parent := thisNode
		// find parent node in level 2 by search parent recursively
		for l := nodeDepth; l > 2; l-- {
			depth2Parent = depth2Parent.Parent()
		}

		// try to find rest of nodes which is not an ancestor in level 2
		for _, n := range lv2NodeChildren {
			if n.Key().Compare(depth2Parent.Key()) != 0 {
				toConnectTree = append(toConnectTree, n)
			}
		}
		connectOrder = depth2Parent.GetOrder(thisNode.Key())
	}

	for _, n := range toConnectTree {
		toConnectNode = append(toConnectNode, n.GetNodeByOrder(connectOrder))
	}

	for i, node := range toConnectNode {
		if node == globalData.thisNode || node == globalData.n1 || node == globalData.n3 {
			continue
		}
		if node == nil {
			fmt.Printf("No.%d of toConnectNode is nil : %+v. Node level is : %d", i, toConnectNode, nodeDepth)
			continue
		}

		peer := node.Value().(*peerEntry)
		priority := fmt.Sprintf("X%d", (i+1)*25)
		log.Infof("%s: this: %x", priority, globalData.publicKey)
		log.Infof("%s: peer: %x", priority, peer)
		messagebus.Bus.Subscriber.Send(priority, peer.publicKey, peer.broadcasts)
		messagebus.Bus.Connector.Send(priority, peer.publicKey, peer.listeners)
	}
	// ***** FIX THIS:   possible treat key as a number and compute; assuming uniformly distributed keys
	// ***** FIX THIS:   but would need the tree search to be able to find the "next highest/lowest key" for this to work
	// ***** FIX THIS: more code to determine some random positions
}
<|endoftext|>


<commit_before>// An exercise from Russ Cox's "A Tour of Go" course at USENIX 2010
// Here's the original paper: http://swtch.com/usenix/go-course.pdf

package main

import (
	"flag"
	"fmt"
	"image"
	"image/color"
	"image/png"
	"log"
	"math/cmplx"
	"os"
)

type Newton struct {
	Dx, Dy int
	Z0, Dz complex128
	N      int
}

func NewNewton(dx, dy int, z0, dz complex128, n int) *Newton {
	return &Newton{dx, dy, z0, dz, n}
}

func (n *Newton) Bounds() image.Rectangle {
	return image.Rect(0, 0, n.Dx, n.Dy)
}

func (n *Newton) ColorModel() color.Model {
	return color.NRGBAModel
}

func (n *Newton) At(x, y int) color.Color {
	// log.Println("Newton:At")
	fx := float64(x) / float64(n.Dx)
	fy := float64(y) / float64(n.Dy)

	Z := n.Z0 + complex(real(n.Dz)*fx, imag(n.Dz)*fy)
	r := []complex128{complex(1, 0), complex(-1, 1), complex(-1, -1)}
	c := []int{0, 0, 0}
	for i, v := range r {
		z := Z
		// log.Println(i, v)
		for c[i] = n.N; c[i] > 0; c[i]-- {
			z0 := z
			z -= (z*z*z - v) / (3 * z * z)
			if cmplx.Abs(z-z0) < 1e-7 {
				break
			}
			// log.Println(i, c[i], z-z0)
		}
	}

	// log.Println(c)
	adj := func(a int) uint8 { return uint8((0xff * a) / n.N) }
	return color.NRGBA{adj(c[2]), adj(c[0]), adj(c[1]), 0xff}
}

func main() {
	var z0r *float64 = flag.Float64("z0r", -3, "z0 real part")
	var z0i *float64 = flag.Float64("z0i", -3, "z0 imaginary part")
	var z1r *float64 = flag.Float64("z1r", 3, "z1 real part")
	var z1i *float64 = flag.Float64("z1i", 3, "z1 imaginary part")

	flag.Parse()

	z0 := complex(*z0r, *z0i)
	z1 := complex(*z1r, *z1i)
	dz := z1 - z0
	n := NewNewton(2048, 2048, z0, dz, 64)
	name := fmt.Sprintf("newton%+f%+fi%+f%+fi.png", real(z0), imag(z0), real(z1), imag(z1))
	if store, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY, 0666); err == nil {
		defer store.Close()
		png.Encode(store, n)
	} else {
		log.Println("newton: ", err)
	}
}
<commit_msg>minor edits.<commit_after>// An exercise from Russ Cox's "A Tour of Go" course at USENIX 2010
// Here's the original paper: http://swtch.com/usenix/go-course.pdf

package main

import (
	"flag"
	"fmt"
	"image"
	"image/color"
	"image/png"
	"log"
	"math/cmplx"
	"os"
)

type Newton struct {
	Dx, Dy int
	Z0, Dz complex128
	N      int
}

func NewNewton(dx, dy int, z0, dz complex128, n int) *Newton {
	return &Newton{dx, dy, z0, dz, n}
}

func (n *Newton) Bounds() image.Rectangle {
	return image.Rect(0, 0, n.Dx, n.Dy)
}

func (n *Newton) ColorModel() color.Model {
	return color.NRGBAModel
}

func (n *Newton) At(x, y int) color.Color {
	fx := float64(x) / float64(n.Dx)
	fy := float64(y) / float64(n.Dy)

	Z := n.Z0 + complex(real(n.Dz)*fx, imag(n.Dz)*fy)
	r := []complex128{complex(1, 0), complex(-1, 1), complex(-1, -1)}
	c := []int{0, 0, 0}
	for i, v := range r {
		z := Z
		for c[i] = n.N; c[i] > 0; c[i]-- {
			z0 := z
			z -= (z*z*z - v) / (3 * z * z)
			if cmplx.Abs(z-z0) < 1e-7 {
				break
			}
		}
	}

	adj := func(a int) uint8 { return uint8((0xff * a) / n.N) }
	return color.NRGBA{adj(c[2]), adj(c[0]), adj(c[1]), 0xff}
}

func main() {
	var z0r *float64 = flag.Float64("z0r", -3, "z0 real part")
	var z0i *float64 = flag.Float64("z0i", -3, "z0 imaginary part")
	var z1r *float64 = flag.Float64("z1r", 3, "z1 real part")
	var z1i *float64 = flag.Float64("z1i", 3, "z1 imaginary part")

	flag.Parse()

	z0 := complex(*z0r, *z0i)
	z1 := complex(*z1r, *z1i)
	dz := z1 - z0
	n := NewNewton(2048, 2048, z0, dz, 64)
	name := fmt.Sprintf("newton%+f%+fi%+f%+fi.png", real(z0), imag(z0), real(z1), imag(z1))
	if store, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY, 0666); err == nil {
		defer store.Close()
		png.Encode(store, n)
	} else {
		log.Println("newton: ", err)
	}
}
<|endoftext|>


<commit_before>package iterbench

import "testing"

func TestIterFor(t *testing.T) {
	_ = iterFor(10)
}

func TestIterGoto(t *testing.T) {
	_ = iterGoto(10)
}

var result int

func BenchmarkIterFor(b *testing.B) {
	var r int
	for n := 0; n < b.N; n++ {
		r = iterFor(10)
	}
	result = r
}

func BenchmarkIterGoto(b *testing.B) {
	var r int
	for n := 0; n < b.N; n++ {
		r = iterGoto(10)
	}
	result = r
}
<commit_msg>iterbench: add subtests<commit_after>package iterbench

import "testing"

type testType struct {
	in   int
	want int
}

type testFunc func(int) int

var tests = []testType{
	{10, 55},
	{100, 5050},
	{1000, 500500},
	{10000, 50005000},
	{100000, 5000050000},
}

func testIter(t *testing.T, tests []testType, funcName string, fn testFunc) {
	for _, tt := range tests {
		if got := iterFor(tt.in); got != tt.want {
			t.Errorf("%s(%d) = %d; want %d",
				funcName, tt.in, got, tt.want)
		}
	}
}

func TestIter(t *testing.T) {
	t.Run("iterFor", func(t *testing.T) {
		testIter(t, tests, "iterFor", iterFor)
	})
	t.Run("iterGoto", func(t *testing.T) {
		testIter(t, tests, "iterGoto", iterGoto)
	})
}

func TestIterGoto(t *testing.T) {
	_ = iterGoto(10)
}

var result int

func BenchmarkIterFor(b *testing.B) {
	var r int
	for n := 0; n < b.N; n++ {
		r = iterFor(10)
	}
	result = r
}

func BenchmarkIterGoto(b *testing.B) {
	var r int
	for n := 0; n < b.N; n++ {
		r = iterGoto(10)
	}
	result = r
}
<|endoftext|>


<commit_before>// Package notify provides an implementation of the Freedesktop Notifications Specification
// using the DBus API.
package notify

import "github.com/godbus/dbus"

// Notification expire timeout
const (
	ExpiresDefault = -1
	ExpiresNever   = 0
)

// Notification Categories
const (
	ClassDevice              = "device"
	ClassDeviceAdded         = "device.added"
	ClassDeviceError         = "device.error"
	ClassDeviceRemoved       = "device.removed"
	ClassEmail               = "email"
	ClassEmailArrived        = "email.arrived"
	ClassEmailBounced        = "email.bounced"
	ClassIm                  = "im"
	ClassImError             = "im.error"
	ClassImReceived          = "im.received"
	ClassNetwork             = "network"
	ClassNetworkConnected    = "network.connected"
	ClassNetworkDisconnected = "network.disconnected"
	ClassNetworkError        = "network.error"
	ClassPresence            = "presence"
	ClassPresenceOffline     = "presence.offline"
	ClassPresenceOnline      = "presence.online"
	ClassTransfer            = "transfer"
	ClassTransferComplete    = "transfer.complete"
	ClassTransferError       = "transfer.error"
)

// Urgency Levels
const (
	UrgencyLow      = 0
	UrgencyNormal   = 1
	UrgencyCritical = 2
)

// Hints
const (
	HintActionIcons   = "action-icons"
	HintCategory      = "category"
	HintDesktopEntry  = "desktop-entry"
	HintImageData     = "image-data"
	HintImagePath     = "image-path"
	HintResident      = "resident"
	HintSoundFile     = "sound-file"
	HintSoundName     = "sound-name"
	HintSuppressSound = "suppress-sound"
	HintTransient     = "transient"
	HintX             = "x"
	HintY             = "y"
	HintUrgency       = "urgency"
)

type Capabilities struct {
	// Supports using icons instead of text for displaying actions.
	ActionIcons bool

	// The server will provide any specified actions to the user.
	Actions bool

	// Supports body text. Some implementations may only show the summary.
	Body bool

	// The server supports hyperlinks in the notifications.
	BodyHyperlinks bool

	// The server supports images in the notifications.
	BodyImages bool

	// Supports markup in the body text.
	BodyMarkup bool

	// The server will render an animation of all the frames in a given image array.
	IconMulti bool

	// Supports display of exactly 1 frame of any given image array.
	IconStatic bool

	// The server supports persistence of notifications. Notifications will be
	// retained until they are acknowledged or removed by the user or recalled by the sender.
	Persistence bool

	// The server supports sounds on notifications.
	Sound bool
}

// GetCapabilities returns the capabilities of the notification server.
func GetCapabilities() (c Capabilities, err error) {
	connection, err := dbus.SessionBus()
	if err != nil {
		return
	}
	obj := connection.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")

	call := obj.Call("org.freedesktop.Notifications.GetCapabilities", 0)
	if call.Err != nil {
		return Capabilities{}, call.Err
	}

	s := []string{}
	if err = call.Store(&s); err != nil {
		return
	}
	for _, v := range s {
		switch v {
		case "action-icons":
			c.ActionIcons = true
			break
		case "actions":
			c.Actions = true
			break
		case "body":
			c.Body = true
			break
		case "body-hyperlinks":
			c.BodyHyperlinks = true
			break
		case "body-images":
			c.BodyImages = true
			break
		case "body-markup":
			c.BodyMarkup = true
			break
		case "icon-multi":
			c.IconMulti = true
			break
		case "icon-static":
			c.IconStatic = true
			break
		case "persistence":
			c.Persistence = true
			break
		case "sound":
			c.Sound = true
			break
		}
	}
	return
}

type ServerInformation struct {
	// The name of the notification server daemon
	Name string

	// The vendor of the notification server
	Vendor string

	// Version of the notification server
	Version string

	// Spec version the notification server conforms to
	SpecVersion string
}

// GetServerInformation returns information about the notification server such as its name
// and version.
func GetServerInformation() (i ServerInformation, err error) {
	connection, err := dbus.SessionBus()
	if err != nil {
		return
	}
	obj := connection.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")

	call := obj.Call("org.freedesktop.Notifications.GetServerInformation", 0)
	if call.Err != nil {
		return ServerInformation{}, call.Err
	}

	if err = call.Store(&i.Name, &i.Vendor, &i.Version, &i.SpecVersion); err != nil {
		return
	}
	return
}

type Notification struct {
	// The optional name of the application sending the notification. Can be blank.
	AppName string

	// The optional notification ID that this notification replaces.
	ReplacesID uint32

	// The optional program icon of the calling application.
	AppIcon string

	// The summary text briefly describing the notification.
	Summary string

	// The optional detailed body text.
	Body string

	// The actions send a request message back to the notification client when invoked.
	Actions []string

	// Hints are a way to provide extra data to a notification server.
	Hints map[string]interface{}

	// The timeout time in milliseconds since the display of the notification at which
	// the notification should automatically close.
	Timeout int32
}

// NewNotification creates a new notification object with some basic information.
func NewNotification(summary, body string) Notification {
	return Notification{Summary: summary, Body: body, Timeout: ExpiresDefault}
}

// Show sends the information in the notification object to the server to be displayed.
func (n Notification) Show() (id uint32, err error) {

	// We need to convert the interface type of the map to dbus.Variant as people
	// dont want to have to import the dbus package just to make use of the notification
	// hints.
	hints := map[string]dbus.Variant{}
	if len(n.Hints) != 0 {
		for k, v := range n.Hints {
			hints[k] = dbus.MakeVariant(v)
		}
	}

	connection, err := dbus.SessionBus()
	if err != nil {
		return
	}
	obj := connection.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")

	call := obj.Call(
		"org.freedesktop.Notifications.Notify",
		0,
		n.AppName,
		n.ReplacesID,
		n.AppIcon,
		n.Summary,
		n.Body,
		n.Actions,
		hints,
		n.Timeout)
	if call.Err != nil {
		return 0, call.Err
	}

	if err = call.Store(&id); err != nil {
		return
	}
	return
}

// CloseNotification closes the notification if it exists using its id.
func CloseNotification(id uint32) (err error) {
	connection, err := dbus.SessionBus()
	if err != nil {
		return
	}
	obj := connection.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")

	call := obj.Call("org.freedesktop.Notifications.CloseNotification", 0, id)
	if call.Err != nil {
		return call.Err
	}
	return
}
<commit_msg>changed notification urgency type to bytes<commit_after>// Package notify provides an implementation of the Freedesktop Notifications Specification
// using the DBus API.
package notify

import "github.com/godbus/dbus"

// Notification expire timeout
const (
	ExpiresDefault = -1
	ExpiresNever   = 0
)

// Notification Categories
const (
	ClassDevice              = "device"
	ClassDeviceAdded         = "device.added"
	ClassDeviceError         = "device.error"
	ClassDeviceRemoved       = "device.removed"
	ClassEmail               = "email"
	ClassEmailArrived        = "email.arrived"
	ClassEmailBounced        = "email.bounced"
	ClassIm                  = "im"
	ClassImError             = "im.error"
	ClassImReceived          = "im.received"
	ClassNetwork             = "network"
	ClassNetworkConnected    = "network.connected"
	ClassNetworkDisconnected = "network.disconnected"
	ClassNetworkError        = "network.error"
	ClassPresence            = "presence"
	ClassPresenceOffline     = "presence.offline"
	ClassPresenceOnline      = "presence.online"
	ClassTransfer            = "transfer"
	ClassTransferComplete    = "transfer.complete"
	ClassTransferError       = "transfer.error"
)

// Urgency Levels
const (
	UrgencyLow      = byte(0)
	UrgencyNormal   = byte(1)
	UrgencyCritical = byte(2)
)

// Hints
const (
	HintActionIcons   = "action-icons"
	HintCategory      = "category"
	HintDesktopEntry  = "desktop-entry"
	HintImageData     = "image-data"
	HintImagePath     = "image-path"
	HintResident      = "resident"
	HintSoundFile     = "sound-file"
	HintSoundName     = "sound-name"
	HintSuppressSound = "suppress-sound"
	HintTransient     = "transient"
	HintX             = "x"
	HintY             = "y"
	HintUrgency       = "urgency"
)

type Capabilities struct {
	// Supports using icons instead of text for displaying actions.
	ActionIcons bool

	// The server will provide any specified actions to the user.
	Actions bool

	// Supports body text. Some implementations may only show the summary.
	Body bool

	// The server supports hyperlinks in the notifications.
	BodyHyperlinks bool

	// The server supports images in the notifications.
	BodyImages bool

	// Supports markup in the body text.
	BodyMarkup bool

	// The server will render an animation of all the frames in a given image array.
	IconMulti bool

	// Supports display of exactly 1 frame of any given image array.
	IconStatic bool

	// The server supports persistence of notifications. Notifications will be
	// retained until they are acknowledged or removed by the user or recalled by the sender.
	Persistence bool

	// The server supports sounds on notifications.
	Sound bool
}

// GetCapabilities returns the capabilities of the notification server.
func GetCapabilities() (c Capabilities, err error) {
	connection, err := dbus.SessionBus()
	if err != nil {
		return
	}
	obj := connection.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")

	call := obj.Call("org.freedesktop.Notifications.GetCapabilities", 0)
	if call.Err != nil {
		return Capabilities{}, call.Err
	}

	s := []string{}
	if err = call.Store(&s); err != nil {
		return
	}
	for _, v := range s {
		switch v {
		case "action-icons":
			c.ActionIcons = true
			break
		case "actions":
			c.Actions = true
			break
		case "body":
			c.Body = true
			break
		case "body-hyperlinks":
			c.BodyHyperlinks = true
			break
		case "body-images":
			c.BodyImages = true
			break
		case "body-markup":
			c.BodyMarkup = true
			break
		case "icon-multi":
			c.IconMulti = true
			break
		case "icon-static":
			c.IconStatic = true
			break
		case "persistence":
			c.Persistence = true
			break
		case "sound":
			c.Sound = true
			break
		}
	}
	return
}

type ServerInformation struct {
	// The name of the notification server daemon
	Name string

	// The vendor of the notification server
	Vendor string

	// Version of the notification server
	Version string

	// Spec version the notification server conforms to
	SpecVersion string
}

// GetServerInformation returns information about the notification server such as its name
// and version.
func GetServerInformation() (i ServerInformation, err error) {
	connection, err := dbus.SessionBus()
	if err != nil {
		return
	}
	obj := connection.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")

	call := obj.Call("org.freedesktop.Notifications.GetServerInformation", 0)
	if call.Err != nil {
		return ServerInformation{}, call.Err
	}

	if err = call.Store(&i.Name, &i.Vendor, &i.Version, &i.SpecVersion); err != nil {
		return
	}
	return
}

type Notification struct {
	// The optional name of the application sending the notification. Can be blank.
	AppName string

	// The optional notification ID that this notification replaces.
	ReplacesID uint32

	// The optional program icon of the calling application.
	AppIcon string

	// The summary text briefly describing the notification.
	Summary string

	// The optional detailed body text.
	Body string

	// The actions send a request message back to the notification client when invoked.
	Actions []string

	// Hints are a way to provide extra data to a notification server.
	Hints map[string]interface{}

	// The timeout time in milliseconds since the display of the notification at which
	// the notification should automatically close.
	Timeout int32
}

// NewNotification creates a new notification object with some basic information.
func NewNotification(summary, body string) Notification {
	return Notification{Summary: summary, Body: body, Timeout: ExpiresDefault}
}

// Show sends the information in the notification object to the server to be displayed.
func (n Notification) Show() (id uint32, err error) {

	// We need to convert the interface type of the map to dbus.Variant as people
	// dont want to have to import the dbus package just to make use of the notification
	// hints.
	hints := map[string]dbus.Variant{}
	if len(n.Hints) != 0 {
		for k, v := range n.Hints {
			hints[k] = dbus.MakeVariant(v)
		}
	}

	connection, err := dbus.SessionBus()
	if err != nil {
		return
	}
	obj := connection.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")

	call := obj.Call(
		"org.freedesktop.Notifications.Notify",
		0,
		n.AppName,
		n.ReplacesID,
		n.AppIcon,
		n.Summary,
		n.Body,
		n.Actions,
		hints,
		n.Timeout)
	if call.Err != nil {
		return 0, call.Err
	}

	if err = call.Store(&id); err != nil {
		return
	}
	return
}

// CloseNotification closes the notification if it exists using its id.
func CloseNotification(id uint32) (err error) {
	connection, err := dbus.SessionBus()
	if err != nil {
		return
	}
	obj := connection.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")

	call := obj.Call("org.freedesktop.Notifications.CloseNotification", 0, id)
	if call.Err != nil {
		return call.Err
	}
	return
}
<|endoftext|>


<commit_before>package server

import (
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

// Start start server
func Start(addr string, appService *ServiceProvider) {
	router := gin.Default()
	router.Use(cors.New(cors.Config{
		AllowMethods: []string{"GET", "POST"},
		AllowHeaders: []string{"Content-Type"},
		ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin",
			"Access-Control-Allow-Headers", "Access-Control-Allow-Methods"},
		//AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			logrus.Info("origin is ", origin)
			return true
		},
		MaxAge: 12 * time.Hour,
	}))

	router.GET("/n/:shortpath", WrapeService(appService, ParseURL))
	router.GET("/health", HealthCheck)
	router.POST("/api/v1/shorten", WrapeService(appService, ShortenURL))

	router.Run(addr)
}

type RequestHandler func(*gin.Context, *ServiceProvider)

func WrapeService(appService *ServiceProvider, handler RequestHandler) gin.HandlerFunc {
	return func(c *gin.Context) {
		handler(c, appService)
	}
}
<commit_msg>add BuildEngine for testable<commit_after>package server

import (
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

// BuildEngine return gin.Engine with route
func BuildEngine(appService *ServiceProvider) *gin.Engine {
	router := gin.Default()
	router.Use(cors.New(cors.Config{
		AllowMethods: []string{"GET", "POST"},
		AllowHeaders: []string{"Content-Type"},
		ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin",
			"Access-Control-Allow-Headers", "Access-Control-Allow-Methods"},
		//AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			logrus.Info("origin is ", origin)
			return true
		},
		MaxAge: 12 * time.Hour,
	}))

	router.GET("/n/:shortpath", WrapeService(appService, ParseURL))
	router.GET("/health", HealthCheck)
	router.POST("/api/v1/shorten", WrapeService(appService, ShortenURL))

	return router
}

// Start start server
func Start(addr string, appService *ServiceProvider) {
	router := BuildEngine(appService)
	router.Run(addr)
}

type RequestHandler func(*gin.Context, *ServiceProvider)

func WrapeService(appService *ServiceProvider, handler RequestHandler) gin.HandlerFunc {
	return func(c *gin.Context) {
		handler(c, appService)
	}
}
<|endoftext|>


<commit_before>/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// spoof contains logic to make polling HTTP requests against an endpoint with optional host spoofing.

package spoof

import (
	"net"
	"net/url"
	"strings"
)

func isTCPTimeout(e error) bool {
	err, ok := e.(net.Error)
	return err != nil && ok && err.Timeout()
}

func isDNSError(err error) bool {
	if err, ok := err.(*url.Error); err != nil && ok {
		if err, ok := err.Err.(*net.OpError); err != nil && ok {
			if err, ok := err.Err.(*net.DNSError); err != nil && ok {
				return true
			}
		}
	}
	return false
}

func isTCPConnectRefuse(err error) bool {
	// The alternative for the string check is:
	// 	errNo := (((err.(*url.Error)).Err.(*net.OpError)).Err.(*os.SyscallError).Err).(syscall.Errno)
	// if errNo == syscall.Errno(0x6f) {...}
	// But with assertions, of course.
	if err != nil && strings.Contains(err.Error(), "connect: connection refused") {
		return true
	}
	return false
}
<commit_msg>Use string match to classify DNS error. (#376)<commit_after>/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// spoof contains logic to make polling HTTP requests against an endpoint with optional host spoofing.

package spoof

import (
	"net"
	"strings"
)

func isTCPTimeout(e error) bool {
	err, ok := e.(net.Error)
	return err != nil && ok && err.Timeout()
}

func isDNSError(err error) bool {
	if err == nil {
		return false
	}
	// Checking by casting to url.Error and casting the nested error
	// seems to be not as robust as string check.
	msg := strings.ToLower(err.Error())
	// Example error message:
	//   > Get http://this.url.does.not.exist: dial tcp: lookup this.url.does.not.exist on 127.0.0.1:53: no such host
	return strings.Contains(msg, "no such host") || strings.Contains(msg, ":53")
}

func isTCPConnectRefuse(err error) bool {
	// The alternative for the string check is:
	// 	errNo := (((err.(*url.Error)).Err.(*net.OpError)).Err.(*os.SyscallError).Err).(syscall.Errno)
	// if errNo == syscall.Errno(0x6f) {...}
	// But with assertions, of course.
	if err != nil && strings.Contains(err.Error(), "connect: connection refused") {
		return true
	}
	return false
}
<|endoftext|>


<commit_before>package functions

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"html/template"
	"io"
	"os"
	"strings"
	"time"
)

var (
	Bold = func(content string) (template.HTML, error) {
		return template.HTML("" + content + ""), nil
	}
	Italic = func(content string) (template.HTML, error) {
		return template.HTML("" + content + ""), nil
	}
	Underline = func(content string) (template.HTML, error) {
		return template.HTML("" + content + ""), nil
	}
)

func Exists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return true
}

func MakeTimestamp() int64 {
	return time.Now().UnixNano() / int64(time.Millisecond)
}

func GetFilenameNoExtension(s string) string {
	n := strings.LastIndexByte(s, '.')
	if n >= 0 {
		return s[:n]
	}
	return s
}

func RemoveDuplicates(a []string) []string {
	result := []string{}
	seen := map[string]string{}
	for _, val := range a {
		if _, ok := seen[val]; !ok {
			result = append(result, val)
			seen[val] = val
		}
	}
	return result
}

func encodeBase64(b []byte) string {
	return base64.StdEncoding.EncodeToString(b)
}

func decodeBase64(s string) []byte {
	data, err := base64.StdEncoding.DecodeString(s)
	if err != nil {
		panic(err)
	}
	return data
}

func Encrypt(key, text []byte) string {
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
	ciphertext := make([]byte, aes.BlockSize+len(text))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		panic(err)
	}
	cfb := cipher.NewCFBEncrypter(block, iv)
	cfb.XORKeyStream(ciphertext[aes.BlockSize:], text)
	return encodeBase64(ciphertext)
}

func Decrypt(key []byte, b64 string) string {
	text := decodeBase64(b64)
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
	if len(text) < aes.BlockSize {
		panic("ciphertext too short")
	}
	iv := text[:aes.BlockSize]
	text = text[aes.BlockSize:]
	cfb := cipher.NewCFBDecrypter(block, iv)
	cfb.XORKeyStream(text, text)
	return string(text)
}
<commit_msg>Added Copyfile to functions<commit_after>package functions

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"html/template"
	"io"
	"os"
	"strings"
	"time"
)

var (
	Bold = func(content string) (template.HTML, error) {
		return template.HTML("" + content + ""), nil
	}
	Italic = func(content string) (template.HTML, error) {
		return template.HTML("" + content + ""), nil
	}
	Underline = func(content string) (template.HTML, error) {
		return template.HTML("" + content + ""), nil
	}
)

func Exists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return true
}

func MakeTimestamp() int64 {
	return time.Now().UnixNano() / int64(time.Millisecond)
}

func GetFilenameNoExtension(s string) string {
	n := strings.LastIndexByte(s, '.')
	if n >= 0 {
		return s[:n]
	}
	return s
}

func RemoveDuplicates(a []string) []string {
	result := []string{}
	seen := map[string]string{}
	for _, val := range a {
		if _, ok := seen[val]; !ok {
			result = append(result, val)
			seen[val] = val
		}
	}
	return result
}

func encodeBase64(b []byte) string {
	return base64.StdEncoding.EncodeToString(b)
}

func decodeBase64(s string) []byte {
	data, err := base64.StdEncoding.DecodeString(s)
	if err != nil {
		panic(err)
	}
	return data
}

func Encrypt(key, text []byte) string {
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
	ciphertext := make([]byte, aes.BlockSize+len(text))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		panic(err)
	}
	cfb := cipher.NewCFBEncrypter(block, iv)
	cfb.XORKeyStream(ciphertext[aes.BlockSize:], text)
	return encodeBase64(ciphertext)
}

func Decrypt(key []byte, b64 string) string {
	text := decodeBase64(b64)
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
	if len(text) < aes.BlockSize {
		panic("ciphertext too short")
	}
	iv := text[:aes.BlockSize]
	text = text[aes.BlockSize:]
	cfb := cipher.NewCFBDecrypter(block, iv)
	cfb.XORKeyStream(text, text)
	return string(text)
}

func CopyFile(src, dst string) (err error) {
	sfi, err := os.Stat(src)
	if err != nil {
		return
	}
	if !sfi.Mode().IsRegular() {
		return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
	}
	dfi, err := os.Stat(dst)
	if err != nil {
		if !os.IsNotExist(err) {
			return
		}
	} else {
		if !(dfi.Mode().IsRegular()) {
			return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
		}
		if os.SameFile(sfi, dfi) {
			return
		}
	}
	if err = os.Link(src, dst); err == nil {
		return
	}
	err = copyFileContents(src, dst)
	return
}

func copyFileContents(src, dst string) (err error) {
	in, err := os.Open(src)
	if err != nil {
		return
	}
	defer in.Close()
	out, err := os.Create(dst)
	if err != nil {
		return
	}
	defer func() {
		cerr := out.Close()
		if err == nil {
			err = cerr
		}
	}()
	if _, err = io.Copy(out, in); err != nil {
		return
	}
	err = out.Sync()
	return
}
<|endoftext|>


<commit_before>package gosql

import (
	"fmt"
	. "github.com/suboat/go-sql-kit"
	"strings"
)

type SQLOrder struct {
	RuleMapping
	OrderRoot
}

func NewSQLOrder() *SQLOrder {
	return new(SQLOrder)
}

func (s *SQLOrder) String(alias ...string) string {
	if s.Values == nil || len(s.Values) == 0 {
		return ""
	}
	set := make([]string, 0, len(s.Values))
	for _, iv := range s.Values {
		if v, ok := iv.(*OrderValue); ok {
			if str := s.valueString(v, alias...); len(str) != 0 {
				set = append(set, str)
			}
		}
	}
	if len(set) == 0 {
		return ""
	}
	return "ORDER BY " + strings.Join(set, ", ")
}

func (s *SQLOrder) valueString(v *OrderValue, alias ...string) string {
	if v == nil {
	} else if !s.IsAllowed(v.Field) {
	} else if v.Field = s.GetMapping(v.Field); len(v.Field) != 0 {
		if f, ok := s.GetRuleMappingResult(v.Field); ok {
			if result, ok := f(v.Field, nil, v.Key, alias...); ok {
				if str, ok := result.(string); ok {
					return str
				}
			}
		}
		if f, ok := s.GetMappingFunc(v.Field); ok {
			if v.Field, _, ok = f(v.Field, nil); !ok {
				return ""
			}
		}
		if len(alias) != 0 {
			v.Field = fmt.Sprintf("%v.%v", alias[0], v.Field)
		}
		return s.ValueString(v)
	}
	return ""
}

func (s *SQLOrder) ValueString(v *OrderValue) string {
	if v.IsASC() {
		return fmt.Sprintf("%v ASC", v.Field)
	}
	return fmt.Sprintf("%v DESC", v.Field)
}

func (s *SQLOrder) JSONtoSQLString(str string, alias ...string) (string, error) {
	if err := s.ParseJSONString(str); err != nil {
		return "", err
	}
	return s.String(alias...), nil
}

func (s *SQLOrder) SQLString(m map[string]interface{}, alias ...string) (string, error) {
	if err := s.Parse(m); err != nil {
		return "", err
	}
	return s.String(alias...), nil
}
<commit_msg>fix: add order rule func<commit_after>package gosql

import (
	"fmt"
	. "github.com/suboat/go-sql-kit"
	"strings"
)

type SQLOrder struct {
	RuleMapping
	OrderRoot
}

func NewSQLOrder() *SQLOrder {
	return new(SQLOrder)
}

func (s *SQLOrder) String(alias ...string) string {
	if s.Values == nil || len(s.Values) == 0 {
		return ""
	}
	set := make([]string, 0, len(s.Values))
	for _, iv := range s.Values {
		if v, ok := iv.(*OrderValue); ok {
			if str := s.valueString(v, alias...); len(str) != 0 {
				set = append(set, str)
			}
		}
	}
	if len(set) == 0 {
		return ""
	}
	return "ORDER BY " + strings.Join(set, ", ")
}

func (s *SQLOrder) valueString(v *OrderValue, alias ...string) string {
	if v == nil {
	} else if !s.IsAllowed(v.Field) {
	} else if v.Field = s.GetMapping(v.Field); len(v.Field) != 0 {
		if f, ok := s.GetRuleMappingResult(v.Field); ok {
			if result, ok := f(v.Field, v.IsASC(), v.Key, alias...); ok {
				if str, ok := result.(string); ok {
					return str
				}
			}
		}
		if len(alias) != 0 {
			v.Field = fmt.Sprintf("%v.%v", alias[0], v.Field)
		}
		return s.ValueString(v)
	}
	return ""
}

func (s *SQLOrder) ValueString(v *OrderValue) string {
	if v.IsASC() {
		return fmt.Sprintf("%v ASC", v.Field)
	}
	return fmt.Sprintf("%v DESC", v.Field)
}

func (s *SQLOrder) JSONtoSQLString(str string, alias ...string) (string, error) {
	if err := s.ParseJSONString(str); err != nil {
		return "", err
	}
	return s.String(alias...), nil
}

func (s *SQLOrder) SQLString(m map[string]interface{}, alias ...string) (string, error) {
	if err := s.Parse(m); err != nil {
		return "", err
	}
	return s.String(alias...), nil
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"github.com/amir/raidman"
	"github.com/supherman/riemann-docker-health/docker"
	"github.com/supherman/riemann-docker-health/docker/cpu"
	"github.com/supherman/riemann-docker-health/docker/mem"
	"log"
	"os"
	"time"
)

var (
	cpuWarning  = flag.Int("cpu_warning", 50, "CPU warning threshold")
	cpuCritical = flag.Int("cpu_critical", 90, "CPU critical threshold")
	memWarning  = flag.Int("memory_warning", 50, "Memory warning threshold")
	memCritical = flag.Int("memory_critical", 90, "Memory critical threshold")
)

type Threshold struct {
	warning  int
	critical int
}

func Hostname() string {
	hostname, err := os.Hostname()
	if err != nil {
		log.Println(err)
	}
	return hostname
}

func Alert(event *raidman.Event) {
	c, err := raidman.Dial("tcp", "localhost:5555")
	if err != nil {
		panic(err)
	}
	err = c.Send(event)

	if err != nil {
		panic(err)
	}
	c.Close()
}

func ComputeState(metric int, threshold *Threshold) string {
	switch {
	case metric >= threshold.critical:
		return "critical"
	case metric >= threshold.warning && metric < threshold.critical:
		return "warning"
	}
	return "ok"
}

func AlertCPU(container string, threshold *Threshold) {
	containerCpu, _ := cpu.GetUsage(container)

	metric := int(containerCpu)
	state := ComputeState(metric, threshold)

	var cpuEvent = &raidman.Event{
		State:   state,
		Service: "cpu",
		Metric:  metric,
		Ttl:     10,
		Host:    fmt.Sprintf("%s %s", Hostname(), container),
	}
	Alert(cpuEvent)
}

func AlertMemory(container string, threshold *Threshold) {
	containerMem, _ := mem.GetPercentage(container)

	metric := int(containerMem)
	state := ComputeState(metric, threshold)

	var memEvent = &raidman.Event{
		State:   state,
		Service: "memory",
		Metric:  metric,
		Ttl:     10,
		Host:    fmt.Sprintf("%s %s", Hostname(), container),
	}
	Alert(memEvent)
}

func main() {
	flag.Parse()
	cpuThreshold := Threshold{*cpuWarning, *cpuCritical}
	memThreshold := Threshold{*memWarning, *memCritical}

	go cpu.Monitor()

	tick := time.NewTicker(1 * time.Second)

	for {
		<-tick.C
		containers, err := docker.ListContainers()
		if err != nil {
			log.Println(err)
			continue
		}

		for _, container := range containers {
			AlertCPU(container, &cpuThreshold)
			AlertMemory(container, &memThreshold)
		}
	}
}
<commit_msg>Accept --host and --port as argument flags<commit_after>package main

import (
	"flag"
	"fmt"
	"github.com/amir/raidman"
	"github.com/supherman/riemann-docker-health/docker"
	"github.com/supherman/riemann-docker-health/docker/cpu"
	"github.com/supherman/riemann-docker-health/docker/mem"
	"log"
	"os"
	"time"
)

var (
	cpuWarning  = flag.Int("cpu_warning", 50, "CPU warning threshold")
	cpuCritical = flag.Int("cpu_critical", 90, "CPU critical threshold")
	memWarning  = flag.Int("memory_warning", 50, "Memory warning threshold")
	memCritical = flag.Int("memory_critical", 90, "Memory critical threshold")
	host        = flag.String("host", "127.0.0.1", "Riemann host (default: 127.0.0.1)")
	port        = flag.String("port", "5555", "Riemann port (default: 5555")
)

type Threshold struct {
	warning  int
	critical int
}

func Hostname() string {
	hostname, err := os.Hostname()
	if err != nil {
		log.Println(err)
	}
	return hostname
}

func Alert(event *raidman.Event) {
	c, err := raidman.Dial("tcp", *host+":"+*port)
	if err != nil {
		panic(err)
	}
	err = c.Send(event)

	if err != nil {
		panic(err)
	}
	c.Close()
}

func ComputeState(metric int, threshold *Threshold) string {
	switch {
	case metric >= threshold.critical:
		return "critical"
	case metric >= threshold.warning && metric < threshold.critical:
		return "warning"
	}
	return "ok"
}

func AlertCPU(container string, threshold *Threshold) {
	containerCpu, _ := cpu.GetUsage(container)

	metric := int(containerCpu)
	state := ComputeState(metric, threshold)

	var cpuEvent = &raidman.Event{
		State:   state,
		Service: "cpu",
		Metric:  metric,
		Ttl:     10,
		Host:    fmt.Sprintf("%s %s", Hostname(), container),
	}
	Alert(cpuEvent)
}

func AlertMemory(container string, threshold *Threshold) {
	containerMem, _ := mem.GetPercentage(container)

	metric := int(containerMem)
	state := ComputeState(metric, threshold)

	var memEvent = &raidman.Event{
		State:   state,
		Service: "memory",
		Metric:  metric,
		Ttl:     10,
		Host:    fmt.Sprintf("%s %s", Hostname(), container),
	}
	Alert(memEvent)
}

func main() {
	fmt.Println("Initializing monitoring agent...")
	flag.Parse()
	cpuThreshold := Threshold{*cpuWarning, *cpuCritical}
	memThreshold := Threshold{*memWarning, *memCritical}

	go cpu.Monitor()

	tick := time.NewTicker(1 * time.Second)

	for {
		<-tick.C
		containers, err := docker.ListContainers()
		if err != nil {
			log.Println(err)
			continue
		}

		for _, container := range containers {
			AlertCPU(container, &cpuThreshold)
			AlertMemory(container, &memThreshold)
		}
	}
}
<|endoftext|>


<commit_before>// +build probe

/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package upgrade

import (
	"io/ioutil"
	"log"
	"os"
	"syscall"
	"testing"

	"knative.dev/serving/test"
	"knative.dev/serving/test/e2e"
	v1test "knative.dev/serving/test/v1"
)

const pipe = "/tmp/prober-signal"

func TestProbe(t *testing.T) {
	// We run the prober as a golang test because it fits in nicely with
	// the rest of our integration tests, and AssertProberDefault needs
	// a *testing.T. Unfortunately, "go test" intercepts signals, so we
	// can't coordinate with the test by just sending e.g. SIGCONT, so we
	// create a named pipe and wait for the upgrade script to write to it
	// to signal that we should stop probing.
	if err := syscall.Mkfifo(pipe, 0666); err != nil {
		t.Fatal("Failed to create pipe:", err)
	}
	defer os.Remove(pipe)

	clients := e2e.Setup(t)
	names := test.ResourceNames{
		Service: "upgrade-probe",
		Image:   test.PizzaPlanet1,
	}
	defer test.TearDown(clients, names)

	objects, err := v1test.CreateServiceReady(t, clients, &names)
	if err != nil {
		t.Fatal("Failed to create Service:", err)
	}
	url := objects.Service.Status.URL.URL()

	// This polls until we get a 200 with the right body.
	assertServiceResourcesUpdated(t, clients, names, url, test.PizzaPlanetText1)

	// Use log.Printf instead of t.Logf because we want to see failures
	// inline with other logs instead of buffered until the end.
	prober := test.RunRouteProber(log.Printf, clients, url, test.AddRootCAtoTransport(t.Logf, clients, test.ServingFlags.Https))
	defer test.AssertProberDefault(t, prober)

	// e2e-upgrade-test.sh will close this pipe to signal the upgrade is
	// over, at which point we will finish the test and check the prober.
	ioutil.ReadFile(pipe)
}
<commit_msg>Make upgrade prober configurable re: SLO. (#8448)<commit_after>// +build probe

/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package upgrade

import (
	"flag"
	"io/ioutil"
	"log"
	"os"
	"syscall"
	"testing"

	"knative.dev/serving/test"
	"knative.dev/serving/test/e2e"
	v1test "knative.dev/serving/test/v1"
)

var (
	successFraction = flag.Float64("probe.success_fraction", 1.0, "Fraction of probes required to pass during upgrade.")
)

const pipe = "/tmp/prober-signal"

func TestProbe(t *testing.T) {
	// We run the prober as a golang test because it fits in nicely with
	// the rest of our integration tests, and AssertProberDefault needs
	// a *testing.T. Unfortunately, "go test" intercepts signals, so we
	// can't coordinate with the test by just sending e.g. SIGCONT, so we
	// create a named pipe and wait for the upgrade script to write to it
	// to signal that we should stop probing.
	if err := syscall.Mkfifo(pipe, 0666); err != nil {
		t.Fatal("Failed to create pipe:", err)
	}
	defer os.Remove(pipe)

	clients := e2e.Setup(t)
	names := test.ResourceNames{
		Service: "upgrade-probe",
		Image:   test.PizzaPlanet1,
	}
	defer test.TearDown(clients, names)

	objects, err := v1test.CreateServiceReady(t, clients, &names)
	if err != nil {
		t.Fatal("Failed to create Service:", err)
	}
	url := objects.Service.Status.URL.URL()

	// This polls until we get a 200 with the right body.
	assertServiceResourcesUpdated(t, clients, names, url, test.PizzaPlanetText1)

	// Use log.Printf instead of t.Logf because we want to see failures
	// inline with other logs instead of buffered until the end.
	prober := test.RunRouteProber(log.Printf, clients, url, test.AddRootCAtoTransport(t.Logf, clients, test.ServingFlags.Https))
	defer test.CheckSLO(*successFraction, t.Name(), prober)

	// e2e-upgrade-test.sh will close this pipe to signal the upgrade is
	// over, at which point we will finish the test and check the prober.
	ioutil.ReadFile(pipe)
}
<|endoftext|>


<commit_before>package sqlgen

import (
	"context"
	"database/sql"
	"errors"

	"github.com/samsarahq/thunder/batch"
)

// DB uses a *sql.DB connection that is established by its owner. DB assumes the
// database connection exists and is alive at all times during the lifecycle of
// the object.
type DB struct {
	Conn   *sql.DB
	Schema *Schema

	batchFetch *batch.Func
}

func NewDB(conn *sql.DB, schema *Schema) *DB {
	db := &DB{
		Conn:   conn,
		Schema: schema,
	}

	db.batchFetch = &batch.Func{
		Many: func(ctx context.Context, items []interface{}) ([]interface{}, error) {
			table := items[0].(*BaseSelectQuery).Table

			// First, build the SQL query.
			filters := make([]Filter, 0, len(items))
			for _, item := range items {
				filters = append(filters, item.(*BaseSelectQuery).Filter)
			}
			clause, args := makeBatchQuery(filters)
			query, err := db.Schema.makeSelect(table.Type, nil, &SelectOptions{
				Where:  clause,
				Values: args,
			})
			if err != nil {
				return nil, err
			}
			selectQuery, err := query.MakeSelectQuery()
			if err != nil {
				return nil, err
			}
			clause, args = selectQuery.ToSQL()

			// Then, run the SQL query.
			res, err := db.Conn.QueryContext(ctx, clause, args...)
			if err != nil {
				return nil, err
			}
			defer res.Close()
			rows, err := db.Schema.ParseRows(selectQuery, res)
			if err != nil {
				return nil, err
			}

			// Finally, match the returned rows against the queries.
			matcher := newMatcher()
			for i, item := range items {
				query := item.(*BaseSelectQuery)
				// XXX: This needs more rigor, and a test. For now, call coerceMap on rows
				// and filters to flatten out all pointers to values, etc., to copy what
				// the row tester does when matching against the binlog. This way, a filter
				// specifying age=48 will match a value *age=48.
				matcher.add(i, coerceMap(query.Filter))
			}
			results := make([][]interface{}, len(items))
			for _, row := range rows {
				f := coerceMap(table.extractRow(row))
				for _, idx := range matcher.match(f) {
					i := idx.(int)
					results[i] = append(results[i], row)
				}
			}

			// Convert the [][]interface{} return type into a []interface{} to satisfy
			// the Batch interface.
			rawResults := make([]interface{}, 0, len(items))
			for _, result := range results {
				rawResults = append(rawResults, result)
			}
			return rawResults, nil
		},
		Shard: func(item interface{}) interface{} {
			return item.(*BaseSelectQuery).Table
		},
	}
	return db
}

func (db *DB) BaseQuery(ctx context.Context, query *BaseSelectQuery) ([]interface{}, error) {
	if query.Options == nil && !db.HasTx(ctx) && batch.HasBatching(ctx) {
		rows, err := db.batchFetch.Invoke(ctx, query)
		if err != nil {
			return nil, err
		}
		return rows.([]interface{}), nil
	}

	selectQuery, err := query.MakeSelectQuery()
	if err != nil {
		return nil, err
	}

	clause, args := selectQuery.ToSQL()

	res, err := db.QueryExecer(ctx).QueryContext(ctx, clause, args...)
	if err != nil {
		return nil, err
	}
	defer res.Close()

	return db.Schema.ParseRows(selectQuery, res)
}

func (db *DB) execWithTrace(ctx context.Context, query SQLQuery, operationName string) (sql.Result, error) {
	clause, args := query.ToSQL()

	return db.QueryExecer(ctx).ExecContext(ctx, clause, args...)
}

// Query fetches a collection of rows from the database
//
// result should be a pointer to a slice of pointers to structs, for example:
//
//   var users []*User
//   if err := db.Query(ctx, &users, nil, nil); err != nil {
//
func (db *DB) Query(ctx context.Context, result interface{}, filter Filter, options *SelectOptions) error {
	query, err := db.Schema.MakeSelect(result, filter, options)
	if err != nil {
		return err
	}

	rows, err := db.BaseQuery(ctx, query)
	if err != nil {
		return err
	}

	return CopySlice(result, rows)
}

// QueryRow fetches a single row from the database
//
// result should be a pointer to a pointer to a struct, for example:
//
//   var user *User
//   if err := db.Query(ctx, &user, Filter{"id": 10}, nil); err != nil {
//
func (db *DB) QueryRow(ctx context.Context, result interface{}, filter Filter, options *SelectOptions) error {
	query, err := db.Schema.MakeSelectRow(result, filter, options)
	if err != nil {
		return err
	}

	rows, err := db.BaseQuery(ctx, query)
	if err != nil {
		return err
	}

	return CopySingletonSlice(result, rows)
}

// InsertRow inserts a single row into the database
//
// row should be a pointer to a struct, for example:
//
//   user := &User{Name: "foo"}
//   if err := db.InsertRow(ctx, user); err != nil {
//
func (db *DB) InsertRow(ctx context.Context, row interface{}) (sql.Result, error) {
	query, err := db.Schema.MakeInsertRow(row)
	if err != nil {
		return nil, err
	}

	return db.execWithTrace(ctx, query, "InsertRow")
}

// UpsertRow inserts a single row into the database
//
// row should be a pointer to a struct, for example:
//
//   user := &User{Name: "foo"}
//   if err := db.UpsertRow(ctx, user); err != nil {
//
func (db *DB) UpsertRow(ctx context.Context, row interface{}) (sql.Result, error) {
	query, err := db.Schema.MakeUpsertRow(row)
	if err != nil {
		return nil, err
	}

	return db.execWithTrace(ctx, query, "UpsertRow")
}

// UpdateRow updates a single row in the database, identified by the row's primary key
//
// row should be a pointer to a struct, for example:
//
//   user := &User{Id; 10, Name: "bar"}
//   if err := db.UpdateRow(ctx, user); err != nil {
//
func (db *DB) UpdateRow(ctx context.Context, row interface{}) error {
	query, err := db.Schema.MakeUpdateRow(row)
	if err != nil {
		return err
	}

	_, err = db.execWithTrace(ctx, query, "UpsertRow")
	return err
}

// DeleteRow deletes a single row from the database, identified by the row's primary key
//
// row should be a pointer to a struct, for example:
//
//   user := &User{Id; 10}
//   if err := db.DeleteRow(ctx, user); err != nil {
//
func (db *DB) DeleteRow(ctx context.Context, row interface{}) error {
	query, err := db.Schema.MakeDeleteRow(row)
	if err != nil {
		return err
	}

	_, err = db.execWithTrace(ctx, query, "DeleteRow")
	return err
}

// txKey is used as a key for a context.Context to hold a transaction.
//
// With multiple open databases, each database can store its own transactions in a context.
type txKey struct {
	db *DB
}

func (db *DB) WithTx(ctx context.Context) (context.Context, *sql.Tx, error) {
	maybeTx := ctx.Value(txKey{db: db})
	if maybeTx != nil {
		return nil, nil, errors.New("already in a tx")
	}

	tx, err := db.Conn.BeginTx(ctx, nil)
	if err != nil {
		return nil, nil, err
	}
	return context.WithValue(ctx, txKey{db: db}, tx), tx, nil
}

func (db *DB) WithExistingTx(ctx context.Context, tx *sql.Tx) (context.Context, error) {
	maybeTx := ctx.Value(txKey{db: db})
	if maybeTx != nil {
		return nil, errors.New("already in a tx")
	}

	return context.WithValue(ctx, txKey{db: db}, tx), nil
}

func (db *DB) HasTx(ctx context.Context) bool {
	return ctx.Value(txKey{db: db}) != nil
}

// A QueryExecer is either a *sql.Tx or a *sql.DB.
type QueryExecer interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	Exec(query string, args ...interface{}) (sql.Result, error)

	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

func (db *DB) QueryExecer(ctx context.Context) QueryExecer {
	maybeTx := ctx.Value(txKey{db: db})
	if maybeTx != nil {
		return maybeTx.(*sql.Tx)
	}
	return db.Conn
}

var _ QueryExecer = &sql.Tx{}
var _ QueryExecer = &sql.DB{}
<commit_msg>sqlgen/db: add method comments for WithTx, WithExistingTx, and HasTx<commit_after>package sqlgen

import (
	"context"
	"database/sql"
	"errors"

	"github.com/samsarahq/thunder/batch"
)

// DB uses a *sql.DB connection that is established by its owner. DB assumes the
// database connection exists and is alive at all times during the lifecycle of
// the object.
type DB struct {
	Conn   *sql.DB
	Schema *Schema

	batchFetch *batch.Func
}

func NewDB(conn *sql.DB, schema *Schema) *DB {
	db := &DB{
		Conn:   conn,
		Schema: schema,
	}

	db.batchFetch = &batch.Func{
		Many: func(ctx context.Context, items []interface{}) ([]interface{}, error) {
			table := items[0].(*BaseSelectQuery).Table

			// First, build the SQL query.
			filters := make([]Filter, 0, len(items))
			for _, item := range items {
				filters = append(filters, item.(*BaseSelectQuery).Filter)
			}
			clause, args := makeBatchQuery(filters)
			query, err := db.Schema.makeSelect(table.Type, nil, &SelectOptions{
				Where:  clause,
				Values: args,
			})
			if err != nil {
				return nil, err
			}
			selectQuery, err := query.MakeSelectQuery()
			if err != nil {
				return nil, err
			}
			clause, args = selectQuery.ToSQL()

			// Then, run the SQL query.
			res, err := db.Conn.QueryContext(ctx, clause, args...)
			if err != nil {
				return nil, err
			}
			defer res.Close()
			rows, err := db.Schema.ParseRows(selectQuery, res)
			if err != nil {
				return nil, err
			}

			// Finally, match the returned rows against the queries.
			matcher := newMatcher()
			for i, item := range items {
				query := item.(*BaseSelectQuery)
				// XXX: This needs more rigor, and a test. For now, call coerceMap on rows
				// and filters to flatten out all pointers to values, etc., to copy what
				// the row tester does when matching against the binlog. This way, a filter
				// specifying age=48 will match a value *age=48.
				matcher.add(i, coerceMap(query.Filter))
			}
			results := make([][]interface{}, len(items))
			for _, row := range rows {
				f := coerceMap(table.extractRow(row))
				for _, idx := range matcher.match(f) {
					i := idx.(int)
					results[i] = append(results[i], row)
				}
			}

			// Convert the [][]interface{} return type into a []interface{} to satisfy
			// the Batch interface.
			rawResults := make([]interface{}, 0, len(items))
			for _, result := range results {
				rawResults = append(rawResults, result)
			}
			return rawResults, nil
		},
		Shard: func(item interface{}) interface{} {
			return item.(*BaseSelectQuery).Table
		},
	}
	return db
}

func (db *DB) BaseQuery(ctx context.Context, query *BaseSelectQuery) ([]interface{}, error) {
	if query.Options == nil && !db.HasTx(ctx) && batch.HasBatching(ctx) {
		rows, err := db.batchFetch.Invoke(ctx, query)
		if err != nil {
			return nil, err
		}
		return rows.([]interface{}), nil
	}

	selectQuery, err := query.MakeSelectQuery()
	if err != nil {
		return nil, err
	}

	clause, args := selectQuery.ToSQL()

	res, err := db.QueryExecer(ctx).QueryContext(ctx, clause, args...)
	if err != nil {
		return nil, err
	}
	defer res.Close()

	return db.Schema.ParseRows(selectQuery, res)
}

func (db *DB) execWithTrace(ctx context.Context, query SQLQuery, operationName string) (sql.Result, error) {
	clause, args := query.ToSQL()

	return db.QueryExecer(ctx).ExecContext(ctx, clause, args...)
}

// Query fetches a collection of rows from the database
//
// result should be a pointer to a slice of pointers to structs, for example:
//
//   var users []*User
//   if err := db.Query(ctx, &users, nil, nil); err != nil {
//
func (db *DB) Query(ctx context.Context, result interface{}, filter Filter, options *SelectOptions) error {
	query, err := db.Schema.MakeSelect(result, filter, options)
	if err != nil {
		return err
	}

	rows, err := db.BaseQuery(ctx, query)
	if err != nil {
		return err
	}

	return CopySlice(result, rows)
}

// QueryRow fetches a single row from the database
//
// result should be a pointer to a pointer to a struct, for example:
//
//   var user *User
//   if err := db.Query(ctx, &user, Filter{"id": 10}, nil); err != nil {
//
func (db *DB) QueryRow(ctx context.Context, result interface{}, filter Filter, options *SelectOptions) error {
	query, err := db.Schema.MakeSelectRow(result, filter, options)
	if err != nil {
		return err
	}

	rows, err := db.BaseQuery(ctx, query)
	if err != nil {
		return err
	}

	return CopySingletonSlice(result, rows)
}

// InsertRow inserts a single row into the database
//
// row should be a pointer to a struct, for example:
//
//   user := &User{Name: "foo"}
//   if err := db.InsertRow(ctx, user); err != nil {
//
func (db *DB) InsertRow(ctx context.Context, row interface{}) (sql.Result, error) {
	query, err := db.Schema.MakeInsertRow(row)
	if err != nil {
		return nil, err
	}

	return db.execWithTrace(ctx, query, "InsertRow")
}

// UpsertRow inserts a single row into the database
//
// row should be a pointer to a struct, for example:
//
//   user := &User{Name: "foo"}
//   if err := db.UpsertRow(ctx, user); err != nil {
//
func (db *DB) UpsertRow(ctx context.Context, row interface{}) (sql.Result, error) {
	query, err := db.Schema.MakeUpsertRow(row)
	if err != nil {
		return nil, err
	}

	return db.execWithTrace(ctx, query, "UpsertRow")
}

// UpdateRow updates a single row in the database, identified by the row's primary key
//
// row should be a pointer to a struct, for example:
//
//   user := &User{Id; 10, Name: "bar"}
//   if err := db.UpdateRow(ctx, user); err != nil {
//
func (db *DB) UpdateRow(ctx context.Context, row interface{}) error {
	query, err := db.Schema.MakeUpdateRow(row)
	if err != nil {
		return err
	}

	_, err = db.execWithTrace(ctx, query, "UpsertRow")
	return err
}

// DeleteRow deletes a single row from the database, identified by the row's primary key
//
// row should be a pointer to a struct, for example:
//
//   user := &User{Id; 10}
//   if err := db.DeleteRow(ctx, user); err != nil {
//
func (db *DB) DeleteRow(ctx context.Context, row interface{}) error {
	query, err := db.Schema.MakeDeleteRow(row)
	if err != nil {
		return err
	}

	_, err = db.execWithTrace(ctx, query, "DeleteRow")
	return err
}

// txKey is used as a key for a context.Context to hold a transaction.
//
// With multiple open databases, each database can store its own transactions in a context.
type txKey struct {
	db *DB
}

// WithTx begins a transaction and returns a derived Context that contains
// that transaction. It also returns the transaction value itself, for the
// caller to manipulate (e.g., Commit).
// It is an error to invoke this method on a Context that already contains
// a transaction for this DB.
func (db *DB) WithTx(ctx context.Context) (context.Context, *sql.Tx, error) {
	maybeTx := ctx.Value(txKey{db: db})
	if maybeTx != nil {
		return nil, nil, errors.New("already in a tx")
	}

	tx, err := db.Conn.BeginTx(ctx, nil)
	if err != nil {
		return nil, nil, err
	}
	return context.WithValue(ctx, txKey{db: db}, tx), tx, nil
}

// WithExistingTx returns a derived Context that contains the provided Tx.
// It is an error to invoke this method on a Context that already contains
// a transaction for this DB.
func (db *DB) WithExistingTx(ctx context.Context, tx *sql.Tx) (context.Context, error) {
	maybeTx := ctx.Value(txKey{db: db})
	if maybeTx != nil {
		return nil, errors.New("already in a tx")
	}

	return context.WithValue(ctx, txKey{db: db}, tx), nil
}

// HasTx returns whether the provided Context contains a transaction for
// this DB.
func (db *DB) HasTx(ctx context.Context) bool {
	return ctx.Value(txKey{db: db}) != nil
}

// A QueryExecer is either a *sql.Tx or a *sql.DB.
type QueryExecer interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	Exec(query string, args ...interface{}) (sql.Result, error)

	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

func (db *DB) QueryExecer(ctx context.Context) QueryExecer {
	maybeTx := ctx.Value(txKey{db: db})
	if maybeTx != nil {
		return maybeTx.(*sql.Tx)
	}
	return db.Conn
}

var _ QueryExecer = &sql.Tx{}
var _ QueryExecer = &sql.DB{}
<|endoftext|>


<commit_before>// Copyright ©2013 The gonum 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 mat64

import (
	"github.com/gonum/blas"
	"github.com/gonum/blas/blas64"
)

var (
	vector *Vector

	_ Matrix = vector

	// _ Cloner      = vector
	// _ Viewer      = vector
	// _ Subvectorer = vector

	// _ Adder     = vector
	// _ Suber     = vector
	// _ Muler = vector
	// _ Dotter    = vector
	// _ ElemMuler = vector

	// _ Scaler  = vector
	// _ Applyer = vector

	// _ Normer = vector
	// _ Sumer  = vector

	// _ Stacker   = vector
	// _ Augmenter = vector

	// _ Equaler       = vector
	// _ ApproxEqualer = vector

	// _ RawMatrixLoader = vector
	// _ RawMatrixer     = vector
)

// Vector represents a column vector.
type Vector struct {
	mat blas64.Vector
	n   int
	// A BLAS vector can have a negative increment, but allowing this
	// in the mat64 type complicates a lot of code, and doesn't gain anything.
	// Vector must have positive increment in this package.
}

// NewVector creates a new Vector of length n. If len(data) == n, data is used
// as the backing data slice. If data == nil, a new slice is allocated. If
// neither of these is true, NewVector will panic.
func NewVector(n int, data []float64) *Vector {
	if len(data) != n && data != nil {
		panic(ErrShape)
	}
	if data == nil {
		data = make([]float64, n)
	}
	return &Vector{
		mat: blas64.Vector{
			Inc:  1,
			Data: data,
		},
		n: n,
	}
}

// ViewVec returns a sub-vector view of the receiver starting at element i and
// extending n columns. If i is out of range, or if n is zero or extend beyond the
// bounds of the Vector ViewVec will panic with ErrIndexOutOfRange. The returned
// Vector retains reference to the underlying vector.
func (v *Vector) ViewVec(i, n int) *Vector {
	if i+n > v.n {
		panic(ErrIndexOutOfRange)
	}
	return &Vector{
		n: n,
		mat: blas64.Vector{
			Inc:  v.mat.Inc,
			Data: v.mat.Data[i*v.mat.Inc:],
		},
	}
}

func (v *Vector) Dims() (r, c int) {
	if v.isZero() {
		return 0, 0
	}
	return v.n, 1
}

// Len returns the length of the vector.
func (v *Vector) Len() int {
	return v.n
}

// T performs an implicit transpose by returning the receiver inside a Transpose.
func (v *Vector) T() Matrix {
	return Transpose{v}
}

// Reset zeros the length of the vector so that it can be reused as the
// receiver of a dimensionally restricted operation.
//
// See the Reseter interface for more information.
func (v *Vector) Reset() {
	// No change of Inc or n to 0 may be
	// made unless both are set to 0.
	v.mat.Inc = 0
	v.n = 0
	v.mat.Data = v.mat.Data[:0]
}

func (v *Vector) RawVector() blas64.Vector {
	return v.mat
}

// CopyVec makes a copy of elements of a into the receiver. It is similar to the
// built-in copy; it copies as much as the overlap between the two matrices and
// returns the number of rows and columns it copied.
func (v *Vector) CopyVec(a *Vector) (n int) {
	n = min(v.Len(), a.Len())
	blas64.Copy(n, a.mat, v.mat)
	return n
}

// ScaleVec scales the vector a by alpha, placing the result in the receiver.
func (v *Vector) ScaleVec(alpha float64, a *Vector) {
	n := a.Len()
	if v != a {
		v.reuseAs(n)
		blas64.Copy(n, a.mat, v.mat)
	}
	blas64.Scal(n, alpha, v.mat)
}

// AddScaledVec adds the vectors a and alpha*b, placing the result in the receiver.
func (v *Vector) AddScaledVec(a *Vector, alpha float64, b *Vector) {
	ar := a.Len()
	br := b.Len()

	if ar != br {
		panic(ErrShape)
	}

	v.reuseAs(ar)

	switch {
	case v == a && v == b: // v <- v + alpha * v = (alpha + 1) * v
		blas64.Scal(ar, alpha+1, v.mat)
	case v == a && v != b: // v <- v + alpha * b
		blas64.Axpy(ar, alpha, b.mat, v.mat)
	case v != a && v == b: // v <- a + alpha * v
		blas64.Scal(ar, alpha, v.mat)
		blas64.Axpy(ar, 1, a.mat, v.mat)
	default: // v <- a + alpha * b
		blas64.Copy(ar, a.mat, v.mat)
		blas64.Axpy(ar, alpha, b.mat, v.mat)
	}
}

// AddVec adds a and b element-wise, placing the result in the receiver.
func (v *Vector) AddVec(a, b *Vector) {
	ar := a.Len()
	br := b.Len()

	if ar != br {
		panic(ErrShape)
	}

	v.reuseAs(ar)

	amat, bmat := a.RawVector(), b.RawVector()
	for i := 0; i < v.n; i++ {
		v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] + bmat.Data[i*bmat.Inc]
	}
}

// SubVec subtracts the vector b from a, placing the result in the receiver.
func (v *Vector) SubVec(a, b *Vector) {
	ar := a.Len()
	br := b.Len()

	if ar != br {
		panic(ErrShape)
	}

	v.reuseAs(ar)

	amat, bmat := a.RawVector(), b.RawVector()
	for i := 0; i < v.n; i++ {
		v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] - bmat.Data[i*bmat.Inc]
	}
}

// MulElemVec performs element-wise multiplication of a and b, placing the result
// in the receiver.
func (v *Vector) MulElemVec(a, b *Vector) {
	ar := a.Len()
	br := b.Len()

	if ar != br {
		panic(ErrShape)
	}

	v.reuseAs(ar)

	amat, bmat := a.RawVector(), b.RawVector()
	for i := 0; i < v.n; i++ {
		v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] * bmat.Data[i*bmat.Inc]
	}
}

// DivElemVec performs element-wise division of a by b, placing the result
// in the receiver.
func (v *Vector) DivElemVec(a, b *Vector) {
	ar := a.Len()
	br := b.Len()

	if ar != br {
		panic(ErrShape)
	}

	v.reuseAs(ar)

	amat, bmat := a.RawVector(), b.RawVector()
	for i := 0; i < v.n; i++ {
		v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] / bmat.Data[i*bmat.Inc]
	}
}

// MulVec computes a * b. The result is stored into the receiver.
// MulVec panics if the number of columns in a does not equal the number of rows in b.
func (v *Vector) MulVec(a Matrix, b *Vector) {
	r, c := a.Dims()
	br := b.Len()
	if c != br {
		panic(ErrShape)
	}
	a, trans := untranspose(a)
	ar, ac := a.Dims()
	v.reuseAs(r)
	var restore func()
	if v == a {
		v, restore = v.isolatedWorkspace(a.(*Vector))
		defer restore()
	} else if v == b {
		v, restore = v.isolatedWorkspace(b)
		defer restore()
	}

	switch a := a.(type) {
	case *Vector:
		if a.Len() == 1 {
			// {1,1} x {1,n}
			av := a.At(0, 0)
			for i := 0; i < b.Len(); i++ {
				v.mat.Data[i*v.mat.Inc] = av * b.mat.Data[i*b.mat.Inc]
			}
			return
		}
		if b.Len() == 1 {
			// {1,n} x {1,1}
			bv := b.At(0, 0)
			for i := 0; i < a.Len(); i++ {
				v.mat.Data[i*v.mat.Inc] = bv * a.mat.Data[i*a.mat.Inc]
			}
			return
		}
		// {n,1} x {1,n}
		var sum float64
		for i := 0; i < c; i++ {
			sum += a.At(i, 0) * b.At(i, 0)
		}
		v.SetVec(0, sum)
		return
	case RawSymmetricer:
		amat := a.RawSymmetric()
		blas64.Symv(1, amat, b.mat, 0, v.mat)
	case RawTriangular:
		v.CopyVec(b)
		amat := a.RawTriangular()
		ta := blas.NoTrans
		if trans {
			ta = blas.Trans
		}
		blas64.Trmv(ta, amat, v.mat)
	case RawMatrixer:
		amat := a.RawMatrix()
		t := blas.NoTrans
		if trans {
			t = blas.Trans
		}
		blas64.Gemv(t, 1, amat, b.mat, 0, v.mat)
	case Vectorer:
		if trans {
			col := make([]float64, ar)
			for c := 0; c < ac; c++ {
				v.mat.Data[c*v.mat.Inc] = blas64.Dot(ar,
					blas64.Vector{Inc: 1, Data: a.Col(col, c)},
					b.mat,
				)
			}
		} else {
			row := make([]float64, ac)
			for r := 0; r < ar; r++ {
				v.mat.Data[r*v.mat.Inc] = blas64.Dot(ac,
					blas64.Vector{Inc: 1, Data: a.Row(row, r)},
					b.mat,
				)
			}
		}
	default:
		if trans {
			col := make([]float64, ar)
			for c := 0; c < ac; c++ {
				for i := range col {
					col[i] = a.At(i, c)
				}
				var f float64
				for i, e := range col {
					f += e * b.mat.Data[i*b.mat.Inc]
				}
				v.mat.Data[c*v.mat.Inc] = f
			}
		} else {
			row := make([]float64, ac)
			for r := 0; r < ar; r++ {
				for i := range row {
					row[i] = a.At(r, i)
				}
				var f float64
				for i, e := range row {
					f += e * b.mat.Data[i*b.mat.Inc]
				}
				v.mat.Data[r*v.mat.Inc] = f
			}
		}
	}
}

// reuseAs resizes an empty vector to a r×1 vector,
// or checks that a non-empty matrix is r×1.
func (v *Vector) reuseAs(r int) {
	if v.isZero() {
		v.mat = blas64.Vector{
			Inc:  1,
			Data: use(v.mat.Data, r),
		}
		v.n = r
		return
	}
	if r != v.n {
		panic(ErrShape)
	}
}

func (v *Vector) isZero() bool {
	// It must be the case that v.Dims() returns
	// zeros in this case. See comment in Reset().
	return v.mat.Inc == 0
}

func (v *Vector) isolatedWorkspace(a *Vector) (n *Vector, restore func()) {
	l := a.Len()
	n = getWorkspaceVec(l, false)
	return n, func() {
		v.CopyVec(n)
		putWorkspaceVec(n)
	}
}
<commit_msg>Add fast paths for a common case<commit_after>// Copyright ©2013 The gonum 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 mat64

import (
	"github.com/gonum/blas"
	"github.com/gonum/blas/blas64"
	"github.com/gonum/internal/asm"
)

var (
	vector *Vector

	_ Matrix = vector

	// _ Cloner      = vector
	// _ Viewer      = vector
	// _ Subvectorer = vector

	// _ Adder     = vector
	// _ Suber     = vector
	// _ Muler = vector
	// _ Dotter    = vector
	// _ ElemMuler = vector

	// _ Scaler  = vector
	// _ Applyer = vector

	// _ Normer = vector
	// _ Sumer  = vector

	// _ Stacker   = vector
	// _ Augmenter = vector

	// _ Equaler       = vector
	// _ ApproxEqualer = vector

	// _ RawMatrixLoader = vector
	// _ RawMatrixer     = vector
)

// Vector represents a column vector.
type Vector struct {
	mat blas64.Vector
	n   int
	// A BLAS vector can have a negative increment, but allowing this
	// in the mat64 type complicates a lot of code, and doesn't gain anything.
	// Vector must have positive increment in this package.
}

// NewVector creates a new Vector of length n. If len(data) == n, data is used
// as the backing data slice. If data == nil, a new slice is allocated. If
// neither of these is true, NewVector will panic.
func NewVector(n int, data []float64) *Vector {
	if len(data) != n && data != nil {
		panic(ErrShape)
	}
	if data == nil {
		data = make([]float64, n)
	}
	return &Vector{
		mat: blas64.Vector{
			Inc:  1,
			Data: data,
		},
		n: n,
	}
}

// ViewVec returns a sub-vector view of the receiver starting at element i and
// extending n columns. If i is out of range, or if n is zero or extend beyond the
// bounds of the Vector ViewVec will panic with ErrIndexOutOfRange. The returned
// Vector retains reference to the underlying vector.
func (v *Vector) ViewVec(i, n int) *Vector {
	if i+n > v.n {
		panic(ErrIndexOutOfRange)
	}
	return &Vector{
		n: n,
		mat: blas64.Vector{
			Inc:  v.mat.Inc,
			Data: v.mat.Data[i*v.mat.Inc:],
		},
	}
}

func (v *Vector) Dims() (r, c int) {
	if v.isZero() {
		return 0, 0
	}
	return v.n, 1
}

// Len returns the length of the vector.
func (v *Vector) Len() int {
	return v.n
}

// T performs an implicit transpose by returning the receiver inside a Transpose.
func (v *Vector) T() Matrix {
	return Transpose{v}
}

// Reset zeros the length of the vector so that it can be reused as the
// receiver of a dimensionally restricted operation.
//
// See the Reseter interface for more information.
func (v *Vector) Reset() {
	// No change of Inc or n to 0 may be
	// made unless both are set to 0.
	v.mat.Inc = 0
	v.n = 0
	v.mat.Data = v.mat.Data[:0]
}

func (v *Vector) RawVector() blas64.Vector {
	return v.mat
}

// CopyVec makes a copy of elements of a into the receiver. It is similar to the
// built-in copy; it copies as much as the overlap between the two matrices and
// returns the number of rows and columns it copied.
func (v *Vector) CopyVec(a *Vector) (n int) {
	n = min(v.Len(), a.Len())
	blas64.Copy(n, a.mat, v.mat)
	return n
}

// ScaleVec scales the vector a by alpha, placing the result in the receiver.
func (v *Vector) ScaleVec(alpha float64, a *Vector) {
	n := a.Len()
	if v != a {
		v.reuseAs(n)
		blas64.Copy(n, a.mat, v.mat)
	}
	blas64.Scal(n, alpha, v.mat)
}

// AddScaledVec adds the vectors a and alpha*b, placing the result in the receiver.
func (v *Vector) AddScaledVec(a *Vector, alpha float64, b *Vector) {
	ar := a.Len()
	br := b.Len()

	if ar != br {
		panic(ErrShape)
	}

	v.reuseAs(ar)

	switch {
	case v == a && v == b: // v <- v + alpha * v = (alpha + 1) * v
		blas64.Scal(ar, alpha+1, v.mat)
	case v == a && v != b: // v <- v + alpha * b
		blas64.Axpy(ar, alpha, b.mat, v.mat)
	case v != a && v == b: // v <- a + alpha * v
		if v.mat.Inc == 1 && a.mat.Inc == 1 {
			// Fast path for a common case.
			v := v.mat.Data
			for i, a := range a.mat.Data {
				v[i] *= alpha
				v[i] += a
			}
			return
		}
		blas64.Scal(ar, alpha, v.mat)
		blas64.Axpy(ar, 1, a.mat, v.mat)
	default: // v <- a + alpha * b
		if v.mat.Inc == 1 && a.mat.Inc == 1 && b.mat.Inc == 1 {
			// Fast path for a common case.
			asm.DaxpyUnitary(alpha, b.mat.Data, a.mat.Data, v.mat.Data)
			return
		}
		blas64.Copy(ar, a.mat, v.mat)
		blas64.Axpy(ar, alpha, b.mat, v.mat)
	}
}

// AddVec adds a and b element-wise, placing the result in the receiver.
func (v *Vector) AddVec(a, b *Vector) {
	ar := a.Len()
	br := b.Len()

	if ar != br {
		panic(ErrShape)
	}

	v.reuseAs(ar)

	amat, bmat := a.RawVector(), b.RawVector()
	for i := 0; i < v.n; i++ {
		v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] + bmat.Data[i*bmat.Inc]
	}
}

// SubVec subtracts the vector b from a, placing the result in the receiver.
func (v *Vector) SubVec(a, b *Vector) {
	ar := a.Len()
	br := b.Len()

	if ar != br {
		panic(ErrShape)
	}

	v.reuseAs(ar)

	amat, bmat := a.RawVector(), b.RawVector()
	for i := 0; i < v.n; i++ {
		v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] - bmat.Data[i*bmat.Inc]
	}
}

// MulElemVec performs element-wise multiplication of a and b, placing the result
// in the receiver.
func (v *Vector) MulElemVec(a, b *Vector) {
	ar := a.Len()
	br := b.Len()

	if ar != br {
		panic(ErrShape)
	}

	v.reuseAs(ar)

	amat, bmat := a.RawVector(), b.RawVector()
	for i := 0; i < v.n; i++ {
		v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] * bmat.Data[i*bmat.Inc]
	}
}

// DivElemVec performs element-wise division of a by b, placing the result
// in the receiver.
func (v *Vector) DivElemVec(a, b *Vector) {
	ar := a.Len()
	br := b.Len()

	if ar != br {
		panic(ErrShape)
	}

	v.reuseAs(ar)

	amat, bmat := a.RawVector(), b.RawVector()
	for i := 0; i < v.n; i++ {
		v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] / bmat.Data[i*bmat.Inc]
	}
}

// MulVec computes a * b. The result is stored into the receiver.
// MulVec panics if the number of columns in a does not equal the number of rows in b.
func (v *Vector) MulVec(a Matrix, b *Vector) {
	r, c := a.Dims()
	br := b.Len()
	if c != br {
		panic(ErrShape)
	}
	a, trans := untranspose(a)
	ar, ac := a.Dims()
	v.reuseAs(r)
	var restore func()
	if v == a {
		v, restore = v.isolatedWorkspace(a.(*Vector))
		defer restore()
	} else if v == b {
		v, restore = v.isolatedWorkspace(b)
		defer restore()
	}

	switch a := a.(type) {
	case *Vector:
		if a.Len() == 1 {
			// {1,1} x {1,n}
			av := a.At(0, 0)
			for i := 0; i < b.Len(); i++ {
				v.mat.Data[i*v.mat.Inc] = av * b.mat.Data[i*b.mat.Inc]
			}
			return
		}
		if b.Len() == 1 {
			// {1,n} x {1,1}
			bv := b.At(0, 0)
			for i := 0; i < a.Len(); i++ {
				v.mat.Data[i*v.mat.Inc] = bv * a.mat.Data[i*a.mat.Inc]
			}
			return
		}
		// {n,1} x {1,n}
		var sum float64
		for i := 0; i < c; i++ {
			sum += a.At(i, 0) * b.At(i, 0)
		}
		v.SetVec(0, sum)
		return
	case RawSymmetricer:
		amat := a.RawSymmetric()
		blas64.Symv(1, amat, b.mat, 0, v.mat)
	case RawTriangular:
		v.CopyVec(b)
		amat := a.RawTriangular()
		ta := blas.NoTrans
		if trans {
			ta = blas.Trans
		}
		blas64.Trmv(ta, amat, v.mat)
	case RawMatrixer:
		amat := a.RawMatrix()
		t := blas.NoTrans
		if trans {
			t = blas.Trans
		}
		blas64.Gemv(t, 1, amat, b.mat, 0, v.mat)
	case Vectorer:
		if trans {
			col := make([]float64, ar)
			for c := 0; c < ac; c++ {
				v.mat.Data[c*v.mat.Inc] = blas64.Dot(ar,
					blas64.Vector{Inc: 1, Data: a.Col(col, c)},
					b.mat,
				)
			}
		} else {
			row := make([]float64, ac)
			for r := 0; r < ar; r++ {
				v.mat.Data[r*v.mat.Inc] = blas64.Dot(ac,
					blas64.Vector{Inc: 1, Data: a.Row(row, r)},
					b.mat,
				)
			}
		}
	default:
		if trans {
			col := make([]float64, ar)
			for c := 0; c < ac; c++ {
				for i := range col {
					col[i] = a.At(i, c)
				}
				var f float64
				for i, e := range col {
					f += e * b.mat.Data[i*b.mat.Inc]
				}
				v.mat.Data[c*v.mat.Inc] = f
			}
		} else {
			row := make([]float64, ac)
			for r := 0; r < ar; r++ {
				for i := range row {
					row[i] = a.At(r, i)
				}
				var f float64
				for i, e := range row {
					f += e * b.mat.Data[i*b.mat.Inc]
				}
				v.mat.Data[r*v.mat.Inc] = f
			}
		}
	}
}

// reuseAs resizes an empty vector to a r×1 vector,
// or checks that a non-empty matrix is r×1.
func (v *Vector) reuseAs(r int) {
	if v.isZero() {
		v.mat = blas64.Vector{
			Inc:  1,
			Data: use(v.mat.Data, r),
		}
		v.n = r
		return
	}
	if r != v.n {
		panic(ErrShape)
	}
}

func (v *Vector) isZero() bool {
	// It must be the case that v.Dims() returns
	// zeros in this case. See comment in Reset().
	return v.mat.Inc == 0
}

func (v *Vector) isolatedWorkspace(a *Vector) (n *Vector, restore func()) {
	l := a.Len()
	n = getWorkspaceVec(l, false)
	return n, func() {
		v.CopyVec(n)
		putWorkspaceVec(n)
	}
}
<|endoftext|>


<commit_before>package hashers

import (
	"math/rand"
)

const (
	AllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)

func RandomString() string {
	result := make([]byte, 12)
	length := len(AllowedChars)
	for index := range result {
		result[index] = AllowedChars[rand.Intn(length)]
	}
	return string(result)
}
<commit_msg>updated RandomString method, use crypto.Rand for security<commit_after>package hashers

import (
	"crypto/rand"
	"math/big"
)

const (
	AllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	MaxSaltSize  = 12
)

func RandomString() string {
	result := make([]byte, MaxSaltSize)
	length := int64(len(AllowedChars))
	for index := range result {
		i, _ := rand.Int(rand.Reader, big.NewInt(length))
		result[index] = AllowedChars[i.Int64()]
	}
	return string(result)
}
<|endoftext|>


<commit_before>// This is the main package for the `packer` application.
package main

import (
	"github.com/mitchellh/packer/packer"
	"github.com/mitchellh/packer/command/build"
	"fmt"
	"os"
)

func main() {
	envConfig := packer.DefaultEnvironmentConfig()
	envConfig.Command["build"] = new(build.Command)

	env, err := packer.NewEnvironment(envConfig)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Packer initialization error: \n\n%s\n", err)
		os.Exit(1)
	}

	os.Exit(env.Cli(os.Args[1:]))
}
<commit_msg>Get packer building again<commit_after>// This is the main package for the `packer` application.
package main

import (
	"github.com/mitchellh/packer/packer"
	"fmt"
	"os"
)

func main() {
	envConfig := packer.DefaultEnvironmentConfig()

	env, err := packer.NewEnvironment(envConfig)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Packer initialization error: \n\n%s\n", err)
		os.Exit(1)
	}

	os.Exit(env.Cli(os.Args[1:]))
}
<|endoftext|>