Skip to content

Commit

Permalink
feature-complete all db and audit tables
Browse files Browse the repository at this point in the history
  • Loading branch information
alvin-reyes committed Mar 14, 2023
1 parent 6afd885 commit 8ec1449
Show file tree
Hide file tree
Showing 19 changed files with 662 additions and 411 deletions.
12 changes: 9 additions & 3 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions db_models/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package db_models

import (
"io/ioutil"
"net/http"
"os"
)

func GetPublicIP() (string, error) {
resp, err := http.Get("https://ifconfig.me") // important to get the public ip if possible.
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}

func GetHostname() string {
hostname, err := os.Hostname()
if err != nil {
return "unknown"
}
return hostname
}
58 changes: 39 additions & 19 deletions db_models/content.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package db_models

import (
"bytes"
"encoding/json"
"fmt"
"gorm.io/gorm"
"time"
)
Expand All @@ -24,22 +22,44 @@ type Content struct {

func (u *Content) AfterSave(tx *gorm.DB) (err error) {

// log this on the event log table
messageBytes, err := json.Marshal(u)
tx.Model(&LogEvent{}).Save(&LogEvent{
LogEventType: "Content",
LogEventObject: messageBytes,
LogEventId: u.ID,
LogEvent: fmt.Sprintf("Content %d saved", u.ID),
Collected: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
return
}
var contentFromDb Content
tx.Model(&Content{}).Where("id = ?", u.ID).First(&contentFromDb)

if contentFromDb.ID == 0 {
return
}
// get instance info
ip, err := GetPublicIP()
if err != nil {
return
}

log := ContentLog{
Name: contentFromDb.Name,
Size: contentFromDb.Size,
Cid: contentFromDb.Cid,
RequestingApiKey: contentFromDb.RequestingApiKey,
PieceCommitmentId: contentFromDb.PieceCommitmentId,
Status: contentFromDb.Status,
ConnectionMode: contentFromDb.ConnectionMode,
LastMessage: contentFromDb.LastMessage,
NodeInfo: GetHostname(),
RequesterInfo: ip,
SystemContentId: contentFromDb.ID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

func Transcode(in, out interface{}) {
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(in)
json.NewDecoder(buf).Decode(out)
deltaMetricsBaseMessage := DeltaMetricsBaseMessage{
ObjectType: "ContentLog",
Object: log,
}

messageBytes, err := json.Marshal(deltaMetricsBaseMessage)
if err != nil {
return err
}
producer.Publish(messageBytes)

return
}
59 changes: 49 additions & 10 deletions db_models/content_deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,54 @@ type ContentDeal struct {
UpdatedAt time.Time `json:"updated_at"`
}

func (u *ContentDeal) AfterSave(tx *gorm.DB) (err error) {
messageBytes, err := json.Marshal(u)
tx.Model(&LogEvent{}).Save(&LogEvent{
LogEventType: "ContentDeal",
LogEventObject: messageBytes,
LogEventId: u.ID,
Collected: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
func (u *ContentDeal) AfterCreate(tx *gorm.DB) (err error) {

var contentDealLog ContentDeal
tx.Model(&ContentDeal{}).Where("id = ?", u.ID).First(&contentDealLog)

if contentDealLog.ID == 0 {
return
}
// get instance info
ip, err := GetPublicIP()
if err != nil {
return
}

log := ContentDealLog{
Content: contentDealLog.Content,
PropCid: contentDealLog.PropCid,
DealUUID: contentDealLog.DealUUID,
Miner: contentDealLog.Miner,
DealID: contentDealLog.DealID,
Failed: contentDealLog.Failed,
Verified: contentDealLog.Verified,
Slashed: contentDealLog.Slashed,
FailedAt: contentDealLog.FailedAt,
DTChan: contentDealLog.DTChan,
TransferStarted: contentDealLog.TransferStarted,
TransferFinished: contentDealLog.TransferFinished,
OnChainAt: contentDealLog.OnChainAt,
SealedAt: contentDealLog.SealedAt,
LastMessage: contentDealLog.LastMessage,
DealProtocolVersion: contentDealLog.DealProtocolVersion,
MinerVersion: contentDealLog.MinerVersion,
NodeInfo: GetHostname(),
RequesterInfo: ip,
SystemContentDealId: contentDealLog.ID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

deltaMetricsBaseMessage := DeltaMetricsBaseMessage{
ObjectType: "ContentDealLog",
Object: log,
}

messageBytes, err := json.Marshal(deltaMetricsBaseMessage)
if err != nil {
return err
}
producer.Publish(messageBytes)
return
}
45 changes: 35 additions & 10 deletions db_models/content_deal_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,40 @@ type ContentDealProposal struct {
UpdatedAt time.Time `json:"updated_at"`
}

func (u *ContentDealProposal) AfterSave(tx *gorm.DB) (err error) {
messageBytes, err := json.Marshal(u)
tx.Model(&LogEvent{}).Save(&LogEvent{
LogEventType: "ContentDealProposal",
LogEventObject: messageBytes,
LogEventId: u.ID,
Collected: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
func (u *ContentDealProposal) AfterCreate(tx *gorm.DB) (err error) {

var contentDealProposal ContentDealProposal
tx.Model(&ContentDealProposal{}).Where("id = ?", u.ID).First(&contentDealProposal)

if contentDealProposal.ID == 0 {
return
}

// get instance info
ip, err := GetPublicIP()
if err != nil {
return
}

log := ContentDealProposalLog{
Content: contentDealProposal.Content,
Unsigned: contentDealProposal.Unsigned,
Signed: contentDealProposal.Signed,
Meta: contentDealProposal.Meta,
NodeInfo: GetHostname(),
RequesterInfo: ip,
SystemContentDealProposalId: u.ID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

deltaMetricsBaseMessage := DeltaMetricsBaseMessage{
ObjectType: "ContentDealProposalLog",
Object: log,
}

messageBytes, err := json.Marshal(deltaMetricsBaseMessage)
producer.Publish(messageBytes)

return
}
48 changes: 38 additions & 10 deletions db_models/content_deal_proposal_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,43 @@ type ContentDealProposalParameters struct {
UpdatedAt time.Time `json:"updated_at" json:"updated-at"`
}

func (u *ContentDealProposalParameters) AfterSave(tx *gorm.DB) (err error) {
messageBytes, err := json.Marshal(u)
tx.Model(&LogEvent{}).Save(&LogEvent{
LogEventType: "ContentDealProposalParameters",
LogEventObject: messageBytes,
LogEventId: u.ID,
Collected: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
func (u *ContentDealProposalParameters) AfterCreate(tx *gorm.DB) (err error) {

var contentDealProposalParams ContentDealProposalParameters
tx.Model(&ContentDealProposalParameters{}).Where("id = ?", u.ID).First(&contentDealProposalParams)

if contentDealProposalParams.ID == 0 {
return
}

// get instance info
ip, err := GetPublicIP()
if err != nil {
return
}
log := ContentDealProposalParametersLog{
Content: contentDealProposalParams.Content,
Label: contentDealProposalParams.Label,
Duration: contentDealProposalParams.Duration,
StartEpoch: contentDealProposalParams.StartEpoch,
EndEpoch: contentDealProposalParams.EndEpoch,
TransferParams: contentDealProposalParams.TransferParams,
RemoveUnsealedCopy: contentDealProposalParams.RemoveUnsealedCopy,
SkipIPNIAnnounce: contentDealProposalParams.SkipIPNIAnnounce,
NodeInfo: GetHostname(),
RequesterInfo: ip,
SystemContentDealProposalParametersId: u.ID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

deltaMetricsBaseMessage := DeltaMetricsBaseMessage{
ObjectType: "ContentDealProposalParametersLog",
Object: log,
}

messageBytes, err := json.Marshal(deltaMetricsBaseMessage)
producer.Publish(messageBytes)

return
}
40 changes: 29 additions & 11 deletions db_models/content_miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,38 @@ type ContentMiner struct {
UpdatedAt time.Time `json:"updated_at"`
}

func (u *ContentMiner) AfterSave(tx *gorm.DB) (err error) {
func (u *ContentMiner) AfterCreate(tx *gorm.DB) (err error) {

if u.Miner == "" {
var contentMiner ContentMiner
tx.Model(&ContentMiner{}).Where("id = ?", u.ID).First(&contentMiner)

if contentMiner.ID == 0 {
return
}

messageBytes, err := json.Marshal(u)
tx.Model(&LogEvent{}).Save(&LogEvent{
LogEventType: "ContentMiner",
LogEventObject: messageBytes,
LogEventId: u.ID,
Collected: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
// get instance info
ip, err := GetPublicIP()
if err != nil {
return
}

log := ContentMinerLog{
Content: contentMiner.Content,
Miner: contentMiner.Miner,
NodeInfo: GetHostname(),
RequesterInfo: ip,
SystemContentMinerId: u.ID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

deltaMetricsBaseMessage := DeltaMetricsBaseMessage{
ObjectType: "ContentMinerLog",
Object: log,
}

messageBytes, err := json.Marshal(deltaMetricsBaseMessage)
producer.Publish(messageBytes)

return
}
Loading

0 comments on commit 8ec1449

Please sign in to comment.