-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,417 additions
and
1,113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* OpenBmclAPI (Golang Edition) | ||
* Copyright (C) 2024 Kevin Z <zyxkad@gmail.com> | ||
* All rights reserved | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
type ConfigHandler interface { | ||
json.Marshaler | ||
json.Unmarshaler | ||
UnmarshalYAML(data []byte) error | ||
MarshalJSONPath(path string) ([]byte, error) | ||
UnmarshalJSONPath(path string, data []byte) error | ||
|
||
Fingerprint() string | ||
DoLockedAction(fingerprint string, callback func(ConfigHandler) error) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* OpenBmclAPI (Golang Edition) | ||
* Copyright (C) 2024 Kevin Z <zyxkad@gmail.com> | ||
* All rights reserved | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
const ( | ||
RealAddrCtxKey = "handle.real.addr" | ||
RealPathCtxKey = "handle.real.path" | ||
AccessLogExtraCtxKey = "handle.access.extra" | ||
) | ||
|
||
func GetRequestRealAddr(req *http.Request) string { | ||
addr, _ := req.Context().Value(RealAddrCtxKey).(string) | ||
return addr | ||
} | ||
|
||
func GetRequestRealPath(req *http.Request) string { | ||
return req.Context().Value(RealPathCtxKey).(string) | ||
} | ||
|
||
func SetAccessInfo(req *http.Request, key string, value any) { | ||
if info, ok := req.Context().Value(AccessLogExtraCtxKey).(map[string]any); ok { | ||
info[key] = value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* OpenBmclAPI (Golang Edition) | ||
* Copyright (C) 2024 Kevin Z <zyxkad@gmail.com> | ||
* All rights reserved | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type StatsManager interface { | ||
GetStatus() StatusData | ||
// if name is empty then gets the overall access data | ||
GetAccessStat(name string) *AccessStatData | ||
} | ||
|
||
type StatusData struct { | ||
StartAt time.Time `json:"startAt"` | ||
Clusters []string `json:"clusters"` | ||
Storages []string `json:"storages"` | ||
} | ||
|
||
type statInstData struct { | ||
Hits int32 `json:"hits"` | ||
Bytes int64 `json:"bytes"` | ||
} | ||
|
||
func (d *statInstData) update(o *statInstData) { | ||
d.Hits += o.Hits | ||
d.Bytes += o.Bytes | ||
} | ||
|
||
// statTime always save a UTC time | ||
type statTime struct { | ||
Hour int `json:"hour"` | ||
Day int `json:"day"` | ||
Month int `json:"month"` | ||
Year int `json:"year"` | ||
} | ||
|
||
func makeStatTime(t time.Time) (st statTime) { | ||
t = t.UTC() | ||
st.Hour = t.Hour() | ||
y, m, d := t.Date() | ||
st.Day = d - 1 | ||
st.Month = (int)(m) - 1 | ||
st.Year = y | ||
return | ||
} | ||
|
||
func (t statTime) IsLastDay() bool { | ||
return time.Date(t.Year, (time.Month)(t.Month+1), t.Day+1+1, 0, 0, 0, 0, time.UTC).Day() == 1 | ||
} | ||
|
||
type ( | ||
statDataHours = [24]statInstData | ||
statDataDays = [31]statInstData | ||
statDataMonths = [12]statInstData | ||
) | ||
|
||
type accessStatHistoryData struct { | ||
Hours statDataHours `json:"hours"` | ||
Days statDataDays `json:"days"` | ||
Months statDataMonths `json:"months"` | ||
} | ||
|
||
type AccessStatData struct { | ||
Date statTime `json:"date"` | ||
accessStatHistoryData | ||
Prev accessStatHistoryData `json:"prev"` | ||
Years map[string]statInstData `json:"years"` | ||
|
||
Accesses map[string]int `json:"accesses"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.