Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cloud-assignment-02
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nils Petter Skålerud
cloud-assignment-02
Commits
e09bda4d
Commit
e09bda4d
authored
2 years ago
by
Marius Nersveen
Browse files
Options
Downloads
Patches
Plain Diff
Parsing CSV file is now only done once
parent
e6570cc7
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
cmd/main.go
+53
-0
53 additions, 0 deletions
cmd/main.go
handlers/currentPercentageHandler.go
+1
-52
1 addition, 52 deletions
handlers/currentPercentageHandler.go
handlers/historyHandler.go
+23
-0
23 additions, 0 deletions
handlers/historyHandler.go
utils/variables.go
+5
-1
5 additions, 1 deletion
utils/variables.go
with
82 additions
and
53 deletions
cmd/main.go
+
53
−
0
View file @
e09bda4d
...
@@ -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
(
...
...
This diff is collapsed.
Click to expand it.
handlers/currentPercentageHandler.go
+
1
−
52
View file @
e09bda4d
...
@@ -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
:
=
create
RenewableEnergyDataset
(
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
,
""
,
" "
)
...
...
This diff is collapsed.
Click to expand it.
handlers/historyHandler.go
+
23
−
0
View file @
e09bda4d
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
}
This diff is collapsed.
Click to expand it.
utils/variables.go
+
5
−
1
View file @
e09bda4d
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment