misc

package
v0.0.0-...-5e0ed37 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Alltrue = &pipescript.Transform{
	Name:          "alltrue",
	Description:   "Returns true if all datapoints seen have been true, otherwise returns false",
	Documentation: string(resources.MustAsset("docs/transforms/alltrue.md")),
	InputSchema: map[string]interface{}{
		"type": "boolean",
	},
	OutputSchema: map[string]interface{}{
		"type": "boolean",
	},
	Constructor: pipescript.NewAggregator(func(e *pipescript.TransformEnv, consts []interface{}, pipes []*pipescript.Pipe, out *pipescript.Datapoint) (*pipescript.Datapoint, error) {
		dp, _, err := e.Next(nil)
		if err != nil || dp == nil {
			return nil, err
		}
		ldp := dp
		out.Timestamp = dp.Timestamp
		for dp != nil {
			b, err := dp.Bool()
			if err != nil {
				return nil, err
			}
			if !b {
				out.Timestamp = dp.Timestamp
				out.Duration = dp.Duration
				out.Data = false
				return out, nil
			}

			ldp = dp
			dp, _, err = e.Next(nil)
			if err != nil {
				return nil, err
			}
		}

		out.Data = true
		out.Duration = ldp.Timestamp + ldp.Duration - out.Timestamp
		return out, nil
	}),
}
View Source
var Anytrue = pipescript.Transform{
	Name:          "anytrue",
	Description:   "Returns true if at least one of the datapoints seen was true",
	Documentation: string(resources.MustAsset("docs/transforms/anytrue.md")),
	InputSchema: map[string]interface{}{
		"type": "boolean",
	},
	OutputSchema: map[string]interface{}{
		"type": "boolean",
	},
	Constructor: pipescript.NewAggregator(func(e *pipescript.TransformEnv, consts []interface{}, pipes []*pipescript.Pipe, out *pipescript.Datapoint) (*pipescript.Datapoint, error) {
		dp, _, err := e.Next(nil)
		if err != nil || dp == nil {
			return nil, err
		}
		ldp := dp
		out.Timestamp = dp.Timestamp
		for dp != nil {
			b, err := dp.Bool()
			if err != nil {
				return nil, err
			}

			ldp = dp
			if b {
				out.Timestamp = dp.Timestamp
				out.Duration = dp.Duration
				out.Data = true
				return out, nil
			}
			dp, _, err = e.Next(nil)
			if err != nil {
				return nil, err
			}
		}

		out.Data = false
		out.Duration = ldp.Timestamp + ldp.Duration - out.Timestamp
		return out, nil
	}),
}
View Source
var Changed = pipescript.Transform{
	Name:          "changed",
	Description:   "Returns true if the datapoint has a different value from the previous one",
	Documentation: string(resources.MustAsset("docs/transforms/changed.md")),
	OutputSchema: map[string]interface{}{
		"type": "boolean",
	},
	Constructor: func(transform *pipescript.Transform, consts []interface{}, pipes []*pipescript.Pipe) (pipescript.TransformIterator, error) {
		return changedIter{}, nil
	},
}
View Source
var Distance = &pipescript.Transform{
	Name:          "distance",
	Description:   "Returns distance in meters from given latitude/longitude coordinates to datapoint",
	Documentation: string(resources.MustAsset("docs/transforms/distance.md")),
	Args: []pipescript.TransformArg{
		{
			Description: "Latitude",
			Type:        pipescript.ConstArgType,
			Schema: map[string]interface{}{
				"type": "number",
			},
		},
		{
			Description: "Longitude",
			Type:        pipescript.ConstArgType,
			Schema: map[string]interface{}{
				"type": "number",
			},
		},
	},
	InputSchema: map[string]interface{}{
		"type": "object",
		"properties": map[string]interface{}{
			"latitude": map[string]interface{}{
				"type": "number",
			},
			"longitude": map[string]interface{}{
				"type": "number",
			},
		},
		"required": []string{"latitude", "longitude"},
	},
	OutputSchema: map[string]interface{}{
		"type": "number",
	},
	Constructor: pipescript.NewBasic(func(consts []interface{}, pipes []*pipescript.Pipe) ([]interface{}, []*pipescript.Pipe, error) {
		c2 := make([]interface{}, 2)
		f, ok := pipescript.Float(consts[0])
		if !ok {
			return nil, nil, errors.New("latitude must be a number")
		}
		c2[0] = f * Radians
		f, ok = pipescript.Float(consts[1])
		if !ok {
			return nil, nil, errors.New("longitude must be a number")
		}
		c2[1] = f * Radians
		return c2, pipes, nil
	}, func(dp *pipescript.Datapoint, args []*pipescript.Datapoint, consts []interface{}, pipes []*pipescript.Pipe, out *pipescript.Datapoint) (*pipescript.Datapoint, error) {
		data, ok := dp.Data.(map[string]interface{})
		if !ok {
			return nil, errors.New("Distance can only be found to objects with latitude and longitude keys")
		}
		v, ok := data["latitude"]
		if !ok {
			return nil, errors.New("Could not find latitude in datapoint")
		}
		lat, ok := v.(float64)
		if !ok {
			return nil, errors.New("Latitude must be a number")
		}

		v, ok = data["longitude"]
		if !ok {
			return nil, errors.New("Could not find longitude in datapoint")
		}
		long, ok := v.(float64)
		if !ok {
			return nil, errors.New("Longitude must be a number")
		}

		lat = lat * Radians
		long = long * Radians

		arglat := consts[0].(float64)
		arglong := consts[1].(float64)

		dlat := lat - arglat
		dlong := long - arglong

		a := math.Sin(dlat/2)*math.Sin(dlat/2) +
			math.Cos(lat)*math.Cos(arglat)*math.Sin(dlong/2)*math.Sin(dlong/2)

		c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
		out.Data = EarthRadius * c

		return out, nil
	}),
}
View Source
var EarthRadius = float64(6371000)

