Code

frontend: Renamed CONNECTION_EXPR to CONNECTION_MATCHER.
[sysdb.git] / src / frontend / grammar.y
index a297714c074f20a22690245c5c51a8ab1f6d89bb..ef479efc336ba30e90182ae72557c0892091fc07 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "core/store.h"
 #include "core/store-private.h"
+#include "core/time.h"
 
 #include "utils/error.h"
 #include "utils/llist.h"
@@ -70,6 +71,7 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
        char *str;
 
        sdb_data_t data;
+       sdb_time_t datetime;
 
        sdb_llist_t     *list;
        sdb_conn_node_t *node;
@@ -87,15 +89,19 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 %token CMP_LT CMP_LE CMP_GE CMP_GT
 %token CONCAT
 
+%token START END
+
 /* NULL token */
 %token NULL_T
 
-%token FETCH LIST LOOKUP
+%token FETCH LIST LOOKUP TIMESERIES
 
 %token <str> IDENTIFIER STRING
 
 %token <data> INTEGER FLOAT
 
+%token <datetime> DATE TIME
+
 /* Precedence (lowest first): */
 %left OR
 %left AND
@@ -107,6 +113,7 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 %nonassoc IS
 %left '+' '-'
 %left '*' '/' '%'
+%left '[' ']'
 %left '(' ')'
 %left '.'
 
@@ -115,6 +122,7 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
        fetch_statement
        list_statement
        lookup_statement
+       timeseries_statement
        matching_clause
        filter_clause
        condition
@@ -124,11 +132,14 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 
 %type <expr> expression
 
-%type <sstr> op
+%type <sstr> cmp
 
 %type <data> data
        interval interval_elem
 
+%type <datetime> datetime
+       start_clause end_clause
+
 %destructor { free($$); } <str>
 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
 
@@ -194,6 +205,8 @@ statement:
        |
        lookup_statement
        |
+       timeseries_statement
+       |
        /* empty */
                {
                        $$ = NULL;
@@ -222,6 +235,7 @@ fetch_statement:
 
                        $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
                                                conn_fetch_t, conn_fetch_destroy));
+                       CONN_FETCH($$)->type = SDB_HOST;
                        CONN_FETCH($$)->name = $3;
                        CONN_FETCH($$)->filter = CONN_MATCHER($4);
                        $$->cmd = CONNECTION_FETCH;
@@ -237,8 +251,8 @@ fetch_statement:
 list_statement:
        LIST IDENTIFIER filter_clause
                {
-                       /* TODO: support other types as well */
-                       if (strcasecmp($2, "hosts")) {
+                       int type = sdb_store_parse_object_type_plural($2);
+                       if (type < 0) {
                                char errmsg[strlen($2) + 32];
                                snprintf(errmsg, sizeof(errmsg),
                                                YY_("unknown data-source %s"), $2);
@@ -250,6 +264,7 @@ list_statement:
 
                        $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
                                                conn_list_t, conn_list_destroy));
+                       CONN_LIST($$)->type = type;
                        CONN_LIST($$)->filter = CONN_MATCHER($3);
                        $$->cmd = CONNECTION_LIST;
                        free($2); $2 = NULL;
@@ -278,6 +293,7 @@ lookup_statement:
 
                        $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
                                                conn_lookup_t, conn_lookup_destroy));
+                       CONN_LOOKUP($$)->type = SDB_HOST;
                        CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
                        CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
                        $$->cmd = CONNECTION_LOOKUP;
@@ -295,6 +311,34 @@ filter_clause:
        |
        /* empty */ { $$ = NULL; }
 
+/*
+ * TIMESERIES <host>.<metric> [START <datetime>] [END <datetime>];
+ *
+ * Returns a time-series for the specified host's metric.
+ */
+timeseries_statement:
+       TIMESERIES STRING '.' STRING start_clause end_clause
+               {
+                       $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+                                               conn_ts_t, conn_ts_destroy));
+                       CONN_TS($$)->hostname = $2;
+                       CONN_TS($$)->metric = $4;
+                       CONN_TS($$)->opts.start = $5;
+                       CONN_TS($$)->opts.end = $6;
+                       $$->cmd = CONNECTION_TIMESERIES;
+               }
+       ;
+
+start_clause:
+       START datetime { $$ = $2; }
+       |
+       /* empty */ { $$ = sdb_gettime() - SDB_INTERVAL_HOUR; }
+
+end_clause:
+       END datetime { $$ = $2; }
+       |
+       /* empty */ { $$ = sdb_gettime(); }
+
 /*
  * Basic expressions.
  */
