|
1 | 1 | import { createKeyspace, createTables } from './migrations.js'
|
2 | 2 | import { Client } from 'cassandra-driver'
|
3 | 3 | import { debug, error } from '../loggingEngine/logging.js'
|
4 |
| -import { |
5 |
| - getCrown, |
6 |
| - upsertArtistScrobble, |
7 |
| - getUserCrowns, |
8 |
| - tryGetToCrown, |
9 |
| - addUserToGroupList, |
10 |
| - getCountPastCrownHolders, linkArtistNameToMbid, getArtistNameFromMbid |
11 |
| -} from './operations.js' |
12 | 4 |
|
13 |
| -const client = new Client({ |
| 5 | +export const client = new Client({ |
14 | 6 | contactPoints: [process.env.CASSANDRA_HOST || 'localhost'],
|
15 | 7 | localDataCenter: 'datacenter1'
|
16 | 8 | })
|
17 | 9 |
|
18 |
| -class GraphEngine { |
19 |
| - hasStarted: boolean = false |
20 |
| - client: Client | undefined = undefined |
21 |
| - |
22 |
| - upsertScrobbles (fmUsername: string, artistMbid: string, playCount: number) { |
23 |
| - if (!this.hasStarted) { |
24 |
| - throw new Error('GraphEngine has not started') |
25 |
| - } |
26 |
| - debug('graphEngine.upsertScrobbles', `upserting scrobbles for ${fmUsername} on ${artistMbid} with playcount ${playCount}`) |
27 |
| - return upsertArtistScrobble(this.client!, fmUsername, artistMbid, playCount) |
28 |
| - } |
29 |
| - |
30 |
| - getCrownOnGroup (groupId: string, artistMbid: string) { |
31 |
| - if (!this.hasStarted) { |
32 |
| - throw new Error('GraphEngine has not started') |
33 |
| - } |
34 |
| - return getCrown(this.client!, groupId, artistMbid) |
35 |
| - } |
36 |
| - |
37 |
| - getUserCrowns (groupId: string, fmUsername: string) { |
38 |
| - if (!this.hasStarted) { |
39 |
| - throw new Error('GraphEngine has not started') |
40 |
| - } |
41 |
| - return getUserCrowns(this.client!, groupId, fmUsername) |
42 |
| - } |
43 |
| - |
44 |
| - tryToStealCrown (groupId: string, artistMbid: string, fmUsername: string, artistName: string, playCount: number = 0) { |
45 |
| - if (!this.hasStarted) { |
46 |
| - throw new Error('GraphEngine has not started') |
47 |
| - } |
48 |
| - return tryGetToCrown(this.client!, groupId, artistMbid, fmUsername, artistName, playCount) |
49 |
| - } |
50 |
| - |
51 |
| - addMemberToGroupList (groupId: string, fmUsername: string) { |
52 |
| - if (!this.hasStarted) { |
53 |
| - throw new Error('GraphEngine has not started') |
54 |
| - } |
55 |
| - return addUserToGroupList(this.client!, groupId, fmUsername).then(() => debug('graphEngine.addMemberToGroupList', `added ${fmUsername} to group ${groupId}`)) |
56 |
| - } |
57 |
| - |
58 |
| - setClient (client: Client) { |
59 |
| - this.client = client |
60 |
| - this.hasStarted = true |
61 |
| - } |
62 |
| - |
63 |
| - getPastCrownHoldersCount (groupId: string, artistMbid: string) { |
64 |
| - if (!this.hasStarted) { |
65 |
| - throw new Error('GraphEngine has not started') |
66 |
| - } |
67 |
| - return getCountPastCrownHolders(this.client!, groupId, artistMbid) |
68 |
| - } |
69 |
| - |
70 |
| - linkArtistNameToMbid (artistName: string, artistMbid: string | undefined) { |
71 |
| - if (!this.hasStarted) { |
72 |
| - throw new Error('GraphEngine has not started') |
73 |
| - } |
74 |
| - return linkArtistNameToMbid(this.client!, artistName, artistMbid) |
75 |
| - } |
76 |
| - |
77 |
| - getArtistNameByMbid (artistMbid: string) { |
78 |
| - if (!this.hasStarted) { |
79 |
| - throw new Error('GraphEngine has not started') |
80 |
| - } |
81 |
| - |
82 |
| - return getArtistNameFromMbid(this.client!, artistMbid) |
83 |
| - } |
84 |
| -} |
85 |
| - |
86 |
| -export const graphEngine = new GraphEngine() |
87 |
| - |
88 | 10 | export const start = async () => {
|
89 | 11 | await client.connect().then(() => debug('graphEngine.main', 'connected to database'))
|
90 | 12 | await createKeyspace(client).then(() => debug('graphEngine.main', 'keyspace created')).catch((e) => error('graphEngine.main', e.stack))
|
91 | 13 | client.keyspace = 'lastgram'
|
92 | 14 |
|
93 | 15 | await createTables(client).then(() => debug('graphEngine.main', 'tables created')).catch((e) => error('graphEngine.main', e.stack))
|
94 |
| - graphEngine.setClient(client) |
95 | 16 | }
|
96 | 17 |
|
0 commit comments