Skip to content
Snippets Groups Projects
insert_influx.py 1.57 KiB
import logging
from datetime import datetime

import requests

from metobs.data import wind_vector_components
from aosstower.level_00.parser import read_frames

LOG = logging.getLogger(__name__)


def _mktime(frame):
    return int((frame['stamp'] - datetime(1970, 1, 1)).total_seconds() * 10**9)


def frame_records(frame):
    nanos = _mktime(frame)
    if 'wind_speed' in frame and 'wind_dir' in frame:
        spd = frame.pop('wind_speed')
        dr = frame.pop('wind_dir')
        e, n, _ = wind_vector_components(spd, dr)
        frame['wind_east'] = e
        frame['wind_north'] = n
    for name, value in frame.items():
        if name == 'stamp':
            continue
        valstr = '{}i'.format(value) if isinstance(value, int) else str(value)
        yield '{},inst=tower,site=aoss value={} {}'.format(name, valstr, nanos)


def file_records(filepath):
    lines = []
    for frame in read_frames(filepath):
        lines += frame_records(frame)
    return lines


def insert(filepath):
    LOG.info('reading %s', filepath)
    lines = file_records(filepath)
    LOG.info("posting %d record from %s...", len(lines), filepath)
    resp = requests.post(
        'http://bikini.ssec.wisc.edu:8086/write?db=metobs',
        data='\n'.join(lines))
    resp.raise_for_status()


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('asciifile')
    args = parser.parse_args()

    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s -- %(message)s')
    logging.getLogger('requests').setLevel(logging.WARN)
    insert(args.asciifile)