This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add country column to identity table (#2083)
* Add country column to identity table * tweak comments * Fix unit tests * linter * prettier * Make call to handleEvent synchronous
- Loading branch information
Franck
authored
Apr 23, 2019
1 parent
bf2900f
commit 8d5a2d6
Showing
7 changed files
with
141 additions
and
10 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
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
71 changes: 71 additions & 0 deletions
71
infra/growth/src/scripts/oneoff/backfillIdentityCountry.js
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,71 @@ | ||
// One-off script to backfill the country column of the identity table. | ||
// | ||
// Note: logically this script belongs more to the identity package | ||
// rather than growth package. But identity does not have all the dependencies | ||
// required (e.g. bridge, growth)... Since this is a one-off script that will | ||
// get deleted after it gets run in production, we made the decision | ||
// to put it in the growth package. | ||
|
||
const _identityModels = require('@origin/identity/src/models') | ||
const _bridgeModels = require('@origin/bridge/src/models') | ||
const db = { ..._identityModels, ..._bridgeModels } | ||
const { ip2geo } = require('../../util/ip2geo') | ||
const parseArgv = require('../../util/args') | ||
|
||
const Logger = require('logplease') | ||
Logger.setLogLevel(process.env.LOG_LEVEL || 'INFO') | ||
const logger = Logger.create('backfill', { showTimestamp: false }) | ||
|
||
async function main(dryRun) { | ||
// Load all identity rows. | ||
// It's a small amount of rows so ok to load them all up in memory. | ||
const identities = await db.Identity.findAll() | ||
logger.info(`Loaded ${identities.length} rows from identity table.`) | ||
|
||
for (const identity of identities) { | ||
if (!identity.country) { | ||
// Get the IP from the most recent attestation | ||
const attestation = await db.Attestation.findOne({ | ||
where: { ethAddress: identity.ethAddress }, | ||
order: [['createdAt', 'DESC']] | ||
}) | ||
if (!attestation) { | ||
logger.info(`No attestation data for identity ${identity.ethAddress}`) | ||
continue | ||
} | ||
const ip = attestation.remoteIpAddress | ||
|
||
// Get the country by doing a geo lookup. | ||
const geo = await ip2geo(ip) | ||
if (!geo) { | ||
logger.info( | ||
`IP lookup failed for identity ${identity.ethAddress} ip=${ip}` | ||
) | ||
continue | ||
} | ||
const country = geo.countryCode | ||
|
||
if (dryRun) { | ||
logger.info( | ||
`Would update identity row with ethAddress ${ | ||
identity.ethAddress | ||
} country: ${country}` | ||
) | ||
} else { | ||
identity.update({ country }) | ||
logger.info( | ||
`Updated identity row with ethAddress ${ | ||
identity.ethAddress | ||
} country: ${country}` | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
const args = parseArgv() | ||
const dryRun = args['--dryRun'] === 'false' ? false : true | ||
|
||
logger.info('Starting backfill...') | ||
logger.info('DryRun mode=', dryRun) | ||
main(dryRun).then(() => logger.info('Done')) |
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 @@ | ||
'use strict' | ||
|
||
const tableName='identity' | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.addColumn( | ||
tableName, | ||
'country', | ||
Sequelize.CHAR(2) | ||
) | ||
}, | ||
down: (queryInterface) => { | ||
return queryInterface.removeColumn( | ||
tableName, | ||
'country' | ||
) | ||
} | ||
} |
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