-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
36a6a9d
commit cf1131f
Showing
15 changed files
with
626 additions
and
8 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
.reading { | ||
.big-number { | ||
line-height: 3.6rem; | ||
height: 4rem; | ||
.trend { | ||
vertical-align: top; | ||
line-height: 3.6rem; | ||
font-size: 1.8rem; | ||
|
||
} | ||
.value { | ||
font-size: 3.6rem; | ||
line-height: 3.6rem; | ||
font-weight: bold; | ||
} | ||
.unit { | ||
font-size: 1.8rem; | ||
line-height: 3.6rem; | ||
} | ||
} | ||
.sparkline { | ||
height: 3.6rem; | ||
width: 100%; | ||
svg { | ||
.cursor { | ||
stroke: $black; | ||
fill: none; | ||
stroke-width: 1.5; | ||
} | ||
|
||
.fill { | ||
fill: $yellow; | ||
} | ||
|
||
.stroke { | ||
stroke: $black; | ||
fill: none; | ||
stroke-width: 1.5; | ||
} | ||
} | ||
} | ||
} |
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,127 @@ | ||
import * as $ from "jquery"; | ||
import * as d3 from "d3"; | ||
import * as strftime from "strftime"; | ||
import BTree from "sorted-btree"; | ||
|
||
class Reading { | ||
constructor(element) { | ||
this.element = element; | ||
this.valueElement = $(this.element).find(".big-number .value")[0]; | ||
this.dateLabelElement = $(this.element).find(".date-line .label")[0]; | ||
this.dateElement = $(this.element).find(".date-line .date")[0]; | ||
this.deviceId = element.dataset["deviceId"]; | ||
this.sensorId = element.dataset["sensorId"]; | ||
this.fromDate = element.dataset["fromDate"] ?? this.getDateString(-24 * 60 * 60 * 1000); | ||
this.toDate = element.dataset["toDate"] ?? this.getDateString(); | ||
this.initialValue = this.valueElement.innerHTML; | ||
this.initialDateLabel = this.dateLabelElement.innerHTML; | ||
this.hoveredDateLabel = this.dateLabelElement.dataset["hoveredText"]; | ||
this.initialDate = this.dateElement.innerHTML; | ||
} | ||
|
||
getDateString(offset = 0) { | ||
return new Date(new Date() - offset).toISOString() | ||
} | ||
|
||
async init() { | ||
await this.initData(); | ||
this.initSparkline(); | ||
} | ||
|
||
async initData() { | ||
const response = await $.ajax({ | ||
url:`/devices/${this.deviceId}/readings?rollup=5m&sensor_id=${this.sensorId}&from=${this.fromDate}&to=${this.toDate}`, | ||
method: "GET", | ||
}); | ||
const timestamps = response.readings.map(x => Date.parse(x[0])); | ||
const values = response.readings.map(x => x[1]); | ||
this.dataTree = new BTree(); | ||
this.data = values.map((value, i) => { | ||
const timestamp = timestamps[i]; | ||
this.dataTree.set(timestamp, value); | ||
return { value: value, time: timestamp }; | ||
}); | ||
this.minTimestamp = Math.min(...timestamps); | ||
this.maxTimestamp = Math.max(...timestamps); | ||
this.maxValue = Math.max(...values) | ||
} | ||
|
||
initSparkline() { | ||
const sparklineElement = $(this.element).find(".sparkline")[0]; | ||
const trendElement = $(this.element).find(".trend")[0]; | ||
const STROKE_OFFSET = 2; | ||
const width = sparklineElement.offsetWidth; | ||
const height = sparklineElement.offsetHeight; | ||
const x = d3.scaleUtc() | ||
.domain([this.minTimestamp, this.maxTimestamp]) | ||
.range([0, width]); | ||
const y = d3.scaleLinear() | ||
.domain([0, this.maxValue]) | ||
.range([height - STROKE_OFFSET, 0]); | ||
const area = d3.area() | ||
.x(d => x(d.time)) | ||
.y0(height) | ||
.y1(d => y(d.value)); | ||
const line = d3.line() | ||
.x(d => x(d.time)) | ||
.y(d => y(d.value)); | ||
const svg = d3.create("svg") | ||
.attr("width", width) | ||
.attr("height", height); | ||
const g = svg.append("g") | ||
.attr("width", width) | ||
.attr("height", height - STROKE_OFFSET) | ||
.attr("transform", "translate(0, 2)"); | ||
g.append("path") | ||
.attr("class", "fill") | ||
.attr("d", area(this.data)) | ||
g.append("path") | ||
.attr("class", "stroke") | ||
.attr("d", line(this.data)); | ||
const cursor = svg.append("path") | ||
.attr("class", "cursor") | ||
.attr("visibility", "hidden") | ||
.attr("d", `M 0,0 L 0,${height} Z`) | ||
|
||
const moveHandler = (event) => { | ||
if (window.TouchEvent && event instanceof TouchEvent) event = event.touches[event.touches.length -1]; | ||
const mouseX = d3.pointer(event)[0]; | ||
const time = x.invert(mouseX).getTime(); | ||
const timestamp = this.dataTree.nextLowerKey(time); | ||
const value = this.dataTree.get(timestamp); | ||
cursor.attr("transform", `translate(${mouseX}, 0)`); | ||
this.valueElement.innerHTML = value.toFixed(2); | ||
this.dateElement.innerHTML = strftime("%B %d, %Y %H:%M", new Date(timestamp)); | ||
} | ||
|
||
const enterHandler = (event) => { | ||
cursor.attr("visibility", "visible"); | ||
trendElement.style.visibility = "hidden"; | ||
this.dateLabelElement.innerHTML = this.hoveredDateLabel; | ||
} | ||
|
||
const leaveHandler = (event) => { | ||
cursor.attr("visibility", "hidden"); | ||
this.valueElement.innerHTML = this.initialValue; | ||
trendElement.style.visibility = "visible"; | ||
this.dateLabelElement.innerHTML = this.initialDateLabel; | ||
this.dateElement.innerHTML = this.initialDate; | ||
} | ||
|
||
$(sparklineElement).on("mouseenter", enterHandler); | ||
$(sparklineElement).on("mouseleave", leaveHandler) | ||
$(sparklineElement).on("touchstart", enterHandler); | ||
$(sparklineElement).on("touchend", leaveHandler) | ||
svg.on("mousemove", moveHandler); | ||
svg.on("touchmove", moveHandler); | ||
sparklineElement.appendChild(svg.node()); | ||
} | ||
} | ||
|
||
export function setupReadings() { | ||
$(".reading").each(function(ix, element) { | ||
const reading = new Reading(element); | ||
reading.init(); | ||
}); | ||
} | ||
|
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<div class="bg-white w-100 border border-thick <%= "p-3 pb-4 pb-md-5" unless local_assigns[:no_padding] %>"> | ||
<div class="bg-white w-100 border border-thick <%= "p-3 pb-4 pb-md-5" unless local_assigns[:no_padding] %> <%= local_assigns[:class] || "" %>"> | ||
<%= yield %> | ||
</div> |
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,28 @@ | ||
<div class="reading" | ||
data-device-id="<%= component.device_id %>" | ||
data-sensor-id="<%= component.sensor_id %>" | ||
data-from-date="<%= local_assigns[:from] || component.last_reading_at - 1.day %>" | ||
data-to-date="<%= local_assigns[:to] || component.last_reading_at %>" | ||
> | ||
<%= render layout: "ui/shared/box", locals: { class: "mb-3 p-3", no_padding: true } do %> | ||
<div class="row align-items-top mb-3"> | ||
<div class="col-12 col-md-6 col-lg-4"> | ||
<h3><%= component.measurement_name %></h3> | ||
<p class="date-line"><span data-hovered-text="<%= t(:reading_hover_reading_label) %>" class="label"><%= t(:reading_last_reading_label) %></span>: <span class="date"><%= component.last_reading_at.to_s(:long) %></span></p> | ||
</div> | ||
<div class="col-12 col-md-6 col-lg-3"> | ||
<div class="big-number text-md-end text-lg-start"> | ||
<span class="trend"><% case component.trend %><% when 1 %>▴<% when -1 %>▾<% else %><strong>=</strong><% end %></span> <span class="value"><%= component.latest_value.round(2) %></span><span class="unit"><%= component.value_unit %></span> | ||
</div> | ||
</div> | ||
<div class="col-12 col-lg-5"> | ||
<div class="sparkline"></div> | ||
</div> | ||
</div> | ||
<div class="row"> | ||
<div class="col-12"> | ||
<p><i><%= component.measurement_description %></i></p> | ||
</div> | ||
</div> | ||
<% end %> | ||
</div> |
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 |
---|---|---|
|
@@ -76,7 +76,7 @@ module.exports = function(api) { | |
{ | ||
async: false | ||
} | ||
] | ||
], | ||
].filter(Boolean) | ||
} | ||
} |
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,3 @@ | ||
en: | ||
reading_last_reading_label: Last reading at | ||
reading_hover_reading_label: Reading at |
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
Oops, something went wrong.