Elastic D&D - Update 16 - Bug & Logic Fixes
Tags: elasticsearch, python
Published: 2024-01-12 21:12:26

In the last post we talked about how fixing the password reset function. If you missed it, you can check that out [here](https://dev.to/thtmexicnkid/elastic-dd-week-15-fixing-password-reset-3nb4)!

## Bug & Logic Fixes
First of all, Happy New Year everyone! 

I haven't added any major features, but I have gotten to work on a few minor things:
* Added a function to get current and previous session numbers

The query in this function gets a recent log that is NOT from "today" ("now/d" is the current date) and grabs the "session" field. It then adds 1 to that value for current session and returns both numbers.

```
def elastic_get_session_numbers(log_index):
    # creates Elastic connection
    client = Elasticsearch(
        elastic_url,
        ca_certs=elastic_ca_certs,
        api_key=elastic_api_key
    )

    # gets last session number
    response = client.search(index=log_index,size=1,sort=["@timestamp:desc"],source=["session"],query={"bool":{"must":[{"range":{"@timestamp":{"lt":"now/d"}}}]}})
    
    # grab last session number and calculate current session number
    last_session = response["hits"]["hits"][0]["_source"]["session"]
    current_session = int(last_session) + 1
    
    return last_session, current_session
```

* Added current session number to the sidebar

The following code is added to the bottom of the sidebar, displaying the current session number.

```
st.text("Current session: " + str(st.session_state.current_session))
```

* Fixed the function to get previous session summary to use previous session number

The function now uses the following code for the Elastic query, which specifies the session number grabbed from the function described above:

```
response = client.search(index=log_index,size=1,source=["message"],query={"bool":{"must":[{"match":{"type":"overview"}},{"match":{"session":session_number}}]}})
```

* Fixed the function to get previous session summary to return generic message if no overview note was added in that session

This part was added out of necessity, as an error would occur if there was no overview log to be found. I'm giving a public thank you to @rusty13jr for finding this for me!

```
try:
        summary = response["hits"]["hits"][0]["_source"]["message"]
    except:
        summary = "No overview log was submitted from last session."
    
    return summary
```

* Added placeholders for question prompts that Veverbot cannot use AI to answer

These will be used in the near future, and will do the work necessary to return useful information to questions that cannot utilize the KNN search.

```
# question prompts
    column1, column2, column3, column4, column5 = st.columns(5)
    column1.button("Question 1 Placeholder",type="primary",on_click=None)
    column2.button("Question 2 Placeholder",type="primary",on_click=None)
    column3.button("Question 3 Placeholder",type="primary",on_click=None)
    column4.button("Question 4 Placeholder",type="primary",on_click=None)
    column5.button("Question 5 Placeholder",type="primary",on_click=None)
    st.header("",divider="grey")
```

## Closing Remarks
I finally have my group in the program and taking notes. Them using it has exposed some bugs and code errors, ultimately making the program better. This is much appreciated by me, so please reach out with any issues you run into if you are using the program as well!

Check out the GitHub repo below. You can also find my Twitch account in the socials link, where I will be actively working on this during the week while interacting with whoever is hanging out!

[GitHub Repo](https://github.com/thtmexicnkid/elastic-dnd)
[Socials](https://allmylinks.com/thtmexicnkid)

Happy Coding,
Joe