EarthRadius is the earth radius in meters

View Source
var First = &pipescript.Transform{
	Name:          "first",
	Description:   "Returns true if first datapoint of a sequence, and false otherwise",
	Documentation: string(resources.MustAsset("docs/transforms/first.md")),
	Constructor: func(transform *pipescript.Transform, consts []interface{}, pipes []*pipescript.Pipe) (pipescript.TransformIterator, error) {
		return &firstIter{true}, nil
	},
}
View Source
var Last = &pipescript.Transform{
	Name:          "last",
	Description:   "Returns true if last datapoint of a sequence, and false otherwise",
	Documentation: string(resources.MustAsset("docs/transforms/last.md")),
	Constructor: func(transform *pipescript.Transform, consts []interface{}, pipes []*pipescript.Pipe) (pipescript.TransformIterator, error) {
		return lastIter{}, nil
	},
}
View Source
var Length = &pipescript.Transform{
	Name:        "length",
	Description: "Returns the length of an array/string, or number of object keys.",
	OutputSchema: map[string]interface{}{
		"type": "integer",
	},
	Constructor: pipescript.NewBasic(nil, func(dp *pipescript.Datapoint, args []*pipescript.Datapoint, consts []interface{}, pipes []*pipescript.Pipe, out *pipescript.Datapoint) (*pipescript.Datapoint, error) {
		switch v := dp.Data.(type) {
		case string:
			out.Data = len(v)
		case []interface{}:
			out.Data = len(v)
		case map[string]interface{}:
			out.Data = len(v)
		default:
			return nil, errors.New("Could not find length of data")
		}

		return out, nil
	}),
}
View Source
var Radians = math.Pi / 180.0

Radians is the multiplication constant to convert degrees to radians

Functions

func Register

func Register()

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL