Skip to content

Commit

Permalink
auto cleanup db
Browse files Browse the repository at this point in the history
  • Loading branch information
wslyvh committed Oct 26, 2023
1 parent fafe4ca commit 8798974
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/services/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ export async function Index(network: NETWORKS = 'mainnet') {
return
}

export async function Cleanup(network: NETWORKS = 'mainnet') {
console.log(`[${network}] Start cleanup..`)

const db = CreateDbClient()
try {
// delete all records where created_at is older than 30 days
const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString()
console.log(`[${network}] Delete records older than ${since}`)
const { data, error } = await db.from(network).delete().lte('created_at', since)

if (error) {
console.error('Error:', error)
throw new Error(error.message)
}

console.log('Data deleted!')
return data
} catch (error) {
console.error('Error:', error)
}
}

export async function GetGasData(network: NETWORKS = 'mainnet') {
console.log('Get GasData', network)
const daily = await GetAverage('day', 1, network)
Expand Down
4 changes: 3 additions & 1 deletion src/tools/gas-indexer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as dotenv from 'dotenv'
import { Index } from 'services/indexer'
import { Cleanup, Index } from 'services/indexer'

dotenv.config()

Expand All @@ -10,5 +10,7 @@ async function run() {

await Promise.all([Index('mainnet'), Index('polygon'), Index('optimism'), Index('arbitrum')])

await Promise.all([Cleanup('polygon'), Cleanup('optimism'), Cleanup('arbitrum')])

console.log('All done!')
}

1 comment on commit 8798974

@vercel
Copy link

@vercel vercel bot commented on 8798974 Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

useweb3 – ./

useweb3.xyz
www.useweb3.xyz
useweb3-git-main-useweb3.vercel.app
useweb3-useweb3.vercel.app

Please sign in to comment.