Skip to content
Snippets Groups Projects
Commit e09bda4d authored by Marius Nersveen's avatar Marius Nersveen
Browse files

Parsing CSV file is now only done once

parent e6570cc7
No related branches found
No related tags found
No related merge requests found
...@@ -3,15 +3,67 @@ package main ...@@ -3,15 +3,67 @@ package main
import ( import (
"Assignment02/handlers" "Assignment02/handlers"
"Assignment02/utils" "Assignment02/utils"
"encoding/csv"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
"strconv"
) )
func createRenewableEnergyDataset(data [][]string) []utils.StructTest {
// convert csv lines to array of structs
var RenewableEnergyDataset []utils.StructTest
for i, line := range data {
if i > 0 { // omit header line
var rec utils.StructTest
var err error
for j, field := range line {
if j == 0 {
rec.Entity = field
} else if j == 1 {
rec.Code = field
} else if j == 2 {
rec.Year, err = strconv.Atoi(field)
} else if j == 3 {
rec.Renewables, err = strconv.ParseFloat(field, 64)
if err != nil {
continue
}
}
}
RenewableEnergyDataset = append(RenewableEnergyDataset, rec)
}
}
return RenewableEnergyDataset
}
func main() { func main() {
serverState := utils.ServerState{} serverState := utils.ServerState{}
// Open the CSV file
fd, err := os.Open(utils.CSVFilePath)
if err != nil {
fmt.Println("Error opening CSV file.")
fmt.Println(err)
}
fmt.Println("Successfully opened the CSV file.")
defer fd.Close() // Remember to close the file at the end of the program
// Read the CSV file
fileReader := csv.NewReader(fd)
data, err := fileReader.ReadAll()
if err != nil {
fmt.Println("Error reading CSV file.")
fmt.Println(err)
}
//fmt.Println(data) // Print the CSV data to the console
//fmt.Fprintf(w, "%v", data) // Print the CSV data to the browser
// Assign successive lines of raw CSV data to fields of the created structs
utils.RenewableEnergyDataset = createRenewableEnergyDataset(data)
// Extract PORT variable from the environment variables0 // Extract PORT variable from the environment variables0
port := os.Getenv("PORT") port := os.Getenv("PORT")
...@@ -23,6 +75,7 @@ func main() { ...@@ -23,6 +75,7 @@ func main() {
http.HandleFunc(utils.DEFAULT_PATH, handlers.DefaultHandler) http.HandleFunc(utils.DEFAULT_PATH, handlers.DefaultHandler)
http.HandleFunc(utils.CURRENT_PATH, handlers.HandleGetRequestForCurrentPercentage) http.HandleFunc(utils.CURRENT_PATH, handlers.HandleGetRequestForCurrentPercentage)
http.HandleFunc(utils.HISTORY_PATH, handlers.HandleGetRequestForHistoricalPercentage)
// Add the // Add the
http.HandleFunc( http.HandleFunc(
......
...@@ -2,43 +2,13 @@ package handlers ...@@ -2,43 +2,13 @@ package handlers
import ( import (
"Assignment02/utils" "Assignment02/utils"
"encoding/csv"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os"
"strconv"
//"strings" //"strings"
) )
func createRenewableEnergyDataset(data [][]string) []utils.StructTest {
// convert csv lines to array of structs
var RenewableEnergyDataset []utils.StructTest
for i, line := range data {
if i > 0 { // omit header line
var rec utils.StructTest
var err error
for j, field := range line {
if j == 0 {
rec.Entity = field
} else if j == 1 {
rec.Code = field
} else if j == 2 {
rec.Year, err = strconv.Atoi(field)
} else if j == 3 {
rec.Renewables, err = strconv.ParseFloat(field, 64)
if err != nil {
continue
}
}
}
RenewableEnergyDataset = append(RenewableEnergyDataset, rec)
}
}
return RenewableEnergyDataset
}
// Dedicated handler for GET requests // Dedicated handler for GET requests
func HandleGetRequestForCurrentPercentage(w http.ResponseWriter, r *http.Request) { func HandleGetRequestForCurrentPercentage(w http.ResponseWriter, r *http.Request) {
...@@ -46,29 +16,8 @@ func HandleGetRequestForCurrentPercentage(w http.ResponseWriter, r *http.Request ...@@ -46,29 +16,8 @@ func HandleGetRequestForCurrentPercentage(w http.ResponseWriter, r *http.Request
//URLParts := strings.Split(r.URL.String(), "/") //URLParts := strings.Split(r.URL.String(), "/")
//countryName := URLParts[??] //countryName := URLParts[??]
// Open the CSV file
fd, err := os.Open(utils.CsvFilePath)
if err != nil {
fmt.Println("Error opening CSV file.")
fmt.Println(err)
}
fmt.Println("Successfully opened the CSV file.")
// Remember to close the file at the end of the program
defer fd.Close()
// Read the CSV file
fileReader := csv.NewReader(fd)
data, err := fileReader.ReadAll()
if err != nil {
fmt.Println("Error reading CSV file.")
fmt.Println(err)
}
//fmt.Println(data) // Print the CSV data to the console
//fmt.Fprintf(w, "%v", data) // Print the CSV data to the browser
// Assign successive lines of raw CSV data to fields of the created structs // Assign successive lines of raw CSV data to fields of the created structs
RenewableEnergyDataset := createRenewableEnergyDataset(data) var RenewableEnergyDataset = utils.RenewableEnergyDataset
// Convert an array of structs to JSON using marshaling functions from the encoding/json package // Convert an array of structs to JSON using marshaling functions from the encoding/json package
jsonData, err := json.MarshalIndent(RenewableEnergyDataset, "", " ") jsonData, err := json.MarshalIndent(RenewableEnergyDataset, "", " ")
......
package handlers package handlers
import (
"Assignment02/utils"
"encoding/json"
"fmt"
"log"
"net/http"
)
func HandleGetRequestForHistoricalPercentage(w http.ResponseWriter, r *http.Request) {
// Assign successive lines of raw CSV data to fields of the created structs
var RenewableEnergyDataset = utils.RenewableEnergyDataset
// Convert an array of structs to JSON using marshaling functions from the encoding/json package
jsonData, err := json.MarshalIndent(RenewableEnergyDataset, "", " ")
if err != nil {
log.Fatal(err)
}
//fmt.Println(string(jsonData)) // Print the JSON data to the console
fmt.Fprintf(w, "%v", string(jsonData)) // Print the JSON data to the browser
}
...@@ -13,4 +13,8 @@ func getCSVFilepath() string { ...@@ -13,4 +13,8 @@ func getCSVFilepath() string {
return absPath return absPath
} }
var CsvFilePath = getCSVFilepath() // Absolute path to the CSV file
var CSVFilePath = getCSVFilepath()
// "Storage" for the renewable energy data. It is set in the main function
var RenewableEnergyDataset []StructTest
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment