-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-dcheck.go
95 lines (72 loc) · 2.31 KB
/
http-dcheck.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
// Copyright 2017 Inca Roads LLC. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Inbound support for the "/check" HTTP topic
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
ttdata "github.com/Safecast/safecast-go"
)
// Handle inbound HTTP requests to do a quick analysis of a device's log file
func inboundWebDeviceCheckHandler(rw http.ResponseWriter, req *http.Request) {
stats.Count.HTTP++
// Set response mime type
rw.Header().Set("Content-Type", "application/json")
// Log it
deviceidstr := req.RequestURI[len(TTServerTopicDeviceCheck):]
timeRange := time.Now().UTC().Format("2006-01")
filename := fmt.Sprintf("%s/%s$%s.json", TTDeviceLogPath, timeRange, DeviceUIDFilename(deviceidstr))
fmt.Printf("%s LOG ANALYSIS request for %s\n", LogTime(), filename)
// Check it
success, s := CheckJSON(SafecastDirectory() + filename)
if !success {
io.WriteString(rw, s)
}
// Done
io.WriteString(rw, s)
}
// CheckJSON performs a standard check on a JSON file
func CheckJSON(infile string) (success bool, result string) {
// Read the log
contents, err := os.ReadFile(infile)
if err != nil {
return false, ErrorString(err)
}
// Begin taking stats
stats := NewMeasurementDataset()
// Split the contents into a number of slices based on the commas
ctmp := strings.Replace(string(contents), "\n,", ",\n", -1)
splitContents := strings.Split(ctmp, ",\n")
for _, c := range splitContents {
// Generate a clean json entry
clean := strings.Replace(c, "\n", "", -1)
if len(clean) == 0 {
continue
}
// Unmarshal it. Badly-formatted json occasionally occurs because of
// concurrent file writes to the log from different process instances,
// but this is rare - so no worry.
value := ttdata.SafecastData{}
err = json.Unmarshal([]byte(clean), &value)
if err != nil {
fmt.Printf("CHECK: error unmarshaling data: %s\n", err)
continue
}
// Take a measurement
MeasurementStat := CheckMeasurement(value)
// Aggregate statistics
AggregateMeasurementIntoDataset(&stats, MeasurementStat)
}
// Measurements completed
AggregationCompleted(&stats)
// Generate the summary of the aggregation
s := GenerateDatasetSummary(stats)
// Done
return true, s
}