Skip to content

Commit

Permalink
Improvements to deal with timestamps and speed
Browse files Browse the repository at this point in the history
  Converts timestamps to 8601 so that ingestor is faster. Its a slight
  decrease in speed here but a huge increase in the ingest script.
  • Loading branch information
caparker committed Sep 20, 2024
1 parent 7ec511c commit 1eb5e26
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
8 changes: 8 additions & 0 deletions fetcher/lib/measure.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class Measures {
return this.measures.length;
}

json() {
return this.measures.map((m) => ({
sensor_id: m.sensor_id,
timestamp: m.timestamp.utc().format(),
measure: m.measure,
}));
}

csv() {
const csvStringifier = createCsvStringifier({
header: this.headers.map((head) => ({
Expand Down
42 changes: 30 additions & 12 deletions fetcher/providers/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const Providers = require('../lib/providers');
const { fetchFile } = require('../lib/utils');
const { Measures, FixedMeasure } = require('../lib/measure');
const { Measurand } = require('../lib/measurand');
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');

dayjs.extend(utc);

const truthy = (value) => {
return [1,true,'TRUE','T','True','t','true'].includes(value);
Expand Down Expand Up @@ -239,6 +242,7 @@ class Client {
this.manufacturer_key = source.meta.manufacturer_key || 'manufacturer_name';
this.model_key = source.meta.model_key || 'model_name';
this.datetime_key = source.meta.timestamp_key || 'datetime';
this.datetime_format = 'YYYY-MM-DD HH-mm-ss';
this.timezone = source.meta.timezone || 'UTC';
this.datasources = {};
this.missing_datasources = [];
Expand All @@ -247,6 +251,7 @@ class Client {
this.measurands = null;
this.measures = new Measures(FixedMeasure);
this.locations = {};
this.log = {}; // track errors and warnings to provide later
}

get provider() {
Expand Down Expand Up @@ -278,15 +283,15 @@ class Client {
const manufacturer = cleanKey(row[this.manufacturer_key]);
const model = cleanKey(row[this.model_key]);
const location_id = this.getLocationId(row);
let key = null;
let key = '';
if (manufacturer && model) {
key = `${manufacturer}:${model}`;
key = `-${manufacturer}:${model}`;
} else if (!manufacturer & !model) {
key = 'default';
// key = 'default';
} else {
key = `${manufacturer || model}`;
key = `-${manufacturer || model}`;
}
return `${location_id}-${key}`;
return `${location_id}${key}`;
}

/**
Expand All @@ -303,7 +308,7 @@ class Client {
if (!measurand) {
throw new Error(`Could not find measurand for ${row.metric}`);
}
let key = [measurand.parameter];
const key = [measurand.parameter];
if (instance) key.push(instance);
if (version) key.push(version);
return `${location_id}-${key.join(':')}`;
Expand Down Expand Up @@ -360,7 +365,7 @@ class Client {
* @returns {string} - formated timestamp string
*/
getDatetime (row) {
return row[this.datetime_key];
return dayjs.utc(row[this.datetime_key], this.datetime_format);
}

/**
Expand All @@ -379,6 +384,12 @@ class Client {
return fetchFile(f);
}

logMessage(type, message, err) {
// check if warning or error
// if strict than throw error, otherwise just log for later
if(!this.log[type]) this.log[type] = [];

Check failure on line 390 in fetcher/providers/generic.js

View workflow job for this annotation

GitHub Actions / build

Expected space(s) after "if"
this.log[type].push({ message, err});

Check failure on line 391 in fetcher/providers/generic.js

View workflow job for this annotation

GitHub Actions / build

A space is required before '}'
}

/**
* Entry point for processing data
Expand Down Expand Up @@ -459,7 +470,7 @@ class Client {
location.add({ sensor_id, system_id, ...d });

} catch (e) {
console.warn(`Error adding sensor: ${e.message}`);
this.logMessage(`Error adding sensor: ${e.message}`, 'error');
}
});
}
Expand Down Expand Up @@ -504,10 +515,12 @@ class Client {
timestamp: datetime,
measure: this.normalize(m),
});
} else {
this.logMessage('VALUE_NOT_FOUND', 'error');
}
});
} catch (e) {
console.warn(`Error adding measurement: ${e.message}`);
this.logMessage('MEASUREMENT_ERROR', 'error', e);
}
});
}
Expand Down Expand Up @@ -543,7 +556,7 @@ class Client {
source: this.provider,
matching_method: 'ingest-id'
},
measures: this.measures.measures,
measures: this.measures.json(),
locations: Object.values(this.locations).map((l)=>l.json())
};
}
Expand All @@ -554,12 +567,17 @@ class Client {
* @returns {object} - json summary object
*/
summary() {
const error_summary = {};
Object.keys(this.log).map((k) => error_summary[k] = this.log[k].length);
return {
source_name: this.provider,
locations: Object.values(this.locations).length,
systems: Object.values(this.locations).map((l) => Object.values(l.systems).length).flat().reduce((d,i) => d + i),
sensors: Object.values(this.locations).map((l) => Object.values(l.systems).map((s) => Object.values(s.sensors).length)).flat().reduce((d,i) => d + i),
measures: this.measures.length,
from: this.measures.from,
to: this.measures.to
errors: error_summary,
from: this.measures.from.utc().format(),
to: this.measures.to.utc().format(),
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/test_measurements_long.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
location,datetime,parameter,value
Test Site #1,2024-01-01,co,20
Test Site #1,2024-01-02,co,20
Test Site #1,2024-01-01 00:00:05 UTC,co,20
Test Site #1,2024-01-02 00:00:05 UTC,co,20

0 comments on commit 1eb5e26

Please sign in to comment.