Skip to content

Commit

Permalink
fix(analytics)!: use rfc 3339 for dates (#3695)
Browse files Browse the repository at this point in the history
  • Loading branch information
erka authored Dec 9, 2024
1 parent dc96995 commit 15da9a2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 35 deletions.
16 changes: 12 additions & 4 deletions internal/server/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ import (

// GetFlagEvaluationsCount is the implemented RPC method that will return aggregated flag evaluation counts.
func (s *Server) GetFlagEvaluationsCount(ctx context.Context, req *analytics.GetFlagEvaluationsCountRequest) (*analytics.GetFlagEvaluationsCountResponse, error) {
fromTime, err := time.Parse(time.DateTime, req.From)
fromTime, err := time.Parse(time.RFC3339, req.From)
if err != nil {
return nil, err
var innerErr error
fromTime, innerErr = time.Parse(time.DateTime, req.From)
if innerErr != nil {
return nil, err
}
}

toTime, err := time.Parse(time.DateTime, req.To)
toTime, err := time.Parse(time.RFC3339, req.To)
if err != nil {
return nil, err
var innerErr error
toTime, innerErr = time.Parse(time.DateTime, req.To)
if innerErr != nil {
return nil, err
}
}

r := &FlagEvaluationsCountRequest{
Expand Down
37 changes: 27 additions & 10 deletions internal/server/analytics/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,33 @@ func TestGetFlagEvaluationsCountClientError(t *testing.T) {
StepMinutes: 1,
}).Return(nil, nil, errors.New("client error"))

service := New(logger, client)
from := "2022-06-09 11:00:00"
to := "2022-06-09 11:30:00"
_, err := service.GetFlagEvaluationsCount(ctx, &analytics.GetFlagEvaluationsCountRequest{
NamespaceKey: "bar",
FlagKey: "foo",
From: from,
To: to,
t.Run("old date format", func(t *testing.T) {
service := New(logger, client)
from := "2022-06-09 11:00:00"
to := "2022-06-09 11:30:00"
_, err := service.GetFlagEvaluationsCount(ctx, &analytics.GetFlagEvaluationsCountRequest{
NamespaceKey: "bar",
FlagKey: "foo",
From: from,
To: to,
})

require.Error(t, err)
require.ErrorContains(t, err, "client error")
})

require.Error(t, err)
require.ErrorContains(t, err, "client error")
t.Run("rfc 3339 date format", func(t *testing.T) {
service := New(logger, client)
from := "2022-06-09T11:00:00Z"
to := "2022-06-09T11:30:00.000Z"
_, err := service.GetFlagEvaluationsCount(ctx, &analytics.GetFlagEvaluationsCountRequest{
NamespaceKey: "bar",
FlagKey: "foo",
From: from,
To: to,
})

require.Error(t, err)
require.ErrorContains(t, err, "client error")
})
}
4 changes: 2 additions & 2 deletions internal/server/analytics/prometheus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *client) GetFlagEvaluationsCount(ctx context.Context, req *panalytics.Fl
)

for i, vv := range v.Values {
timestamps[i] = time.UnixMilli(int64(vv.Timestamp)).Format(time.DateTime)
timestamps[i] = time.UnixMilli(int64(vv.Timestamp)).UTC().Format(time.RFC3339)
values[i] = float32(math.Round(float64(vv.Value)))
}

Expand All @@ -85,7 +85,7 @@ func (c *client) GetFlagEvaluationsCount(ctx context.Context, req *panalytics.Fl
// if there is no data. The vector(0) has only points with zero values. We need to combine it
for i := len(m) - 2; i >= 0; i-- {
for _, vv := range m[i].Values {
t := time.UnixMilli(int64(vv.Timestamp)).Format(time.DateTime)
t := time.UnixMilli(int64(vv.Timestamp)).UTC().Format(time.RFC3339)
if i := slices.Index(timestamps, t); i != -1 {
values[i] += float32(math.Round(float64(vv.Value)))
}
Expand Down
30 changes: 11 additions & 19 deletions ui/src/app/flags/analytics/Analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSelector } from 'react-redux';
import { useOutletContext } from 'react-router-dom';
import { PauseIcon, PlayIcon } from '@heroicons/react/24/outline';
import 'chartjs-adapter-date-fns';
import { addMinutes, format, parseISO } from 'date-fns';
import { addMinutes } from 'date-fns';
import { selectCurrentNamespace } from '~/app/namespaces/namespacesSlice';
import { IFlag } from '~/types/Flag';
import { Graph } from '~/components/graphs';
Expand All @@ -17,8 +17,6 @@ type AnalyticsProps = {
flag: IFlag;
};

const timeFormat = 'yyyy-MM-dd HH:mm:ss';

interface IDuration {
value: number;
}
Expand Down Expand Up @@ -71,23 +69,17 @@ export default function Analytics() {

const d = new Date();
d.setSeconds(0);
const nowISO = parseISO(d.toISOString());
d.setMilliseconds(0);

const getFlagEvaluationCount = useGetFlagEvaluationCountQuery(
{
namespaceKey: namespace.key,
flagKey: flag.key,
from: format(
addMinutes(
addMinutes(
nowISO,
selectedDuration?.value ? selectedDuration.value * -1 : -60
),
nowISO.getTimezoneOffset()
),
timeFormat
),
to: format(addMinutes(nowISO, nowISO.getTimezoneOffset()), timeFormat)
from: addMinutes(
d,
selectedDuration?.value ? selectedDuration.value * -1 : -60
).toISOString(),
to: d.toISOString()
},
{
pollingInterval,
Expand All @@ -97,8 +89,8 @@ export default function Analytics() {

const flagEvaluationCount = useMemo(() => {
return {
timestamps: getFlagEvaluationCount.data?.timestamps,
values: getFlagEvaluationCount.data?.values
timestamps: getFlagEvaluationCount.data?.timestamps || [],
values: getFlagEvaluationCount.data?.values || []
};
}, [getFlagEvaluationCount]);

Expand Down Expand Up @@ -142,8 +134,8 @@ export default function Analytics() {
</div>
<div className="mt-10">
<Graph
timestamps={flagEvaluationCount.timestamps || []}
values={flagEvaluationCount.values || []}
timestamps={flagEvaluationCount.timestamps}
values={flagEvaluationCount.values}
flagKey={flag.key}
/>
</div>
Expand Down

0 comments on commit 15da9a2

Please sign in to comment.