Skip to content

Commit eab09df

Browse files
committed
Document methods.
1 parent c2c9d5c commit eab09df

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

internal/app/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import (
1212
"time"
1313
)
1414

15+
// OAuth holds a json web token parsed from the auth response.
1516
type OAuth struct {
1617
AuthorizationToken string `json:"authorizationToken"`
1718
}
1819

20+
// Auth authenticates with the api and returns a json web token for use with the api.
1921
func Auth(config UtilityConfig) (string, error) {
2022
client := &http.Client{}
2123
postData := fmt.Sprintf("userId=%s&password=%s", config.Username, config.Password)
@@ -43,6 +45,7 @@ func Auth(config UtilityConfig) (string, error) {
4345
return oauth.AuthorizationToken, nil
4446
}
4547

48+
// PollRequest is request information sent to the api to fetch data.
4649
type PollRequest struct {
4750
TimeFrame string `json:"timeFrame"`
4851
UserId string `json:"userId"`
@@ -55,6 +58,9 @@ type PollRequest struct {
5558
EndDateTime int64 `json:"endDateTime"`
5659
}
5760

61+
// FetchData calls the api to get data for a particular time period.
62+
// Note that the api may return a PENDING status or actual data.
63+
// However, parsing of the response is handled in ParseReader.
5864
func FetchData(start, end time.Time, config UtilityConfig, jwt string) (io.ReadCloser, error) {
5965
client := http.Client{}
6066
pollRequest := PollRequest{

internal/app/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"gopkg.in/yaml.v3"
1313
)
1414

15+
// InfluxConfig is the config for the VictoriaMetrics connection, via the influxdb client.
1516
type InfluxConfig struct {
1617
Host string `yaml:"host"`
1718
User string `yaml:"user"`
@@ -20,6 +21,10 @@ type InfluxConfig struct {
2021
Insecure bool `yaml:"insecure"`
2122
}
2223

24+
// UtilityConfig is the config for Novec.
25+
// Password is hashed or encrypted in some unknown way, and must be retrieved from your browser. (TBD)
26+
// Account is your account number, available on your bill.
27+
// ServiceLocation appears to be an internal number, and must be retrieved from your browser. (TBD)
2328
type UtilityConfig struct {
2429
ApiUrl string `yaml:"api_url"`
2530
Username string `yaml:"username"`
@@ -28,12 +33,14 @@ type UtilityConfig struct {
2833
ServiceLocation string `yaml:"service_location"`
2934
}
3035

36+
// Config is the config format for electric-usage-downloader
3137
type Config struct {
3238
ExtractDays int `yaml:"extract_days"`
3339
Utility UtilityConfig `yaml:"utility"`
3440
InfluxDB InfluxConfig `yaml:"influxdb"`
3541
}
3642

43+
// Main runs the program.
3744
func Main() error {
3845
configFlag := flag.String("config", "config.yaml", "Config file")
3946
startFlag := flag.String("start", "", "Start date of period to extract from electric co.")

internal/app/metrics.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/influxdata/influxdb-client-go/v2/api/write"
1010
)
1111

12+
// WriteMetrics writes ElectricUsage data to victoriametrics.
13+
// This method writes a point every minute instead of following the time span of ElectricUsage.
1214
func WriteMetrics(records []ElectricUsage, config InfluxConfig) error {
1315
opts := influxdb2.DefaultOptions()
1416
if config.Insecure {

internal/app/parser.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,62 @@ import (
99
"time"
1010
)
1111

12+
// ElectricUsage contains usage and cost information for a defined period of time.
1213
type ElectricUsage struct {
1314
StartTime time.Time
1415
EndTime time.Time
1516
WattHours int64
1617
CostInCents int64
1718
}
1819

20+
// Response holds the parsed response from the Novec poll api.
21+
// If Status is "PENDING", this means we need to make the same request again
22+
// as data is still being prepared.
1923
type Response struct {
2024
Status string `json:"status"`
2125
Data map[string][]NovecData `json:"data"`
2226
}
2327

28+
// NovecData holds parsed response data from the Novec poll api.
29+
// It holds the Type of data ("USAGE" or "COST"), and the Series
2430
type NovecData struct {
2531
Type string `json:"type"`
2632
Series []NovecSeries `json:"series"`
2733
}
2834

35+
// NovecSeries holds parsed response data from the Novec poll api.
36+
// It holds a list of NovecPoints.
2937
type NovecSeries struct {
3038
Data []NovecPoint `json:"data"`
3139
}
3240

41+
// NovecPoint holds parsed response data from the Novec poll api.
42+
// It holds a timestamp, UnixMillis, which is actually in the America/New_York timezone instead of
43+
// UTC as it should be.
44+
// It also holds the Value of the point in dollars or kWh.
3345
type NovecPoint struct {
3446
UnixMillis int64 `json:"x"`
3547
Value float64 `json:"y"`
3648
}
3749

50+
// RetryableError is an error that indicates to the retryer in Main that another
51+
// poll request to FetchData should be made.
3852
type RetryableError struct {
3953
Msg string
4054
}
4155

56+
// NewRetryableError creates a RetryableError
4257
func NewRetryableError(msg string) *RetryableError {
4358
return &RetryableError{Msg: msg}
4459
}
4560

61+
// Error implements type error for RetryableError.
4662
func (t *RetryableError) Error() string {
4763
return t.Msg
4864
}
4965

66+
// ParseReader parses the json response received in FetchData from the Novec poll api.
67+
// It can return a normal error, a RetryableError, or parsed ElectricUsage.
5068
func ParseReader(reader io.ReadCloser) ([]ElectricUsage, error) {
5169
defer func() {
5270
if err := reader.Close(); err != nil {

0 commit comments

Comments
 (0)