This commit is contained in:
NY 2025-03-07 13:52:35 +08:00
parent 82f69596b8
commit c4534abe52
2 changed files with 62 additions and 38 deletions

View File

@ -1,11 +1,12 @@
import pytz
from pkg.tool import get_tweets_since_last_friday
from pkg.tool import get_tweets_since_last_friday,format_time_str
from pkg.dash.app_init import app
from dash.dependencies import Input, Output
from datetime import timedelta
from datetime import datetime
from dash import html
@app.callback(
[Output('info-tooltip', 'children')],
[Input('clock-interval', 'n_intervals')]
@ -15,38 +16,51 @@ def update_info(n):
pace = calculate_tweet_pace()
decline_rates = calculate_pace_decline_rate()
pace_increases = calculate_pace_increase_in_hour()
table_rows = [
html.Tr([html.Td('Pace (Xtracker)', style={'textAlign': 'center'}),
html.Td(pace, style={'textAlign': 'center'})]),
html.Tr([html.Td('-Minute', style={'textAlign': 'center'}),
html.Td(decline_rates['decline_per_minute'], style={'textAlign': 'center'})]),
html.Tr([html.Td('+0(1h)', style={'textAlign': 'center'}),
html.Td(decline_rates['decline_per_hour'], style={'textAlign': 'center'})]),
html.Tr([html.Td('+1(1h)', style={'textAlign': 'center'}),
html.Td(pace_increases['increase_1'], style={'textAlign': 'center'})]),
html.Tr([html.Td('+5(1h)', style={'textAlign': 'center'}),
html.Td(pace_increases['increase_5'], style={'textAlign': 'center'})]),
html.Tr([html.Td('+10(1h)', style={'textAlign': 'center'}),
html.Td(pace_increases['increase_10'], style={'textAlign': 'center'})]),
html.Tr([html.Td('+20(1h)', style={'textAlign': 'center'}),
html.Td(pace_increases['increase_20'], style={'textAlign': 'center'})]),
html.Tr([html.Td('+30(1h)', style={'textAlign': 'center'}),
html.Td(pace_increases['increase_30'], style={'textAlign': 'center'})]),
_, days_to_next_friday = get_pace_params()
table_style_border = {'textAlign': 'center', 'border': '1px solid white'}
table_style = {'textAlign': 'center'}
# First table for Pace
pace_table_rows = [
html.Tr([
html.Th('Pace', style=table_style_border),
html.Th('DCL', style=table_style_border),
html.Th('0(1h)', style=table_style_border),
html.Th('1(1h)', style=table_style_border),
html.Th('5(1h)', style=table_style_border),
html.Th('10(1h)', style=table_style_border),
html.Th('20(1h)', style=table_style_border),
html.Th('30(1h)', style=table_style_border)
]),
html.Tr([
html.Td(f"{pace:.2f}", style=table_style_border),
html.Td(decline_rates, style=table_style_border),
html.Td(f"{pace_increases['increase_0']:.2f}", style=table_style_border),
html.Td(f"{pace_increases['increase_1']:.2f}", style=table_style_border),
html.Td(f"{pace_increases['increase_5']:.2f}", style=table_style_border),
html.Td(f"{pace_increases['increase_10']:.2f}", style=table_style_border),
html.Td(f"{pace_increases['increase_20']:.2f}", style=table_style_border),
html.Td(f"{pace_increases['increase_30']:.2f}", style=table_style_border)
]),
html.Tr([
html.Td(f"Remain: {format_time_str(days_to_next_friday)}",
colSpan=8,
style=table_style)
])
]
table = html.Table(table_rows, style={
pace_table = html.Table(pace_table_rows, style={
'width': '100%',
'textAlign': 'left',
'borderCollapse': 'collapse'
})
return [table]
return [pace_table]
def get_pace_params():
est = pytz.timezone('US/Eastern')
now = datetime.now(est)
today = now.date()
days_to_next_friday = (4 - today.weekday()) % 7
if days_to_next_friday == 0:
days_to_next_friday = 7
next_friday = (now.replace(hour=12, minute=0, second=0, microsecond=0) +
timedelta(days=days_to_next_friday))
if now > next_friday:
@ -55,31 +69,31 @@ def get_pace_params():
tweet_count = get_tweets_since_last_friday()
return tweet_count, days_to_next_friday
def calculate_tweet_pace():
tweet_count, days_to_next_friday = get_pace_params()
pace = (tweet_count / (7 - days_to_next_friday)) * days_to_next_friday + tweet_count
return round(pace, 6) if pace > 0 else float(tweet_count)
def calculate_pace_decline_rate():
tweet_count, days_to_next_friday = get_pace_params()
T = 7
decline_per_day = -(tweet_count * T) / ((T - days_to_next_friday) ** 2)
decline_per_hour = decline_per_day / 24
decline_per_minute = decline_per_hour / 60
return {
'decline_per_hour': round(decline_per_hour, 6),
'decline_per_minute': round(decline_per_minute, 6)
}
return round(decline_per_hour, 2)
def calculate_pace_increase_in_hour():
tweet_count, days_to_next_friday = get_pace_params()
current_pace = (tweet_count / (7 - days_to_next_friday)) * days_to_next_friday + tweet_count
future_days = days_to_next_friday - (1 / 24)
increments = [1, 5, 10, 20, 30]
increments = [0, 1, 5, 10, 20, 30]
pace_increases = {}
for increment in increments:
new_tweet_count = tweet_count + increment
new_pace = (new_tweet_count / (7 - future_days)) * future_days + new_tweet_count
pace_increase = new_pace - current_pace
pace_increases[f'increase_{increment}'] = round(pace_increase, 6)
# Add current pace to the increase value
pace_increases[f'increase_{increment}'] = round(current_pace + pace_increase, 2)
return pace_increases

View File

@ -54,18 +54,28 @@ def get_tweets_since_last_friday():
est = pytz.timezone('US/Eastern')
now_est = datetime.now(est)
today = now_est.date()
days_since_last_friday = (today.weekday() - 4) % 7
last_friday = today - timedelta(days=days_since_last_friday)
last_friday_datetime = est.localize(datetime.combine(last_friday, datetime.strptime("12:00", "%H:%M").time()))
this_friday = today - timedelta(days=(today.weekday() - 4) % 7)
days_since_friday = (today.weekday() - 4) % 7
this_friday = today - timedelta(days=days_since_friday)
this_friday_datetime = est.localize(datetime.combine(this_friday, datetime.strptime("12:00", "%H:%M").time()))
if now_est < this_friday_datetime and today.weekday() != 4:
last_friday -= timedelta(days=7)
last_friday_datetime = est.localize(datetime.combine(last_friday, datetime.strptime("12:00", "%H:%M").time()))
last_friday = this_friday - timedelta(days=7)
last_friday_datetime = est.localize(datetime.combine(last_friday, datetime.strptime("12:00", "%H:%M").time()))
if now_est < this_friday_datetime:
start_datetime = last_friday_datetime
else:
start_datetime = this_friday_datetime
if hasattr(render_data, 'global_df') and not render_data.global_df.empty:
df = render_data.global_df.copy()
mask = df['datetime_est'] >= last_friday_datetime
mask = df['datetime_est'] >= start_datetime
filtered_df = df[mask]
tweet_count = len(filtered_df)
return int(tweet_count)
return 0
return 0
def format_time_str(days_to_next_friday):
total_seconds = days_to_next_friday * 24 * 60 * 60
days = int(total_seconds // (24 * 60 * 60))
hours = int((total_seconds % (24 * 60 * 60)) // (60 * 60))
minutes = int((total_seconds % (60 * 60)) // 60)
seconds = int(total_seconds % 60)
total_hours = round(days_to_next_friday * 24, 2)
return f"{days}d {hours}h {minutes:02d}m {seconds:02d}s ({total_hours}h)"