Skip to content

Commit

Permalink
Merge pull request #6 from vinted/fix/get_datetime_on_each_request
Browse files Browse the repository at this point in the history
Fix/get datetime on each request
  • Loading branch information
Seitanas authored Sep 9, 2024
2 parents f7a0113 + 5aeba50 commit 310a0e0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Exporter is designed to build and display metrics based on GraphQL query results
```

Query supports dynamic `datetime` field. It can be specified as template variable.
For example when specified `{{NOW ""-1h"}}` query will generate `datetime` that existed one hour ago.
For example when specified `{{NOW \"-1h\"}}` query will generate `datetime` that existed one hour ago.

Metrics results in:

Expand Down
2 changes: 1 addition & 1 deletion config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"CacheExpire": 0,
"queries":[
{
"query": "query {device_list {name serial custom_fields}} {{NOW "-1h"}}",
"query": "query {device_list {name serial custom_fields}} {{NOW \"-1h\"}}",
"metrics": [
{
"description": "Deprecation date",
Expand Down
28 changes: 2 additions & 26 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package config

import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"text/template"
"time"
)

type Cfg struct {
Expand All @@ -34,13 +30,6 @@ var (
ConfigPath string
)

var funcMap = template.FuncMap{
"NOW": func(t string) (string, error) {
d, err := time.ParseDuration(t)
return time.Now().UTC().Add(d).Format(time.RFC3339), err
},
}

func Init(configPath string) error {
ConfigPath = configPath
content := []byte(`{}`)
Expand All @@ -51,25 +40,12 @@ func Init(configPath string) error {
return err
}
}

if len(content) == 0 {
content = []byte(`{}`)
}

if err != nil {
return fmt.Errorf("%s", err)
}
tpl, err := template.New("query").Funcs(funcMap).Parse(string(content))
if err != nil {
return fmt.Errorf("%s", err)
}

var templateBuffer bytes.Buffer
err = tpl.Execute(&templateBuffer, nil)
if err != nil {
return fmt.Errorf("Template error %s", err)
}

err = json.Unmarshal(templateBuffer.Bytes(), &Config)
err = json.Unmarshal(content, &Config)
if err != nil {
return err
}
Expand Down
21 changes: 20 additions & 1 deletion pkg/graphql/graphql.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
package graphql

import (
"bytes"
"fmt"
"github.com/vinted/graphql-exporter/pkg/config"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"text/template"
"time"
)

var funcMap = template.FuncMap{
"NOW": func(t string) (string, error) {
d, err := time.ParseDuration(t)
return time.Now().UTC().Add(d).Format(time.RFC3339), err
},
}

func GraphqlQuery(query string) ([]byte, error) {
params := url.Values{}
params.Add("query", query)
tpl, err := template.New("query").Funcs(funcMap).Parse(query)
if err != nil {
return nil, fmt.Errorf("%s", err)
}
var templateBuffer bytes.Buffer
err = tpl.Execute(&templateBuffer, nil)
if err != nil {
return nil, fmt.Errorf("Template error %s", err)
}
params.Add("query", templateBuffer.String())
u, err := url.ParseRequestURI(config.Config.GraphqlURL)
if err != nil {
log.Printf("Error parsing URL: %s\n", err)
Expand Down

0 comments on commit 310a0e0

Please sign in to comment.