d3.range
1d3.range([start, ]stop[, step])
Returns an array containing an arithmetic progression, similar to the Python built-in range. This method is often used to iterate over a sequence of uniformly-spaced numeric values, such as the indexes of an array or the ticks of a linear scale.
If step is ommitted, it defaults to 1. If start ommitted, it defaults to 0. The stop value is exclusive; it is not included in the result.
The arguments are not required to be integers; however, the results are more pred ...
Quick Reference of D3
Selectionsd3.select()Returns a reference to the first element found.
d3.selectAll()Returns references to all found elements
selection.append()Takes a selection, creates a new element inside of it, then returns a reference to the newly created element.
1const newCirlce = svg.append('circle')
selection.remove()Takes a selection, removes it from the DOM, and returns a reference to the deleted selection.
selection.text()Takes a selection, sets its text content, and returns a reference to ...
Simple Api of Mongoose
Two methods to connect to Mongo DB
mongoose.connect
mongoose.createConnection
123456const dbURL = 'mongodb://...'const dbOptions = { user: 'db_user', pass: 'db_pass'}const adminConnection = mongoose.createConnection(dbURL, dbOptions)
Close Connection123456789101112131415mongoose.connection.close(() => { console.log('Connection cvlosed')})adminConnection.close(() => { console.log('adminConnection closed') ...
Notes on Interactive Data Visualization for the Web
Introducing D3D3 – also referred to as d3.js – is a JavaScript library for creating data visualization. But that kind of undersell it.
The abbreviation D3 reference the tool’s full name, Data-Driven Documents. The data is provided by you, and the documents are web-based documents, meaning anything that can be rendered by a web browser, such as HTML and SVG. D3 does the driviing, in the sense that it connects the data to the documents.
DataData is an extemely broad term, only slightly less vague ...
Simple Usage of SW
Register123456navigator.serviceWorker.register('your_service_worker,js', { scope: 'your_path_name' }).then((worker) => { console.log('[SW] Registration succeeded.')}).catch((err) => { console.log('[SW] Registration failed with ' + err)})
Unregister12345678910111213141516navigator.serviceWorker.getRegistration('your_service_worker.js', { scope: 'your_path_name' }).then((worker) => { ...
Glossary of Terms on InfluxDB
aggregationAn InfluxQL function that returns an aggregated value across a set of points
batchA collection of points in line protocol format, separated by newlines(0x0A). A batch of points may be submitted to the database using a single HTTP request to the write endpoint. This makes writes via the HTTP API much more performant by drastically reducting the HTTP overhead. InfluxData recommends batch size of 5,000-10,000 points, although different use cases may be better served by significantly smal ...
APIs of InfluxDB
The InfluxDB API provides a simple way interact with the database. It uses HTTP response codes, HTTP authentication, JWT Token, and basic authentication and responses are returned in JSON.
The following sections assume your InfluxDB instance is running on localhost port 8086 and HTTPS is not enabled.
Endpoints
Endpoint
Description
/ping
use /ping to check the status of your InfluxDB instance and your version of InfluxDB
/query
use /query to query data and manage databsae, retention polici ...
Writing Data in InfluxDB With HTTP API
Create a database using the HTTP APITo create a database send a POST request to the /query endpoint and set the URL parameter q to CREATE DATABASE <new_database_name>. The example below sends a request to InfluxDB running on locahost and create the database mydb.
1curl -i -XPOST http://localhost:8086/query --data-urlencoded "q=CREATE DATABASE mydb"
Writing data using the HTTP APIThe HTTP API is the primary means of writing data into InfluxDB, by sending POST requests to the /wri ...
Querying InfluxDB With the HTTP API
The HTTP API is the primary means for querying data in InfluxDB.
To perform a query send a GET request to the /query endpoint, set the URL parameter db as the target database, and set the URL parameter q as your query.
1curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'"
InfluxDB returns JSON. The results of your ...
Simple About InfluxDB
Install InfluxDB on MacOS12brew updatebrew install influxdb
To run influxDB at startup, run this command
1ln -sfv /usr/local/opt/influxdb/*.plist ~/Library/LaunchAgents
Now, to start InfluxDB, run this command(Accept the request of incoming connections in the window that appears)
1influxd -config /usr/local/etc/influxdb.conf
Check if InfluxDB worksInfluxDB exposes an API with which one can easily check its good working condition. Here’s what you will receive in return for this curl command
12 ...
Usage of Node-Influx
IntroductionInfluxDB is a time series database, so it would make sense that the concept of time is moderately important when dealing with it.
By default, Influx will store all dates you give to it as a nanosecond precision timestamp, whereas in JavaScript, most of the time we’re dealing with millisecond precision timestamps, which we get from Date.now() or myDate.getTime(). This presents a bit of a problem for us JavaScripters, since nanosecond-precision timestamps are stored as 64-bit unsigned ...
Example of Node-Influx
Express Response Times ExampleIn this example we’ll create a server which has an index page that prints out ‘hello world’, and a page
http://localhost:3000/times which prints out the last ten response times that influxDB gave us.
The end result should look something like this:
123456789101112curl -s localhost:3000Hello worldcurl -s localhost:3000/times | jq[ { "time": "2016-10-09T19:13:26.815Z", "duration": 205, "host": "ares.peet.io&qu ...
Snippets of Koa Middlewares
koa-compressCompress middleware for Koa
Example123456789const compress = require('koa-compress')const Koa = require('koa')const app = new Koa()app.use(compress({ filter: (content_type) => /text/i.test(content_type), threshold: 2048, flush: require('zlib').Z_SYNC_FLUSH,}))
OptionsThe options are passed to zlib
filter: An optional function that checks the response content type to decide whether to compress. By default, it uses compressible
threshol ...
Git Merge and Rebase
Conceptual OverviewThe first thing to understand about git rebase is that it solves the same problem as git merge. Both of these commands are designed to integrate changes from one branch into another branch – they just do it in very different ways.
Consider what happens when you start working on a new feature in a dedicated branch, then another team member updates the master branch with new commits. The results in a forked history which should be familiar to anyone who has used Git as a collabo ...
Type Compatibility in TypeScript
Type Compaibility in TypeScript is based on Structural Subtyping. Structural Typing is a way of relating types based solely on their members. This is in contrast with nominal typing. Consider the following code:
1234567891011interface Named { name: string}class Person { name: string}let p: Named// OK, because of structural typingp = new Person()
In nominally-typed language like C# or Java, the equivalnet code would be error because the Person class does not explicitly desc ...
Mannual of Ownership in Rust
Ownership is Rust’s most unique feature, and it enables Rust to make memory safety guarantees without needing a garbage collector. Therefore, it’s important to understand how ownership works in Rust. In this chapter we’ll talk about ownership as well as several related features: borrowing, slices, and how Rust lays data out in memory.
What is OwnershipRust’s central feature is ownership, although the feature is staightforward to explain, it has deep implications of the rest of the language.
All ...
Basic Info of Web Worker
123// index.jsvar worker = new Worker ('worker.js')worker.postMessage() // start the worker
postMessage 方法可以接受字符串或 JSON 对象
123456// index.jsworker.addEventListener('message', (e) => { console.log('worker said: ', e.data)}, false)worker.postMessage('hello world')
1234// worker.jsself.addEventListener('message', (e) => { self.postMessage(e.data)}, false)
index.js 中调用 postMessage() 时, worker.js 通过 message 时间处理消息, index ...
Deep in Viewport
vw(viewport width) and vh(viewport height) are legnth units that represent exactly 1% of the size of any given viewport, regardless of its measurements. rem(short for root em) is also similar in its functionality, although it deals specifically with font sizes, and derives its name from the value fo the font size of the root element – which should default to 16 pixels on most browsers.
There are also a few more viewport units available for use, such as vmin and vmax which refer respectively to 1 ...
Utils in Axios
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107// global toStringvar toString = Object.prototype.toString// isArrayfunction isArray (val) { return toString.call(val) === '[object Array]'}// isArrayBufferfunction isArrayBuffer (val) { return toString.call(val) === '[object arrayBuffer]'}// is ...
Concept of PWA
Advantages of Progressive Web Apps:
Reliable - Load instantly and never show the dinasaur.
Fast - Respond quickly to user interactions with silky smooth animations.
Enaging - Feel like a natural app on the device, with immersive user experience.
What is a Progressive Web App
Progressive - Works for every user, regardless of browser choice because it’s built with progressive enhancement as a core tenet.
Responsive - Fits any form factor: desktop, mobile, tablet, or whatever is next.
Connect ...