Developing for Splunk Enterprise

Django - Web Framework - Split very long command in multiple lines ?

SplunkTrust
SplunkTrust

Hi All,

I have a very simple question, i could not yet answer myself, how can we achieve to split commands within a Django view into multiple lines for more readability and visibility ?

I have some views with commands becoming more and more complex while my Django dev increased, one command per line becomes very hard to read, a backslash "\" does not work and generates a page error, a line return doesn't work either...

Thanks a lot!

Tags (2)
0 Karma

SplunkTrust
SplunkTrust

Let me give you an example of what i called a very long and complex search, in the present case this concerns a prediction interface about IBM Mainframes CPU consummation, the user has various setting choices downstreamed by forms.
Also some periods (4) will provide the user a way to influence the result. (to integrate projects prevision and so on)

This kind of complex search will necessary be quite long...

    search='index=$index$ report=$report$ PSERIES=$valuespseries$ curr_shared_proc_pool_name="DefaultPool" OR "Not_Applicable" earliest="$earlyval$" latest="$lateval$" PoolIdle>0 | eval conso=round(max_conso_pool,2) | eval percent_conso=(conso/CPU_active)*100 | eval percent_conso=round(percent_conso,2) | eval percent_conso_installed=(conso/CPU_installed)*100 | eval percent_conso_installed=round(percent_conso_installed,2) | where ( conso>0) AND ( percent_conso<100 ) | dedup _time,PSERIES | timechart span=$span$ max(conso) As max_conso | predict max_conso As Projection lower30=low upper30=high future_timespan=$future_timespan$ algorithm=$algorithm$ holdback=$holdback$ | eval Projection=if(_time>if("$manualtime_period1$" == "now", now(),strptime("$manualtime_period1$", "%d/%m/%Y")),Projection+$manualcpus_period1$,Projection) | eval Projection=if(_time>if("$manualtime_period2$" == "now", now(),strptime("$manualtime_period2$", "%d/%m/%Y")),Projection+$manualcpus_period2$,Projection) | eval Projection=if(_time>if("$manualtime_period3$" == "now", now(),strptime("$manualtime_period3$", "%d/%m/%Y")),Projection+$manualcpus_period3$,Projection) | eval Projection=if(_time>if("$manualtime_period4$" == "now", now(),strptime("$manualtime_period4$", "%d/%m/%Y")),Projection+$manualcpus_period4$,Projection) | eval low=if(_time>if("$manualtime_period1$" == "now", now(),strptime("$manualtime_period1$", "%d/%m/%Y")),low+$manualcpus_period1$,low) | eval low=if(_time>if("$manualtime_period2$" == "now", now(),strptime("$manualtime_period2$", "%d/%m/%Y")),low+$manualcpus_period2$,low) | eval low=if(_time>if("$manualtime_period3$" == "now", now(),strptime("$manualtime_period3$", "%d/%m/%Y")),low+$manualcpus_period3$,low) | eval low=if(_time>if("$manualtime_period4$" == "now", now(),strptime("$manualtime_period4$", "%d/%m/%Y")),low+$manualcpus_period4$,low) | eval high=if(_time>if("$manualtime_period1$" == "now", now(),strptime("$manualtime_period1$", "%d/%m/%Y")),high+$manualcpus_period1$,high) | eval high=if(_time>if("$manualtime_period2$" == "now", now(),strptime("$manualtime_period2$", "%d/%m/%Y")),high+$manualcpus_period2$,high) | eval high=if(_time>if("$manualtime_period3$" == "now", now(),strptime("$manualtime_period3$", "%d/%m/%Y")),high+$manualcpus_period3$,high) | eval high=if(_time>if("$manualtime_period4$" == "now", now(),strptime("$manualtime_period4$", "%d/%m/%Y")),high+$manualcpus_period4$,high)'|token_safe 

Even with some optimization that would always be possible (and even without the story of four manual periods), you can easily see how it can be quite hard to read within a same string line

0 Karma

Motivator

Also, here is many ways through javascript to have multiple lines for one: http://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript

0 Karma

SplunkTrust
SplunkTrust

Yes you're definitively right, this search has to be improved 🙂 I'll take note of your comment thanks

0 Karma

Motivator

Since there is a lot of repetition there you could split out a huge section of it into 2 javascript functions. that simply return the portion of text that is repeated with a variable for Period.

0 Karma

SplunkTrust
SplunkTrust

Hi,

Thank you for answering.

The second, i mean i can't break the search line string within a search manager into multiple lines for more visibility in a django view.

It seems we have to keep all the command line on the same line or it breaks the page and generates some Django error page.

In a simple or advanced xml view, you can simply use a line return in the same command.

As far an other example, in savedsearches.conf you can break (split) search line into multiple by suffixing with a backslash until the last line.

I have several views with complex searches that cannot be decomposed more than than they already are (forms downstreaming variables depending on users choices and various reasons due to the data itself and operation required)

That would be much better to be able to split a search line into multiple line until the end of the search, is it impossible in django html view ?

Thanks for you help

0 Karma

Splunk Employee
Splunk Employee

Do you mean splitting up various arguments of a template tag, like this:

{% block managers %}
    {% searchmanager
        autostart=False
        id="mysearch"
        search="| metadata index=_internal type=sourcetypes"
    %}
    {% searchmanager
        autostart=True
        id="othersearch"
        search="index=_internal sourcetype=$mysourcetype$ | head 10"|token_safe
    %}
{% endblock managers %}

Or did you mean that if a search string is very long, it cannot be broken up into multiple lines? If it is the latter, I'd recommend actually setting your long searches as variables in your view function rather than doing it in the template.

0 Karma