Skip to content

Commit

Permalink
Linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
caparker committed Jun 18, 2024
1 parent 752811e commit 46b34fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
2 changes: 1 addition & 1 deletion fetcher/lib/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Providers {
* Given a measures object, save it to s3
*
* @param {string} provider The name of the provider (ie purpleair)
* @param {Measures} measures A measurements object of measures
* @param {Measures} data A object with measures
* @param {string} id An optional identifier to use when creating filename
*/
static async put_measures_json(provider, data, id) {
Expand Down
4 changes: 2 additions & 2 deletions fetcher/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ async function putObject(text, Bucket, Key, gzip = true, ContentType = 'applicat

/**
*
* @param {} text
* @param {} key
* @param {string} text the string to be saved to a file
* @param {string} key the the file path. Usually the same key that would be used in the cloud storage
*/
async function putFile(text, key) {
const fpath = path.join(homedir, `Downloads/${key}`);
Expand Down
60 changes: 20 additions & 40 deletions fetcher/providers/clarity.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,10 @@


const Providers = require('../lib/providers');
const { VERBOSE, request } = require('../lib/utils');
const { request } = require('../lib/utils');
const { Measures, FixedMeasure } = require('../lib/measure');
const { Measurand } = require('../lib/measurand');

const lookup = {
relHumid: ['relativehumidity', '%'], // RelativeHumidity
temperature: ['temperature', 'c'], // Temperature
pm2_5ConcMass: ['pm25', 'μg/m3'], // PM2.5 mass concentration
pm1ConcMass: ['pm1', 'μg/m3'], // PM1 mass concentration
pm10ConcMass: ['pm10', 'μg/m3'], // PM10 mass concentration
no2Conc: ['no2', 'ppb'], // NO2 volume concentration
windSpeed: ['windspeed', 'm/s'], // Wind speed
windDirection: ['winddirection', 'degrees'] // Wind direction, compass degrees (0°=North, then clockwise)
};


class ClarityApi {
/**
Expand All @@ -36,7 +25,7 @@ class ClarityApi {
this.datasources = {};
this.missing_datasources = [];
this.parameters = {
pm2_5ConcMassIndividual: [ 'pm25', 'ug/m3' ],
pm2_5ConcMassIndividual: ['pm25', 'ug/m3']
};
// holder for the locations
this.measures = new Measures(FixedMeasure);
Expand All @@ -52,15 +41,15 @@ class ClarityApi {
}

get baseUrl() {
return "https://clarity-data-api.clarity.io";
return 'https://clarity-data-api.clarity.io';
}

async fetchMeasurands() {
this.measurands = await Measurand.getIndexedSupportedMeasurands(this.parameters);
}

addToMissingDatasources(ds) {
if(!this.missing_datasources.includes(ds.datasourceId)) {
if (!this.missing_datasources.includes(ds.datasourceId)) {
console.warn('Adding to missing datasources', ds);
this.missing_datasources.push(ds.datasourceId);
}
Expand All @@ -69,11 +58,10 @@ class ClarityApi {

/**
* Fetch the list of datasources and convert to object for reference later
* @returns {}
* @returns {array} a list of datasources
*/
async fetchDatasources() {
//const url = "https://openmap.clarity.io/api/measurements/current?reference=true&populate=device&withCharacteristics=pm2_5ConcMass";
const url = "https://clarity-data-api.clarity.io/v1/open/datasources";
const url = 'https://clarity-data-api.clarity.io/v1/open/datasources';
const response = await request({
url,
json: true,
Expand All @@ -87,7 +75,7 @@ class ClarityApi {
// const arr = response.body.filter(d => d.dataOrigin!='Reference Site');
// reshape to make it easier to use
console.debug(`Found ${Object.keys(response.body.datasources).length} datasources`);
this.datasources = response.body.datasources; //Object.assign({}, ...arr.map(item => ({[`${item.datasourceId}`]: item})));
this.datasources = response.body.datasources; // Object.assign({}, ...arr.map(item => ({[`${item.datasourceId}`]: item})));
return this.datasources;
}

Expand All @@ -99,32 +87,24 @@ class ClarityApi {
*/
getSensorId(meas) {
const measurand = this.measurands[meas.metric];
if(!measurand) {
if (!measurand) {
throw new Error(`Could not find measurand for ${meas.metric}`);
}
return `clarity-${meas.datasourceId}-${measurand.parameter}`;
}

getLocationId(loc) {
//const datasource = this.datasources[loc.datasourceId];
//if(!datasource) {
// this.addToMissingDatasources(loc);
// throw new Error(`Could not find datasource for ${loc.datasourceId}`);
//}
//if(!datasource.deviceId) {
// throw new Error(`Missing device id`);
//}
return `clarity-${loc.datasourceId}`;
}

getLabel(loc) {
const datasource = this.datasources[loc.datasourceId];
if(!datasource) {
if (!datasource) {
this.addToMissingDatasources(loc.datasourceId);
throw new Error(`Could not find datasource for ${loc.datasourceId}`);
}
// still return a label even if we are missing one
return !!datasource.name ? datasource.name : 'Missing device name';
return datasource.name ? datasource.name : 'Missing device name';
}

normalize(meas) {
Expand All @@ -133,7 +113,7 @@ class ClarityApi {
}

async fetchData() {
const dsurl = "/v1/open/all-recent-measurement/pm25/individual";
const dsurl = '/v1/open/all-recent-measurement/pm25/individual';
const url = `${this.baseUrl}${dsurl}`;

await this.fetchMeasurands();
Expand All @@ -156,22 +136,22 @@ class ClarityApi {
console.debug(`Found ${measurements.length} measurements for ${datasources.length} datasources`);

// translate the dataources to locations
datasources.map(d => {
datasources.map((d) => {
try {
this.locations.push({
location: this.getLocationId(d),
label: this.getLabel(d),
ismobile: false,
lon: d.lon,
lat: d.lat,
lat: d.lat
});
} catch (e) {
console.warn(`Error adding location: ${e.message}`);
}
});


measurements.map( m => {
measurements.map( (m) => {
// really seems like the measures.push method
// should handle the sensor/ingest id
// and the normalizing??
Expand All @@ -182,20 +162,20 @@ class ClarityApi {
timestamp: m.time,
flags: { 'clarity/qc': m.qc }
});
} catch(e) {
} catch (e) {
// console.warn(`Error adding measurement: ${e.message}`);
}
});

if(this.missing_datasources.length) {
if (this.missing_datasources.length) {
console.warn(`Could not find details for ${this.missing_datasources.length} datasources`, this.missing_datasources);
}

this.fetched = true;
}

data() {
if(!this.fetched) {
if (!this.fetched) {
console.warn('Data has not been fetched');
}
return {
Expand All @@ -205,16 +185,16 @@ class ClarityApi {
matching_method: 'ingest-id'
},
measures: this.measures.measures,
locations: this.locations,
locations: this.locations
};
}

summary() {
if(!this.fetched) {
if (!this.fetched) {
console.warn('Data has not been fetched');
return {
source_name: this.source.provider,
message: 'Data has not been fetched',
message: 'Data has not been fetched'
};
} else {
return {
Expand Down

0 comments on commit 46b34fd

Please sign in to comment.