Let’s make a html file named simple-webapp.html. machbase-neo supports .html, .js and .css file editing
Since v8.0.14.
If you are using previous version of machbase-neo, please update it or use your favorite editor instead.
Edit and save html file, open in web browser by click βΊ button on left top corner of the editor.
<html><head><script>functionsubmitData(){constvalue=Number(document.getElementById("userInput").value)fetch("http://127.0.0.1:5654/db/write/EXAMPLE?timeformat=ms",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({"data":{"columns":["name","time","value"],"rows":[["webapp",Date.now(),value]]}})}).then(function(rsp){returnrsp.json()}).then(function(obj){document.getElementById("rspInput").innerHTML='<pre>'+JSON.stringify(obj,null,2)+'</pre>'})}</script></head><body><h4> Input data </h4><form><inputid="userInput"><ahref="#"onClick="submitData()">Submit</a></form><divid="rspInput"></div></body></html>
line 6: timeformat=ms Declare the data payload contains time value in millisecond unix epoch. which is used in line 15 Date.now().
line 9: Set content-type of the payload, so that machbase-neo interprets it properly.
line 11-18: The actual data in payload. This example has only one record in the rows field, but it may contain multiple records,
Read data
Extend the simple-webapp.html to read data, add new function queryData(){...} and “query” action.
26
27
28
29
30
31
32
33
34
functionqueryData(){constquery=`select * from example where name = 'webapp' limit 5`constopt=document.querySelector('input[name="opt"]:checked').value+'=true'fetch(`http://127.0.0.1:5654/db/query?timeformat=ms&`+opt+`&q=`+query).then(function(rsp){returnrsp.json()}).then(function(obj){document.getElementById("rspQuery").innerHTML='<pre>'+JSON.stringify(obj,null,2)+'</pre>'})}
line 28: Specify the time format of result should be unix epoch in milliseconds.
94
95
96
97
98
99
100
<h4> Query data </h4><inputtype="radio"name="opt"value="none"checked/>none
<inputtype="radio"name="opt"value="transpose"/>transpose
<inputtype="radio"name="opt"value="rowsFlatten"/>rowsFlatten
<inputtype="radio"name="opt"value="rowsArray"/>rowsArray
<ahref="#"onClick="queryData()">Query</a><divid=rspQuery></div>
Markdown
The query result can be transform to markdown using TQL, let’s add function markdownData(){...} and “Markdown” action.
36
37
38
39
40
41
42
43
44
45
46
47
functionmarkdownData(){constasHtml=document.getElementById("htmlMarkdown").checkedfetch(`http://127.0.0.1:5654/db/tql`,{method:"POST",body:` SQL("select * from example where name = 'webapp' limit 100")
MARKDOWN( html(`+asHtml+`), timeformat('Default'), tz('Local'), briefCount(10) )`}).then(function(rsp){returnrsp.text()}).then(function(obj){document.getElementById("rspMarkdown").innerHTML='<pre>'+obj+'</pre>'})}
line 36: Get output format option if HTML or MARKDOWN text.
line 40: MARKDOWN() TQL SINK with options html(boolean), timeformat(), tz()…
You can visualize data with any chart library that you prefer by retrieving the query result in JSON and CSV via HTTP API.
But it is still simple and powerful using TQL CHART() SINK.
This example shows how to visualize data with TQL without any extra tool.
Add function chartData() and a helper function loadJS().