Skip to content

Commit

Permalink
feat: remove node free/used resources
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanVerstraete committed Apr 11, 2022
1 parent 314490d commit 316ce31
Show file tree
Hide file tree
Showing 8 changed files with 3 additions and 180 deletions.
18 changes: 0 additions & 18 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ type Node @entity {
city: String
publicConfig: PublicConfig @derivedFrom(field: "node")
resourcesTotal: NodeResourcesTotal @derivedFrom(field: "node")
resourcesUsed: NodeResourcesUsed @derivedFrom(field: "node")
resourcesFree: NodeResourcesFree @derivedFrom(field: "node")
uptime: BigInt
created: Int!
farmingPolicyId: Int!
Expand All @@ -94,22 +92,6 @@ type NodeResourcesTotal @entity {
mru: BigInt!
}

type NodeResourcesUsed @entity {
node: Node! @unique
hru: BigInt!
sru: BigInt!
cru: BigInt!
mru: BigInt!
}

type NodeResourcesFree @entity {
node: Node! @unique
hru: BigInt!
sru: BigInt!
cru: BigInt!
mru: BigInt!
}

type Interfaces @entity {
node: Node!
name: String!
Expand Down
52 changes: 1 addition & 51 deletions src/mappings/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
EventHandlerContext,
Store
} from "@subsquid/substrate-processor";
import { ContractState, PublicIp, NameContract, NodeContract, ContractBillReport, DiscountLevel, ContractResources, NodeResourcesFree, NodeResourcesUsed, NodeResourcesTotal, Node, RentContract, Farm, NruConsumption } from "../model";
import { ContractState, PublicIp, NameContract, NodeContract, ContractBillReport, DiscountLevel, ContractResources, NodeResourcesTotal, Node, RentContract, Farm, NruConsumption } from "../model";
import { SmartContractModuleContractCreatedEvent, SmartContractModuleContractUpdatedEvent, SmartContractModuleNodeContractCanceledEvent, SmartContractModuleNameContractCanceledEvent, SmartContractModuleContractBilledEvent, SmartContractModuleUpdatedUsedResourcesEvent, SmartContractModuleNruConsumptionReportReceivedEvent, SmartContractModuleRentContractCanceledEvent } from "../types/events";

export async function contractCreated(ctx: EventHandlerContext) {
Expand Down Expand Up @@ -168,31 +168,6 @@ export async function nodeContractCanceled(ctx: EventHandlerContext) {
savedPublicIP.contractId = BigInt(0)
await ctx.store.save<PublicIp>(savedPublicIP)
}

const usedResources = await ctx.store.get(ContractResources, { where: { contract: savedContract }})
if (!usedResources) return

const resourcesUsed = await ctx.store.get(NodeResourcesUsed, { where: { node: savedNode }})
const resourcesFree = await ctx.store.get(NodeResourcesFree, { where: { node: savedNode }})
const resourcesTotal = await ctx.store.get(NodeResourcesTotal, { where: { node: savedNode }})

// update used
if (resourcesUsed) {
resourcesUsed.cru -= usedResources.cru
resourcesUsed.sru -= usedResources.sru
resourcesUsed.hru -= usedResources.hru
resourcesUsed.mru -= usedResources.mru
await ctx.store.save<NodeResourcesUsed>(resourcesUsed)
}

// update free
if (resourcesFree && resourcesUsed && resourcesTotal) {
resourcesFree.cru = resourcesTotal.cru - resourcesUsed.cru
resourcesFree.sru = (BigInt(2) * resourcesTotal.sru) - resourcesUsed.sru
resourcesFree.hru = resourcesTotal.hru - resourcesUsed.hru
resourcesFree.mru = resourcesTotal.mru - resourcesUsed.mru
await ctx.store.save<NodeResourcesFree>(resourcesFree)
}
}

export async function nameContractCanceled(ctx: EventHandlerContext) {
Expand Down Expand Up @@ -268,31 +243,6 @@ export async function contractUpdateUsedResources(ctx: EventHandlerContext) {
savedContract.resourcesUsed = contractUsedResources

await ctx.store.save<NodeContract>(savedContract)

const node = await ctx.store.get(Node, { where: { nodeID: savedContract.nodeID }})
if (!node) return

const resourcesUsed = await ctx.store.get(NodeResourcesUsed, { where: { node: node }})
const resourcesFree = await ctx.store.get(NodeResourcesFree, { where: { node: node }})
const resourcesTotal = await ctx.store.get(NodeResourcesTotal, { where: { node: node }})

// update used
if (resourcesUsed) {
resourcesUsed.cru += usedResources.used.cru
resourcesUsed.sru += usedResources.used.sru
resourcesUsed.hru += usedResources.used.hru
resourcesUsed.mru += usedResources.used.mru
await ctx.store.save<NodeResourcesUsed>(resourcesUsed)
}

// update free
if (resourcesFree && resourcesUsed && resourcesTotal) {
resourcesFree.cru = resourcesTotal.cru - resourcesUsed.cru
resourcesFree.sru = (BigInt(2) * resourcesTotal.sru) - resourcesUsed.sru
resourcesFree.hru = resourcesTotal.hru - resourcesUsed.hru
resourcesFree.mru = resourcesTotal.mru - resourcesUsed.mru
await ctx.store.save<NodeResourcesFree>(resourcesFree)
}
}

export async function nruConsumptionReportReceived(ctx: EventHandlerContext) {
Expand Down
41 changes: 1 addition & 40 deletions src/mappings/nodes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
EventHandlerContext,
} from "@subsquid/substrate-processor";
import { Node, Location, PublicConfig, CertificationType, Interfaces, UptimeEvent, NodeResourcesUsed, NodeResourcesFree, NodeResourcesTotal } from "../model";
import { Node, Location, PublicConfig, CertificationType, Interfaces, UptimeEvent, NodeResourcesTotal } from "../model";
import { TfgridModuleNodeDeletedEvent, TfgridModuleNodePublicConfigStoredEvent, TfgridModuleNodeStoredEvent, TfgridModuleNodeUpdatedEvent, TfgridModuleNodeUptimeReportedEvent } from "../types/events";

export async function nodeStored(ctx: EventHandlerContext) {
Expand Down Expand Up @@ -92,25 +92,7 @@ export async function nodeStored(ctx: EventHandlerContext) {
resourcesTotal.mru = nodeEvent.resources.mru
resourcesTotal.cru = nodeEvent.resources.cru

const resourcesUsed = new NodeResourcesUsed()
resourcesUsed.node = newNode
resourcesUsed.id = ctx.event.id
resourcesUsed.sru = BigInt(0)
resourcesUsed.hru = BigInt(0)
resourcesUsed.mru = BigInt(0)
resourcesUsed.cru = BigInt(0)

const resourcesFree = new NodeResourcesFree()
resourcesFree.node = newNode
resourcesFree.id = ctx.event.id
resourcesFree.sru = nodeEvent.resources.sru
resourcesFree.hru = nodeEvent.resources.hru
resourcesFree.mru = nodeEvent.resources.mru
resourcesFree.cru = nodeEvent.resources.cru

await ctx.store.save<NodeResourcesTotal>(resourcesTotal)
await ctx.store.save<NodeResourcesUsed>(resourcesUsed)
await ctx.store.save<NodeResourcesFree>(resourcesFree)

if (nodeEvent.publicConfig) {
const pubConfig = new PublicConfig()
Expand Down Expand Up @@ -174,19 +156,6 @@ export async function nodeUpdated(ctx: EventHandlerContext) {
resourcesTotal.mru = nodeEvent.resources.mru
resourcesTotal.cru = nodeEvent.resources.cru
await ctx.store.save<NodeResourcesTotal>(resourcesTotal)

// recalculate free resources
let resourcesFree = await ctx.store.get(NodeResourcesFree, { where: { node: savedNode } })
if (resourcesFree) {
let resourcesUsed = await ctx.store.get(NodeResourcesUsed, { where: { node: savedNode } })
if (resourcesUsed) {
resourcesFree.sru = resourcesTotal.sru - resourcesUsed.sru
resourcesFree.hru = resourcesTotal.hru - resourcesUsed.hru
resourcesFree.mru = resourcesTotal.mru - resourcesUsed.mru
resourcesFree.cru = resourcesTotal.cru - resourcesUsed.cru
await ctx.store.save<NodeResourcesFree>(resourcesFree)
}
}
}

savedNode.country = nodeEvent.country.toString()
Expand Down Expand Up @@ -283,14 +252,6 @@ export async function nodeDeleted(ctx: EventHandlerContext) {
if (resourcesTotal) {
await ctx.store.remove(resourcesTotal)
}
const resourcesFree = await ctx.store.get(NodeResourcesFree, { where: { node: savedNode } })
if (resourcesFree) {
await ctx.store.remove(resourcesFree)
}
const resourcesUsed = await ctx.store.get(NodeResourcesUsed, { where: { node: savedNode } })
if (resourcesUsed) {
await ctx.store.remove(resourcesUsed)
}
const pubConfig = await ctx.store.get(PublicConfig, { where: { node: savedNode } })
if (pubConfig) {
await ctx.store.remove(pubConfig)
Expand Down
2 changes: 0 additions & 2 deletions src/model/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export * from "./_certificationType"
export * from "./publicIp.model"
export * from "./node.model"
export * from "./nodeResourcesTotal.model"
export * from "./nodeResourcesUsed.model"
export * from "./nodeResourcesFree.model"
export * from "./interfaces.model"
export * from "./publicConfig.model"
export * from "./location.model"
Expand Down
8 changes: 0 additions & 8 deletions src/model/generated/node.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import * as marshal from "./marshal"
import {Location} from "./location.model"
import {PublicConfig} from "./publicConfig.model"
import {NodeResourcesTotal} from "./nodeResourcesTotal.model"
import {NodeResourcesUsed} from "./nodeResourcesUsed.model"
import {NodeResourcesFree} from "./nodeResourcesFree.model"
import {Interfaces} from "./interfaces.model"
import {CertificationType} from "./_certificationType"

Expand Down Expand Up @@ -45,12 +43,6 @@ export class Node {
@OneToOne_(() => NodeResourcesTotal)
resourcesTotal!: NodeResourcesTotal | undefined | null

@OneToOne_(() => NodeResourcesUsed)
resourcesUsed!: NodeResourcesUsed | undefined | null

@OneToOne_(() => NodeResourcesFree)
resourcesFree!: NodeResourcesFree | undefined | null

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: true})
uptime!: bigint | undefined | null

Expand Down
30 changes: 0 additions & 30 deletions src/model/generated/nodeResourcesFree.model.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/model/generated/nodeResourcesUsed.model.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
const processor = new SubstrateProcessor("substrate_threefold");

processor.setTypesBundle("typegen/typesBundle.json");
processor.setBatchSize(2000);
processor.setBatchSize(500);
processor.setPrometheusPort(44233)

processor.setDataSource({
Expand Down

0 comments on commit 316ce31

Please sign in to comment.