From f06a9f4e94d1e2f757548d031beec01cfc0eade2 Mon Sep 17 00:00:00 2001 From: Jeremy Edwards <1312331+jeremyje@users.noreply.github.com> Date: Sun, 22 Jan 2023 14:47:38 -0800 Subject: [PATCH] Move converter code --- cmd/converter/converter.go | 123 +++--------------------------- internal/converter.go | 148 +++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 111 deletions(-) create mode 100644 internal/converter.go diff --git a/cmd/converter/converter.go b/cmd/converter/converter.go index 306b43c..574d348 100644 --- a/cmd/converter/converter.go +++ b/cmd/converter/converter.go @@ -15,122 +15,23 @@ package main import ( - "bufio" - "encoding/json" - "fmt" + "flag" "log" - "os" - "time" + "strings" - "github.com/jeremyje/coretemp-exporter/drivers/common" - pb "github.com/jeremyje/coretemp-exporter/proto" - "google.golang.org/protobuf/encoding/protojson" - "google.golang.org/protobuf/types/known/timestamppb" + "github.com/jeremyje/coretemp-exporter/internal" ) -// HardwareInfo includes CPU and motherboard information about the host machine. -type HardwareInfo struct { - // Load as a percentage [0-100]. - Load []int `json:"load" yaml:"load"` - // TJMax is the thermal junction maximum of the CPU. Once the temperature hits this limit the CPU will thermal throttle. - TJMax []float64 `json:"tjMax" yaml:"tjMax"` - // CoreCount is the total number of cores across all CPUs on the machine. - CoreCount int `json:"coreCount" yaml:"coreCount"` - // CPUCount is the number of CPUs on the machine. - CPUCount int `json:"cpuCount" yaml:"cpuCount"` - // TemperatureCelcius is the temperature of each core expressed in Celcius. - TemperatureCelcius []float64 `json:"temperatureCelcius" yaml:"temperatureCelcius"` - // VID is the voltage requested by the CPU. - VID float64 `json:"vid" yaml:"vid"` - // CPUSpeed is the clock frequency of the CPU. - CPUSpeed float64 `json:"cpuSpeed" yaml:"cpuSpeed"` - // FSBSpeed is the clock frequency of the front side bus. - FSBSpeed float64 `json:"fsbSpeed" yaml:"fsbSpeed"` - // Multiplier is the front side bus multipler of the CPU. - Multiplier float64 `json:"multiplier" yaml:"multiplier"` - // CPUName is the name and model of the CPU. - CPUName string `json:"cpuName" yaml:"cpuName"` - // Timestamp is the time that the sample was taken. - Timestamp time.Time `json:"timestamp" yaml:"timestamp"` -} +var ( + oldFilesFlag = flag.String("old", "cputemps.log,cputemps.ndjson", "Comma separated list of old files.") + newFileFlag = flag.String("new", "new.ndjson", "The new ndjson file") +) func main() { - os.Remove("new.ndjson") - fp, err := os.Create("new.ndjson") - check(err) - defer fp.Close() - - ln := 0 - filenames := []string{"cputemps.log", "cputemps.ndjson"} - for _, filename := range filenames { - log.Printf("OPEN %s", filename) - in, err := os.Open(filename) - check(err) - defer in.Close() - scanner := bufio.NewScanner(in) - for scanner.Scan() { - ln++ - if ln%10000 == 0 { - log.Printf("Line: %s:%d", filename, ln) - } - pba := &pb.MachineMetrics{} - line := scanner.Bytes() - if len(line) < 10 { - continue - } - err := json.Unmarshal(line, &pba) - if err != nil || pba == nil || len(pba.GetName()) == 0 { - info := &HardwareInfo{} - err := json.Unmarshal(line, &info) - check(err) - pba = convertToProto(info) - if len(pba.Name) == 0 { - check(fmt.Errorf("empty name: %+v", pba)) - } - } - - newLine, err := protojson.Marshal(pba) - check(err) - _, err = fp.Write(newLine) - check(err) - _, err = fp.WriteString("\n") - check(err) - } - } -} - -func copyInt(i []int) []int32 { - a := []int32{} - for _, v := range i { - a = append(a, int32(v)) - } - return a -} - -func convertToProto(info *HardwareInfo) *pb.MachineMetrics { - return &pb.MachineMetrics{ - Name: "quartz", - Device: []*pb.DeviceMetrics{ - { - Name: info.CPUName, - Kind: "cpu", - Temperature: common.Average(info.TemperatureCelcius), - Cpu: &pb.CpuDeviceMetrics{ - Load: copyInt(info.Load), - Temperature: info.TemperatureCelcius, - NumCores: int32(info.CoreCount), - FrequencyMhz: info.CPUSpeed, - FsbFrequencyMhz: info.FSBSpeed, - }, - }, - }, - Timestamp: timestamppb.New(info.Timestamp), - } -} - -func check(err error) { - if err != nil { - log.Printf("ERROR: %s", err) - panic(err) + if err := internal.ConvertV1ToV2(&internal.ConvertArgs{ + OldFiles: strings.Split(*oldFilesFlag, ","), + NewFile: *newFileFlag, + }); err != nil { + log.Fatal(err) } } diff --git a/internal/converter.go b/internal/converter.go new file mode 100644 index 0000000..81ace51 --- /dev/null +++ b/internal/converter.go @@ -0,0 +1,148 @@ +// Copyright 2023 Jeremy Edwards +// +// 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 internal + +import ( + "bufio" + "encoding/json" + "fmt" + "log" + "os" + "time" + + "github.com/jeremyje/coretemp-exporter/drivers/common" + pb "github.com/jeremyje/coretemp-exporter/proto" + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/types/known/timestamppb" +) + +type ConvertArgs struct { + OldFiles []string + NewFile string +} + +// HardwareInfo includes CPU and motherboard information about the host machine. +type HardwareInfo struct { + // Load as a percentage [0-100]. + Load []int `json:"load" yaml:"load"` + // TJMax is the thermal junction maximum of the CPU. Once the temperature hits this limit the CPU will thermal throttle. + TJMax []float64 `json:"tjMax" yaml:"tjMax"` + // CoreCount is the total number of cores across all CPUs on the machine. + CoreCount int `json:"coreCount" yaml:"coreCount"` + // CPUCount is the number of CPUs on the machine. + CPUCount int `json:"cpuCount" yaml:"cpuCount"` + // TemperatureCelcius is the temperature of each core expressed in Celcius. + TemperatureCelcius []float64 `json:"temperatureCelcius" yaml:"temperatureCelcius"` + // VID is the voltage requested by the CPU. + VID float64 `json:"vid" yaml:"vid"` + // CPUSpeed is the clock frequency of the CPU. + CPUSpeed float64 `json:"cpuSpeed" yaml:"cpuSpeed"` + // FSBSpeed is the clock frequency of the front side bus. + FSBSpeed float64 `json:"fsbSpeed" yaml:"fsbSpeed"` + // Multiplier is the front side bus multipler of the CPU. + Multiplier float64 `json:"multiplier" yaml:"multiplier"` + // CPUName is the name and model of the CPU. + CPUName string `json:"cpuName" yaml:"cpuName"` + // Timestamp is the time that the sample was taken. + Timestamp time.Time `json:"timestamp" yaml:"timestamp"` +} + +func ConvertV1ToV2(args *ConvertArgs) error { + os.Remove(args.NewFile) + fp, err := os.Create(args.NewFile) + if err != nil { + return err + } + defer fp.Close() + + ln := 0 + for _, filename := range args.OldFiles { + log.Printf("OPEN %s", filename) + in, err := os.Open(filename) + if err != nil { + return err + } + defer in.Close() + scanner := bufio.NewScanner(in) + for scanner.Scan() { + ln++ + if ln%10000 == 0 { + log.Printf("Line: %s:%d", filename, ln) + } + pba := &pb.MachineMetrics{} + line := scanner.Bytes() + if len(line) < 10 { + continue + } + err := json.Unmarshal(line, &pba) + if err != nil || pba == nil || len(pba.GetName()) == 0 { + info := &HardwareInfo{} + err := json.Unmarshal(line, &info) + if err != nil { + return err + } + pba = convertToProto(info) + if len(pba.Name) == 0 { + if err != nil { + return fmt.Errorf("empty name: %+v", pba) + } + } + } + + newLine, err := protojson.Marshal(pba) + if err != nil { + return err + } + _, err = fp.Write(newLine) + if err != nil { + return err + } + _, err = fp.WriteString("\n") + if err != nil { + return err + } + } + } + return nil +} + +func copyInt(i []int) []int32 { + a := []int32{} + for _, v := range i { + a = append(a, int32(v)) + } + return a +} + +func convertToProto(info *HardwareInfo) *pb.MachineMetrics { + return &pb.MachineMetrics{ + Name: "quartz", + Device: []*pb.DeviceMetrics{ + { + Name: info.CPUName, + Kind: "cpu", + Temperature: common.Average(info.TemperatureCelcius), + Cpu: &pb.CpuDeviceMetrics{ + Load: copyInt(info.Load), + Temperature: info.TemperatureCelcius, + NumCores: int32(info.CoreCount), + FrequencyMhz: info.CPUSpeed, + FsbFrequencyMhz: info.FSBSpeed, + }, + }, + }, + Timestamp: timestamppb.New(info.Timestamp), + } +}