Skip to content

Commit

Permalink
Added property type column in export (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjtokenring authored Sep 5, 2024
1 parent 59ef703 commit 6827c00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions business/tsextractor/tsextractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ func (a *TsExtractor) populateNumericTSDataIntoS3(
a.logger.Debugf("Thing %s - Property %s - %d values\n", thingID, propertyID, response.CountValues)
sampleCount += response.CountValues

propertyName := extractPropertyName(thing, propertyID)
propertyName, propertyType := extractPropertyNameAndType(thing, propertyID)

for i := 0; i < len(response.Times); i++ {

ts := response.Times[i]
value := response.Values[i]
samples = append(samples, composeRow(ts, thingID, thing.Name, propertyID, propertyName, strconv.FormatFloat(value, 'f', -1, 64)))
samples = append(samples, composeRow(ts, thingID, thing.Name, propertyID, propertyName, propertyType, strconv.FormatFloat(value, 'f', -1, 64)))
}
}

Expand All @@ -183,26 +183,29 @@ func (a *TsExtractor) populateNumericTSDataIntoS3(
return nil
}

func composeRow(ts time.Time, thingID string, thingName string, propertyID string, propertyName string, value string) []string {
row := make([]string, 6)
func composeRow(ts time.Time, thingID string, thingName string, propertyID string, propertyName string, propertyType string, value string) []string {
row := make([]string, 7)
row[0] = ts.UTC().Format(time.RFC3339)
row[1] = thingID
row[2] = thingName
row[3] = propertyID
row[4] = propertyName
row[5] = value
row[5] = propertyType
row[6] = value
return row
}

func extractPropertyName(thing iotclient.ArduinoThing, propertyID string) string {
func extractPropertyNameAndType(thing iotclient.ArduinoThing, propertyID string) (string, string) {
propertyName := ""
propertyType := ""
for _, prop := range thing.Properties {
if prop.Id == propertyID {
propertyName = prop.Name
propertyType = prop.Type
break
}
}
return propertyName
return propertyName, propertyType
}

func isStringProperty(ptype string) bool {
Expand Down Expand Up @@ -258,7 +261,7 @@ func (a *TsExtractor) populateStringTSDataIntoS3(
a.logger.Debugf("Thing %s - String Property %s - %d values\n", thingID, propertyID, response.CountValues)
sampleCount += response.CountValues

propertyName := extractPropertyName(thing, propertyID)
propertyName, propertyType := extractPropertyNameAndType(thing, propertyID)

for i := 0; i < len(response.Times); i++ {

Expand All @@ -267,7 +270,7 @@ func (a *TsExtractor) populateStringTSDataIntoS3(
if value == nil {
continue
}
samples = append(samples, composeRow(ts, thingID, thing.Name, propertyID, propertyName, interfaceToString(value)))
samples = append(samples, composeRow(ts, thingID, thing.Name, propertyID, propertyName, propertyType, interfaceToString(value)))
}
}

Expand Down Expand Up @@ -318,7 +321,7 @@ func (a *TsExtractor) populateRawTSDataIntoS3(
a.logger.Infof("Thing %s - Query %s Property %s - %d values\n", thingID, response.Query, propertyID, response.CountValues)
sampleCount += response.CountValues

propertyName := extractPropertyName(thing, propertyID)
propertyName, propertyType := extractPropertyNameAndType(thing, propertyID)

for i := 0; i < len(response.Times); i++ {

Expand All @@ -327,7 +330,7 @@ func (a *TsExtractor) populateRawTSDataIntoS3(
if value == nil {
continue
}
samples = append(samples, composeRow(ts, thingID, thing.Name, propertyID, propertyName, interfaceToString(value)))
samples = append(samples, composeRow(ts, thingID, thing.Name, propertyID, propertyName, propertyType, interfaceToString(value)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
baseTmpStorage = "/tmp"
)

var csvHeader = []string{"timestamp", "thing_id", "thing_name", "property_id", "property_name", "value"}
var csvHeader = []string{"timestamp", "thing_id", "thing_name", "property_id", "property_name", "property_type", "value"}

func NewWriter(destinationHour time.Time, logger *logrus.Entry) (*CsvWriter, error) {
filePath := fmt.Sprintf("%s/%s.csv", baseTmpStorage, destinationHour.Format("2006-01-02-15:04"))
Expand Down

0 comments on commit 6827c00

Please sign in to comment.