Query and display weather data for all country
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
--add-opens=java.base/java.util=ALL-UNNAMED
|
||||
--add-opens=java.base/java.lang=ALL-UNNAMED
|
||||
@@ -1 +1,2 @@
|
||||
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0")
|
||||
addSbtPlugin("nl.gn0s1s" % "sbt-dotenv" % "3.0.0")
|
||||
|
||||
@@ -14,7 +14,7 @@ object DBConnection {
|
||||
private def getDbPassword = sys.env.getOrElse("POSTGRES_PASSWORD", "")
|
||||
|
||||
private def getDatabaseUrl: String =
|
||||
sys.env.getOrElse("DATABASE_URL", s"postgres://${getDbUser}:${getDbPassword}@postgres:5432/${getDbName}")
|
||||
sys.env.getOrElse("DATABASE_URL", s"postgres://${getDbUser}:${getDbPassword}@0.0.0.0:5432/${getDbName}") // host - postgres for docker, 0.0.0.0 outside docker
|
||||
|
||||
private def parseUrl(url: String): Either[String, PostgresConfig] = {
|
||||
Try {
|
||||
|
||||
@@ -7,7 +7,7 @@ import doobie._
|
||||
import doobie.implicits._
|
||||
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.{LocalDate, LocalDateTime, OffsetDateTime, ZoneId, ZonedDateTime}
|
||||
import java.time.{LocalDate, LocalDateTime, OffsetDateTime}
|
||||
import doobie.postgres.implicits._
|
||||
import org.typelevel.log4cats.Logger
|
||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||
@@ -23,8 +23,6 @@ object PostgresService {
|
||||
}
|
||||
|
||||
class PostgresService(transactor: Transactor[IO], log: Logger[IO]) {
|
||||
// in pgAdmin run: ```SET TIMEZONE = 'Europe/Riga';```
|
||||
|
||||
def save(fileName: String, content: String): IO[String] = {
|
||||
val strLines = content.split(System.lineSeparator()).toList
|
||||
val weatherStationData = strLines.flatMap(Parser.parseLine)
|
||||
@@ -36,6 +34,30 @@ class PostgresService(transactor: Transactor[IO], log: Logger[IO]) {
|
||||
}
|
||||
}
|
||||
|
||||
def queryCountry(from: LocalDateTime, to: LocalDateTime, fieldList: NonEmptyList[String]): IO[Map[String, (Option[Double], Option[Double], Option[Double], Option[Double])]] = {
|
||||
def queryForField(field: String, from: LocalDateTime, to: LocalDateTime): ConnectionIO[(String, (Option[Double], Option[Double], Option[Double], Option[Double]))] =
|
||||
{
|
||||
val query =
|
||||
fr"SELECT " ++
|
||||
fr" ROUND(CAST(MIN(" ++ Fragment.const(field) ++ fr") AS NUMERIC), 1), " ++
|
||||
fr" ROUND(CAST(MAX(" ++ Fragment.const(field) ++ fr") AS NUMERIC), 1), " ++
|
||||
fr" ROUND(CAST(AVG(" ++ Fragment.const(field) ++ fr") AS NUMERIC), 1), " ++
|
||||
fr" ROUND(CAST(SUM(" ++ Fragment.const(field) ++ fr") AS NUMERIC), 1)" ++
|
||||
fr" FROM weather" ++
|
||||
fr" WHERE dateTime BETWEEN $from AND $to"
|
||||
|
||||
query.query[(Option[Double], Option[Double], Option[Double], Option[Double])]
|
||||
.unique
|
||||
.map { case (min, max, avg, sum) =>
|
||||
field -> (min, max, avg, sum)
|
||||
}
|
||||
}
|
||||
|
||||
fieldList.toList.traverse { field =>
|
||||
queryForField(field, from, to).transact(transactor)
|
||||
}.map(_.toMap)
|
||||
}
|
||||
|
||||
def query(userQuery: UserQuery): IO[Map[String, Option[AggregateValue]]] = {
|
||||
if (userQuery.field == "phenomena") { // this handles strings
|
||||
// TODO query list and distinct values from phenomena
|
||||
@@ -171,12 +193,6 @@ class PostgresService(transactor: Transactor[IO], log: Logger[IO]) {
|
||||
.transact(transactor)
|
||||
}
|
||||
|
||||
def readFile(fileName: String): IO[List[String]] = ???
|
||||
|
||||
def getInRange(from: LocalDateTime, to: LocalDateTime): IO[List[String]] = ???
|
||||
|
||||
def getDates: IO[List[LocalDate]] = ???
|
||||
|
||||
def getDateTimeEntries(dateTime: LocalDateTime): IO[List[String]] = {
|
||||
val columnNames = List("City; TempMax; TempMin; TempAvg; Precipitation; WindAvg; WindMax; VisibilityMin; VisibilityAvg; SnowAvg; AtmPressure; DewPoint; Humidity; SunDuration; Phenomena")
|
||||
val result = fr"""
|
||||
@@ -247,38 +263,4 @@ class PostgresService(transactor: Transactor[IO], log: Logger[IO]) {
|
||||
.transact(transactor)
|
||||
}
|
||||
}
|
||||
|
||||
def selectWeatherTable(): IO[List[(String, Option[Double])]] = {
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
val from = LocalDateTime.parse("20230516_0100", formatter)
|
||||
val to = LocalDateTime.parse("20230516_1500", formatter)
|
||||
val citiesNel = NonEmptyList.of("Rīga", "Rēzekne")
|
||||
val granularity = "HOUR" // "HOUR", "DAY", "MONTH", "YEAR"
|
||||
val columnName = "tempMin"
|
||||
|
||||
val baseQuery =
|
||||
fr"""
|
||||
SELECT
|
||||
city,
|
||||
MIN(""" ++ Fragment.const(columnName) ++ fr""") AS tempMin
|
||||
FROM weather
|
||||
WHERE
|
||||
""" ++ Fragments.in(fr"city", citiesNel) ++ fr"""
|
||||
AND dateTime BETWEEN $from AND $to
|
||||
AND """ ++ Fragment.const(columnName) ++fr""" IS NOT NULL
|
||||
GROUP BY city, EXTRACT(""" ++ Fragment.const(granularity) ++ fr""" FROM dateTime)
|
||||
"""
|
||||
|
||||
baseQuery
|
||||
.query[(String, Option[Double])]
|
||||
.to[List]
|
||||
.transact(transactor)
|
||||
}
|
||||
|
||||
def dropWeatherTable(): IO[Int] = {
|
||||
for {
|
||||
dropTableSql <- getResourceContent("/db/drop_weather_table.sql")
|
||||
result <- Update0(dropTableSql, None).run.transact(transactor)
|
||||
} yield result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import cats.implicits.toTraverseOps
|
||||
import com.comcast.ip4s.IpLiteralSyntax
|
||||
import db.PostgresService
|
||||
import fetch.FetchService
|
||||
import parse.{Aggregate}
|
||||
import server.ValidateRoutes.{AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateMonths}
|
||||
import parse.Aggregate
|
||||
import server.ValidateRoutes.{AggFieldList, AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateMonths}
|
||||
import io.circe.{Json, Printer}
|
||||
import org.http4s._
|
||||
import org.http4s.dsl.io._
|
||||
@@ -50,14 +50,19 @@ class Server(postgresService: PostgresService, fetch: FetchService, log: Logger[
|
||||
|
||||
private val apiRoutes = HttpRoutes.of[IO] {
|
||||
|
||||
// http://0.0.0.0:8080/api/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempMax/max
|
||||
case GET -> Root / "query" / DateTimeRange(from, to) / Granularity(granularity) / CityList(cities) / field / AggKey(key) =>
|
||||
// http://0.0.0.0:8080/api/query/city/Liepāja,Rēzekne/20230414_2200-20230501_1230/hour/tempMax/max
|
||||
case GET -> Root / "query" / "city" / CityList(cities) / DateTimeRange(from, to) / Granularity(granularity) / field / AggKey(key) =>
|
||||
val userQuery = UserQuery(cities, field, key, granularity, from, to)
|
||||
|
||||
postgresService.query(userQuery)
|
||||
.map(result => ResponseWrapper(result, userQuery))
|
||||
.flatMap(responseWrapper => Ok(responseWrapper.asJson.pretty))
|
||||
|
||||
// http://0.0.0.0:8080/api/query/country/20230414_2200-20230501_1230/tempMax,tempMin,tempAvg,precipitation
|
||||
case GET -> Root / "query" / "country" / DateTimeRange(from, to) / AggFieldList(fieldList) =>
|
||||
postgresService.queryCountry(from, to, fieldList)
|
||||
.flatMap(result => Ok(result.asJson.pretty))
|
||||
|
||||
// http://0.0.0.0:8080/api/fetch/date/20230514
|
||||
case GET -> Root / "fetch" / "date" / ValidateDate(date) =>
|
||||
val result = for {
|
||||
|
||||
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import cats.data.NonEmptyList
|
||||
import parse.Aggregate.AggregateKey
|
||||
import parse.WeatherData
|
||||
|
||||
import java.time.{LocalDate, LocalDateTime}
|
||||
import java.time.format.DateTimeFormatter
|
||||
@@ -56,6 +57,14 @@ object ValidateRoutes {
|
||||
}
|
||||
}
|
||||
|
||||
object AggFieldList {
|
||||
def unapply(str: String): Option[NonEmptyList[String]] = {
|
||||
val weatherFields = WeatherData.getKeys
|
||||
val filteredList = str.split(",").toList.filter(weatherFields.contains)
|
||||
NonEmptyList.fromList(filteredList)
|
||||
}
|
||||
}
|
||||
|
||||
object AggKey {
|
||||
def unapply(str: String): Option[AggregateKey] = {
|
||||
AggregateKey.fromString(str)
|
||||
|
||||
+12
-9
@@ -1,31 +1,34 @@
|
||||
import { Component, createSignal } from 'solid-js';
|
||||
import { Aggregator } from './aggregator/Aggregator';
|
||||
|
||||
import styles from './css/App.module.css';
|
||||
import { FileManager } from './fileManager/FileManager';
|
||||
import { Country } from './country/Country';
|
||||
import { Cities } from './cities/Cities';
|
||||
import { Database } from './database/Database';
|
||||
|
||||
console.log("env:", import.meta.env.MODE);
|
||||
console.log("api host:", import.meta.env.VITE_API_HOST);
|
||||
|
||||
type Section = "aggregator" | "mapView" | "fileManager";
|
||||
type Section = "cities" | "country" | "database";
|
||||
|
||||
const App: Component = () => {
|
||||
const [getSection, setSection] = createSignal<Section>("aggregator");
|
||||
const [getSection, setSection] = createSignal<Section>("country");
|
||||
|
||||
const section = () => {
|
||||
switch(getSection()) {
|
||||
case "fileManager": return <FileManager />;
|
||||
case "aggregator":
|
||||
case "database": return <Database />;
|
||||
case "country": return <Country />;
|
||||
case "cities": return <Cities />
|
||||
default:
|
||||
return <Aggregator />;
|
||||
return <Cities />;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div class={styles.App}>
|
||||
<div class={styles.sections}>
|
||||
<span onClick={() => setSection("aggregator")}>aggregator</span> |
|
||||
<span onClick={() => setSection("fileManager")}>file manager</span>
|
||||
<span onClick={() => setSection("cities")}>cities</span> |
|
||||
<span onClick={() => setSection("country")}>latvia</span> |
|
||||
<span onClick={() => setSection("database")}>database</span>
|
||||
</div>
|
||||
{ section() }
|
||||
</div>
|
||||
|
||||
@@ -6,12 +6,12 @@ import { SelectCity } from "./SelectCity";
|
||||
import { SelectField } from "./SelectField";
|
||||
import { SelectGranularity } from "./SelectGranularity";
|
||||
import { SelectKey } from "./SelectKey";
|
||||
import { SelectTimeRange } from "./SelectTimeRange";
|
||||
import { SelectTimeRange } from "../components/SelectTimeRange";
|
||||
|
||||
const nowRounded = new Date(new Date().setMinutes(30));
|
||||
const dayAgo = moment(nowRounded).subtract(1, "days").subtract(30, "minutes").toDate();
|
||||
|
||||
export function Aggregator() {
|
||||
export function Cities() {
|
||||
const [getCities, setCities] = createSignal<Set<string>>(new Set([]));
|
||||
const [getStart, setStart] = createSignal(dayAgo);
|
||||
const [getEnd, setEnd] = createSignal(nowRounded);
|
||||
@@ -21,7 +21,7 @@ export function Aggregator() {
|
||||
|
||||
return (
|
||||
<div class="aggregator">
|
||||
<h2>Aggregator</h2>
|
||||
<h2>Cities</h2>
|
||||
<div class="container">
|
||||
<div class="column">
|
||||
<SelectCity getCities={getCities} setCities={setCities} />
|
||||
@@ -22,7 +22,7 @@ export const QueryResult: Component<{
|
||||
const fetchQuery = async (timestamp: number) => {
|
||||
if (cities() === "") return new Error("ERROR: Select cities!");
|
||||
await new Promise(resolve => setTimeout(resolve, 500))
|
||||
const response = await fetch(`${apiHost}/api/query/${queryStart()}-${queryEnd()}/${props.getGranularity()}/${cities()}/${props.getField()}/${props.getKey()}`);
|
||||
const response = await fetch(`${apiHost}/api/query/city/${cities()}/${queryStart()}-${queryEnd()}/${props.getGranularity()}/${props.getField()}/${props.getKey()}`);
|
||||
const json = await response.json();
|
||||
return json;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { createSignal } from "solid-js";
|
||||
|
||||
import { QueryResult } from "./result/QueryResult";
|
||||
import { SelectTimeRange } from "../components/SelectTimeRange";
|
||||
import moment from "moment";
|
||||
import { weatherField } from "../consts";
|
||||
|
||||
const nowRounded = new Date(new Date().setMinutes(30));
|
||||
const dayAgo = moment(nowRounded).subtract(1, "days").subtract(30, "minutes").toDate();
|
||||
|
||||
export function Country() {
|
||||
const weatherFieldNumeric = weatherField.filter(f => f !== "phenomena");
|
||||
const [getStart, setStart] = createSignal(dayAgo);
|
||||
const [getEnd, setEnd] = createSignal(nowRounded);
|
||||
const [getFields, setFields] = createSignal(weatherFieldNumeric);
|
||||
|
||||
return (
|
||||
<div class="fileManager">
|
||||
<h2>Latvia</h2>
|
||||
<div class="container">
|
||||
<div class="column">
|
||||
column1
|
||||
</div>
|
||||
<div class="column">
|
||||
<SelectTimeRange
|
||||
getStart={getStart}
|
||||
setStart={setStart}
|
||||
getEnd={getEnd}
|
||||
setEnd={setEnd}
|
||||
/>
|
||||
</div>
|
||||
<div class="column">
|
||||
<QueryResult
|
||||
getFields={getFields}
|
||||
getStart={getStart}
|
||||
getEnd={getEnd}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import moment from "moment";
|
||||
import { Accessor, Component, createResource, createSignal } from "solid-js";
|
||||
|
||||
import { apiHost } from "../../consts";
|
||||
import { Result } from "./Result";
|
||||
|
||||
|
||||
export const QueryResult: Component<{
|
||||
getStart: Accessor<Date>,
|
||||
getEnd: Accessor<Date>,
|
||||
getFields: Accessor<string[]>,
|
||||
}> = (props) => {
|
||||
const queryStart = () => moment(props.getStart()).format("YYYYMMDD_HHmm");
|
||||
const queryEnd = () => moment(props.getEnd()).format("YYYYMMDD_HHmm");
|
||||
const [getTimestamp, setTimestamp] = createSignal<number>(0);
|
||||
|
||||
const fetchQuery = async (timestamp: number) => {
|
||||
if (props.getFields().length === 0) return new Error("ERROR: Select weather parameters!");
|
||||
await new Promise(resolve => setTimeout(resolve, 500))
|
||||
const response = await fetch(`${apiHost}/api/query/country/${queryStart()}-${queryEnd()}/${props.getFields().join(",")}`);
|
||||
const json = await response.json();
|
||||
return json;
|
||||
}
|
||||
|
||||
const [queryResource] = createResource(getTimestamp, fetchQuery);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<input
|
||||
type="button"
|
||||
class="primary"
|
||||
value="Query Data"
|
||||
onClick={() => setTimestamp(Date.now())}
|
||||
/>
|
||||
{ queryResource.loading && (
|
||||
<div>
|
||||
<span class="spinner"></span>
|
||||
<span style={{ "padding-left": "16px" }}>Loading query</span>
|
||||
</div>
|
||||
)}
|
||||
{ queryResource.error && (
|
||||
<div>Error while querying: ${queryResource.error}</div>
|
||||
)}
|
||||
{ queryResource() && queryResource() instanceof Error &&
|
||||
<div>{ queryResource().message }</div>
|
||||
}
|
||||
{ queryResource() &&
|
||||
<Result result={queryResource} />
|
||||
}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Component, Resource } from "solid-js";
|
||||
|
||||
export interface ResultData {
|
||||
[field: string]: [number, number, number, number];
|
||||
}
|
||||
|
||||
export const Result: Component<{
|
||||
result: Resource<ResultData>
|
||||
}> = ({ result: resultResource }) => {
|
||||
const countryData = () => Object.entries(resultResource()!)
|
||||
.sort((a, b) => a[0] > b[0] ? 1 : -1)
|
||||
.map(([field, values]) => <tr>
|
||||
<td>{field}</td>
|
||||
<td>{values[0]}</td>
|
||||
<td>{values[1]}</td>
|
||||
<td>{values[2]}</td>
|
||||
<td>{values[3]}</td>
|
||||
</tr>)
|
||||
|
||||
return (
|
||||
<div>
|
||||
<table>
|
||||
<thead><tr>
|
||||
<td>Param</td>
|
||||
<td>min</td>
|
||||
<td>max</td>
|
||||
<td>avg</td>
|
||||
<td>sum</td>
|
||||
</tr></thead>
|
||||
{ countryData() }
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import { FetchFiles } from "./FetchFiles";
|
||||
import { FileContent } from "./FileContent";
|
||||
import { FileNameList } from "./FileNameList";
|
||||
|
||||
export function FileManager() {
|
||||
export function Database() {
|
||||
const [getDate, setDate] = createSignal(new Date());
|
||||
const [getFileName, setFileName] = createSignal("");
|
||||
|
||||
@@ -15,7 +15,7 @@ export function FileManager() {
|
||||
|
||||
return (
|
||||
<div class="fileManager">
|
||||
<h2>File manager</h2>
|
||||
<h2>Database</h2>
|
||||
<div class="container">
|
||||
<div class="column">
|
||||
<DateList getDate={getDate} setDate={setDate} />
|
||||
Reference in New Issue
Block a user