Skip to content

Commit

Permalink
Fixed reports
Browse files Browse the repository at this point in the history
  • Loading branch information
matisiekpl committed Sep 13, 2023
1 parent 65ba53f commit a2758d4
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 55 deletions.
11 changes: 7 additions & 4 deletions internal/model/plan.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package model

type Plan struct {
Name string `json:"name"`
Distance string `json:"distance"`
Duration string `json:"duration"`
Waypoints []Waypoint `json:"waypoints"`
Name string `json:"name"`
Distance string `json:"distance"`
Duration string `json:"duration"`
WindSpeed string `json:"windSpeed"`
WindDirection string `json:"windDirection"`
Speed string `json:"speed"`
Waypoints []Waypoint `json:"waypoints"`
}
1 change: 1 addition & 0 deletions internal/model/waypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

type Waypoint struct {
Name string `json:"name"`
Icao string `json:"icao"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
MagneticTrack string `json:"magneticTrack"`
Expand Down
7 changes: 7 additions & 0 deletions internal/model/weather.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

type Weather struct {
Name string
METAR string
TAF string
}
113 changes: 71 additions & 42 deletions internal/repository/assets/report.html
Original file line number Diff line number Diff line change
@@ -1,46 +1,75 @@
<html lang="pl">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">.
<title>Raport lotu</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
<style>
h1 {
text-align: center;
}
img,table {
margin-left: auto;
margin-right: auto;
min-width: 80%;
display: block;
}
</style>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Plan lotu</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
</head>
<body style="padding: 16px;">
<h1 class="title">Plan lotu</h1>
<table class="table is-bordered">
<tr>
<td></td>
<td>Punkt</td>
<td>NKDM</td>
<td>KM</td>
<td>Dystans</td>
<td>Prędkość podróżna</td>
<td>Czas</td>
</tr>
{{#each plan.Waypoints}}
<tr>
<td>{{inc @index}}</td>
<td>{{this.Name}}</td>
<td>{{this.MagneticTrack}}</td>
<td>{{this.MagneticHeading}}</td>
<td>{{this.Distance}}</td>
<td>{{this.GroundSpeed}}</td>
<td>{{this.Duration}}</td>
</tr>
{{/each}}
</table>
<div style="width: 100%;">
<img src="{{map}}" alt="Mapa">
<body class="font-sans bg-white">

<div class="container mx-auto p-8">
<header class="text-center">
<h1 class="text-3xl font-bold">Plan lotu</h1>
<p class="text-lg text-gray-600">Wiatr: {{plan.windSpeed}} / {{plan.windDirection}}</p>
<p class="text-lg text-gray-600">TAS: {{plan.speed}}</p>
<p class="text-lg text-gray-600">Trasa: {{plan.distance}} / {{plan.duration}}</p>
</header>

<div class="mb-8 text-center text-italic">
https://strefypowietrzne.pl/
</div>

<main>
<!-- Route Plan Table -->
<table class="w-full border-collapse border border-gray-300 mb-8">
<thead>
<tr>
<th class="border border-gray-300 p-2">#</th>
<th class="border border-gray-300 p-2">Punkt</th>
<th class="border border-gray-300 p-2">NKDM</th>
<th class="border border-gray-300 p-2">KM</th>
<th class="border border-gray-300 p-2">Dystans</th>
<th class="border border-gray-300 p-2">Prędkość podróżna</th>
<th class="border border-gray-300 p-2">Czas</th>
</tr>
</thead>
<tbody>
{{#each plan.Waypoints}}
<tr>
<td class="border border-gray-300 p-2">{{inc @index}}</td>
<td class="border border-gray-300 p-2">{{this.Name}}</td>
<td class="border border-gray-300 p-2">{{this.MagneticTrack}}</td>
<td class="border border-gray-300 p-2">{{this.MagneticHeading}}</td>
<td class="border border-gray-300 p-2">{{this.Distance}}</td>
<td class="border border-gray-300 p-2">{{this.GroundSpeed}}</td>
<td class="border border-gray-300 p-2">{{this.Duration}}</td>
</tr>
{{/each}}
</tbody>
</table>


<table class="w-full border-collapse border border-gray-300">
<thead>
<tr>
<th class="border border-gray-300 p-2">ICAO</th>
<th class="border border-gray-300 p-2">METAR</th>
<th class="border border-gray-300 p-2">TAF</th>
</tr>
</thead>
<tbody>
{{#each weather}}
<tr>
<td class="border border-gray-300 p-2">{{this.Name}}</td>
<td class="border border-gray-300 p-2">{{this.METAR}}</td>
<td class="border border-gray-300 p-2">{{this.TAF}}</td>
</tr>
{{/each}}
</tbody>
</table>
</main>
</div>

</body>
</html>
</html>
9 changes: 5 additions & 4 deletions internal/repository/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type ReportRepository interface {
GenerateReport(plan model.Plan) ([]byte, error)
GenerateReport(plan model.Plan, weather []model.Weather) ([]byte, error)
}

type reportRepository struct {
Expand All @@ -30,7 +30,7 @@ func newReportRepository() ReportRepository {
//go:embed assets/report.html
var reportTemplate string

func (r reportRepository) GenerateReport(plan model.Plan) ([]byte, error) {
func (r reportRepository) GenerateReport(plan model.Plan, weather []model.Weather) ([]byte, error) {
mapImage, err := r.createMapImage(plan)
var mapImageBuffer bytes.Buffer
err = png.Encode(&mapImageBuffer, mapImage)
Expand All @@ -39,8 +39,9 @@ func (r reportRepository) GenerateReport(plan model.Plan) ([]byte, error) {
}
encodedMapImage := "data:image/png;base64," + base64.StdEncoding.EncodeToString(mapImageBuffer.Bytes())
result, err := raymond.Render(reportTemplate, map[string]interface{}{
"plan": plan,
"map": encodedMapImage,
"plan": plan,
"map": encodedMapImage,
"weather": weather,
})
pdf, err := wkhtmltopdf.NewPDFGenerator()
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions internal/repository/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ type Repositories interface {
Publication() PublicationRepository
Notam() NotamRepository
Report() ReportRepository
Weather() WeatherRepository
}

type repositories struct {
publication PublicationRepository
notam NotamRepository
report ReportRepository
weather WeatherRepository
}

func (r repositories) Publication() PublicationRepository {
Expand All @@ -23,14 +25,19 @@ func (r repositories) Notam() NotamRepository {
func (r repositories) Report() ReportRepository {
return r.report
}
func (r repositories) Weather() WeatherRepository {
return r.weather
}

func NewRepositories() Repositories {
publicationRepository := newPublicationRepository()
notamRepository := newNotamRepository()
reportRepository := newReportRepository()
weatherRepository := newWeatherRepository()
return &repositories{
publication: publicationRepository,
notam: notamRepository,
report: reportRepository,
weather: weatherRepository,
}
}
43 changes: 43 additions & 0 deletions internal/repository/weather.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package repository

import (
"io"
"net/http"
"strings"
)

type WeatherRepository interface {
GetMETAR(icao string) string
GetTAF(icao string) string
}

type weatherRepository struct {
}

func newWeatherRepository() WeatherRepository {
return &weatherRepository{}
}

func (w weatherRepository) GetMETAR(icao string) string {
resp, err := http.Get("https://beta.aviationweather.gov/cgi-bin/data/metar.php?ids=" + icao)
if err != nil {
return ""
}
result, err := io.ReadAll(resp.Body)
if err != nil {
return ""
}
return strings.TrimSpace(string(result))
}

func (w weatherRepository) GetTAF(icao string) string {
resp, err := http.Get("https://beta.aviationweather.gov/cgi-bin/data/taf.php?ids=" + icao)
if err != nil {
return ""
}
result, err := io.ReadAll(resp.Body)
if err != nil {
return ""
}
return strings.TrimSpace(string(result))
}
20 changes: 16 additions & 4 deletions internal/service/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@ type ReportService interface {
}

type reportService struct {
reportRepository repository.ReportRepository
reportRepository repository.ReportRepository
weatherRepository repository.WeatherRepository
}

func newReportService(reportRepository repository.ReportRepository) ReportService {
return reportService{reportRepository}
func newReportService(reportRepository repository.ReportRepository, weatherRepository repository.WeatherRepository) ReportService {
return reportService{reportRepository, weatherRepository}
}

func (r reportService) GenerateReport(plan model.Plan) ([]byte, error) {
return r.reportRepository.GenerateReport(plan)
var weather []model.Weather
for _, waypoint := range plan.Waypoints {
if len(waypoint.Icao) == 4 {
metar := r.weatherRepository.GetMETAR(waypoint.Icao)
taf := r.weatherRepository.GetTAF(waypoint.Icao)
if metar != "" {
weather = append(weather, model.Weather{Name: waypoint.Icao, METAR: metar, TAF: taf})
}
}
}

return r.reportRepository.GenerateReport(plan, weather)
}
2 changes: 1 addition & 1 deletion internal/service/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type services struct {
func NewServices(repositories repository.Repositories) Services {
publicationService := newPublicationService(repositories.Publication())
notamService := newNotamService(repositories.Notam())
reportService := newReportService(repositories.Report())
reportService := newReportService(repositories.Report(), repositories.Weather())
return &services{
publication: publicationService,
report: reportService,
Expand Down

0 comments on commit a2758d4

Please sign in to comment.