Skip to content

Commit 7733591

Browse files
committed
simplify: timeseries: cnvert to TimeItem.Int64() and TimeItem.Float64()
1 parent 69cbd2c commit 7733591

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

data/timeseries/interval/xox_month.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewXoXTimeSeries(ds timeseries.TimeSeries) (XoXGrowth, error) {
2424
if err != nil {
2525
return xox, errors.Wrap(err, "timeseries.NewXoXTimeSeries")
2626
}
27-
xoxPoint := XoxPoint{Time: dateNow, Value: itemNow.ValueInt64()}
27+
xoxPoint := XoxPoint{Time: dateNow, Value: itemNow.Int64()}
2828

2929
quarterAgo := month.MonthBegin(dateNow, -3)
3030
yearAgo := month.MonthBegin(dateNow, -12)
@@ -34,26 +34,26 @@ func NewXoXTimeSeries(ds timeseries.TimeSeries) (XoXGrowth, error) {
3434
monthAgo := month.MonthBegin(dateNow, -1)
3535
xoxPoint.TimeMonthAgo = monthAgo
3636
if itemMonthAgo, ok := ds.ItemMap[monthAgo.Format(time.RFC3339)]; ok {
37-
xoxPoint.MMAgoValue = itemMonthAgo.ValueInt64()
38-
xoxPoint.MNowValue = itemNow.ValueInt64()
39-
xoxPoint.MOldValue = itemMonthAgo.ValueInt64()
40-
xoxPoint.MoM = mathutil.PercentChangeToXoX(itemNow.ValueFloat64() / itemMonthAgo.ValueFloat64())
41-
xoxPoint.MoMAggregate = mathutil.PercentChangeToXoX(itemNow.ValueFloat64() / itemMonthAgo.ValueFloat64())
37+
xoxPoint.MMAgoValue = itemMonthAgo.Int64()
38+
xoxPoint.MNowValue = itemNow.Int64()
39+
xoxPoint.MOldValue = itemMonthAgo.Int64()
40+
xoxPoint.MoM = mathutil.PercentChangeToXoX(itemNow.Float64() / itemMonthAgo.Float64())
41+
xoxPoint.MoMAggregate = mathutil.PercentChangeToXoX(itemNow.Float64() / itemMonthAgo.Float64())
4242
}
4343
}
4444
if itemMonthQuarterAgo, ok := ds.ItemMap[quarterAgo.Format(time.RFC3339)]; ok {
45-
xoxPoint.MQAgoValue = itemMonthQuarterAgo.ValueInt64()
45+
xoxPoint.MQAgoValue = itemMonthQuarterAgo.Int64()
4646
xoxPoint.QNowValue = AggregatePriorMonths(ds, dateNow, 3)
4747
xoxPoint.QOldValue = AggregatePriorMonths(ds, month.MonthBegin(dateNow, -3), 3)
48-
xoxPoint.QoQ = mathutil.PercentChangeToXoX(itemNow.ValueFloat64() / itemMonthQuarterAgo.ValueFloat64())
48+
xoxPoint.QoQ = mathutil.PercentChangeToXoX(itemNow.Float64() / itemMonthQuarterAgo.Float64())
4949
xoxPoint.QoQAggregate = mathutil.PercentChangeToXoX(
5050
float64(xoxPoint.QNowValue) / float64(xoxPoint.QOldValue))
5151
}
5252
if itemMonthYearAgo, ok := ds.ItemMap[yearAgo.Format(time.RFC3339)]; ok {
53-
xoxPoint.MYAgoValue = itemMonthYearAgo.ValueInt64()
53+
xoxPoint.MYAgoValue = itemMonthYearAgo.Int64()
5454
xoxPoint.YNowValue = AggregatePriorMonths(ds, dateNow, 12)
5555
xoxPoint.YOldValue = AggregatePriorMonths(ds, month.MonthBegin(dateNow, -12), 12)
56-
xoxPoint.YoY = mathutil.PercentChangeToXoX(itemNow.ValueFloat64() / itemMonthYearAgo.ValueFloat64())
56+
xoxPoint.YoY = mathutil.PercentChangeToXoX(itemNow.Float64() / itemMonthYearAgo.Float64())
5757
xoxPoint.YoYAggregate = mathutil.PercentChangeToXoX(
5858
float64(xoxPoint.YNowValue) / float64(xoxPoint.YOldValue))
5959
/*

data/timeseries/time_item.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ type TimeItem struct {
1515
ValueFloat float64
1616
}
1717

18-
func (item *TimeItem) ValueInt64() int64 {
18+
func (item *TimeItem) Int64() int64 {
1919
if item.IsFloat {
2020
return int64(item.ValueFloat)
2121
}
2222
return item.Value
2323
}
2424

25-
func (item *TimeItem) ValueFloat64() float64 {
25+
func (item *TimeItem) Float64() float64 {
2626
if item.IsFloat {
2727
return item.ValueFloat
2828
}

data/timeseries/time_series.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (ts *TimeSeries) LastItem(skipIfTimePartialValueLessPrev bool) (TimeItem, e
178178
dtNow = month.MonthBegin(dtNow, 0)
179179
}
180180
if itemLast.Time.Equal(dtNow) {
181-
if itemLast.ValueInt64() > itemPrev.ValueInt64() {
181+
if itemLast.Int64() > itemPrev.Int64() {
182182
return itemLast, nil
183183
} else {
184184
return itemPrev, nil

data/timeseries/time_series_set_report.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ func (set *TimeSeriesSet) ToTable(opts *TimeSeriesSetTableOpts) (table.Table, er
214214
} else {
215215
line = append(line, strconv.Itoa(int(item.Value)))
216216
}
217-
lineTotal += item.ValueFloat64()
218-
seriesValues = append(seriesValues, item.ValueFloat64())
217+
lineTotal += item.Float64()
218+
seriesValues = append(seriesValues, item.Float64())
219219
}
220220
}
221221
if opts.TotalInclude {

data/timeseries/xoxconv/xox_time_series.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import (
44
"time"
55

66
"github.com/grokify/gocharts/data/timeseries"
7-
"github.com/grokify/simplego/fmt/fmtutil"
87
"github.com/grokify/simplego/time/timeutil"
98
)
109

1110
// TimeSeriesXoX converts a `timeseries.TimeSeries`
1211
// XoX statistics.
1312
func TimeSeriesXoX(ts timeseries.TimeSeries) (XOXFloat64, error) {
1413
tsMonth := ts.ToMonth(true)
15-
fmtutil.PrintJSON(tsMonth)
1614

1715
xox := XOXFloat64{}
1816
tiNow, err := tsMonth.Last()
@@ -22,35 +20,35 @@ func TimeSeriesXoX(ts timeseries.TimeSeries) (XOXFloat64, error) {
2220
xox.Now = Compare{
2321
XoX: "NOW",
2422
Time: tiNow.Time,
25-
Value: tiNow.ValueFloat64()}
23+
Value: tiNow.Float64()}
2624
mago, err := tsMonth.Get(timeutil.TimeDt6SubNMonths(tiNow.Time, 1))
2725
if err == nil {
2826
xox.Month = Compare{
2927
XoX: "MOM",
3028
Time: mago.Time,
31-
Value: mago.ValueFloat64()}
32-
if mago.ValueFloat64() != 0 {
33-
xox.Month.Change = (tiNow.ValueFloat64() - mago.ValueFloat64()) / mago.ValueFloat64()
29+
Value: mago.Float64()}
30+
if mago.Float64() != 0 {
31+
xox.Month.Change = (tiNow.Float64() - mago.Float64()) / mago.Float64()
3432
}
3533
}
3634
qago, err := tsMonth.Get(timeutil.TimeDt6SubNMonths(tiNow.Time, 3))
3735
if err == nil {
3836
xox.Quarter = Compare{
3937
XoX: "QOQ",
4038
Time: qago.Time,
41-
Value: qago.ValueFloat64()}
42-
if qago.ValueFloat64() != 0 {
43-
xox.Quarter.Change = (tiNow.ValueFloat64() - qago.ValueFloat64()) / qago.ValueFloat64()
39+
Value: qago.Float64()}
40+
if qago.Float64() != 0 {
41+
xox.Quarter.Change = (tiNow.Float64() - qago.Float64()) / qago.Float64()
4442
}
4543
}
4644
yago, err := tsMonth.Get(timeutil.TimeDt6SubNMonths(tiNow.Time, 12))
4745
if err == nil {
4846
xox.Year = Compare{
4947
XoX: "YOY",
5048
Time: yago.Time,
51-
Value: yago.ValueFloat64()}
52-
if yago.ValueFloat64() != 0 {
53-
xox.Year.Change = (tiNow.ValueFloat64() - yago.ValueFloat64()) / yago.ValueFloat64()
49+
Value: yago.Float64()}
50+
if yago.Float64() != 0 {
51+
xox.Year.Change = (tiNow.Float64() - yago.Float64()) / yago.Float64()
5452
}
5553
}
5654
return xox, nil

0 commit comments

Comments
 (0)