Skip to content

Commit

Permalink
feat: add logger for pow events (#210)
Browse files Browse the repository at this point in the history
* feat: add logger for pow events

* feat: add error checking when setting up requests and improve on error messages

* feat: add a generic post client to send json to a url

* refactor: update to generic post client

* refactor: update to generic post client
  • Loading branch information
AquiGorka authored Jun 27, 2024
1 parent d776217 commit ec2d59b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 35 deletions.
25 changes: 23 additions & 2 deletions pkg/http/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
"net/http"
"net/url"
"strings"
"time"

"github.com/lilypad-tech/lilypad/pkg/web3"
"github.com/hashicorp/go-retryablehttp"
"github.com/lilypad-tech/lilypad/pkg/web3"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -321,6 +322,26 @@ func GetRequestBuffer(
return &buf, nil
}

func GenericJSONPostClient(url string, json string) (*http.Response, error) {
data := []byte(json)
client := &http.Client{Timeout: time.Second * 1}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
fmt.Printf("error setting up the request: %s", err)
return nil, err
}
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
fmt.Printf("error sending the request: %s", err)
return nil, err
}
resp.Body.Close()

return resp, nil
}

func PostRequest[RequestType any, ResultType any](
options ClientOptions,
path string,
Expand All @@ -329,7 +350,7 @@ func PostRequest[RequestType any, ResultType any](
var result ResultType
dataBytes, err := json.Marshal(data)
if err != nil {
return result, fmt.Errorf("THIS IS A JOSN ERROR: %s", err.Error())
return result, fmt.Errorf("THIS IS A JSON ERROR: %s", err.Error())
}
return PostRequestBuffer[ResultType](
options,
Expand Down
60 changes: 27 additions & 33 deletions pkg/metricsDashboard/logger.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
package metricsDashboard

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"

"github.com/lilypad-tech/lilypad/pkg/data"
"github.com/lilypad-tech/lilypad/pkg/http"
)

const jobsEndpoint = "jobs"
const nodeInfoEndpoint = "nodes"
const nodeConnectionEndpoint = "uptimes"
const dealsEndpoint = "deals"
const namespace = "metrics-dashboard"

var host = os.Getenv("API_HOST")

func trackEvent(path string, json string) {
if host == "" {
return
}

var url = host + "metrics-dashboard/" + path

data := []byte(json)

client := &http.Client{Timeout: time.Second * 1}
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
resp.Body.Close()
type DealPayload struct {
ID string
JobCreator string
ResourceProvider string
JobID string
}

func TrackJobOfferUpdate(evOffer data.JobOfferContainer) {
if host == "" {
return
}
var module = evOffer.JobOffer.Module.Name
if module == "" {
module = evOffer.JobOffer.Module.Repo + ":" + evOffer.JobOffer.Module.Hash
Expand All @@ -58,10 +46,14 @@ func TrackJobOfferUpdate(evOffer data.JobOfferContainer) {
byts, _ := json.Marshal(data)
payload := string(byts)

trackEvent(jobsEndpoint, payload)
url := host + namespace + "/" + jobsEndpoint
http.GenericJSONPostClient(url, payload)
}

func TrackNodeInfo(resourceOffer data.ResourceOffer) {
if host == "" {
return
}
data := map[string]interface{}{
"ID": resourceOffer.ResourceProvider,
"GPU": resourceOffer.Spec.GPU,
Expand All @@ -72,7 +64,8 @@ func TrackNodeInfo(resourceOffer data.ResourceOffer) {
byts, _ := json.Marshal(data)
payload := string(byts)

trackEvent(nodeInfoEndpoint, payload)
url := host + namespace + "/" + nodeInfoEndpoint
http.GenericJSONPostClient(url, payload)
}

type NodeConnectionParams struct {
Expand All @@ -83,6 +76,9 @@ type NodeConnectionParams struct {
}

func TrackNodeConnectionEvent(params NodeConnectionParams) {
if host == "" {
return
}
data := map[string]interface{}{
"ID": params.ID,
"Event": params.Event,
Expand All @@ -93,19 +89,17 @@ func TrackNodeConnectionEvent(params NodeConnectionParams) {
byts, _ := json.Marshal(data)
payload := string(byts)

trackEvent(nodeConnectionEndpoint, payload)
}

type DealPayload struct {
ID string
JobCreator string
ResourceProvider string
JobID string
url := host + namespace + "/" + nodeConnectionEndpoint
http.GenericJSONPostClient(url, payload)
}

func TrackDeal(params DealPayload) {
if host == "" {
return
}
byts, _ := json.Marshal(params)
payload := string(byts)

trackEvent(dealsEndpoint, payload)
url := host + namespace + "/" + dealsEndpoint
http.GenericJSONPostClient(url, payload)
}
35 changes: 35 additions & 0 deletions pkg/powLogs/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package powLogs

import (
"encoding/json"
"os"

"github.com/lilypad-tech/lilypad/pkg/http"
)

type PowLog struct {
POWTime string // YYYY-MM-DD HH::MM:ss (triggers are every hour on the hour and randomly within the hour)
// t := time.Now(); t.Format(time.RFC3339)
Caller string // cron or rp
CallerID string // cron or rp public address
Step string // text instead of numbers, this way we can query by giving a sort order to the events if we know all the steps, and we do not need to reorder all events if we add one in between
Payload string // a json payload
Status string // log/info/warn/error a way to split an event payload into multiple entries to then be able to filter logs
}

const namespace = "pow-logs"
const eventsEndpoint = "events"

var host = os.Getenv("API_HOST")

func TrackEvent(data PowLog) {
if host == "" {
return
}

var url = host + namespace + "/" + eventsEndpoint
byts, _ := json.Marshal(data)
payload := string(byts)

http.GenericJSONPostClient(url, payload)
}

0 comments on commit ec2d59b

Please sign in to comment.