-
Notifications
You must be signed in to change notification settings - Fork 823
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'stage' into production
- Loading branch information
Showing
12 changed files
with
342 additions
and
102 deletions.
There are no files selected for viewing
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,96 @@ | ||
const mysql = require('mysql') | ||
const path = require('path') | ||
const fs = require('fs/promises') | ||
const { getConfig } = require('../server/js/config-helper') | ||
|
||
const DOWNLOADERS_FOLDER = path.join(__dirname, 'downloaders') | ||
|
||
const { MYSQLHOST, MYSQLUSER, MYSQLPASS, MYSQLDBNAME } = getConfig() | ||
|
||
const dbConfig = { | ||
host: MYSQLHOST, | ||
user: MYSQLUSER, | ||
password: MYSQLPASS, | ||
database: MYSQLDBNAME, | ||
} | ||
|
||
const db = mysql.createConnection(dbConfig) | ||
|
||
const BATCH_SIZE = 1000 | ||
|
||
const queryDb = (query, params) => | ||
new Promise((resolve, reject) => { | ||
db.query(query, params, (err, results, fields) => { | ||
if (err) reject(err) | ||
try { | ||
const res = results[0] | ||
resolve(res) | ||
} catch { | ||
console.log(results) | ||
reject('Something went wrong') | ||
} | ||
}) | ||
}) | ||
|
||
const sleep = ms => { | ||
return new Promise(resolve => setTimeout(resolve, ms)) | ||
} | ||
|
||
const processRelease = async filepath => { | ||
const contents = await fs.readFile(path.join(DOWNLOADERS_FOLDER, filepath)) | ||
const users = JSON.parse(contents) | ||
const userEmails = users.map(entry => entry.user.email) | ||
const referrer = 'Hugging Face' | ||
const releaseDir = filepath.split('.json')[0] | ||
|
||
const referrerId = await queryDb(`SELECT id FROM referrers WHERE name = ?`, [ | ||
referrer, | ||
]) | ||
const datasetId = await queryDb( | ||
`SELECT id FROM datasets WHERE release_dir = ?`, | ||
[releaseDir] | ||
) | ||
const unknownLocaleId = await queryDb( | ||
`SELECT id FROM locales WHERE name = ?`, | ||
['unknown'] | ||
) | ||
|
||
let start = 0 | ||
let end = BATCH_SIZE | ||
let batch = userEmails.slice(start, end) | ||
|
||
while (batch.length > 0) { | ||
const values = batch.map(email => [ | ||
email, | ||
unknownLocaleId.id, | ||
datasetId.id, | ||
referrerId.id, | ||
]) | ||
|
||
console.log(values) | ||
|
||
await queryDb( | ||
` | ||
INSERT INTO downloaders (email, locale_id, dataset_id, referrer_id) | ||
VALUES ? | ||
`, | ||
[values] | ||
) | ||
|
||
start += BATCH_SIZE | ||
end += BATCH_SIZE | ||
batch = userEmails.slice(start, end) | ||
|
||
await sleep(2000) | ||
} | ||
} | ||
|
||
const main = async () => { | ||
const releases = await fs.readdir(DOWNLOADERS_FOLDER) | ||
|
||
for (const release of releases) { | ||
await processRelease(release) | ||
} | ||
} | ||
|
||
main() |
13 changes: 13 additions & 0 deletions
13
server/src/application/bulk-submissions/repository/user-client-repository.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,13 @@ | ||
import { pipe } from 'fp-ts/lib/function' | ||
import { taskEither as TE } from 'fp-ts' | ||
import { queryDb } from '../../../infrastructure/db/mysql' | ||
|
||
export const fetchUserClientEmailById = (id: string) => | ||
pipe( | ||
queryDb(` | ||
SELECT email FROM user_clients WHERE client_id = ? | ||
`)([id]), | ||
TE.map( | ||
([[result]]: Array<Array<{ email: string }> | Array<any>>) => result.email | ||
) | ||
) |
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 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 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 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
Oops, something went wrong.