What syntax to use for parsing {{ from_dttm }} and {{ to_dttm }} Jinja variables as datetime objects in Custom SQL queries? - postgresql

Question: How to correctly format {{ from_dttm }} and {{ to_dttm }} default Jinja variables so that they are parsed as datetime objects in Apache Superset Custom SQL metrics?
MWE: Say I want to show what is the time range covered by the data I use in my dashboards — what can be affected by the Time Range filter.
I use the public.birth_names demo dataset for the sake of the example.
So I create a BigNumber chart, with the following custom Metric:
age(
{% if from_dttm is not none %}
'{{ from_dttm }}'
{% else %}
min(ds)
{% endif %}
,
{% if to_dttm is not none %}
'{{ to_dttm }}'
{% else %}
max(ds)
{% endif %}
)
However, if I format the Jinja variables as:
{{ from_dttm }}, I get:
Error: syntax error at or near "{"
LINE 1: SELECT age({{ from_dttm }} , '{{ to_dttm }}') AS "age(
'{{ from_dttm }}', I get
Error: invalid input syntax for type timestamp with time zone: "{{ from_dttm }}"
LINE 1: SELECT age('{{ from_dttm }}'
"{{ from_dttm }}", I get
Error: column "{{ from_dttm }}" does not exist
LINE 1: SELECT age("{{ from_dttm }}" ,
I'm using Superset at 5ae7e5499 (Latest commit on Mar 25, 2022), with PostgreSQL as back-end db engine.

After offline discussion with #villebro, it turns out that:
'{{ from_dttm }}' is the valid syntax — one can try with a simpler example: MIN('{{ from_dttm }}'): this works, whereas both other syntaxes yield an error.
however, there is still a bug in current (version 1.4.*) versions, where {{ from_dttm }} is not rendered (what caused AGE() to yield an error in the particular example above). This issue has been raised in #19564, and a fix submitted in #19565.

Related

how to make repo status appear in posh git (oh-my-posh)

so basically i want the repository status to be shown and not hidden like this example :
and my terminal without theme shows the repo status but when i apply a theme the numbers (repo status ) disapear like yo see in this pic :
so any fix to that prob ??
The theme you have chosen does not include the git add/modified/deleted/untracked counts. For example paradox.omp.json does not include them. To include these counts, you can either:
Choose a theme which does include the full git information, such as peru
or
Edit your oh-my-posh theme file (e.g. paradox.omp.json) to include the git counts. Find the git segment, and add the following lines into the properties section:
"fetch_status": true,
"template": "{{ .HEAD }} {{ .BranchStatus }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0}} \uF692 {{ .StashCount }}{{ end }}{{ if gt .WorktreeCount 0}} \uf1bb {{ .WorktreeCount }}{{ end }}"

how to set mongodb replica set using ansible

Need to set up mongo dB replica set in 3 instances ,one can be primary and rest two will be secondary.
Anyone can suggest me about how can I write the playbook.
Have started mongo shell in three servers and initiate the replication name
'''replication:
replSetName: "testingrs"'''
Ansible provides already plugin for it: community.mongodb.mongodb_replicaset
When I deployed my MongoDB sharded cluster, the plugin was still version 1.0 and had many limitations. We also had some problems with installing pymongo, so I developed the tasks manually. However, I think with current version there is no need anymore to write the tasks by your own.
Anyway, my playbook looks like this:
- name: Check if Replicaset is already initialized
shell:
cmd: "/usr/bin/mongo --norc --quiet localhost:{{ ports.config }}"
executable: /bin/bash
stdin: "rs.status().codeName"
register: result
changed_when: false
check_mode: no
- set_fact:
rs_initiate: |
{% set members = [] %}
{% for host in groups['config'] | sort %}
{% set m = {'_id': loop.index0 } %}
{% set _ = m.update({'host': host + '.' + domain + ':' + ports.config | string }) %}
{% set _ = members.append(m) %}
{% endfor %}
{% set init = {'_id': replica_set.conf} %}
{% set _ = init.update({'members': members}) %}
{{ init }}
rs: |
{% set i = (result.stdout == 'NotYetInitialized') %}
{% for host in ansible_play_hosts %}
{% set i = i and (hostvars[host].result.stdout == 'NotYetInitialized') %}
{% endfor %}
{{ {'NotYetInitialized': i} }}
- name: Init Replicaset
shell:
cmd: "/usr/bin/mongo --norc --quiet localhost:{{ ports.config }}"
executable: /bin/bash
stdin: |
rs.initiate({{ rs_initiate | to_json }})
rs.status()
while (! db.isMaster().ismaster ) sleep(1000)
when: rs.NotYetInitialized and inventory_hostname_short == (groups['config'] | sort | first)
One issue I had was to deal with authentication, because when you deploy a MongoDB from scratch then no user exist. Thus when you like to run the playbook multiple times, you have to distinct with and without authentication.
My playbook contains these tasks:
- name: Check if authentication is enabled
shell:
cmd: "/usr/bin/mongo --norc --quiet localhost:{{ ports.router }}"
executable: /bin/bash
stdin: "rs.status().codeName"
register: result
ignore_errors: yes
changed_when: false
when: inventory_hostname_short == (groups['application'] | sort | first)
- name: Authenticate if needed
set_fact:
authenticate: "{{ (result.stdout == 'Unauthorized') | ternary('-u admin -p ' + password[env].admin + ' --authenticationDatabase admin','') }}"
when: inventory_hostname_short == (groups['application'] | sort | first)
- name: Create users
shell:
cmd: "/usr/bin/mongo {{ authenticate }} --norc --quiet localhost:{{ ports.router }}"
executable: /bin/bash
stdin: |
admin = db.getSiblingDB("admin")
admin.createUser({ user: "admin", pwd: "{{ password[env].admin }}", roles: ["root"] })
admin.auth("admin", "{{ password[env].admin }}")
// create more users if needed
admin.createUser(...)
when: inventory_hostname_short == (groups['application'] | sort | first)

Helm template, command

i want to pass two arguments to the to "deployment template" from the values.yaml the result should be like this :
in values.yaml i have this >>>
And here is my template deployment file :
{{ if .Values.containerCommand }}
command: [{{ .Values.containerCommand }}]
{{ end }}
The questions is how can i pass two arguments to "containerCommand" value.
Because when i want to these arguments to the helm, i got an error:
I got an error Error: UPGRADE FAILED: error validating "": error validating data: ValidationError(Deployment.spec.template.spec.containers[0].command[0]): invalid type for io.k8s.api.core.v1.Container.command: got "array", expected "string"
You can iterate through the collection using range operator, like following
{{ if .Values.containerCommand }}
command: {{- range .Values.containerCommand }}
- {{ . }} {{- end}}
{{- end}}
To learn details check here

Ansible - 'unicode object' has no attribute 'file_input'

I'm working with Ansible 2.2.1.0 and I work on a old project made by someone else with errors.
I have the following variables in my code:
software_output:
- { file_input: 'Download_me.zip', file_output: 'download.zip' }
software_version:"0.5,0.6"
And I have this shell module instruction to download on a FTP:
- name: "MySoftware | get package on FTP"
shell: >
curl --ftp-ssl -k {{ ' --ssl-allow-beast ' if os == 'aix' else "" }} -# -f -u {{ ftp_user }}:{{ ftp_password }} -f "{{ ftp_url | replace('##software_version##',item[1]) }}{{ item[0].file_input }}"
-o {{ require_inst_dir }}/{{ item[0].file_output }} 2>/dev/null
with_nested:
- software_output
- "{{ software_version.split(',') }}"
when: software_version is defined
But it doesn't work at all, I have the following error:
'unicode object' has no attribute 'file_input'
It looks like with_nested is not used as it has to be used, did I missed something?
In:
with_nested:
- software_output
software_output is a string software_output.
To refer to the variable value, change to:
with_nested:
- "{{ software_output }}"
Long time ago the first syntax was valid, but it was long time ago.

Use Twig object as tag

Is there any way to use any random object as a tag in Twig?
I know I can set a variable - it's just that this would really be cleaner.
i.e.
{{ get_an_entry() }}
{{ name }} is {{ id }}
{{ end }}
or
{{ entry }}
{{ name }} is {{ id }}
{{ end }}
In the examples, the name and id values would come from entry or the return of get_an_entry()
Yes! It just need the __toString() PHP magic method implemented. An usage like that in fact triggers a string conversion.