Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analytics #768

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Add analytics #768

wants to merge 3 commits into from

Conversation

humphd
Copy link
Collaborator

@humphd humphd commented Jan 3, 2025

Part of #765. I've added code to pull some analytics from the db, and created two ways to view it:

  • use the /analytics slash command, which dumps your stats as JSON into messages in the chat
  • in the Settings modal, I added another temporary pane, to try adding some charts

The UI for this is all terrible at the moment, but the stuff you get out of it is cool. It would be easy to add more.

Copy link

cloudflare-workers-and-pages bot commented Jan 4, 2025

Deploying chatcraft-org with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2ebeff6
Status: ✅  Deploy successful!
Preview URL: https://6b6a6d44.console-overthinker-dev.pages.dev
Branch Preview URL: https://humphd-analytics.console-overthinker-dev.pages.dev

View logs

@humphd humphd marked this pull request as ready for review January 4, 2025 21:34
@humphd
Copy link
Collaborator Author

humphd commented Jan 4, 2025

OK, made this better:

  • /analytics works, but I've added an "Analytics" menu item
  • both add an app message that shows analytics
  • you can switch between periods (hour, day, week, ...)

Here's how it looks:

Screenshot 2025-01-04 at 4 31 31 PM

Could use a few improvements that I'm too lazy to figure out:

  • How to support "Delete" in these ComponentMessages
  • Better colours for dark vs. light mode (Rechart might support it, not sure)
  • More analytics...

@Kannav02
Copy link
Contributor

Kannav02 commented Jan 7, 2025

Hey @humphd ,

For the analytics, I think adding user metrics to supplement the given analytics would be good, like kind of having an average token usage per user, error rate trend

I was also wondering, what the "Delete" functionality stands for here, is it related to how the analytics are generated when some responses are deleted? I am a bit confused

Also, If you want I can work on adding more analytics and any additional features.

Thank you for your time

src/lib/analytics.ts Outdated Show resolved Hide resolved
@humphd
Copy link
Collaborator Author

humphd commented Jan 14, 2025

Switched to use the new DuckDB stuff, and it mostly works, but the SQL is not right, since my graphs don't look right. My SQL isn't correct.

Screenshot 2025-01-13 at 9 14 02 PM

@tarasglek
Copy link
Owner

tarasglek commented Jan 14, 2025

It would be helpful to get a better description of what these queries are aiming.

@tarasglek
Copy link
Owner

tarasglek commented Jan 14, 2025

Note querying SQL in a time series way used too advanced for me. Your time series query still is

  • You are doing too much, you can do the time interval(eg year) + bucket(day) outside of SQL and dramatically simplify, eg pass em as params
  • Rest of it looks correct, just need to look at raw data..looking

@tarasglek
Copy link
Owner

tarasglek commented Jan 14, 2025

So this is the query i got

WITH time_period AS (
  SELECT INTERVAL '1 month' AS period
),
hourly_stats AS (
  SELECT 
    DATE_TRUNC('hour', CAST(date AS TIMESTAMP)) AS hour,
    COUNT(DISTINCT chatId) AS num_chats,
    COUNT(*) AS num_messages,
    SUM(LENGTH(text)) AS chars
  FROM messages
  WHERE CAST(date AS TIMESTAMP) >= CURRENT_DATE - (SELECT period FROM time_period)
  GROUP BY DATE_TRUNC('hour', CAST(date AS TIMESTAMP))
)
SELECT 
  strftime('%Y-%m-%d %H:00', hour) AS hour,
  num_chats,
  num_messages,
  chars
FROM hourly_stats
ORDER BY hour;

gonna pause here for now. it seems to make sense on my data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants