Skip to content

Commit

Permalink
feat: add tracking job and module events (#123)
Browse files Browse the repository at this point in the history
* chore: allow preserving env when running local doppler env injection

* feat: add metrics dashboard logger package

* feat: track job offer updates
  • Loading branch information
AquiGorka authored May 30, 2024
1 parent f957f9e commit acf81d1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
5 changes: 4 additions & 1 deletion pkg/jobcreator/onchain_jobcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions pkg/metricsDashboard/logger.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 10 additions & 3 deletions stack
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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 "$@"

0 comments on commit acf81d1

Please sign in to comment.