Skip to main content

A utility module to count/aggregate logs along time.

Project description

log-count-utils

Introduction & Usage

Suppose we have an action log data df like

user_id timestamp expense
0 2021-02-18 10:00:00 100
0 2021-02-18 10:00:10 10
0 2021-02-18 10:00:21 1
0 2021-02-18 11:00:21 0.1
1 2020-02-18 10:00:10 100
1 2020-02-18 10:00:20 10
1 2020-02-18 10:00:20 1
1 2020-02-18 10:00:29 0

Suppose that you have to compute the following quantity for each row in this dataframe:

  • the number of actions each user has taken within 10 seconds
  • total amount of expenses of a user within 10 seconds

The following naive way is fine for this tiny example but becomes costly (O(N^2)) for large data frame.

from datetime import timedelta
import numpy as np

td = timedelta(seconds=10)

answers = []
for uid, time_point in zip(df.user_id, df.timestamp):
    cnt = np.sum(
        (df.user_id == uid) & (df.timestamp < time_point) & (df.timestamp >= (time_point - td))
    )
    answers.append(cnt)

If df is sorted (by user_id as the primary and timestamp as the secondary key), we can do this blazing fast (O(N)) using log_count_util.

from log_count_util import find_n_records_within_interval

answers = find_n_records_within_interval(
    df.user_id, df.timestamp, df_user_id, df.timestamp, td
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

log-count-util-0.1.0.tar.gz (9.2 kB view hashes)

Uploaded Source

Built Distributions

log_count_util-0.1.0-cp39-cp39-win_amd64.whl (61.1 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

log_count_util-0.1.0-cp39-cp39-win32.whl (54.0 kB view hashes)

Uploaded CPython 3.9 Windows x86

log_count_util-0.1.0-cp39-cp39-manylinux2010_x86_64.whl (809.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

log_count_util-0.1.0-cp39-cp39-manylinux2010_i686.whl (806.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

log_count_util-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl (73.6 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

log_count_util-0.1.0-cp38-cp38-win_amd64.whl (61.6 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

log_count_util-0.1.0-cp38-cp38-win32.whl (54.0 kB view hashes)

Uploaded CPython 3.8 Windows x86

log_count_util-0.1.0-cp38-cp38-manylinux2010_x86_64.whl (808.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

log_count_util-0.1.0-cp38-cp38-manylinux2010_i686.whl (805.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

log_count_util-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl (73.4 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

log_count_util-0.1.0-cp37-cp37m-win_amd64.whl (62.1 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

log_count_util-0.1.0-cp37-cp37m-win32.whl (54.6 kB view hashes)

Uploaded CPython 3.7m Windows x86

log_count_util-0.1.0-cp37-cp37m-manylinux2010_x86_64.whl (821.5 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

log_count_util-0.1.0-cp37-cp37m-manylinux2010_i686.whl (812.1 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

log_count_util-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl (72.9 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page