forked from mozilla/firefox-health-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mozilla#44 from mozilla/nimbledroid_data_fetching
Fetch Nimbledroid data outside of endpoint
- Loading branch information
Showing
10 changed files
with
137 additions
and
22 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import asyncRedis from 'async-redis'; | ||
import NimbledroidClient from '../utils/NimbledroidClient'; | ||
|
||
if ( | ||
!process.env.REDIS_URL || | ||
!process.env.NIMBLEDROID_API_KEY || | ||
!process.env.NIMBLEDROID_EMAIL | ||
) { | ||
throw Error('You need to set NIMBLEDROID_EMAIL, NIMBLEDROID_API_KEY and REDIS_URL'); | ||
} | ||
|
||
const nimbledroidClient = new NimbledroidClient( | ||
process.env.NIMBLEDROID_EMAIL, | ||
process.env.NIMBLEDROID_API_KEY, | ||
); | ||
const redisClient = asyncRedis.createClient(process.env.REDIS_URL); | ||
|
||
redisClient.on('error', (err) => { | ||
console.error(err); | ||
}); | ||
|
||
// eslint-disable-next-line consistent-return | ||
const storeProfilingRunIfMissing = async (profilingRunData) => { | ||
const KNOWN_STATUS = ['Crawling', 'Failed', 'Profiling', 'Profiled']; | ||
const { status, url } = profilingRunData; | ||
if (!KNOWN_STATUS.includes(status)) { | ||
throw Error(`Status: ${status} is new to us; Handle it in the code.`); | ||
} | ||
|
||
// e.g. cache:https://nimbledroid.com/api/v2/users/npark@mozilla.com/apps/org.mozilla.klar/apks/103 | ||
const key = `cache:${url}`; | ||
// The status 'Failed' means 'completed' in the Nimbledroid API | ||
if (status === 'Failed') { | ||
const cached = await redisClient.get(key); | ||
if (!cached) { | ||
console.log(`Storing ${key}`); | ||
await redisClient.set(key, JSON.stringify(profilingRunData)); | ||
} | ||
} | ||
}; | ||
|
||
const storeDataInRedis = async (data) => { | ||
await Promise.all(Object.keys(data).map(index => | ||
storeProfilingRunIfMissing(data[index]))); | ||
}; | ||
|
||
const fetchData = async productName => | ||
nimbledroidClient.getNimbledroidData(productName); | ||
|
||
const main = async () => { | ||
console.log('Fetching each product can take between 20-40 seconds.'); | ||
try { | ||
await Promise.all(['klar', 'focus'].map(async (productName) => { | ||
console.log(`Fetching ${productName}`); | ||
const productData = await fetchData(productName); | ||
await storeDataInRedis(productData); | ||
})); | ||
} catch (e) { | ||
console.error(e); | ||
} finally { | ||
process.exit(); | ||
} | ||
}; | ||
|
||
main(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* This module produces the data needed for the api/android/nimbledroid endpoint | ||
* The data can either be in Redis or in-memory | ||
*/ | ||
import asyncRedis from 'async-redis'; | ||
|
||
const client = asyncRedis.createClient(process.env.REDIS_URL); | ||
|
||
const queryNimbledroidData = async (product) => { | ||
const cachedKeys = await client.keys(`cache:*nimble*${product}/apks/*`); | ||
const data = await Promise.all(cachedKeys.map(async key => | ||
JSON.parse(await client.get(key)))); | ||
if (data.length === 0) { | ||
throw Error('The script that fetches data should have run before hitting the API.'); | ||
} | ||
return data; | ||
}; | ||
|
||
export default queryNimbledroidData; |
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