get data at dashboard page
This commit is contained in:
parent
ea231d12da
commit
53194fc66c
|
@ -2,26 +2,58 @@ import { createSignal, onMount } from "solid-js";
|
||||||
import BeakerIcon from "../icons/BeakerIcon";
|
import BeakerIcon from "../icons/BeakerIcon";
|
||||||
import EyeDropperIcon from "../icons/EyeDropperIcon";
|
import EyeDropperIcon from "../icons/EyeDropperIcon";
|
||||||
import { Chart, registerables } from "chart.js";
|
import { Chart, registerables } from "chart.js";
|
||||||
|
import supabase from "../utils/supabase";
|
||||||
|
import { getTimes } from "../utils/dates";
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
let canvas: any;
|
let canvas: any;
|
||||||
const [chart, setChart] = createSignal<Chart | null>(null);
|
const [chart, setChart] = createSignal<Chart | null>(null);
|
||||||
|
const [suhu, setSuhu] = createSignal<number>(0);
|
||||||
|
const [kelembaban, setKelembaban] = createSignal<number>(0);
|
||||||
|
const [kadarGas, setKadarGas] = createSignal<number[]>([]);
|
||||||
|
const [timeStamps, setTimeStamps] = createSignal<any[]>([]);
|
||||||
|
|
||||||
const renderChart = () => {
|
const getData = async () => {
|
||||||
const date = new Date();
|
const { data } = await supabase
|
||||||
const his =
|
.from("kondisi_tapai")
|
||||||
date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
|
.select("*")
|
||||||
const val = Math.round(Math.random() * 30);
|
.order("created_at", { ascending: false })
|
||||||
|
.limit(10);
|
||||||
|
|
||||||
let labels = [his];
|
if (data != null) {
|
||||||
let data = [val];
|
const lastItem = data[0];
|
||||||
|
|
||||||
|
setSuhu(lastItem.suhu);
|
||||||
|
setKelembaban(lastItem.kelembaban);
|
||||||
|
setKadarGas([]);
|
||||||
|
setTimeStamps([]);
|
||||||
|
|
||||||
|
data.forEach((item) => {
|
||||||
|
setKadarGas((val) => {
|
||||||
|
val.push(item.kadar_gas);
|
||||||
|
return val;
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeStamps((val) => {
|
||||||
|
val.push(getTimes(item.created_at));
|
||||||
|
return val;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderChart = async () => {
|
||||||
|
await getData();
|
||||||
|
|
||||||
|
let labels = timeStamps();
|
||||||
|
let datas = kadarGas();
|
||||||
|
|
||||||
while (labels.length < 10) {
|
while (labels.length < 10) {
|
||||||
labels.push("-");
|
labels.push("-");
|
||||||
}
|
}
|
||||||
|
|
||||||
while (data.length < 10) {
|
while (datas.length < 10) {
|
||||||
data.push(0);
|
datas.push(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!chart()) {
|
if (!chart()) {
|
||||||
|
@ -32,7 +64,7 @@ export default function () {
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
label: "Kadar Gas",
|
label: "Kadar Gas",
|
||||||
data: data.reverse(),
|
data: datas.reverse(),
|
||||||
borderColor: "#4784f5",
|
borderColor: "#4784f5",
|
||||||
fill: true,
|
fill: true,
|
||||||
cubicInterpolationMode: "monotone",
|
cubicInterpolationMode: "monotone",
|
||||||
|
@ -55,8 +87,8 @@ export default function () {
|
||||||
display: true,
|
display: true,
|
||||||
text: "Nilai",
|
text: "Nilai",
|
||||||
},
|
},
|
||||||
suggestedMin: 0,
|
min: 0,
|
||||||
suggestedMax: 50,
|
max: 50,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -65,17 +97,20 @@ export default function () {
|
||||||
setChart(myChart);
|
setChart(myChart);
|
||||||
} else {
|
} else {
|
||||||
chart()?.data.labels?.splice(0, 1);
|
chart()?.data.labels?.splice(0, 1);
|
||||||
chart()?.data.labels?.push(his);
|
|
||||||
chart()?.data.datasets[0].data.splice(0, 1);
|
chart()?.data.datasets[0].data.splice(0, 1);
|
||||||
chart()?.data.datasets[0].data.push(val);
|
|
||||||
|
chart()?.data.labels?.push(getTimes());
|
||||||
|
chart()?.data.datasets[0].data.push(kadarGas()[0]);
|
||||||
|
|
||||||
chart()?.update();
|
chart()?.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(renderChart, 1000);
|
setTimeout(async () => {
|
||||||
|
await renderChart();
|
||||||
|
}, 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
onMount(() => {
|
onMount(async () => {
|
||||||
Chart.register(...registerables);
|
Chart.register(...registerables);
|
||||||
renderChart();
|
renderChart();
|
||||||
});
|
});
|
||||||
|
@ -89,11 +124,11 @@ export default function () {
|
||||||
<div class="grid grid-cols-2 grow gap-5 ">
|
<div class="grid grid-cols-2 grow gap-5 ">
|
||||||
<div class="bg-white rounded shadow min-h-24 flex flex-col items-center justify-center py-8">
|
<div class="bg-white rounded shadow min-h-24 flex flex-col items-center justify-center py-8">
|
||||||
<EyeDropperIcon class="w-12 h-12 inline-block" />
|
<EyeDropperIcon class="w-12 h-12 inline-block" />
|
||||||
<div class="text-3xl mt-5">30 °C</div>
|
<div class="text-3xl mt-5">{suhu()} °C</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="bg-white rounded shadow min-h-24 flex flex-col items-center justify-center py-8">
|
<div class="bg-white rounded shadow min-h-24 flex flex-col items-center justify-center py-8">
|
||||||
<BeakerIcon class="w-12 h-12 inline-block" />
|
<BeakerIcon class="w-12 h-12 inline-block" />
|
||||||
<div class="text-3xl mt-5">70 %</div>
|
<div class="text-3xl mt-5">{kelembaban()} %</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
export function getTimes(date: any = null) {
|
||||||
|
const dates = !!date ? new Date(date) : new Date();
|
||||||
|
|
||||||
|
const hour = dates.getHours() < 10 ? '0' + dates.getHours() : dates.getHours();
|
||||||
|
const minute = dates.getMinutes() < 10 ? '0' + dates.getMinutes() : dates.getMinutes();
|
||||||
|
const second = dates.getSeconds() < 10 ? '0' + dates.getSeconds() : dates.getSeconds();
|
||||||
|
|
||||||
|
const his = hour + ":" + minute + ":" + second;
|
||||||
|
|
||||||
|
return his;
|
||||||
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
const SUPABASE_URL = "https://oxmfbobxmqldgthethlz.supabase.co";
|
|
||||||
const SUPABASE_ANON_KEY= "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im94bWZib2J4bXFsZGd0aGV0aGx6Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDgwNjQ1NDksImV4cCI6MjAyMzY0MDU0OX0.pTDI9CsiN8wthOWhHjM1dONrRP_Hd7BcbwfKgeKGhtU";
|
|
||||||
|
|
||||||
import { createClient } from "@supabase/supabase-js";
|
import { createClient } from "@supabase/supabase-js";
|
||||||
|
|
||||||
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
|
const supabaseUrl = "https://oxmfbobxmqldgthethlz.supabase.co";
|
||||||
|
const supabaseKey= "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im94bWZib2J4bXFsZGd0aGV0aGx6Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDgwNjQ1NDksImV4cCI6MjAyMzY0MDU0OX0.pTDI9CsiN8wthOWhHjM1dONrRP_Hd7BcbwfKgeKGhtU";
|
||||||
|
|
||||||
|
const supabase = createClient(supabaseUrl, supabaseKey);
|
||||||
|
|
||||||
export default supabase;
|
export default supabase;
|
Loading…
Reference in New Issue