Skip to content

Commit

Permalink
webhook_history stores UNIX timestamps instead of DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jaitjacob committed Sep 19, 2024
1 parent 6aa1d5e commit cf1cb81
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Change Webhook History timestamp column type from DateTime to Integer UNIX timestamp
Revision ID: bb52d9f878f5
Create Date: 2024-09-19 18:07:28.504280
"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import func


# revision identifiers, used by Alembic.
revision = 'bb52d9f878f5'
down_revision = '06b208e317a3'
branch_labels = None
depends_on = None


def upgrade():
op.add_column('webhook_history', sa.Column('created_on', sa.Integer(), nullable=True))
op.drop_column('webhook_history', 'timestamp')

def downgrade():
op.add_column('webhook_history', sa.Column('timestamp', sa.DateTime(timezone=True), server_default=func.now()))
op.drop_column('webhook_history', 'created_on')
7 changes: 0 additions & 7 deletions frontend/coprs_frontend/coprs/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,6 @@ def time_ago(time_in, until=None):
diff = now - datetime.datetime.fromtimestamp(time_in)
return humanize.naturaldelta(diff)

@app.template_filter('humanize_datetime')
def humanize_datetime(datetime_in):
"""Convert DateTime object to human readable form of elapsed time."""
if datetime_in is None:
return " - "
return humanize.naturaltime(datetime.datetime.now(tz=datetime.timezone.utc) - datetime_in)

@app.template_filter("natural_time_delta")
def natural_time_delta(seconds: int) -> str:
"""
Expand Down
2 changes: 1 addition & 1 deletion frontend/coprs_frontend/coprs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,7 @@ def backend_enqueue_buildchroots(self):
class WebhookHistory(db.Model):
'''Represents a Webhook UUID & a build initiated by it.'''
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(DateTime(timezone=True), server_default=func.now())
created_on = db.Column(db.Integer, nullable=True)
# Null values are possible via custom webhook implementation that do not pass a UUID or User Agent.
user_agent = db.Column(db.Text,nullable=True)
webhook_uuid = db.Column(db.Text, nullable=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h3>Webhook History</h3>
</thead>
{% for webhook in webhook_history %}
<tr>
<td class="webhook_timestamp" data-toggle="tooltip" title="{{webhook.timestamp|localized_time(g.user.timezone)}}">{{webhook.timestamp|localized_time(g.user.timezone)}} ({{webhook.timestamp|humanize_datetime()}})</td>
<td class="webhook_timestamp" data-toggle="tooltip" title="{{webhook.created_on|localized_time(g.user.timezone)}}">{{webhook.created_on|localized_time(g.user.timezone)}} ({{webhook.created_on|time_ago()}})</td>
<td>{{webhook.webhook_uuid}} </td>
<td>{{webhook.user_agent}}</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import tempfile
import time
import shutil
from typing import Optional

Expand Down Expand Up @@ -91,7 +92,8 @@ def add_webhook_history_record(webhook_uuid, user_agent='Not Set',
log.debug("No build initiated. Webhook not logged to db.")
return

webhookRecord = models.WebhookHistory(webhook_uuid=webhook_uuid,
webhookRecord = models.WebhookHistory(created_on=int(time.time()),
webhook_uuid=webhook_uuid,
user_agent=user_agent)
db.session.add(webhookRecord)
db.session.commit()
Expand Down

0 comments on commit cf1cb81

Please sign in to comment.