From 3f62e9c5d855a6f320908b36a4bcff183d2e71ca Mon Sep 17 00:00:00 2001 From: Sosokker Date: Sun, 2 Feb 2025 04:58:56 +0700 Subject: [PATCH] feat: add endpoint to request data + function to normalize value --- cmd/data/data.csv | 130 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/data/test.csv | 6 ++- cmd/main.go | 43 +++++++++++++-- 3 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 cmd/data/data.csv diff --git a/cmd/data/data.csv b/cmd/data/data.csv new file mode 100644 index 0000000..460e118 --- /dev/null +++ b/cmd/data/data.csv @@ -0,0 +1,130 @@ +Place,Latitude,Longitude +AK,63.588753,-154.493062 +AL,32.318231,-86.902298 +AR,35.20105,-91.831833 +AZ,34.048928,-111.093731 +CA,36.778261,-119.417932 +CO,39.550051,-105.782067 +CT,41.603221,-73.087749 +DC,38.905985,-77.033418 +DE,38.910832,-75.52767 +FL,27.664827,-81.515754 +GA,32.157435,-82.907123 +HI,19.898682,-155.665857 +IA,41.878003,-93.097702 +ID,44.068202,-114.742041 +IL,40.633125,-89.398528 +IN,40.551217,-85.602364 +KS,39.011902,-98.484246 +KY,37.839333,-84.270018 +LA,31.244823,-92.145024 +MA,42.407211,-71.382437 +MD,39.045755,-76.641271 +ME,45.253783,-69.445469 +MI,44.314844,-85.602364 +MN,46.729553,-94.6859 +MO,37.964253,-91.831833 +MS,32.354668,-89.398528 +MT,46.879682,-110.362566 +NC,35.759573,-79.0193 +ND,47.551493,-101.002012 +NE,41.492537,-99.901813 +NH,43.193852,-71.572395 +NJ,40.058324,-74.405661 +NM,34.97273,-105.032363 +NV,38.80261,-116.419389 +NY,43.299428,-74.217933 +OH,40.417287,-82.907123 +OK,35.007752,-97.092877 +OR,43.804133,-120.554201 +PA,41.203322,-77.194525 +PR,18.220833,-66.590149 +RI,41.580095,-71.477429 +SC,33.836081,-81.163725 +SD,43.969515,-99.901813 +TN,35.517491,-86.580447 +TX,31.968599,-99.901813 +UT,39.32098,-111.093731 +VA,37.431573,-78.656894 +VT,44.558803,-72.577841 +WA,47.751074,-120.740139 +WI,43.78444,-88.787868 +WV,38.597626,-80.454903 +WY,43.075968,-107.290284 +กรุงเทพมหานคร,13.7278956,100.52412349999997 +กระบี่,8.0862997,98.90628349999997 +กาญจนบุรี,14.0227797,99.53281149999998 +กาฬสินธุ์,16.4314078,103.5058755 +กำแพงเพชร,16.4827798,99.52266179999992 +ขอนแก่น,16.4419355,102.8359921 +จันทบุรี,12.61134,102.10385459999998 +ฉะเชิงเทรา,13.6904194,101.07795959999999 +ชลบุรี,13.3611431,100.98467170000004 +ชัยนาท,15.1851971,100.12512500000003 +ชัยภูมิ,15.8068173,102.03150270000003 +ชุมพร,10.4930496,99.18001989999993 +เชียงราย,19.9071656,99.83095500000002 +เชียงใหม่,18.7877477,98.99313110000003 +ตรัง,7.5593851,99.61100650000003 +ตราด,12.2427563,102.51747339999997 +ตาก,16.8839901,99.12584979999997 +นครนายก,14.2069466,101.21305110000003 +นครปฐม,13.8199206,100.06216760000007 +นครพนม,17.392039,104.76955079999993 +นคราชสีมา,14.9798997,102.09776929999998 +นครศรีธรรมราช,8.4303975,99.96312190000003 +นครสวรรค์,15.6930072,100.12255949999997 +นนทบุรี,13.8621125,100.51435279999998 +นราธิวาส,6.4254607,101.82531429999995 +น่าน,18.7756318,100.77304170000002 +บุรีรัมย์,14.9930017,103.10291910000001 +ปทุมธานี,14.0208391,100.52502759999993 +ประจวบคีรีขันธ์,11.812367,99.79732709999996 +ปราจีนบุรี,14.0509704,101.37274389999993 +ปัตตานี,6.869484399999999,101.25048259999994 +พระนครศรีอยุธยา,14.3532128,100.56895989999998 +พะเยา,19.1664789,99.9019419 +พังงา,8.4407456,98.51930319999997 +พัทลุง,7.6166823,100.07402309999998 +พิจิตร,16.4429516,100.34823289999997 +พิษณุโลก,16.8298048,100.26149150000003 +เพชรบุรี,13.1111601,99.93913069999996 +เพชรบูรณ์,16.4189807,101.15509259999999 +แพร่,18.1445774,100.14028310000003 +ภูเก็ต,7.9810496,98.36388239999997 +มหาสารคาม,16.1850896,103.30264609999995 +มุกดาหาร,16.542443,104.72091509999996 +แม่ฮ่องสอน,19.2990643,97.96562259999996 +ยโสธร,15.792641,104.14528270000005 +ยะลา,6.541147,101.28039469999999 +ร้อยเอ็ด,16.0538196,103.65200359999994 +ระนอง,9.9528702,98.60846409999999 +ระยอง,12.6833115,101.23742949999996 +ราชบุรี,13.5282893,99.81342110000003 +ลพบุรี,14.7995081,100.65337060000002 +ลำปาง,18.2888404,99.49087399999996 +ลำพูน,18.5744606,99.0087221 +เลย,17.4860232,101.72230020000006 +ศรีสะเกษ,15.1186009,104.32200949999992 +สกลนคร,17.1545995,104.1348365 +สงขลา,7.1756004,100.61434699999995 +สตูล,6.6238158,100.06737440000006 +สมุทรปราการ,13.5990961,100.59983190000003 +สมุทรสงคราม,13.4098217,100.00226450000002 +สมุทรสาคร,13.5475216,100.27439559999993 +สระแก้ว,13.824038,102.0645839 +สระบุรี,14.5289154,100.91014210000003 +สิงห์บุรี,14.8936253,100.39673140000002 +สุโขทัย,17.0055573,99.82637120000004 +สุพรรณบุรี,14.4744892,100.11771279999994 +สุราษฎร์ธานี,9.1382389,99.32174829999997 +สุรินทร์,14.882905,103.49371070000007 +หนองคาย,17.8782803,102.74126380000007 +หนองบัวลำภู,17.2218247,102.42603680000002 +อ่างทอง,14.5896054,100.45505200000002 +อำนาจเจริญ,15.8656783,104.62577740000006 +อุดรธานี,17.4138413,102.78723250000007 +อุตรดิตถ์,17.6200886,100.09929420000003 +อุทัยธานี,15.3835001,100.02455269999996 +อุบลราชธานี,15.2286861,104.85642170000006 +บึงกาฬ,18.3609104,103.64644629999998 \ No newline at end of file diff --git a/cmd/data/test.csv b/cmd/data/test.csv index 3d3d558..ca85255 100644 --- a/cmd/data/test.csv +++ b/cmd/data/test.csv @@ -1,2 +1,4 @@ -กรุงเทพมหานคร,13.7278956,100.52412349999997 -กระบี่,8.0862997,98.90628349999997 \ No newline at end of file +Place,Latitude,Longitude +AK,63.588753,-154.493062 +AL,32.318231,-86.902298 +AR,35.20105,-91.831833 \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index a0a39fe..0d7c77c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -31,7 +31,7 @@ type DataEntry struct { Coord Coordinate `json:"coord"` Weather []Weather `json:"weather"` Rain struct { - PerHour float64 `json:"1h"` + PerHour float32 `json:"1h"` } `json:"rain"` } @@ -47,6 +47,13 @@ func (d DataEntry) String() string { return fmt.Sprintf("Place Name: %s\nCoordinate: %s\nWeather: %s\nRain per Hour: %f\n", d.Name, d.Coord, d.Weather, d.Rain.PerHour) } +func (d *DataEntry) normalizeRainPerHour(min, max float32) { + if max == 0 { + return + } + d.Rain.PerHour = (float32(d.Rain.PerHour) - min) / (max - min) * 100 +} + func loadEnv() string { apiKeys, err := godotenv.Read() if err != nil { @@ -124,14 +131,44 @@ func readCoordData(filepath string, coordCh chan<- Coordinate) { close(coordCh) } -func main() { +// lat, lon, rain+place +// state-id, rainfall (scale 100) + +func rawDataHandler(w http.ResponseWriter, r *http.Request) { apiKey := loadEnv() coordCh := make(chan Coordinate) go readCoordData("data/test.csv", coordCh) + var max float32 = 0.0 + var min float32 = 10000000.0 + + item := strings.TrimSpace(r.URL.Query().Get("scale")) + scale, _ := strconv.ParseBool(item) + + var entries []DataEntry for coord := range coordCh { entry := fetchWeatherData(coord.Lat, coord.Lon, apiKey) - fmt.Println(entry) + if entry.Rain.PerHour > max { + max = entry.Rain.PerHour + } + if entry.Rain.PerHour < min { + min = entry.Rain.PerHour + } + + if scale { + entry.normalizeRainPerHour(min, max) + } + entries = append(entries, entry) + } + + if err := json.NewEncoder(w).Encode(&entries); err != nil { + http.Error(w, "Failed to encode JSON", http.StatusInternalServerError) + return } } + +func main() { + http.HandleFunc("/api/data", rawDataHandler) // /api/data?scale=100 + log.Fatal(http.ListenAndServe(":8080", nil)) +}