File: snapshot.go

package info (click to toggle)
golang-github-zorkian-go-datadog-api 2.30.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,628 kB
  • sloc: makefile: 28; sh: 13
file content (45 lines) | stat: -rw-r--r-- 1,250 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * Datadog API for Go
 *
 * Please see the included LICENSE file for licensing information.
 *
 * Copyright 2016 by authors and contributors.
 */

package datadog

import (
	"fmt"
	"net/url"
	"time"
)

func (client *Client) doSnapshotRequest(values url.Values) (string, error) {
	out := struct {
		SnapshotURL string `json:"snapshot_url,omitempty"`
	}{}
	if err := client.doJsonRequest("GET", "/v1/graph/snapshot?"+values.Encode(), nil, &out); err != nil {
		return "", err
	}
	return out.SnapshotURL, nil
}

// Snapshot creates an image from a graph and returns the URL of the image.
func (client *Client) Snapshot(query string, start, end time.Time, eventQuery string) (string, error) {
	options := map[string]string{"metric_query": query, "event_query": eventQuery}

	return client.SnapshotGeneric(options, start, end)
}

// Generic function for snapshots, use map[string]string to create url.Values() instead of pre-defined params
func (client *Client) SnapshotGeneric(options map[string]string, start, end time.Time) (string, error) {
	v := url.Values{}
	v.Add("start", fmt.Sprintf("%d", start.Unix()))
	v.Add("end", fmt.Sprintf("%d", end.Unix()))

	for opt, val := range options {
		v.Add(opt, val)
	}

	return client.doSnapshotRequest(v)
}