Restore legacy API scalar format

This commit is contained in:
Fabian Reinartz 2015-07-16 19:27:56 +02:00
parent 742efb4538
commit 62b4e89b39
2 changed files with 15 additions and 5 deletions

View File

@ -55,20 +55,25 @@ func TestQuery(t *testing.T) {
status: http.StatusOK,
bodyRe: `{"type":"error","value":"Parse error at char 1: no expression found in input","version":1}`,
},
{
queryStr: "expr=1.4",
status: http.StatusOK,
bodyRe: `{"type":"scalar","value":"1.4","version":1}`,
},
{
queryStr: "expr=testmetric",
status: http.StatusOK,
bodyRe: `{"type":"vector","value":\[\{"metric":{"__name__":"testmetric"},"value":"0","timestamp":\d+\.\d+}\],"version":1\}`,
bodyRe: `{"type":"vector","value":\[{"metric":{"__name__":"testmetric"},"value":"0","timestamp":\d+\.\d+}\],"version":1}`,
},
{
queryStr: "expr=testmetric&timestamp=" + testTimestamp.String(),
status: http.StatusOK,
bodyRe: `{"type":"vector","value":\[\{"metric":{"__name__":"testmetric"},"value":"0","timestamp":` + testTimestamp.String() + `}\],"version":1\}`,
bodyRe: `{"type":"vector","value":\[{"metric":{"__name__":"testmetric"},"value":"0","timestamp":` + testTimestamp.String() + `}\],"version":1}`,
},
{
queryStr: "expr=testmetric&timestamp=" + testTimestamp.Add(-time.Hour).String(),
status: http.StatusOK,
bodyRe: `{"type":"vector","value":\[\],"version":1\}`,
bodyRe: `{"type":"vector","value":\[\],"version":1}`,
},
{
queryStr: "timestamp=invalid",

View File

@ -133,11 +133,16 @@ func (pv plainVec) String() string {
// which does not fit the response format for the legacy API.
type plainScalar promql.Scalar
func (pv plainScalar) Type() promql.ExprType {
func (ps plainScalar) MarshalJSON() ([]byte, error) {
s := strconv.FormatFloat(float64(ps.Value), 'f', -1, 64)
return json.Marshal(&s)
}
func (plainScalar) Type() promql.ExprType {
return promql.ExprScalar
}
func (pv plainScalar) String() string {
func (plainScalar) String() string {
return ""
}