<a id="debugging"></a>

# Debugging

Here we would like to describe possible techniques that could help you to find and understand issues in your code.

## Show stack trace

We provide ability to show line of code where the issue happens in case if code is written in onetick.py.

In code your set or [`otp.config['show_stack_info']`](https://docs.pip.distribution.sol.onetick.com/api/config.html.md#onetick.py.configuration.Config) or the `OTP_SHOW_STACK_INFO` env variable to enable this option (more about it is [here](https://docs.pip.distribution.sol.onetick.com/static/configuration/root.html.md#configuration))

In tests your could use the `--show-stack-trace` [option](https://docs.pip.distribution.sol.onetick.com/static/testing/features.html.md#show-stack-trace-flag).

## Dumping ticks

The [`otp.Source.dump()`](https://docs.pip.distribution.sol.onetick.com/api/source/dump.html.md#onetick.py.Source.dump) method allows to print out ticks
in different parts of the complex onetick-py queries.

That might help to find ticks that leads to incorrect behaviour and reproduce it then in tests.

Simple example:

```default
t = otp.Ticks(A=[1, 2, 3])
t.dump()
t = t.first()
df = otp.run(t)
```

Standard output (the result of [`dump()`](https://docs.pip.distribution.sol.onetick.com/api/source/dump.html.md#onetick.py.Source.dump) method):

```default
#TIMESTAMP,A
01-12-2003 00:00:00.000000000,1
01-12-2003 00:00:00.001000000,2
01-12-2003 00:00:00.002000000,3
```

The result of the query:

```default
print(df)
        Time  A
0 2003-12-01  1
```

## onetick view

`onetick view` is a terminal UI for browsing OneTick databases and their content.

This tool is built on top of the `onetick.py` and distributed
as a separate python package.

The `onetick-ext-view` python package is available on PyPI [https://pypi.org/project/onetick-ext-view](https://pypi.org/project/onetick-ext-view):

```default
pip install onetick-ext-view
```

After installation a developer just needs to go to a folder with OneTick databases
and execute the `onetick view` command in terminal -- it automatically discovers
databases and shows them.

You can also connect to remote Tick Server by specifying it's address via --remote-ts argument.

Usually it's convenient to use this tool along with the `--keep-generated` [flag in tests](https://docs.pip.distribution.sol.onetick.com/static/testing/features.html.md#keep-generated-flag),
because this flag provides all databases are used in tests with their content.

![image](static/testing/images/onetick-view.png)

## onetick read

`onetick read` is a CLI tool to read OneTick databases.

See the full documentation in `onetick read` [Guide](https://docs.pip.distribution.sol.onetick.com/static/tools/read.html.md#onetick-read).

## Save .otq file

.otq file is a OneTick text file that represents OneTick query graph and its parameters.

[`otp.Source`](https://docs.pip.distribution.sol.onetick.com/api/source/root.html.md#onetick.py.Source) object has [`.to_otq()`](https://docs.pip.distribution.sol.onetick.com/api/source/to_otq.html.md#onetick.py.Source.to_otq) method
that allows to save a file on the disk with goal to check the OTQ graph.

#### NOTE
This method has symbols and start/end query interval parameters
that can be passed there the same as for [`otp.run`](https://docs.pip.distribution.sol.onetick.com/api/run.html.md#onetick.py.run) method.
But in this case these parameters will be written to the .otq file itself.

It's often useful when you think the `onetick.py` constructs the wrong OneTick graph
or if you want to check some implementation details.

Simple example that creates an .otq file and returns a path to it (with the name of the main query inside the file):

```default
t = otp.Tick(A=1)
t.to_otq()
# '/tmp/test_user/run_20251202_181018_11054/impetuous-bullfrog.to_otq.otq::query'
```

In this file you will see the OneTick graph query representation and its parameters:

```default
[query]
...
NODE_2 = TICK_GENERATOR(BUCKET_INTERVAL=0,BUCKET_TIME=BUCKET_START,FIELDS="long A=1")
...

[_meta]
...
end_expression = PARSE_NSECTIME("%Y-%m-%d %H:%M:%S.%J", "2003-12-04 00:00:00.000000000", _TIMEZONE)
start_expression = PARSE_NSECTIME("%Y-%m-%d %H:%M:%S.%J", "2003-12-01 00:00:00.000000000", _TIMEZONE)
TZ = EST5EDT
USER_NAME = user
...
```

## Setting name of the query

Method [`otp.Source.set_name`](https://docs.pip.distribution.sol.onetick.com/api/source/set_name.html.md#onetick.py.Source.set_name) can be used to set the name of the source.
This name will be used as a part of the resulting .otq file name and as the name of the query inside this file.

## Graph rendering

Alternatively we could propose to use the [`otp.Source.render_otq()`](https://docs.pip.distribution.sol.onetick.com/api/source/render_otq.html.md#onetick.py.Source.render_otq) method
or [`render_otq`](https://docs.pip.distribution.sol.onetick.com/api/misc/render.html.md#onetick.py.utils.render_otq) function on an .otq file,
which draws a OneTick query graph in different formats using [graphviz](https://graphviz.org/) library.

It could help with quick investigations, because the OneTick .otq file format is not readable for big queries.

Simple example that draws a graph in the default  *.svg* format and returns a path to it:

```default
t = otp.Tick(A=1)
t.render_otq()
# '/tmp/test_user/run_20251202_181018_11054/flat-lemur.svg'
```

In this file you will see the OneTick graph query visual representation:

![image](static/testing/images/flat-lemur.svg)

#### NOTE
You can also try to use the older [`.render()`](https://docs.pip.distribution.sol.onetick.com/api/source/render.html.md#onetick.py.Source.render) method
of the [`otp.Source`](https://docs.pip.distribution.sol.onetick.com/api/source/root.html.md#onetick.py.Source), however it produces less useful output by different methods.

## Logging symbols

Configuration parameter [`otp.config.log_symbol`](https://docs.pip.distribution.sol.onetick.com/api/config.html.md#onetick.py.configuration.Config.log_symbol)
and parameter `log_symbol` of function [`otp.run`](https://docs.pip.distribution.sol.onetick.com/api/run.html.md#onetick.py.run) can be used to enable
printing of each processed symbol to the standard output.

Note that this method has some limitations:

- In this case function [`otp.run`](https://docs.pip.distribution.sol.onetick.com/api/run.html.md#onetick.py.run) is called in callback mode,
  thus it doesn't return pandas.DataFrame or other object.
- This only works with unbound symbols, so it will not work
  if [`otp.merge`](https://docs.pip.distribution.sol.onetick.com/api/functions/merge.html.md#onetick.py.merge) is used to merge symbols into one flow.
