From acf81d14097b7388c6a70ed230837b0a75363c6a Mon Sep 17 00:00:00 2001 From: Gorka Ludlow Date: Thu, 30 May 2024 16:36:00 -0300 Subject: [PATCH] feat: add tracking job and module events (#123) * chore: allow preserving env when running local doppler env injection * feat: add metrics dashboard logger package * feat: track job offer updates --- pkg/jobcreator/onchain_jobcreator.go | 5 ++- pkg/metricsDashboard/logger.go | 54 ++++++++++++++++++++++++++++ stack | 13 +++++-- 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 pkg/metricsDashboard/logger.go diff --git a/pkg/jobcreator/onchain_jobcreator.go b/pkg/jobcreator/onchain_jobcreator.go index 9c071604..c06ce794 100644 --- a/pkg/jobcreator/onchain_jobcreator.go +++ b/pkg/jobcreator/onchain_jobcreator.go @@ -6,11 +6,12 @@ import ( "math/big" "strings" + "github.com/davecgh/go-spew/spew" "github.com/lilypad-tech/lilypad/pkg/data" + "github.com/lilypad-tech/lilypad/pkg/metricsDashboard" "github.com/lilypad-tech/lilypad/pkg/system" "github.com/lilypad-tech/lilypad/pkg/web3" jobcreatorweb3 "github.com/lilypad-tech/lilypad/pkg/web3/bindings/jobcreator" - "github.com/davecgh/go-spew/spew" ) const JOB_PRICE = 2 @@ -63,6 +64,8 @@ func (jobCreator *OnChainJobCreator) Start(ctx context.Context, cm *system.Clean return errorChan } + jobCreator.SubscribeToJobOfferUpdates(metricsDashboard.TrackJobOfferUpdate) + jobCreator.controller.SubscribeToJobOfferUpdates(func(evOffer data.JobOfferContainer) { if evOffer.State != data.GetAgreementStateIndex("ResultsAccepted") { return diff --git a/pkg/metricsDashboard/logger.go b/pkg/metricsDashboard/logger.go new file mode 100644 index 00000000..51de1b86 --- /dev/null +++ b/pkg/metricsDashboard/logger.go @@ -0,0 +1,54 @@ +package metricsDashboard + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "os" + "time" + + "github.com/lilypad-tech/lilypad/pkg/data" +) + +var host = os.Getenv("API_HOST") +var endpoint = "metrics-dashboard/logs" +var url = host + endpoint + +func TrackEvent(json string) { + 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() +} + +func TrackJobOfferUpdate(evOffer data.JobOfferContainer) { + var module = evOffer.JobOffer.Module.Name + if module == "" { + module = evOffer.JobOffer.Module.Repo + ":" + evOffer.JobOffer.Module.Hash + } + + data := map[string]interface{}{ + "ID": evOffer.ID, + "JobOfferID": evOffer.JobOffer.ID, + "CreatedAt": evOffer.JobOffer.CreatedAt, + "JobCreator": evOffer.JobCreator, + "DealID": evOffer.DealID, + "State": evOffer.State, + "Module": module, + "Timestamp": time.Now().UnixMilli(), + "Event": "JobOfferUpdate", + } + byts, _ := json.Marshal(data) + payload := string(byts) + + TrackEvent(payload) +} diff --git a/stack b/stack index e6e3f5e5..8dc143c9 100755 --- a/stack +++ b/stack @@ -218,7 +218,7 @@ function run-cowsay-onchain() { function solver() { echo "- Reminder to do doppler setup to project->solver and config->dev" - doppler run -p solver -c dev -- go run . solver + doppler run --preserve-env -p solver -c dev -- go run . solver } function solver-docker-build() { @@ -245,7 +245,7 @@ function solver-docker-run() { function job-creator() { echo "- Reminder to do doppler setup to project->job-creator and config->dev" - doppler run -p job-creator -c dev -- go run . jobcreator + doppler run --preserve-env -p job-creator -c dev -- go run . jobcreator } function job-creator-docker-build() { @@ -270,7 +270,7 @@ function job-creator-docker-run() { function resource-provider() { echo "- Reminder to do doppler setup to project->resource-provider and config->dev" - doppler run -p resource-provider -c dev -- go run . resource-provider "$@" + doppler run --preserve-env -p resource-provider -c dev -- go run . resource-provider "$@" } function resource-provider-docker-build() { @@ -344,4 +344,11 @@ function integration-tests() { doppler run -p integration-tests -c dev -- go test -v -count 1 . } +############################################################################ +# run +############################################################################ +function run() { + doppler run --preserve-env -p run -c dev -- go run . run "$@" +} + eval "$@"