-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
166 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const arsenal = require('arsenal'); | ||
const bucketclient = require('bucketclient'); | ||
|
||
const { BucketClientInterface } = arsenal.storage.metadata.bucketclient; | ||
const { splitter } = arsenal.constants; | ||
|
||
const rootLogger = require('./log'); | ||
const env = require('./env'); | ||
const utils = require('./utils'); | ||
|
||
const params = { | ||
bucketdBootstrap: [env.scubaBucketd], | ||
https: env.scubaBucketdTls ? env.tls.certs : undefined, | ||
}; | ||
|
||
const metadata = new BucketClientInterface(params, bucketclient, rootLogger); | ||
|
||
const listObjects = utils.retryable(metadata.listObject.bind(metadata)); | ||
|
||
|
||
/** | ||
* Left-pad a string representation of a value with a given template. | ||
* For example: pad('foo', '00000') gives '00foo'. | ||
* | ||
* @param {any} value - value to pad | ||
* @param {string} template - padding template | ||
* @returns {string} - padded string | ||
*/ | ||
function padLeft(value, template) { | ||
return `${template}${value}`.slice(-template.length); | ||
} | ||
|
||
function roundToDay(timestamp) { | ||
return new Date( | ||
Date.UTC(timestamp.getUTCFullYear(), timestamp.getUTCMonth(), timestamp.getUTCDate(), 23, 59, 59, 999), | ||
); | ||
} | ||
|
||
const LENGTH_TS = 14; | ||
const MAX_TS = 10 ** LENGTH_TS - 1; // good until 16 Nov 5138 | ||
const TEMPLATE_TS = new Array(LENGTH_TS + 1).join('0'); | ||
|
||
function formatMetricsKey(resourceName, timestamp) { | ||
const ts = padLeft(MAX_TS - roundToDay(timestamp).getTime(), TEMPLATE_TS); | ||
return `${resourceName}/${ts}`; | ||
|
||
} | ||
|
||
async function getMetrics(classType, resourceName, logId, timestamp, log) { | ||
const listingParams = { | ||
maxKeys: 1, | ||
listingType: 'Basic', | ||
gte: formatMetricsKey(resourceName, timestamp), | ||
lte: `${resourceName}/${padLeft(MAX_TS, TEMPLATE_TS)}`, | ||
}; | ||
|
||
const bucket = `${classType}${splitter}${logId}`; | ||
console.dir({ | ||
listingParams, | ||
bucket, | ||
}, { depth: null }); | ||
|
||
try { | ||
const resp = await listObjects(bucket, listingParams, log); | ||
if (resp.length === 0) { | ||
return null; | ||
} | ||
|
||
const { key, value } = resp[0]; | ||
return { | ||
key, | ||
value: JSON.parse(value), | ||
}; | ||
} catch (error) { | ||
if (error.NoSuchBucket) { | ||
return null; | ||
} | ||
log.error('Error during listing', { error }); | ||
throw error; | ||
} | ||
} | ||
|
||
module.exports = { | ||
getMetrics, | ||
}; |
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