Skip to content

Instantly share code, notes, and snippets.

@dekarrin
Created February 26, 2021 20:51
Show Gist options
  • Save dekarrin/c57262edeef55c75d31d244c219fb008 to your computer and use it in GitHub Desktop.
Save dekarrin/c57262edeef55c75d31d244c219fb008 to your computer and use it in GitHub Desktop.
InfluxDB Writer Scripts
#!/bin/bash
set -e
is_int() {
local re='^[0-9]+$'
[[ "$1" =~ $re ]] || return 1
return 0
}
if [ "$1" = "-h" -o "$1" = "--help" ]
then
echo "do -h for help, or give argument for day to edit relative to today (-1, 32, etc)"
exit 0
fi
read -p "Author: " -r raw_author
read -p "Work: " -r raw_work
read -p "Draft: " -r draft
is_int "$draft" || { echo "not an integer: $draft" >&2 ; exit 1 ; }
read -p "Words: " -r words
is_int "$words" || { echo "not an integer: $words" >&2 ; exit 1 ; }
read -p "Complete Chapters: " -r complete_chapters
is_int "$complete_chapters" || { echo "not an integer: $complete_chapters" >&2 ; exit 1 ; }
read -p "Planned Chapters: " -r planned_chapters
is_int "$planned_chapters" || { echo "not an integer: $planned_chapters" >&2 ; exit 1 ; }
read -p "Complete Percent: " -r percent
work="$(echo "$raw_work" | sed 's/ /\\ /g')"
author="$(echo "$raw_author" | sed 's/ /\\ /g')"
rel_day=0
if [ -n "$1" ]
then
rel_day="$1"
fi
time="$(./get-day-ts.sh $rel_day)"
ilp_line='writing,author='"$author"',work='"$work"',draft='"$draft"' words='"$words"',chapters_complete='"$complete_chapters"',chapters_planned='"$planned_chapters"',percent='"$percent $time"
echo ""
echo "Writing the following line:"
echo ""
echo " $ilp_line"
echo ""
read -p "is that okay (y/n)? " -r ok
if [ "$ok" = "y" -o "$ok" = "Y" ]
then
echo "$ilp_line" | ./exflow.exe
else
echo "need to type Y or y to confirm; abandoning"
fi
// Package main provides a program to write arbirary data to a db
// using a configured password and username.
//
// Takes ILP-formated lines provided on stdin and writes them to the
// DB.
package main
import (
"bufio"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
// getting influxdb2 at v2.2.2 as of this writing
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
)
func main() {
var configFilePath string
flag.StringVar(&configFilePath, "c", "exflow.json", "give path to the config file")
flag.Parse()
conf, err := readConfig(configFilePath)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
opts := influxdb2.DefaultOptions()
if conf.SkipVerify {
opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: true})
}
client := influxdb2.NewClientWithOptions(conf.Host, fmt.Sprintf("%s:%s", conf.Username, conf.Password), opts)
defer client.Close()
writeAPI := client.WriteAPI("", conf.Database)
defer writeAPI.Flush()
errorsCh := writeAPI.Errors()
// Create go proc for reading and logging errors
go func() {
for err := range errorsCh {
fmt.Printf("error writing to DB: %s\n", err.Error())
}
}()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
writeAPI.WriteRecord(line)
}
if err := scanner.Err(); err != nil {
fmt.Printf("%v\n", err)
os.Exit(2)
}
}
type config struct {
Host string `json:"host"`
Username string `json:"username"`
Password string `json:"password"`
Database string `json:"database"`
SkipVerify bool `json:"skip_verify"`
}
func readConfig(filename string) (c config, err error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return c, fmt.Errorf("load config: %v", err)
}
err = json.Unmarshal(content, &c)
if err != nil {
return c, fmt.Errorf("decode config: %v", err)
}
if !strings.HasPrefix(strings.ToLower(c.Host), "http") {
return c, fmt.Errorf("host must start with HTTP:// or HTTPS://")
}
return c, nil
}
#!/bin/bash
now_date="$(date +%s)"
((now_date /= 86400))
((now_date *= 86400))
((now_date++))
if [ -n "$1" ]
then
to_add="$1"
((to_add *= 86400))
((now_date += to_add))
fi
echo "${now_date}000000000"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment