Grafana: avoid variable interpolation by escaping $ - influxdb

I have a query in Grafana for templating (against Influx) which has a tag which key contains the $ character.
How can I escape $ to avoid its interpolation with the variable?
Query:
SHOW TAG VALUES FROM "exdemon-analysis" WITH KEY = "analyzed_metric.attributes.$owner"
"analyzed_metric.attributes.$owner" is the key, $owner should not be replaced.

The variable name was also $owner, which makes Grafana replace the $owner string in the field.
Plus, it seems I hit a bug in Influx. It does not like the combination of "." and measurement name with "special characters" in my case "-".
With the following query and not having var with name $owner the problem was solved:
SHOW TAG VALUES FROM "exdemon_analysis" WITH KEY = "analyzed_metric.attributes.$owner"

Related

Rails query to substitute field and query

could someone please help me to write a query to substitute hyphens in rails DB field with space?
For eg:
If I have a field called 'name' in a table User having a value 'asdc-sd bc', and want to remove special characters like '-' and replace it with space to match with a given name 'asdc sd bc'.
I tried using lower to convert the name to lowercase but am not able to find out how to substitute the hyphens with space. Please help!
You can use SQL REPLACE function to replace - with spaces(' ').
For example, if you are querying on name column of User model, you can write your query like below:
query_str = 'asdc sd bc'
User.where("REPLACE(name, '-', ' ') = ?", query_str)

InfluxDB where query on tag values

I am trying to write a where clause on InfluxDB where the points are filtered via their key values.
My points have the field ping_to_google.com and the tag key user where user can be raspi-2 or raspi-5. This is a sample output of the database:
> select * from networks where time > now()-1h
name: networks
time ping_to_google.com user
---- ------------------ ----
1645494054000000000 3.528 raspi-2
1645494078000000000 3.578 raspi-2
I am using InfluxDB version 1.8 and InfluxDB shell version 1.6.4
This query will work!
select * from networks where time > now()-1h AND "user"='raspi-2'
According to the documentation of the influxDB you should only single qoute tag values:
Tags
tag_key <operator> ['tag_value']
Single quote tag values in the WHERE clause. Queries with unquoted tag values or double quoted tag values will not return any data and, in most cases, will not return an error.
They don't specify how to write tag keys. It is crazy that the database does not produce any errors in most cases. Also, it's not clear (and very unexpected after reading this documentation) that the tag key should be double-qouted. But this works and is how to do it!

Rails query by number of digits in field

I have a Rails app with a table: "clients". the clients table has a field: phone. phone data type is string. I'm using postgresql. I would like to write a query which selects all clients which have a phone value containing more than 10 digits. phone does not have a specific format:
+1 781-658-2687
+1 (207) 846-3332
2067891111
(345)222-777
123.234.3443
etc.
I've been trying variations of the following:
Client.where("LENGTH(REGEXP_REPLACE(phone,'[^\d]', '')) > 10")
Any help would be great.
You almost have it but you're missing the 'g' option to regexp_replace, from the fine manual:
The regexp_replace function provides substitution of new text for substrings that match POSIX regular expression patterns. [...] The flags parameter is an optional text string containing zero or more single-letter flags that change the function's behavior. Flag i specifies case-insensitive matching, while flag g specifies replacement of each matching substring rather than only the first one.
So regexp_replace(string, pattern, replacement) behaves like Ruby's String#sub whereas regexp_replace(string, pattern, replacement, 'g') behaves like Ruby's String#gsub.
You'll also need to get a \d through your double-quoted Ruby string all the way down to PostgreSQL so you'll need to say \\d in your Ruby. Things tend to get messy when everyone wants to use the same escape character.
This should do what you want:
Client.where("LENGTH(REGEXP_REPLACE(phone, '[^\\d]', '', 'g')) > 10")
# --------------------------------------------^^---------^^^
Try this:
phone_number.gsub(/[^\d]/, '').length

Passing a string saved in a bash variable with an apostrophe to psql query using a bash script

In a BASH script, I am reading in a list of strings from a text file that may contain apostrophe ('). Each string in the list is saved to a BASH environment variable that is passed to my psql query. I have tried everything so far but still when I loop through the list, if I counter an apostrophe, my query fails.
Here is a snipit of the code that fails:
SELECT * FROM table_1 WHERE id = $myid AND name = '$namelist';
namelist is the file that has the entries which may contain apostrophes.
Thanks for you help
Use a prepared SQL statement to avoid SQL injection.
You may also need a solution from this post.

How can I update parts of the string that matches some regexp

I have string "(1,2,3,4,5,6),(1,2,3)" I would like to change it to "('1','2','3','4','5','6'),('1','2','3')" - replase all parts that mathces /([^,)("])/ with the '$1', '$2' etc
"(1,2,3,4,5,6),(1,2,3)".gsub(/([^,)("]\w*)/,"'\\1'")
gsub is a "global replace" method in String class. It finds all occurrences of given regular expression and replaces them with the string given as the second parameter (as opposed to sub which replaces first occurrence only). That string can contain references to groups marked with () in the regexp. First group is \1, second is \2, and so on.
Try
mystring.gsub(/([\w.]+)/, '\'\1\'')
This will replace numbers (ints/floats) and words with their "quote-surrounded" selves while leaving punctuation (except the dot) alone.
UPDATED: I think you want to search for this
(([^,)("])+)
And replace it with this
'$1'
the looks for anything 1 or more times and assigns it to the $1 variable slot due to using the parenthesis around the "\d". The replace part will use what it finds as the replacement value.

Resources