-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
170 lines (143 loc) · 5.62 KB
/
state.go
File metadata and controls
170 lines (143 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2025 Matthew Gall <me@matthewgall.dev>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
)
type CachedSavingSessions struct {
Data *SavingSessionsResponse `json:"data"`
Timestamp time.Time `json:"timestamp"`
}
type CachedFreeElectricitySessions struct {
Data *FreeElectricitySessionsResponse `json:"data"`
Timestamp time.Time `json:"timestamp"`
}
type CachedCampaignStatus struct {
Data map[string]bool `json:"data"`
Timestamp time.Time `json:"timestamp"`
}
type CachedOctoPoints struct {
Data int `json:"data"`
Timestamp time.Time `json:"timestamp"`
}
type CachedWheelOfFortuneSpins struct {
Data *WheelOfFortuneSpins `json:"data"`
Timestamp time.Time `json:"timestamp"`
}
type CachedAccountInfo struct {
Data *AccountInfo `json:"data"`
Timestamp time.Time `json:"timestamp"`
}
type CachedMeterDevices struct {
Data []string `json:"data"`
Timestamp time.Time `json:"timestamp"`
}
type CachedUsageMeasurements struct {
Data []UsageMeasurement `json:"data"`
Timestamp time.Time `json:"timestamp"`
Days int `json:"days"` // Track how many days of data this represents
}
type AppState struct {
AlertStates map[string]*FreeElectricityAlertState `json:"alert_states"`
KnownSessions map[int]bool `json:"known_sessions"`
KnownFreeElectricitySessions map[string]bool `json:"known_free_electricity_sessions"`
CachedSavingSessions *CachedSavingSessions `json:"cached_saving_sessions,omitempty"`
CachedFreeElectricity *CachedFreeElectricitySessions `json:"cached_free_electricity,omitempty"`
CachedCampaignStatus *CachedCampaignStatus `json:"cached_campaign_status,omitempty"`
CachedOctoPoints *CachedOctoPoints `json:"cached_octo_points,omitempty"`
CachedWheelOfFortuneSpins *CachedWheelOfFortuneSpins `json:"cached_wheel_of_fortune_spins,omitempty"`
CachedAccountInfo *CachedAccountInfo `json:"cached_account_info,omitempty"`
CachedMeterDevices *CachedMeterDevices `json:"cached_meter_devices,omitempty"`
CachedUsageMeasurements *CachedUsageMeasurements `json:"cached_usage_measurements,omitempty"`
JWTToken string `json:"jwt_token,omitempty"`
JWTTokenExpiry time.Time `json:"jwt_token_expiry,omitempty"`
LastUpdated time.Time `json:"last_updated"`
}
func getStateFilePath(accountID string) (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user home directory: %w", err)
}
configDir := filepath.Join(homeDir, ".config", "octojoin")
if err := os.MkdirAll(configDir, 0755); err != nil {
return "", fmt.Errorf("failed to create config directory: %w", err)
}
// Use account ID in filename to separate cache per account
return filepath.Join(configDir, fmt.Sprintf("state_%s.json", accountID)), nil
}
func LoadState(accountID string) (*AppState, error) {
statePath, err := getStateFilePath(accountID)
if err != nil {
return nil, err
}
// If file doesn't exist, return empty state
if _, err := os.Stat(statePath); os.IsNotExist(err) {
return &AppState{
AlertStates: make(map[string]*FreeElectricityAlertState),
KnownSessions: make(map[int]bool),
KnownFreeElectricitySessions: make(map[string]bool),
LastUpdated: time.Now(),
}, nil
}
data, err := os.ReadFile(statePath)
if err != nil {
return nil, fmt.Errorf("failed to read state file: %w", err)
}
var state AppState
if err := json.Unmarshal(data, &state); err != nil {
return nil, fmt.Errorf("failed to parse state file: %w", err)
}
// Initialize maps if they're nil (for backward compatibility)
if state.AlertStates == nil {
state.AlertStates = make(map[string]*FreeElectricityAlertState)
}
if state.KnownSessions == nil {
state.KnownSessions = make(map[int]bool)
}
if state.KnownFreeElectricitySessions == nil {
state.KnownFreeElectricitySessions = make(map[string]bool)
}
return &state, nil
}
func (s *AppState) Save(accountID string) error {
statePath, err := getStateFilePath(accountID)
if err != nil {
return err
}
s.LastUpdated = time.Now()
data, err := json.MarshalIndent(s, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal state: %w", err)
}
if err := os.WriteFile(statePath, data, 0644); err != nil {
return fmt.Errorf("failed to write state file: %w", err)
}
return nil
}
func (s *AppState) IsCacheValid(cacheTime time.Time, maxAge time.Duration) bool {
return time.Since(cacheTime) < maxAge
}
func (s *AppState) CleanupExpiredSessions() {
// Clean up alert states for sessions that have ended
for code := range s.AlertStates {
// Clean up very old alert states
if time.Since(s.LastUpdated) > StateCleanupAge {
delete(s.AlertStates, code)
}
}
}