23 lines
834 B
Python
23 lines
834 B
Python
from flask import current_app, url_for
|
|
import os
|
|
import pandas as pd
|
|
|
|
def prepare_data(df):
|
|
df["timestamp"] = pd.to_datetime(df["timestamp"])
|
|
df["last_action"] = pd.to_datetime(df["last_action"])
|
|
df["prev_timestamp"] = df.groupby("user_id")["timestamp"].shift(1)
|
|
df["was_active"] = (df["timestamp"] - df["last_action"]) <= pd.Timedelta(seconds=60)
|
|
df["was_active"] = df["was_active"].fillna(False)
|
|
df['hour'] = df['timestamp'].dt.hour
|
|
return df
|
|
|
|
def mk_plotdir(output_filename):
|
|
plots_dir = os.path.join(current_app.root_path, "static", "plots")
|
|
os.makedirs(plots_dir, exist_ok=True)
|
|
|
|
output_path = os.path.join(plots_dir, output_filename)
|
|
|
|
plot_url = url_for('static', filename=f'plots/{output_filename}', _external=True)
|
|
|
|
return {'output_path': output_path, 'plot_url': plot_url}
|