@@ -311,7 +355,7 @@ condition:
 
                        $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
                                                conn_matcher_t, conn_matcher_destroy));
-                       $$->cmd = CONNECTION_EXPR;
+                       $$->cmd = CONNECTION_MATCHER;
                        CONN_MATCHER($$)->matcher = $1;
                }
        ;
@@ -349,41 +393,41 @@ matcher:
        ;
 
 /*
- * <object_type>.<object_attr> <op> <value>
+ * <object_type>.<object_attr> <cmp> <value>
  *
  * Parse matchers comparing object attributes with a value.
  */
 compare_matcher:
-       ':' IDENTIFIER op expression
+       '.' IDENTIFIER cmp expression
                {
                        $$ = sdb_store_matcher_parse_field_cmp($2, $3, $4);
                        free($2); $2 = NULL;
                        sdb_object_deref(SDB_OBJ($4));
                }
        |
-       IDENTIFIER op expression
+       IDENTIFIER cmp expression
                {
                        $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3);
                        free($1); $1 = NULL;
                        sdb_object_deref(SDB_OBJ($3));
                }
        |
-       IDENTIFIER '.' IDENTIFIER op expression
+       IDENTIFIER '[' IDENTIFIER ']' cmp expression
                {
-                       $$ = sdb_store_matcher_parse_cmp($1, $3, $4, $5);
+                       $$ = sdb_store_matcher_parse_cmp($1, $3, $5, $6);
                        free($1); $1 = NULL;
                        free($3); $3 = NULL;
-                       sdb_object_deref(SDB_OBJ($5));
+                       sdb_object_deref(SDB_OBJ($6));
                }
        |
-       IDENTIFIER '.' IDENTIFIER IS NULL_T
+       IDENTIFIER '[' IDENTIFIER ']' IS NULL_T
                {
                        $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
                        free($1); $1 = NULL;
                        free($3); $3 = NULL;
                }
        |
-       IDENTIFIER '.' IDENTIFIER IS NOT NULL_T
+       IDENTIFIER '[' IDENTIFIER ']' IS NOT NULL_T
                {
                        sdb_store_matcher_t *m;
                        m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
@@ -437,13 +481,27 @@ expression:
                        sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
                }
        |
-       ':' IDENTIFIER
+       expression CONCAT expression
+               {
+                       $$ = sdb_store_expr_create(SDB_DATA_CONCAT, $1, $3);
+                       sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
+                       sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
+               }
+       |
+       '.' IDENTIFIER
                {
                        int field = sdb_store_parse_field_name($2);
                        free($2); $2 = NULL;
                        $$ = sdb_store_expr_fieldvalue(field);
                }
        |
+       IDENTIFIER '[' IDENTIFIER ']'
+               {
+                       $$ = sdb_store_expr_attrvalue($3);
+                       free($1); $1 = NULL;
+                       free($3); $3 = NULL;
+               }
+       |
        data
                {
                        $$ = sdb_store_expr_constvalue(&$1);
@@ -451,7 +509,7 @@ expression:
                }
        ;
 
-op:
+cmp:
        CMP_EQUAL { $$ = "="; }
        |
        CMP_NEQUAL { $$ = "!="; }
@@ -476,9 +534,19 @@ data:
        |
        FLOAT { $$ = $1; }
        |
+       datetime { $$.type = SDB_TYPE_DATETIME; $$.data.datetime = $1; }
+       |
        interval { $$ = $1; }
        ;
 
+datetime:
+       DATE TIME { $$ = $1 + $2; }
+       |
+       DATE { $$ = $1; }
+       |
+       TIME { $$ = $1; }
+       ;
+
 interval:
        interval interval_elem
                {