Debian Codegraph @

htrace-htraced/go/web/app/predicate.js

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 var htrace = htrace || {};
21
22 htrace.checkStringIsPositiveWholeNumber = function(val) {
23 if (!val.match(/^[0-9]([0-9]*)$/)) {
24 if (!val.match(/[^\s]/)) {
25 throw "You entered an empty string into a numeric field.";
26 }
27 throw "Non-numeric characters found.";
28 }
29 };
30
31 htrace.checkStringIsNotEmpty = function(val) {
32 if (!val.match(/[^\s]/)) {
33 throw "You entered an empty string into a text field.";
34 }
35 };
36
37 // Predicate type
38 htrace.PType = Backbone.Model.extend({
39 initialize: function(options) {
40 this.name = options.name;
41 this.field = options.field;
42 this.op = options.op;
43 },
44
45 // Try to normalize a value of this type into something htraced can accept.
46 // Returns a string containing the normalized value on success. Throws a
47 // string explaining the parse error otherwise.
48 // Dates are represented by milliseconds since the epoch; span ids don't start
49 // with 0x.
50 normalize: function(val) {
51 switch (this.field) {
52 case "begin":
53 return htrace.parseDate(val).valueOf().toString();
54 case "end":
55 return htrace.parseDate(val).valueOf().toString();
56 case "description":
57 htrace.checkStringIsNotEmpty(val);
58 return val;
59 case "duration":
60 htrace.checkStringIsPositiveWholeNumber(val);
61 return val;
62 case "spanid":
63 return htrace.normalizeSpanId(val);
64 case "tracerid":
65 htrace.checkStringIsNotEmpty(val);
66 return val;
67 default:
68 return "Normalization not implemented for field '" + this.field + "'";
69 }
70 },
71
72 getDefaultValue: function() {
73 switch (this.field) {
74 case "begin":
75 return htrace.dateToString(moment());
76 case "end":
77 return htrace.dateToString(moment());
78 case "description":
79 return "";
80 case "duration":
81 return "0";
82 case "spanid":
83 return "";
84 case "tracerid":
85 return "";
86 default:
87 return "(unknown)";
88 }
89 }
90 });
91
92 htrace.parsePType = function(name) {
93 switch (name) {
94 case "Began at or after":
95 return new htrace.PType({name: name, field:"begin", op:"ge"});
96 case "Began at or before":
97 return new htrace.PType({name: name, field:"begin", op:"le"});
98 case "Ended at or after":
99 return new htrace.PType({name: name, field:"end", op:"ge"});
100 case "Ended at or before":
101 return new htrace.PType({name: name, field:"end", op:"le"});
102 case "Description contains":
103 return new htrace.PType({name: name, field:"description", op:"cn"});
104 case "Description is exactly":
105 return new htrace.PType({name: name, field:"description", op:"eq"});
106 case "Duration is at least":
107 return new htrace.PType({name: name, field:"duration", op:"ge"});
108 case "Duration is at most":
109 return new htrace.PType({name: name, field:"duration", op:"le"});
110 case "Span ID is":
111 return new htrace.PType({name: name, field:"spanid", op:"eq"});
112 case "TracerId contains":
113 return new htrace.PType({name: name, field:"tracerid", op:"cn"});
114 case "TracerId is exactly":
115 return new htrace.PType({name: name, field:"tracerid", op:"eq"});
116 default:
117 return null
118 }
119 };
120
121 htrace.Predicate = function(options) {
122 this.op = options.ptype.op;
123 this.field = options.ptype.field;
124 this.val = options.val;
125 return this;
126 };