Skip to content

Commit

Permalink
Add version to analytics resources
Browse files Browse the repository at this point in the history
  • Loading branch information
hawyar committed Jun 21, 2023
1 parent 6ebd744 commit 0de5097
Show file tree
Hide file tree
Showing 7 changed files with 21,828 additions and 29 deletions.
3 changes: 2 additions & 1 deletion common/data/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@casimir/data",
"private": true,
"version": "1.0.0",
"main": "src/index.ts",
"scripts": {
"build": "echo '@casimir/data build not specified. Disregard this warning and any listed errors above if @casimir/types is not needed for the current project build.' && exit 0",
Expand All @@ -20,4 +21,4 @@
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0"
}
}
}
24 changes: 16 additions & 8 deletions common/data/src/schemas/wallet.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,36 @@
"type": "object",
"properties": {
"wallet_address": {
"type": "string"
"type": "string",
"description": "The wallet address"
},
"wallet_balance": {
"type": "string"
"type": "string",
"description": "The wallet balance"
},
"tx_direction": {
"type": "string"
"type": "string",
"description": "Ingoing or outgoing transaction"
},
"tx_id": {
"type": "string"
"type": "string",
"description": "The transaction id"
},
"received_at": {
"type": "string"
"type": "string",
"description": "The time the transaction was received"
},
"amount": {
"type": "string"
"type": "string",
"description": "The amount of the transaction"
},
"price": {
"type": "string"
"type": "string",
"description": "The exchnage price at the time of the transaction"
},
"gas_fee": {
"type": "string"
"type": "string",
"description": "The gas fee of the transaction"
}
}
}
2 changes: 1 addition & 1 deletion infrastructure/cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"constructs": "^10.0.0",
"source-map-support": "^0.5.16"
}
}
}
10 changes: 7 additions & 3 deletions infrastructure/cdk/src/providers/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pascalCase } from '@casimir/helpers'
import { ProjectConfig } from '../interfaces/ProjectConfig'
import { version } from '@casimir/data/package.json'

/**
* CDK app config
Expand All @@ -11,6 +12,7 @@ export class Config implements ProjectConfig {
public readonly rootDomain
public readonly subdomains
public readonly nodesIp
public readonly dataVersion

/** List of required environment variables */
public readonly requiredEnvVars = ['PROJECT', 'STAGE', 'AWS_ACCOUNT', 'AWS_REGION']
Expand All @@ -31,6 +33,7 @@ export class Config implements ProjectConfig {
wildcard: '*'
}
this.nodesIp = process.env.NODES_IP as string
this.dataVersion = version.split('.')[0]
}

/**
Expand Down Expand Up @@ -64,15 +67,16 @@ export class Config implements ProjectConfig {
* Get stack resource name with project prefix and stage suffix
* @param stackName Stack name
* @param resourceName Resource name
* @param version Optional resource version
* @returns Resource name
* @example
* ```typescript
* const resourceName = config.getFullStackResourceName('etl', 'event-bucket')
* console.log(resourceName) // EtlEventBucketDev
* ```
*/
getFullStackResourceName(stackName: string, resourceName: string): string {
return this.project + pascalCase(stackName) + pascalCase(resourceName) + this.stage
getFullStackResourceName(stackName: string, resourceName: string, version?: string): string {
// CasimirEtlEventBucketDev1
return this.project + pascalCase(stackName) + pascalCase(resourceName) + this.stage + version
}

}
18 changes: 14 additions & 4 deletions infrastructure/cdk/src/providers/etl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ export class EtlStack extends cdk.Stack {
})

/** Create S3 buckets */
const eventBucket = new s3.Bucket(this, config.getFullStackResourceName(this.name, 'event-bucket'))
const walletBucket = new s3.Bucket(this, config.getFullStackResourceName(this.name, 'wallet-bucket'))
const stakingActionBucket = new s3.Bucket(this, config.getFullStackResourceName(this.name, 'staking-action-bucket'))
const eventBucket = new s3.Bucket(this, config.getFullStackResourceName(this.name, 'event-bucket', config.dataVersion), {
bucketName: kebabCase(config.getFullStackResourceName(this.name, 'event-bucket', config.dataVersion)),
})

