-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from Freika/exports
Exports
- Loading branch information
Showing
38 changed files
with
502 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.5.3 | ||
0.6.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
class ExportsController < ApplicationController | ||
before_action :authenticate_user! | ||
before_action :set_export, only: %i[destroy] | ||
|
||
def index | ||
@exports = current_user.exports.order(created_at: :desc).page(params[:page]) | ||
end | ||
|
||
def create | ||
export_name = "#{params[:start_at].to_date}_#{params[:end_at].to_date}" | ||
export = current_user.exports.create(name: export_name, status: :created) | ||
|
||
ExportJob.perform_later(export.id, params[:start_at], params[:end_at]) | ||
|
||
redirect_to exports_url, notice: 'Export was successfully initiated. Please wait until it\'s finished.' | ||
rescue StandardError => e | ||
export&.destroy | ||
|
||
redirect_to exports_url, alert: "Export failed to initiate: #{e.message}", status: :unprocessable_entity | ||
end | ||
|
||
def destroy | ||
@export.destroy | ||
|
||
redirect_to exports_url, notice: 'Export was successfully destroyed.', status: :see_other | ||
end | ||
|
||
private | ||
|
||
def set_export | ||
@export = current_user.exports.find(params[:id]) | ||
end | ||
|
||
def export_params | ||
params.require(:export).permit(:name, :url, :status) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ExportsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class ExportJob < ApplicationJob | ||
queue_as :exports | ||
|
||
def perform(export_id, start_at, end_at) | ||
export = Export.find(export_id) | ||
|
||
Exports::Create.new(export:, start_at:, end_at:).call | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class Export < ApplicationRecord | ||
belongs_to :user | ||
|
||
enum status: { created: 0, processing: 1, completed: 2, failed: 3 } | ||
|
||
validates :name, presence: true | ||
|
||
before_destroy :delete_export_file | ||
|
||
private | ||
|
||
def delete_export_file | ||
file_path = Rails.root.join('public', 'exports', "#{name}.json") | ||
|
||
File.delete(file_path) if File.exist?(file_path) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
class Exports::Create | ||
def initialize(export:, start_at:, end_at:) | ||
@export = export | ||
@user = export.user | ||
@start_at = start_at | ||
@end_at = end_at | ||
end | ||
|
||
def call | ||
export.update!(status: :processing) | ||
|
||
points = time_framed_points(start_at, end_at, user) | ||
data = ::ExportSerializer.new(points, user.email).call | ||
file_path = Rails.root.join('public', 'exports', "#{export.name}.json") | ||
|
||
File.open(file_path, 'w') { |file| file.write(data) } | ||
|
||
export.update!(status: :completed, url: "exports/#{export.name}.json") | ||
rescue StandardError => e | ||
Rails.logger.error("====Export failed to create: #{e.message}") | ||
|
||
export.update!(status: :failed) | ||
end | ||
|
||
private | ||
|
||
attr_reader :user, :export, :start_at, :end_at | ||
|
||
def time_framed_points(start_at, end_at, user) | ||
user.tracked_points.without_raw_data.where('timestamp >= ? AND timestamp <= ?', start_at.to_i, end_at.to_i) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<% content_for :title, "Exports" %> | ||
|
||
<div class="w-full"> | ||
<div class="flex justify-between items-center mb-5"> | ||
<h1 class="font-bold text-4xl">Exports</h1> | ||
</div> | ||
|
||
<div id="exports" class="min-w-full"> | ||
<% if @exports.empty? %> | ||
<div class="hero min-h-80 bg-base-200"> | ||
<div class="hero-content text-center"> | ||
<div class="max-w-md"> | ||
<h1 class="text-5xl font-bold">Hello there!</h1> | ||
<p class="py-6"> | ||
Here you'll find your exports, created on <%= link_to 'Points', points_url, class: 'link' %> page. But now there are none. | ||
</p> | ||
</div> | ||
</div> | ||
</div> | ||
<% else %> | ||
<div class="overflow-x-auto"> | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Status</th> | ||
<th>Actions</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @exports.each do |export| %> | ||
<tr> | ||
<td><%= export.name %></td> | ||
<td><%= export.status %></td> | ||
<td> | ||
<% if export.completed? %> | ||
<%= link_to 'Download', export.url, class: "px-4 py-2 bg-blue-500 text-white rounded-md", download: export.name %> | ||
<% end %> | ||
<%= link_to 'Delete', export, data: { confirm: "Are you sure?", turbo_confirm: "Are you sure?", turbo_method: :delete }, method: :delete, class: "px-4 py-2 bg-red-500 text-white rounded-md" %> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,52 @@ | ||
<% content_for :title, 'Imports' %> | ||
|
||
<div class="w-full"> | ||
<div class="flex justify-between items-center"> | ||
<h1 class="font-bold text-4xl">Imports</h1> | ||
<%= link_to "New import", new_import_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> | ||
</div> | ||
|
||
<div id="imports" class="min-w-full"> | ||
<div class="overflow-x-auto"> | ||
<table class="table"> | ||
<!-- head --> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Processed</th> | ||
<th>Doubles</th> | ||
<th>Created at</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @imports.each do |import| %> | ||
<% if @imports.empty? %> | ||
<div class="hero min-h-80 bg-base-200"> | ||
<div class="hero-content text-center"> | ||
<div class="max-w-md"> | ||
<h1 class="text-5xl font-bold">Hello there!</h1> | ||
<p class="py-6"> | ||
Here you'll find your imports, But now there are none. Let's <%= link_to 'create one', new_import_path, class: 'link' %>! | ||
</p> | ||
</div> | ||
</div> | ||
</div> | ||
<% else %> | ||
<div class="overflow-x-auto"> | ||
<table class="table"> | ||
<!-- head --> | ||
<thead> | ||
<tr> | ||
<td> | ||
<%= link_to import.name, import, class: 'underline hover:no-underline' %> (<%= import.source %>) | ||
</td> | ||
<td> | ||
<%= "✅" if import.processed == import.raw_points %> | ||
<%= "#{import.processed}/#{import.raw_points}" %> | ||
</td> | ||
<td><%= import.doubles %></td> | ||
<td><%= import.created_at.strftime("%d.%m.%Y, %H:%M") %></td> | ||
<th>Name</th> | ||
<th>Processed</th> | ||
<th>Doubles</th> | ||
<th>Created at</th> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</div> | ||
</thead> | ||
<tbody> | ||
<% @imports.each do |import| %> | ||
<tr> | ||
<td> | ||
<%= link_to import.name, import, class: 'underline hover:no-underline' %> (<%= import.source %>) | ||
</td> | ||
<td> | ||
<%= "✅" if import.processed == import.raw_points %> | ||
<%= "#{import.processed}/#{import.raw_points}" %> | ||
</td> | ||
<td><%= import.doubles %></td> | ||
<td><%= import.created_at.strftime("%d.%m.%Y, %H:%M") %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
Oops, something went wrong.