-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
swap from postgres to elastic for analytics
- Loading branch information
Showing
7 changed files
with
179 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/database/migrations/exec/1644249527-remove_analytics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import driver from '../../driver' | ||
|
||
export async function up (db: typeof driver): Promise<void> { | ||
await db`DROP TABLE IF EXISTS analytics;` | ||
} | ||
|
||
export async function down (db: typeof driver): Promise<void> { | ||
await db` | ||
CREATE TABLE IF NOT EXISTS analytics ( | ||
id UUID NOT NULL PRIMARY KEY, | ||
timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(), | ||
type INTEGER NOT NULL, | ||
guild_id BIGINT, | ||
user_id BIGINT NOT NULL, | ||
data JSONB NOT NULL, | ||
name TEXT NOT NULL | ||
); | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ScheduledJob } from './base' | ||
import { flush } from '../utils/elastic' | ||
|
||
const job = new ScheduledJob('analytics-prune') | ||
// every hour | ||
.setInterval(60 * 60 * 1000) | ||
.setExec(async () => { | ||
await flush() | ||
}) | ||
|
||
if (process.env.ELASTIC_URL !== undefined) job.start() | ||
|
||
export default job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Client } from '@elastic/elasticsearch' | ||
import { BulkStats } from '@elastic/elasticsearch/lib/Helpers' | ||
import { format } from 'date-fns' | ||
import discord from '../structures/client' | ||
import { debug, trace, warn } from './logger' | ||
|
||
const storage = new Set() | ||
|
||
export function add (data: any): number { | ||
data['@timestamp'] = new Date().toISOString() | ||
storage.add(data) | ||
return storage.size | ||
} | ||
|
||
export function drop (): number { | ||
storage.clear() | ||
return storage.size | ||
} | ||
|
||
export async function * drain (): AsyncGenerator<any> { | ||
for (const data of storage) { | ||
storage.delete(data) | ||
yield data | ||
} | ||
} | ||
|
||
export async function flush (): Promise<BulkStats> { | ||
if (process.env.ELASTIC_URL === undefined) throw new Error('Trying to flush to Elasticsearch, but no URL is set.') | ||
debug('Flushing elasticsearch', 'Elastic') | ||
const client = new Client({ | ||
node: process.env.ELASTIC_URL, | ||
// auth can be either be a b64 encoded api key, or basic auth incorporated into the URL | ||
...(process.env.ELASTIC_API_KEY !== undefined | ||
? { | ||
auth: { | ||
apiKey: process.env.ELASTIC_API_KEY | ||
} | ||
} | ||
: {}) | ||
}) | ||
const result = await client.helpers.bulk({ | ||
datasource: drain(), | ||
onDocument (doc) { | ||
return { | ||
create: { | ||
_index: `wildbeast-${discord.client.applicationId}-${format(new Date(), 'yyyy-MM-dd')}` | ||
} | ||
} | ||
} | ||
}) | ||
debug(`Flushed ${result.successful} analytics documents to Elastic, ${result.failed} failed`, 'Elastic') | ||
trace(result, 'Elastic') | ||
if (result.failed > 0) { | ||
warn(`Failed to flush ${result.failed} analytics documents to Elastic`, 'Elastic') | ||
} | ||
// Persistent connection to elastic isn't necessary, close it | ||
// we recreate the client on the next flush anyway | ||
await client.close() | ||
return result | ||
} |