const walletBucket = new s3.Bucket(this, config.getFullStackResourceName(this.name, 'wallet-bucket', config.dataVersion), {
bucketName: kebabCase(config.getFullStackResourceName(this.name, 'wallet-bucket', config.dataVersion)),
})

const stakingActionBucket = new s3.Bucket(this, config.getFullStackResourceName(this.name, 'staking-action-bucket', config.dataVersion), {
bucketName: kebabCase(config.getFullStackResourceName(this.name, 'staking-action-bucket', config.dataVersion)),
})

new s3.Bucket(this, config.getFullStackResourceName(this.name, 'output-bucket'))
new s3.Bucket(this, config.getFullStackResourceName(this.name, 'output-bucket'), {
bucketName: kebabCase(config.getFullStackResourceName(this.name, 'output-bucket')),
})

/** Create Glue tables */
new glue.Table(this, config.getFullStackResourceName(this.name, 'event-table'), {
Expand All @@ -53,6 +62,7 @@ export class EtlStack extends cdk.Stack {
columns: walletColumns,
dataFormat: glue.DataFormat.JSON,
})

new glue.Table(this, config.getFullStackResourceName(this.name, 'staking-action-table'), {
database: database,
tableName: snakeCase(config.getFullStackResourceName(this.name, 'staking-action-table')),
Expand Down
31 changes: 21 additions & 10 deletions infrastructure/cdk/test/etl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cdk from 'aws-cdk-lib'
import * as assertions from 'aws-cdk-lib/assertions'
import { Config } from '../src/providers/config'
import { EtlStack } from '../src/providers/etl'
import { eventSchema, aggSchema, Schema } from '@casimir/data'
import { Schema, eventSchema, walletSchema, stakingActionSchema } from '@casimir/data'

test('ETL stack created', () => {
const config = new Config()
Expand All @@ -16,11 +16,7 @@ test('ETL stack created', () => {

const eventTable = Object.keys(resource).filter(key => key.includes('EventTable'))
const eventColumns = resource[eventTable[0]].Properties.TableInput.StorageDescriptor.Columns


/** Get Glue Columns from JSON Schema for each table */
const eventGlueSchema = new Schema(eventSchema).getGlueColumns()
const aggGlueSchema = new Schema(aggSchema).getGlueColumns()

for (const column of eventColumns) {
const { Name: name, Type: type } = column
Expand All @@ -31,13 +27,28 @@ test('ETL stack created', () => {
expect(columnName).toEqual(name)
}

const aggTable = Object.keys(resource).filter(key => key.includes('AggTable'))[0]
const aggColumns = resource[aggTable].Properties.TableInput.StorageDescriptor.Columns
const walletTable = Object.keys(resource).filter(key => key.includes('WalletTable'))[0]
const walletColumns = resource[walletTable].Properties.TableInput.StorageDescriptor.Columns
const walletGlueSchema = new Schema(walletSchema).getGlueColumns()


for (const column of walletColumns) {
const { Name: name, Type: type } = column
const columnName = Object.keys(walletSchema.properties).filter(key => key === name)[0]
const columnType = walletGlueSchema.filter(key => key.name === name)[0].type.inputString

expect(columnType).toEqual(type)
expect(columnName).toEqual(name)
}

const stakingActionTable = Object.keys(resource).filter(key => key.includes('StakingActionTable'))[0]
const stakingActionColumns = resource[stakingActionTable].Properties.TableInput.StorageDescriptor.Columns
const stakingActionGlueSchema = new Schema(stakingActionSchema).getGlueColumns()

for (const column of aggColumns) {
for (const column of stakingActionColumns) {
const { Name: name, Type: type } = column
const columnName = Object.keys(aggSchema.properties).filter(key => key === name)[0]
const columnType = aggGlueSchema.filter(key => key.name === name)[0].type.inputString
const columnName = Object.keys(stakingActionSchema.properties).filter(key => key === name)[0]
const columnType = stakingActionGlueSchema.filter(key => key.name === name)[0].type.inputString

expect(columnType).toEqual(type)
expect(columnName).toEqual(name)
Expand Down
Loading

0 comments on commit 0de5097

Please sign in to comment.