Created
March 29, 2020 17:19
-
-
Save mrtycjan/be5a5afd0339e81ef9de3076c7d1ed0a to your computer and use it in GitHub Desktop.
influx node covid 19 client
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
import { InfluxDB, WritePrecision, Point } from '@influxdata/influxdb-client'; | |
import {default as axios, AxiosResponse} from 'axios'; | |
const config = { | |
url: 'http://localhost:9999', | |
token: 'your_token_here', | |
org: 'your_orgid_here', | |
bucket: 'corona_live', | |
} | |
// Create client and writeApi instance | |
const client = new InfluxDB({url: config.url, token: config.token}); | |
const writeApi = client.getWriteApi(config.org, config.bucket, WritePrecision.s); | |
//Interfaces for api reponse | |
interface CoronaCountry { | |
country: string; | |
cases: number; | |
todayCases: number; | |
deaths: number; | |
todayDeaths: number; | |
recovered: number; | |
active: number; | |
critical: number; | |
casesPerOneMillion: number; | |
} | |
interface CoronaGlobal { | |
cases: number; | |
deaths: number; | |
recovered: number; | |
} | |
// Factory for global stats | |
const globalPointFactory = async (): Promise<Point> => { | |
// Get data from endpoint | |
const result: AxiosResponse<CoronaGlobal> = await axios.get('https://corona.lmao.ninja/all'); | |
//Write in influxdb | |
return new Point('corona_global') | |
.tag('target', 'global') | |
.intField('cases', result.data.cases) | |
.intField('death', result.data.deaths) | |
.intField('recovered', result.data.recovered); | |
} | |
// Factory for stats per country | |
const countryPointCollectionFactory = async (): Promise<Point[]> => { | |
// Get data from endpoint | |
const result: AxiosResponse<CoronaCountry[]> = await axios.get('https://corona.lmao.ninja/countries'); | |
return result.data.map(countryData => { | |
return new Point(countryData.country) | |
.tag('target', countryData.country) | |
.intField('cases', countryData.cases) | |
.intField('death', countryData.deaths) | |
.intField('recovered', countryData.recovered) | |
.intField('todayCases', countryData.todayCases) | |
.intField('todayDeaths', countryData.todayDeaths) | |
.intField('active', countryData.active) | |
.intField('critical', countryData.critical) | |
.intField('casesPerOneMillion', countryData.casesPerOneMillion); | |
}); | |
} | |
// Periodically (1 second) send global stats to influx | |
setInterval(async () => { | |
const point = await globalPointFactory(); | |
writeApi.writePoint(point); | |
}, 1000); | |
// Periodically (1 second) send stat for each country to influx | |
setInterval(async () => { | |
const pointCollection = await countryPointCollectionFactory(); | |
pointCollection.map(point => { | |
writeApi.writePoint(point); | |
}) | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment