Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Better error handling for Airnotes with no recent data #40

Merged
merged 3 commits into from May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public -s",
"start": "sirv public --single --port 5555",
"test": "jest",
"test:watch": "npm run test -- --watch"
},
Expand Down
127 changes: 66 additions & 61 deletions server/server.js
@@ -1,108 +1,113 @@
'use strict';
const Hapi = require('@hapi/hapi');
const axios = require('axios');
"use strict";
const Hapi = require("@hapi/hapi");
const axios = require("axios");

const server = Hapi.server({
port: 3000,
host: '0.0.0.0', // needed for Render deployment
host: "0.0.0.0", // needed for Render deployment
});

const buildBody = (device_uid, to, from) => {
return {
"size": 500,
"sort": [
size: 500,
sort: [
{
"when_captured": {
"order": "desc"
}
}
when_captured: {
order: "desc",
},
},
],
"query": {
"bool": {
"must": [],
"filter": [
query: {
bool: {
must: [],
filter: [
{
"bool": {
"should": [
bool: {
should: [
{
"match_phrase": {
"device_urn": "note:" + device_uid
}
}
]
}
match_phrase: {
device_urn: "note:" + device_uid,
},
},
],
},
},
{
"range": {
"service_uploaded": {
"format": "strict_date_optional_time",
"gte": from,
"lte": to
}
}
}
]
}
}
range: {
service_uploaded: {
format: "strict_date_optional_time",
gte: from,
lte: to,
},
},
},
],
},
},
};
}
};

const url = 'https://40ad140d461d810ac41ed710b5c7a5b6.us-west-2.aws.found.io:9243/_search';
const url =
"https://40ad140d461d810ac41ed710b5c7a5b6.us-west-2.aws.found.io:9243/_search";
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + new Buffer(process.env.SAFECAST_USERNAME + ':' + process.env.SAFECAST_PASSWORD).toString('base64')
"Content-Type": "application/json",
Authorization:
"Basic " +
new Buffer(
process.env.SAFECAST_USERNAME + ":" + process.env.SAFECAST_PASSWORD
).toString("base64"),
};

const init = async () => {
await server.register([
{
plugin: require('@hapi/inert'),
options: {}
plugin: require("@hapi/inert"),
options: {},
},
{
plugin: require('hapi-pino'),
plugin: require("hapi-pino"),
options: {
prettyPrint: true,
logEvents: ['response', 'onPostStart']
}
}]);
logEvents: ["response", "onPostStart"],
},
},
]);

server.route({
method: 'GET',
path: '/',
method: "GET",
path: "/",
options: {
cors: {
origin: [
'http://localhost:5000',
'https://airnote.live'
]
origin: ["http://localhost:5555", "https://airnote.live"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This port is all that changed. The rest is Prettier auto formatting.

},
handler: async (request, h) => {
const device_uid = request.query.device_uid;
const to = request.query.to;
const from = request.query.from;

try {
const response = await axios.post(url, buildBody(device_uid, to, from), {
headers: headers,
});
return h.response(response.data)
.type('application/json')
.code(200);
} catch(err) {
const response = await axios.post(
url,
buildBody(device_uid, to, from),
{
headers: headers,
}
);
return h.response(response.data).type("application/json").code(200);
} catch (err) {
return h.response().code(500);
}
}
}
},
},
});

await server.start();
console.log(`Server running at: ${server.info.uri}`);
};

process.on('unhandledRejection', (err) => {
process.on("unhandledRejection", (err) => {
console.log(err);
process.exit(1);
});

init();
init();
24 changes: 19 additions & 5 deletions src/routes/Dashboard/Dashboard.svelte
Expand Up @@ -40,6 +40,10 @@
localStorage.setItem('tempDisplay', tempDisplay);
}

const getSafecastDashboardLink = (deviceUID) => (
`http://tt.safecast.org/dashboard/note:${deviceUID}`
);

const downloadData = () => {
const csv = 'data:text/csv;charset=utf-8,' +
unparse(readings);
Expand Down Expand Up @@ -105,10 +109,20 @@
{#if noDataError}
<div class="alert">
<h4 class="alert-heading">{NO_DATA_ERROR_HEADING}</h4>
There is no data associated with this Airnote. If this is a new Airnote,
it may take several hours for your device to report its first readings.
For help setting up your Airnote, visit
<a href='https://start.airnote.live'>start.airnote.live</a>.
<p>
This Airnote has not reported data in the last seven days. If this is a new
Airnote, it may take several hours for your device to report its first
readings. For help setting up your Airnote, visit
<a href='https://start.airnote.live'>start.airnote.live</a>.
</p>

<p>
If this is a device that has previously reported readings, you can view
historical data on
<a href={getSafecastDashboardLink(deviceUID)}>this device’s Safecast dashboard</a>,
and <a href="https://discuss.blues.io">reach out on our forum</a> if you need
help getting your Airnote back up and running.
</p>
</div>
{/if}

Expand Down Expand Up @@ -279,7 +293,7 @@
</div>

<div class="full-data-link">
<a in:fade href="http://tt.safecast.org/dashboard/note:{deviceUID}" class="svg-link" target="_blank">
<a in:fade href="{getSafecastDashboardLink(deviceUID)}" class="svg-link" target="_blank">
<span>View full data</span>
<ExternalLinkIcon />
</a>
Expand Down