Skip to content

Commit

Permalink
Some bug fixes and adding date to key path
Browse files Browse the repository at this point in the history
  • Loading branch information
caparker committed May 30, 2024
1 parent ad3e336 commit 58c1efd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
6 changes: 4 additions & 2 deletions fetcher/lib/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
fetchSecret,
getObject,
putObject,
putFile,
prettyPrintStation
} = require('./utils');

Expand Down Expand Up @@ -149,6 +150,7 @@ class Providers {

if (DRYRUN) {
console.log(`Would have saved ${measures.length} measurements to '${Bucket}/${Key}'`);
putFile(compressedString, Key);
return new Promise((y) => y(true));
}
if (VERBOSE) console.debug(`Saving measurements to ${Bucket}/${Key}`);
Expand All @@ -170,13 +172,13 @@ class Providers {
const Bucket = process.env.BUCKET;
const today = dayjs().format('YYYY-MM-DD');
const filename = id || `${Math.floor(Date.now() / 1000)}-${Math.random().toString(36).substring(8)}`;
//const Key = `${process.env.STACK}/measures/${provider}/${filename}.json.gz`;
const Key = `local-testing/measures/${provider}/${today}/${filename}.json.gz`;
const Key = `${process.env.STACK}/measures/${provider}/${today}/${filename}.json.gz`;
const compressedString = await gzip(JSON.stringify(data));


if (DRYRUN) {
console.log(`Would have saved ${data.measures.length} measurements and ${data.locations.length} stations to '${Bucket}/${Key}'`);
putFile(compressedString, Key);
return new Promise((y) => y(true));
}
if (VERBOSE) console.debug(`Saving measurements to ${Bucket}/${Key}`);
Expand Down
17 changes: 17 additions & 0 deletions fetcher/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const zlib = require('zlib');
const { promisify } = require('util');
const request = promisify(require('request'));
const fs = require('node:fs');
const path = require('path');

const { SecretsManagerClient, GetSecretValueCommand } = require('@aws-sdk/client-secrets-manager');
const { S3Client, GetObjectCommand, PutObjectCommand } = require('@aws-sdk/client-s3');
Expand Down Expand Up @@ -63,6 +65,20 @@ async function putObject(text, Bucket, Key, gzip = true, ContentType = 'applicat
return await s3.send(cmd);
}


/**

Check warning on line 69 in fetcher/lib/utils.js

View workflow job for this annotation

GitHub Actions / build

JSDoc syntax error
*
* @param {} text
* @param {} key
*/
async function putFile(text, key) {
let fpath = `/home/christian/Downloads/${key}`;

Check failure on line 75 in fetcher/lib/utils.js

View workflow job for this annotation

GitHub Actions / build

'fpath' is never reassigned. Use 'const' instead
await fs.mkdirSync(path.dirname(fpath), { recursive: true });
await fs.writeFileSync(fpath, text);
}



/**
* Retrieve secret from AWS Secrets Manager
* @param {string} source The source object for which we are fetching a secret.
Expand Down Expand Up @@ -199,6 +215,7 @@ module.exports = {
DRYRUN,
getObject,
putObject,
putFile,
prettyPrintStation,
checkResponseData
};
10 changes: 3 additions & 7 deletions fetcher/providers/clarity.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,10 @@ class ClarityApi {
*/
getSensorId(meas) {
const measurand = this.measurands[meas.metric];
const datasource = this.datasources[meas.datasourceId];
if(!measurand) {
throw new Error(`Could not find measurand for ${meas.metric}`);
}
if(!datasource) {
this.addToMissingDatasources(meas);
throw new Error(`Could not find datasource for ${meas.datasourceId}/${meas.metric}`);
}
return `clarity-${datasource.datasourceId}-${measurand.parameter}`;
return `clarity-${meas.datasourceId}-${measurand.parameter}`;
}

getLocationId(loc) {
Expand Down Expand Up @@ -167,6 +162,7 @@ class ClarityApi {
this.locations.push({
location: this.getLocationId(d),
label: this.getLabel(d),
ismobile: false,
lon: d.lon,
lat: d.lat,
});
Expand Down Expand Up @@ -207,7 +203,7 @@ class ClarityApi {
meta: {
schema: 'v0.1',
source: 'clarity',
matching_method: 'source-spatial'
matching_method: 'ingest-id'
},
measures: this.measures.measures,
locations: this.locations,
Expand Down

0 comments on commit 58c1efd

Please sign in to comment.