How can a user track time in Logseq?

[I’m posting a number of questions, hopefully I or others will provide answers]

2 Likes

I hightly relies on org-clock-in for time log. But there some other things that user can adopt to their own work flow. For example, time-block technique, which the user assign a specific goal during the time period and only focus on the things closely related to the content belong to the block.

1 Like

I’ve used org’s time tracking in the past and I love it. Currently I moved my whole workflow to .md is it possible to still use org-clock-in in a markdown file? Or does it need to be a .org file?

Thanks!

1 Like

This is a very abstract answer. Hopefully someone can provide some low-level info, such as: what do I type or click to start timing a task, to stop timing a task, to see how many minutes have passed, to add up multiple timing sessions for one task, etc.?

5 Likes

I found some documentation on the Org site:
https://orgmode.org/org.html#Clocking-Work-Time

None of this seems to work though. :thinking: Checking tasks as done seem to log the time between task creation and done, but that’s not very helpful.

1 Like

Apparently start time is set when the task is marked as “NOW”, not on creation. So you can start and end tasks pretty well. But only tasks.

To me, three different ways.

  1. The one cited above, to write manually time on the open block that I will work today

  2. Using TODO ! When you use todo/doing/done or now/waiting/later, you have a clock-time inside your todo. At each time that you will move a todo to doing or now, the clock will start. When you have finish, click on doing or now again, if the task is not done, you will see the time passed on it on the right. If it’s done just check the box. You will can query it and access to logbook where all task time is conserved.

  3. Pomodoro, there is too plugin for it. The one named itself Pomodoro, the other is Agenda.
    I prefer the one on Agenda as it keep track of all the pomodoro you did.

I personnaly use 2 and 3 method together. Like that I can see the overall time needed to accomplish a work, and the effective time worked, without procrastination or pauses.

2 Likes

I created a way to log time in logseq and generate a report with a bash script.

First I add entries into my daily journal with this format:

  - #ABCTimeTracking
	- 20min
		- Sterling call to address server issue
  - #XYZTimeTracking
	- 1h 15min
		- Sterling call to address server issue

ABC and XYZ is the “client” variable. I usually use two or three letters to designate a client.
You can make these entries in your journal and the script below will retrieve them by date range.

Save the bash script as logseq_search.sh and make it executable (chmod u+x):

#!/bin/bash

# Directory containing the text files
directory="/home/adam/logseq/journals"

# Function to search for TimeTracking entries based on the provided keywords
search_time_tracking() {
    local keyword=$1
    awk '
    BEGIN {inside=0}
    /#'$keyword'TimeTracking/ {inside=1; print; next}
    inside && /^[ \t]/ {print; next}
    !/^[ \t]/ {inside=0}
    ' "$file"
}

# Function to check if a date is within the range
is_within_range() {
    local date=$1
    local start_date=$2
    local end_date=$3

    if [[ "$date" > "$start_date" && "$date" < "$end_date" ]] || [[ "$date" == "$start_date" ]] || [[ "$date" == "$end_date" ]]; then
        return 0
    else
        return 1
    fi
}

# Function to convert time string to total minutes
convert_time_to_minutes() {
    local time_str=$1
    local total_minutes=0

    if [[ $time_str =~ ([0-9]+)h ]]; then
        total_minutes=$((total_minutes + ${BASH_REMATCH[1]} * 60))
    fi
    if [[ $time_str =~ ([0-9]+)min ]]; then
        total_minutes=$((total_minutes + ${BASH_REMATCH[1]}))
    fi

    echo $total_minutes
}

# Read the start and end dates from arguments
start_date=$(date -d "$1" +"%Y/%m/%d")
end_date=$(date -d "$2" +"%Y/%m/%d")
shift 2

# Declare associative arrays to hold entries and time sums for each keyword
declare -A entries_by_keyword
declare -A time_sum_by_keyword

# Loop through each text file in the directory
# Change year/month if necessary
for file in "$directory"/2024*.md; do
    # Extract date from file path
    file_path=$file
    file_name=$(basename "$file_path")
    date_str=$(echo "$file_name" | grep -oP '\d{4}_\d{2}_\d{2}' | sed 's/_/\//g')

    # Check if the date is within the specified range
    if is_within_range "$date_str" "$start_date" "$end_date"; then
        # Format date
        formatted_date=$(date -d "$date_str" +"%m/%d/%Y")

        # Loop through each keyword argument provided to the script
        for keyword in "$@"; do
            entries=$(search_time_tracking "$keyword")
            if [[ -n "$entries" ]]; then
                # Extract time entries and sum them up
                while IFS= read -r line; do
                    time_str=$(echo "$line" | grep -oP '\b[0-9]+h\b|\b[0-9]+min\b')
                    if [[ -n "$time_str" ]]; then
                        time_in_minutes=$(convert_time_to_minutes "$time_str")
                        ((time_sum_by_keyword[$keyword]+=$time_in_minutes))
                    fi
                done <<< "$entries"

                entries_by_keyword["$keyword"]+=$'\n'"Date: $formatted_date"$'\n'"$entries"$'\n'
            fi
        done
    fi
done

# Print the collected entries and total time, grouped by keyword
for keyword in "$@"; do
    if [[ -n "${entries_by_keyword[$keyword]}" ]]; then
        echo ""
        echo "#################### $keyword TimeTracking"
        echo "${entries_by_keyword[$keyword]}"
        total_time=${time_sum_by_keyword[$keyword]}
        total_hours=$((total_time / 60))
        total_minutes=$((total_time % 60))
        echo "Total Time: $total_hours hours and $total_minutes minutes"
    fi
done

Modify and run this command:

sh ~/logseq_search.sh "2024-06-29" "2024-07-09" ABC XYX # time tracking report

You can add any number of client names, like ABC shown above. You can also choose the date range.

Hope that helps!

There’s currently no official support from wakatime which provides automatic time tracking and report as long as you install it.

I’d suggest that we voice up and get Wakatime to support logseq! It is free and open source and has already been very popular in many many mainstream applications, inlcuding JetBrain family and Obsidian, among other famous applications.

I am not affiliated with wakatime in any way but I am a heavy user of Logseq. I’d really hope that there’s application that can track my time in logseq and requires no human input from myself.