Initial commit from private

This commit is contained in:
2019-07-15 20:51:25 -04:00
commit 264f03a22f
55 changed files with 2006 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
using Dapper;
using Microsoft.Extensions.Configuration;
using Npgsql;
using Weather.Models;
namespace Weather.Service.Data
{
public class Database
{
private readonly IConfiguration _configuration;
public Database(IConfiguration configuration)
{
_configuration = configuration;
}
public void EnsureDatabase()
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder
{
Host = _configuration["Weather:Database:Host"],
Username = _configuration["Weather:Database:User"],
Password = _configuration["Weather:Database:Password"],
Database = "postgres"
};
using (var connection = new NpgsqlConnection(connectionStringBuilder.ConnectionString))
{
var command = new NpgsqlCommand { Connection = connection };
connection.Open();
// Check to see if the database exists
command.CommandText = $"SELECT TRUE from pg_database WHERE datname='{_configuration["Weather:Database:Name"]}'";
var databaseExists = (bool?)command.ExecuteScalar();
// Create database if needed
if (!databaseExists.GetValueOrDefault(false))
{
command.CommandText = $"CREATE DATABASE {_configuration["Weather:Database:Name"]}";
command.ExecuteNonQuery();
}
// Switch to the database now that we're sure it exists
connection.ChangeDatabase(_configuration["Weather:Database:Name"]);
var schema = ResourceReader.GetString("Weather.Service.Data.Resources.Schema.sql");
// Make sure the database is up to date
command.CommandText = schema;
command.ExecuteNonQuery();
}
}
private NpgsqlConnection CreateConnection()
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder
{
Host = _configuration["Weather:Database:Host"],
Username = _configuration["Weather:Database:User"],
Password = _configuration["Weather:Database:Password"],
Database = _configuration["Weather:Database:Name"]
};
var connection = new NpgsqlConnection(connectionStringBuilder.ConnectionString);
connection.Open();
return connection;
}
public void StoreWeatherData(WeatherMessage weatherMessage)
{
using (var connection = CreateConnection())
{
var query = ResourceReader.GetString("Weather.Service.Data.Resources.CreateReading.sql");
connection.Execute(query, weatherMessage);
}
}
}
}

View File

@@ -0,0 +1,6 @@
INSERT INTO weather_reading (timestamp, wind_direction, wind_speed, humidity, humidity_temperature, rain, pressure,
pressure_temperature, battery_level, light_level, latitude, longitude, altitude,
satellite_count, gps_timestamp)
VALUES (:timestamp, :windDirection, :windSpeed, :humidity, :humidityTemperature, :rain, :pressure, :pressureTemperature,
:batteryLevel, :lightLevel, :latitude, :longitude, :altitude, :satelliteCount, :gpsTimestamp)
ON CONFLICT DO NOTHING

View File

@@ -0,0 +1,24 @@
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
CREATE TABLE IF NOT EXISTS weather_reading
(
timestamp timestamptz NOT NULL
CONSTRAINT weather_reading_pk
PRIMARY KEY,
wind_direction int NOT NULL,
wind_speed double precision NOT NULL,
humidity double precision NOT NULL,
humidity_temperature double precision NOT NULL,
rain double precision NOT NULL,
pressure double precision NOT NULL,
pressure_temperature double precision NOT NULL,
battery_level double precision NOT NULL,
light_level double precision NOT NULL,
latitude double precision NOT NULL,
longitude double precision NOT NULL,
altitude double precision NOT NULL,
satellite_count double precision NOT NULL,
gps_timestamp timestamptz NOT NULL
);
SELECT create_hypertable('weather_reading', 'timestamp', if_not_exists => TRUE);