Skip to content

Commit

Permalink
Merge pull request #1 from mtenrero/react-app
Browse files Browse the repository at this point in the history
React app fully working
  • Loading branch information
mtenrero authored Aug 18, 2019
2 parents f67aaf1 + b840f52 commit e2383b3
Show file tree
Hide file tree
Showing 45 changed files with 25,286 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

vendor/
__debug_bin
react-ui/yarn.lock
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,35 @@

This reposiory contains the code for the Golang Cloud Functions/Lambdas used to parse and analyze your real consumptions provided by your electricity distribution company and gives you practical information about hourly and daily usage.

It also contains the React web-app already published with Github Pages for public usage: [https://mtenrero.github.io/electricity-bill-analyzer/](https://mtenrero.github.io/electricity-bill-analyzer/)

This will help you in making clever decissions about your electricity provider promotions and product selection.

## How it works?

You need to provide a CSV file from your electricity provider [Iberdrola Distribución](https://www.iberdroladistribucionelectrica.com/consumidores/inicio.html) to the published web app [https://mtenrero.github.io/electricity-bill-analyzer/](https://mtenrero.github.io/electricity-bill-analyzer/).

![Frontend File Selection Image](./readme-files/front.png)

Then a report summary will appear on your screen:

![Report Summary Web Image](./readme-files/report_front.png)

## The report I'm providing it's not compatible

Feel free to open an issue and I investigate the cause and I'll try to add your consumption schema to the analyzer. Please provide full information: country, and electricity brand distributor name.

## Technical data

The backend is written in pure Go and the frontend is made with React and Recharts lib.

### parser

For now, it's only compatible with Spanish Iberdrola (i-DE) electricity provider, which allows the contract owner download a CSV file with the summary of the power consumed.

The package has a byte array as argument with the content of the previously mentioned CSV file
The package has a byte array as argument with the content of the previously mentioned CSV file.

You can grab this file registering yourself into your electricity provider like: [Iberdrola Distribución](https://www.iberdroladistribucionelectrica.com/consumidores/inicio.html)

### cloud-functions

Expand Down
2 changes: 1 addition & 1 deletion analyze.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package electricity

import (
"encoding/json"
Expand Down
12 changes: 7 additions & 5 deletions analyzers/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ type Report struct {
}

type ReportWeekDayHourly struct {
WeekDay *int `json:"weekDay,omitempty"`
Analysis Report `json:"report,omitempty"`
WeekDay *int `json:"weekDay,omitempty"`
WeekString *string `json:"weekString,omitempty`
Analysis Report `json:"report,omitempty"`
}

type ReportValue struct {
Hour *int `json:"hour,omitempty"`
WeekDay *int `json:"weekDay,omitempty"`
Mean *float64 `json:"mean,omitempty"`
Hour *int `json:"hour,omitempty"`
WeekDay *int `json:"weekDay,omitempty"`
WeekString *string `json:"weekString,omitempty"`
Mean *float64 `json:"mean,omitempty"`
}
5 changes: 3 additions & 2 deletions analyzers/weekDayHours.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ func ReportWeekDaysHourly(consumptions *parser.Consumptions) []ReportWeekDayHour
for weekDay := 0; weekDay < 7; weekDay++ {
weekDayToSet := weekDay
reportweekDay := ReportWeekDayHourly{
WeekDay: &weekDayToSet,
Analysis: ReportHourly(consumptionsByWeekDay[weekDay]),
WeekDay: &weekDayToSet,
WeekString: WeekDayString(weekDayToSet),
Analysis: ReportHourly(consumptionsByWeekDay[weekDay]),
}
report[weekDay] = reportweekDay
}
Expand Down
2 changes: 1 addition & 1 deletion analyzers/weekDayHours_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestReportWeekDaysHourly(t *testing.T) {

report := ReportWeekDaysHourly(&consumptions)

if *report[0].Analysis[4].Mean != 5.895 {
if *report[0].Analysis.Analysis[4].Mean != 5.895 {
t.Fail()
}
}
14 changes: 11 additions & 3 deletions analyzers/weekday.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ package analyzers
import (
"strconv"
"strings"
"time"

"github.com/mtenrero/electricity-bill-analyzer/parser"
"github.com/thoas/go-funk"
"gonum.org/v1/gonum/stat"
)

// Analyzes the Consumption done based on weeekdays intervals and returns the consumption avg for each weekDay
// WeekDayString returns the weekDay English string
func WeekDayString(weekDay int) *string {
weekDayString := time.Weekday(weekDay).String()
return &weekDayString
}

// ReportWeekDays Analyzes the Consumption done based on weeekdays intervals and returns the consumption avg for each weekDay
func ReportWeekDays(consumptions *parser.Consumptions) Report {
analysis := make([]ReportValue, 7)

Expand All @@ -26,8 +33,9 @@ func ReportWeekDays(consumptions *parser.Consumptions) Report {

weekDayMean := stat.Mean(listConsumptions, nil)
analysis[weekDay] = ReportValue(ReportValue{
WeekDay: &weekDayToSet,
Mean: &weekDayMean,
WeekDay: &weekDayToSet,
WeekString: WeekDayString(weekDayToSet),
Mean: &weekDayMean,
})
}

Expand Down
4 changes: 1 addition & 3 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/bin/bash

if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
}
gcloud functions deploy FullMeanAnalysis-1 --runtime go111
59 changes: 59 additions & 0 deletions go-analyzer-function/analyze.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/mtenrero/electricity-bill-analyzer/analyzers"
"github.com/mtenrero/electricity-bill-analyzer/parser"
)

func FullMeanAnalysis(w http.ResponseWriter, r *http.Request) {

(w).Header().Set("Access-Control-Allow-Origin", "*")
(w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")

r.ParseMultipartForm(10 << 20)
file, _, err := r.FormFile("file")
if err != nil {
fmt.Println("Error Retrieving the File")
fmt.Println(err)
return
}
defer file.Close()

b, err := ioutil.ReadAll(file)
if err != nil {
http.Error(w, "Could not read request", http.StatusBadRequest)
}

consumptions, err := parser.ParseCSVBytes(b)

if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
w.Write([]byte("Error processing CSV data file. It may not have the appropriate format"))
}

weekDaysHourReport := analyzers.ReportWeekDaysHourly(&consumptions)
hourlyReport := analyzers.ReportHourly(&consumptions)
weekDaysReport := analyzers.ReportWeekDays(&consumptions)

fullReport := analyzers.ResponseReport{
WeekDaysReport: weekDaysReport,
HourlyReport: hourlyReport,
WeekDayHoursReport: weekDaysHourReport,
}

jsonReport, err := json.Marshal(fullReport)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error generating JSON report"))
}

w.Header().Add("Content-Type", "application/json")
w.Write(jsonReport)

}
8 changes: 4 additions & 4 deletions parser/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/csv"
"io"
"log"
"strconv"
)

Expand All @@ -15,7 +14,7 @@ func ParseCSVBytes(content []byte) ([]ConsumptionEntry, error) {
var consumptions = make([]ConsumptionEntry, 0)

var header []string
var err error
var glerror error

for {
record, err := reader.Read()
Expand All @@ -24,7 +23,8 @@ func ParseCSVBytes(content []byte) ([]ConsumptionEntry, error) {
break
}
if err != nil {
log.Fatal(err)
glerror = err
break
}
if header == nil {
header = record
Expand All @@ -41,5 +41,5 @@ func ParseCSVBytes(content []byte) ([]ConsumptionEntry, error) {
}
}

return consumptions, err
return consumptions, glerror
}
23 changes: 23 additions & 0 deletions react-ui/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
21 changes: 21 additions & 0 deletions react-ui/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}"
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\start"
}
]
}
68 changes: 68 additions & 0 deletions react-ui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.<br>
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br>
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.<br>
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.<br>
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br>
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

### Analyzing the Bundle Size

This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

### Making a Progressive Web App

This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

### Advanced Configuration

This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration

### Deployment

This section has moved here: https://facebook.github.io/create-react-app/docs/deployment

### `npm run build` fails to minify

This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
Loading

0 comments on commit e2383b3

Please sign in to comment.