/
statistic_analysis_controller.go
186 lines (161 loc) · 7.54 KB
/
statistic_analysis_controller.go
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package plantcontroller
import (
config "github.com/paulmuenzner/powerplantmanager/config"
model "github.com/paulmuenzner/powerplantmanager/models"
errHandler "github.com/paulmuenzner/powerplantmanager/services/errorHandler"
responsehandler "github.com/paulmuenzner/powerplantmanager/services/responseHandler"
cookie "github.com/paulmuenzner/powerplantmanager/utils/cookies"
logger "github.com/paulmuenzner/powerplantmanager/utils/logs"
mongodb "github.com/paulmuenzner/powerplantmanager/utils/mongoDB"
"github.com/paulmuenzner/powerplantmanager/utils/statistic"
"net/http"
"time"
"go.mongodb.org/mongo-driver/bson"
)
func GetPlantStatistics(mongoDBInterface *mongodb.MethodInterface) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
//////////////////////////////////////////////////////
///////// SETUP //////////////////////////////////////
//
neutralResponseErr := "We appologize. Statistic evaluation currently not possible due to github.com/paulmuenzner/powerplantmanager updates. Please, try again later."
// Extracting the userId value from the claim data and converting its type to string
claimData, err := cookie.GetCookieData(r, config.AuthCookieName)
if err != nil {
logger.GetLogger().Error("Cannot extract data/claim from cookie in validator 'GetPlantStatisticsValidation'. Error: ", err, "Cookie name: ", config.AuthCookieName)
// Neutral message
errHandler.HandleError(w, neutralResponseErr, errHandler.InternalServerError)
return
}
userID, ok := claimData["data"].(map[string]interface{})["userId"].(string)
if !ok {
logger.GetLogger().Error("Cannot parse and access userId from 'claimData[\"data\"]' in 'GetPlantStatistics()'.")
errHandler.HandleError(w, neutralResponseErr, errHandler.InternalServerError)
return
}
//////////////////////////////////////////////
// REQUEST BODY VALIDATION ///////////////////
//
// Access the parsed JSON data from the context, here req
dataBody, ok := r.Context().Value("requestBody").(map[string]interface{})
if !ok {
logger.GetLogger().Error("Cannot parse and access JSON of requestBody in 'GetPlantStatistics'.")
errHandler.HandleError(w, neutralResponseErr, errHandler.InternalServerError)
return
}
///////////////////////////////////////////////////////
// Get start and end date for period to analyze
//////////
// Start time period
dateStartString, dateStartValid := dataBody["dateStart"].(string)
if !dateStartValid {
errHandler.HandleError(w, neutralResponseErr, errHandler.InternalServerError)
return
}
// End time period
dateEndString, dateEndValid := dataBody["dateEnd"].(string)
if !dateEndValid {
errHandler.HandleError(w, neutralResponseErr, errHandler.InternalServerError)
return
}
// Parse the string into a time.Time object
dateStart, err := time.Parse(time.RFC3339Nano, dateStartString)
if err != nil {
logger.GetLogger().Error("Cannot parse dateStartString in 'GetPlantStatistics'. Error: ", err)
errHandler.HandleError(w, neutralResponseErr, errHandler.InternalServerError)
return
}
dateEnd, err := time.Parse(time.RFC3339Nano, dateEndString)
if err != nil {
logger.GetLogger().Error("Cannot parse dateEndString in 'GetPlantStatistics'. Error: ", err)
errHandler.HandleError(w, neutralResponseErr, errHandler.InternalServerError)
return
}
// Get plant-related collection name
collectionNameLogger, ok := r.Context().Value("plantCollectionNameLogger").(string)
if !ok {
logger.GetLogger().Error("Cannot parse and access JSON of collectionNameLogger in 'GetPlantStatistics'.")
errHandler.HandleError(w, neutralResponseErr, errHandler.InternalServerError)
return
}
//////////////////////////////////////////////
// REQUEST BODY VALIDATION ///////////////////
//
// Query plant logs
filter := bson.M{
"created_at": bson.M{
"$gte": dateStart,
"$lt": dateEnd,
},
}
var plantLogger []model.PlantLogger
var sortCriteria bson.D = bson.D{}
err = mongoDBInterface.RepositoryInterface.FindManyInMongo(config.DatabaseNamePlantLogger, filter, collectionNameLogger, sortCriteria, &plantLogger)
if err != nil {
log := "User with _id " + userID + " requested non-existing plant logger for collection name " + collectionNameLogger + " in controller 'GetPlantStatistics'."
logger.GetLogger().Error(log, err)
errHandler.HandleError(w, "Requested plant not available.", errHandler.BadRequest)
return
}
// Restructure retrieved data for statistical analysis
solarRadiation := []float64{}
powerOutputs := []float64{}
for _, plant := range plantLogger {
solarRadiation = append(solarRadiation, plant.SolarRadiation)
powerOutputs = append(powerOutputs, plant.PowerOutput)
}
// Mean
meanPowerOutputs, _ := statistic.Mean(powerOutputs)
meanSolarRadiation, _ := statistic.Mean(solarRadiation)
// Median
medianPowerOutputs, _ := statistic.Median(powerOutputs, 0.5, nil)
medianSolarRadiation, _ := statistic.Median(solarRadiation, 0.5, nil)
// Variance
variancePowerOutputs, _ := statistic.Variance(powerOutputs, nil)
varianceSolarRadiation, _ := statistic.Variance(solarRadiation, nil)
// Variance
standardDeviationPowerOutputs, _ := statistic.StandardDeviation(powerOutputs, nil)
standardDeviationSolarRadiation, _ := statistic.StandardDeviation(solarRadiation, nil)
// Skewness
skewnessPowerOutputs, _ := statistic.Skewness(powerOutputs, nil)
skewnessSolarRadiation, _ := statistic.Skewness(solarRadiation, nil)
// Quantile
quantile25PowerOutputs, quantile75PowerOutputs, iqrPowerOutputs, lowerBoundPowerOutputs, upperBoundPowerOutputs, outliersPowerOutputs, quantile90PowerOutputs, quantile95PowerOutputs := statistic.Quantile(powerOutputs)
quantile25SolarRadiation, quantile75SolarRadiation, iqrSolarRadiation, lowerBoundSolarRadiation, upperBoundSolarRadiation, outliersSolarRadiation, quantile90SolarRadiation, quantile95SolarRadiation := statistic.Quantile(solarRadiation)
// Correlation
correlationPowerSolar, _ := statistic.Correlation(powerOutputs, solarRadiation)
data := map[string]interface{}{
"powerOutput": map[string]interface{}{
"mean": meanPowerOutputs,
"variance": variancePowerOutputs,
"median": medianPowerOutputs,
"standardDeviation": standardDeviationPowerOutputs,
"skewness": skewnessPowerOutputs,
"quantile25": quantile25PowerOutputs,
"quantile75": quantile75PowerOutputs,
"quantile90": quantile90PowerOutputs,
"quantile95": quantile95PowerOutputs,
"interquartileRange": iqrPowerOutputs,
"lowerBound": lowerBoundPowerOutputs,
"upperBound": upperBoundPowerOutputs,
"outliers": outliersPowerOutputs,
},
"solarRadiation": map[string]interface{}{
"mean": meanSolarRadiation,
"variance": varianceSolarRadiation,
"median": medianSolarRadiation,
"standardDeviation": standardDeviationSolarRadiation,
"skewness": skewnessSolarRadiation,
"quantile25": quantile25SolarRadiation,
"quantile75": quantile75SolarRadiation,
"quantile90": quantile90SolarRadiation,
"quantile95": quantile95SolarRadiation,
"interquartileRange": iqrSolarRadiation,
"lowerBound": lowerBoundSolarRadiation,
"upperBound": upperBoundSolarRadiation,
"outliers": outliersSolarRadiation,
},
"correlationPowerSolar": correlationPowerSolar,
}
responsehandler.HandleSuccess(w, "Requested statistical data retrieved.", responsehandler.OK, data)
}
}