mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-25 06:22:24 +02:00
Log hishtory version in internal analytics
This commit is contained in:
parent
f533004d6b
commit
a85315d366
@ -44,6 +44,7 @@ type UsageData struct {
|
|||||||
NumEntriesHandled int `json:"num_entries_handled"`
|
NumEntriesHandled int `json:"num_entries_handled"`
|
||||||
LastQueried time.Time `json:"last_queried"`
|
LastQueried time.Time `json:"last_queried"`
|
||||||
NumQueries int `json:"num_queries"`
|
NumQueries int `json:"num_queries"`
|
||||||
|
Version string `json:"version"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRequiredQueryParam(r *http.Request, queryParam string) string {
|
func getRequiredQueryParam(r *http.Request, queryParam string) string {
|
||||||
@ -54,16 +55,24 @@ func getRequiredQueryParam(r *http.Request, queryParam string) string {
|
|||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getHishtoryVersion(r *http.Request) string {
|
||||||
|
return r.Header.Get("X-Hishtory-Version")
|
||||||
|
}
|
||||||
|
|
||||||
func updateUsageData(r *http.Request, userId, deviceId string, numEntriesHandled int, isQuery bool) {
|
func updateUsageData(r *http.Request, userId, deviceId string, numEntriesHandled int, isQuery bool) {
|
||||||
var usageData []UsageData
|
var usageData []UsageData
|
||||||
GLOBAL_DB.Where("user_id = ? AND device_id = ?", userId, deviceId).Find(&usageData)
|
GLOBAL_DB.Where("user_id = ? AND device_id = ?", userId, deviceId).Find(&usageData)
|
||||||
if len(usageData) == 0 {
|
if len(usageData) == 0 {
|
||||||
GLOBAL_DB.Create(&UsageData{UserId: userId, DeviceId: deviceId, LastUsed: time.Now(), NumEntriesHandled: numEntriesHandled})
|
GLOBAL_DB.Create(&UsageData{UserId: userId, DeviceId: deviceId, LastUsed: time.Now(), NumEntriesHandled: numEntriesHandled, Version: getHishtoryVersion(r)})
|
||||||
} else {
|
} else {
|
||||||
|
usage := usageData[0]
|
||||||
GLOBAL_DB.Model(&UsageData{}).Where("user_id = ? AND device_id = ?", userId, deviceId).Update("last_used", time.Now()).Update("last_ip", getRemoteAddr(r))
|
GLOBAL_DB.Model(&UsageData{}).Where("user_id = ? AND device_id = ?", userId, deviceId).Update("last_used", time.Now()).Update("last_ip", getRemoteAddr(r))
|
||||||
if numEntriesHandled > 0 {
|
if numEntriesHandled > 0 {
|
||||||
GLOBAL_DB.Exec("UPDATE usage_data SET num_entries_handled = COALESCE(num_entries_handled, 0) + ? WHERE user_id = ? AND device_id = ?", numEntriesHandled, userId, deviceId)
|
GLOBAL_DB.Exec("UPDATE usage_data SET num_entries_handled = COALESCE(num_entries_handled, 0) + ? WHERE user_id = ? AND device_id = ?", numEntriesHandled, userId, deviceId)
|
||||||
}
|
}
|
||||||
|
if usage.Version != getHishtoryVersion(r) {
|
||||||
|
GLOBAL_DB.Exec("UPDATE usage_data SET version = ? WHERE user_id = ? AND device_id = ?", getHishtoryVersion(r), userId, deviceId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if isQuery {
|
if isQuery {
|
||||||
GLOBAL_DB.Exec("UPDATE usage_data SET num_queries = COALESCE(num_queries, 0) + 1, last_queried = ? WHERE user_id = ? AND device_id = ?", time.Now(), userId, deviceId)
|
GLOBAL_DB.Exec("UPDATE usage_data SET num_queries = COALESCE(num_queries, 0) + 1, last_queried = ? WHERE user_id = ? AND device_id = ?", time.Now(), userId, deviceId)
|
||||||
@ -79,7 +88,8 @@ func usageStatsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
MAX(usage_data.last_used) as last_active,
|
MAX(usage_data.last_used) as last_active,
|
||||||
COALESCE(STRING_AGG(DISTINCT usage_data.last_ip, ' ') FILTER (WHERE usage_data.last_ip != 'Unknown'), 'Unknown') as ip_addresses,
|
COALESCE(STRING_AGG(DISTINCT usage_data.last_ip, ' ') FILTER (WHERE usage_data.last_ip != 'Unknown'), 'Unknown') as ip_addresses,
|
||||||
COALESCE(SUM(usage_data.num_queries), 0) as num_queries,
|
COALESCE(SUM(usage_data.num_queries), 0) as num_queries,
|
||||||
COALESCE(MAX(usage_data.last_queried), 'January 1, 1970') as last_queried
|
COALESCE(MAX(usage_data.last_queried), 'January 1, 1970') as last_queried,
|
||||||
|
STRING_AGG(DISTINCT usage_data.version, ' ') as versions
|
||||||
FROM devices
|
FROM devices
|
||||||
INNER JOIN usage_data ON devices.device_id = usage_data.device_id
|
INNER JOIN usage_data ON devices.device_id = usage_data.device_id
|
||||||
GROUP BY devices.user_id
|
GROUP BY devices.user_id
|
||||||
@ -89,7 +99,7 @@ func usageStatsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
tbl := table.New("Registration Date", "Num Devices", "Num Entries", "Num Queries", "Last Active", "Last Query", "IPs")
|
tbl := table.New("Registration Date", "Num Devices", "Num Entries", "Num Queries", "Last Active", "Last Query", "Versions", "IPs")
|
||||||
tbl.WithWriter(w)
|
tbl.WithWriter(w)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var registrationDate time.Time
|
var registrationDate time.Time
|
||||||
@ -99,11 +109,13 @@ func usageStatsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
var ipAddresses string
|
var ipAddresses string
|
||||||
var numQueries int
|
var numQueries int
|
||||||
var lastQueried time.Time
|
var lastQueried time.Time
|
||||||
err = rows.Scan(®istrationDate, &numDevices, &numEntries, &lastUsedDate, &ipAddresses, &numQueries, &lastQueried)
|
var versions string
|
||||||
|
err = rows.Scan(®istrationDate, &numDevices, &numEntries, &lastUsedDate, &ipAddresses, &numQueries, &lastQueried, &versions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
tbl.AddRow(registrationDate.Format("2006-01-02"), numDevices, numEntries, numQueries, lastUsedDate.Format("2006-01-02"), lastQueried.Format("2006-01-02"), ipAddresses)
|
versions = strings.ReplaceAll(versions, "Unknown", "")
|
||||||
|
tbl.AddRow(registrationDate.Format("2006-01-02"), numDevices, numEntries, numQueries, lastUsedDate.Format("2006-01-02"), lastQueried.Format("2006-01-02"), versions, ipAddresses)
|
||||||
}
|
}
|
||||||
tbl.Print()
|
tbl.Print()
|
||||||
}
|
}
|
||||||
@ -540,7 +552,7 @@ func withLogging(h func(http.ResponseWriter, *http.Request)) http.Handler {
|
|||||||
h(&lrw, r)
|
h(&lrw, r)
|
||||||
|
|
||||||
duration := time.Since(start)
|
duration := time.Since(start)
|
||||||
fmt.Printf("%s %s %#v %s %s %s\n", r.RemoteAddr, r.Method, r.RequestURI, r.Header.Get("X-Hishtory-Version"), duration.String(), byteCountToString(responseData.size))
|
fmt.Printf("%s %s %#v %s %s %s\n", r.RemoteAddr, r.Method, r.RequestURI, getHishtoryVersion(r), duration.String(), byteCountToString(responseData.size))
|
||||||
}
|
}
|
||||||
return http.HandlerFunc(logFn)
|
return http.HandlerFunc(logFn)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user