Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for ASRC, comment for posts and any updates #52

Merged
merged 17 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
3104835
UPD: добавляет параметризированные запросы на API (start_datetime end…
owlscatcher Mar 4, 2024
ce40b2c
FIX: исправляет seeds
owlscatcher Mar 4, 2024
3d511ea
UPD: добавляет Faker в проект и обновляет сиды
owlscatcher Mar 27, 2024
3118c31
FIX: исправляет проблему с медленным вызовом на api/v1/{:id}/history
owlscatcher Mar 27, 2024
b458742
ADD: добавляет Makefile для удобства
owlscatcher Mar 27, 2024
20927f3
UPD: обновляет Seed, обновляет Dockerfile и Gemfile
owlscatcher Mar 27, 2024
44e9c30
UPD: обновляет вызов API на index роут с параметрами
owlscatcher Mar 27, 2024
6a00937
ADD: добавляет миддлвар для органичивания количества запросов на API …
owlscatcher Mar 27, 2024
2c65f6e
UPD: применяет правила линтера rubocop
owlscatcher Mar 28, 2024
b6e66c8
ADD: добавляет лимит на размер временного окна выборки по всей таблиц…
owlscatcher Mar 28, 2024
b662680
FIX: исправляет на верный HTTP код ответ при неправильном окне запроса
owlscatcher Mar 28, 2024
a841c88
FIX: исправляет ошибку при запросе на History без параметров
owlscatcher Mar 28, 2024
b995c95
UPD: Обновляет посты, добавляет комментарии и лайки
owlscatcher Jun 4, 2024
885cbf1
UPD: Обновляет посты, добавляет комментарии и лайки (1)
owlscatcher Jun 4, 2024
d4dd757
UPD: Обновляет посты, добавляет комментарии и лайки (2)
owlscatcher Jun 4, 2024
620a390
UPD: Обновляет readme
owlscatcher Jun 7, 2024
f83ce3f
Fixed CI
owlscatcher Oct 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
env:
COVERAGE: true
RAILS_ENV: test
run: docker-compose --file docker-compose.ci.yml run ci
run: docker compose --file docker-compose.ci.yml run ci
367 changes: 186 additions & 181 deletions .rubocop.yml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions .slim-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
AllCops:
NewCops: enable
TargetRubyVersion: "3.2"

linters:
LineLength:
max: 120
RedundantDiv:
enabled: false
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ ADD . $RAILS_ROOT
ENV PATH=$RAILS_ROOT/bin:${PATH}

EXPOSE 3000
CMD ["bin/prod"]
CMD ["make start"]
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ gem 'sidekiq', '~> 7.1', '>= 7.1.1'
gem 'sidekiq-cron', '~> 1.10', '>= 1.10.1'
gem 'sidekiq-failures', '~> 1.0', '>= 1.0.4'

gem 'ancestry', '~> 4.3', '>= 4.3.3'
gem 'pagy', '~> 6.0'
gem 'ransack', '~> 4.0'
gem 'responders', '~> 3.1'
Expand All @@ -66,11 +67,16 @@ gem 'prawn-table', '~> 0.2.2'
gem 'devise', '~> 4.2'
gem 'cancancan', '~> 3.5'
gem 'tzinfo'
gem 'slim', '~> 5.2', '>= 5.2.1'
gem 'slim_lint', '~> 0.27.0'
gem 'slim-rails', '~> 3.6', '>= 3.6.3'

group :development, :test do
gem 'debug', platforms: [:mri, :mingw, :x64_mingw]

gem 'factory_bot_rails'
gem 'rubocop'
gem 'faker'
end

group :development do
Expand Down
21 changes: 21 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
ancestry (4.3.3)
activerecord (>= 5.2.6)
ast (2.4.2)
bcrypt (3.1.18)
bindex (0.8.1)
Expand Down Expand Up @@ -114,6 +116,8 @@ GEM
factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
faker (3.3.0)
i18n (>= 1.8.11, < 2)
fugit (1.8.1)
et-orbi (~> 1, >= 1.2.7)
raabro (~> 1.4)
Expand Down Expand Up @@ -256,6 +260,16 @@ GEM
simple_form (5.2.0)
actionpack (>= 5.2)
activemodel (>= 5.2)
slim (5.2.1)
temple (~> 0.10.0)
tilt (>= 2.1.0)
slim-rails (3.6.3)
actionpack (>= 3.1)
railties (>= 3.1)
slim (>= 3.0, < 6.0, != 5.0.0)
slim_lint (0.27.0)
rubocop (>= 1.0, < 2.0)
slim (>= 3.0, < 6.0)
sprockets (4.2.0)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand All @@ -272,7 +286,9 @@ GEM
state_machines-activemodel (>= 0.8.0)
stimulus-rails (1.2.1)
railties (>= 6.0.0)
temple (0.10.3)
thor (1.2.1)
tilt (2.3.0)
timeout (0.3.1)
ttfunk (1.7.0)
turbo-rails (1.3.3)
Expand Down Expand Up @@ -307,6 +323,7 @@ PLATFORMS

DEPENDENCIES
active_model_serializers (~> 0.10.13)
ancestry (~> 4.3, >= 4.3.3)
bcrypt (~> 3.1, >= 3.1.18)
bootsnap
cancancan (~> 3.5)
Expand All @@ -315,6 +332,7 @@ DEPENDENCIES
debug
devise (~> 4.2)
factory_bot_rails
faker
importmap-rails
jbuilder
jsbundling-rails
Expand All @@ -333,6 +351,9 @@ DEPENDENCIES
sidekiq-cron (~> 1.10, >= 1.10.1)
sidekiq-failures (~> 1.0, >= 1.0.4)
simple_form (~> 5.2)
slim (~> 5.2, >= 5.2.1)
slim-rails (~> 3.6, >= 3.6.3)
slim_lint (~> 0.27.0)
sprockets-rails
state_machines (~> 0.5.0)
state_machines-activerecord (~> 0.8.0)
Expand Down
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
setup:
bundle install
yarn install
yarn build
yarn build:css
bin/rails db:drop db:create db:migrate db:seed

start:
bundle install
yarn install
yarn build
yarn build:css
rm ./tmp/pids/server.pid
bin/dev

clean:
bin/rails db:drop db:create db:migrate db:seed


check: test lint

lint:
bundle exec rubocop -a
# bundle exec slim-lint app/views/

test:
bin/rails test

.PHONY: test
71 changes: 23 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,74 +9,49 @@ for ARMStrong, OMIT and assistant for shield "D".

## Before start

### Configuring your workspace:

#### Install and configure Docker and Docker-Compose;

Installing:
### Local installation:

```bash
sudo dnf remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine

sudo dnf -y install dnf-plugins-core

sudo dnf config-manager \
--add-repo \
https://download.docker.com/linux/fedora/docker-ce.repo

sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin
git clone [email protected]:digital-armstrong/Armstrong.Web.git && \
cd ./Armstrong.Web && \
docker compose run --rm web bash -c "make setup"
```

Fixing problem:
This commands cloned repo and setup prroject dependency. Also you can manually install all deps:

```bash
usermod -a -G docker $USER
bundle install
yarn install
yarn build
yarn build:css
bin/rails db:drop db:create db:migrate db:seed
```

Start after system be load:
## Start

After installing the dependencies, you can run the project in docker compose:

```bash
sudo systemctl enable docker
docker compose up --build
```

#### Install [ZSH and Oh-my-zsh](https://ohmyz.sh/)
That's all. =)

Install:
## Other options

```bash
sudo dnf install zsh
```
All of the below commands must be executed inside the container.

```bash
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
```
Connect to a running container: `docker exec -it armstrongweb_web_1 bash`
Clean startup with command execution: `docker compose run --rm web bash -c "<your command>"`

Set as default shell:
### Refresh database

```bash
chsh -s $(which zsh)
make cleanup
```

#### Install [git-flow](https://github.com/petervanderdoes/gitflow-avh)

Install:
### Test and lint

```bash
$ wget -q https://raw.githubusercontent.com/petervanderdoes/gitflow-avh/develop/contrib/gitflow-installer.sh && sudo bash gitflow-installer.sh install stable; rm gitflow-installer.sh
make check
```

### Read manual

- [Git-flow](https://jeffkreeftmeijer.com/git-flow/)
- [Git-flow usage](https://github.com/petervanderdoes/gitflow-avh#git-flow-usage)


7 changes: 6 additions & 1 deletion app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ def create

def destroy
user = User.find(params[:id])
assigned_inspections_count = Inspection.where(creator_id: params[:id]).or(Inspection.where(performer_id: params[:id])).count + user.posts.count
assigned_inspections_count =
Inspection.
where(creator_id: params[:id]).
or(Inspection.where(performer_id: params[:id])).
count + user.posts.
count

if assigned_inspections_count.zero?
@user.destroy
Expand Down
37 changes: 33 additions & 4 deletions app/controllers/api/v1/histories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,48 @@ module Api
class V1::HistoriesController < ApplicationController
before_action :set_channel, only: [:show]

def index
datetime_interval = diff_hours(params[:start_datetime], params[:end_datetime])
histories = if params[:start_datetime].present? && params[:end_datetime].present? && datetime_interval < 2
History.where(event_datetime: params[:start_datetime]..params[:end_datetime])
else
{
code: 403,
message: 'Forbidden. Parameters is not a valid or range of the datetime is too long.',
hint: 'The interval between start_datetime and end_datetime should be no more than two hours.',
}.to_json
end

render(json: histories, except: [:id, :event_impulse_value, :event_not_system_value, :created_at, :updated_at])
end

def show
histories = History.select { |hs| hs.channel_id == @channel_id }.last(100)
histories = if params[:start_datetime].present? && params[:end_datetime].present?
History.where(event_datetime: params[:start_datetime]..params[:end_datetime],
channel_id: @channel.id)
else
History.where(channel_id: @channel.id).last(100)
end

result = histories.sort { |a, b| a[:event_datetime] <=> b[:event_datetime] }

render(json: result)
render(json: result, except: [:created_at, :updated_at])
end

private

def set_channel
@channel = Channel.find(params[:id])
@channel_id = @channel.id
rescue ActiveRecord::RecordNotFound => e
render(json: { error: e }, status: :not_found)
end

# Returns the integer value of the difference
# between the start and end datetime values
def diff_hours(start_date, end_date)
if start_date && end_date
diff_in_seconds = end_date.to_time - start_date.to_time
(diff_in_seconds / (60**2)).to_i
end
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def current_ability

private

def set_time_zone(&block)
def set_time_zone(&)
timezone = if current_user.nil?
'UTC'
else
current_user.timezone
end
Time.use_zone(timezone, &block)
Time.use_zone(timezone, &)
end

protected
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/concerns/organization_concern.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module OrganizationConcern
extend ActiveSupport::Concern

# rubocop:disable Metrics/BlockLength

included do
def organization_index
@query = Organization.ransack(params[:q])
Expand Down Expand Up @@ -57,4 +59,6 @@ def organization_params
)
end
end

# rubocop:enable Metrics/BlockLength
end
8 changes: 4 additions & 4 deletions app/controllers/inspection_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def tasks(condition, method_name)
@query = Inspection.ransack(params[:q])
@query.sorts = ['updated_at desc']
@previous_action = method_name
if method_name != :service_tasks
@pagy, @inspections = pagy(@query.result.where(condition).
includes(:device, :creator, :performer))
else
if method_name == :service_tasks
@pagy, @inspections = pagy(@query.result.joins(:device).where(condition).
includes(:device, :creator, :performer))
else
@pagy, @inspections = pagy(@query.result.where(condition).
includes(:device, :creator, :performer))
end
end

Expand Down
9 changes: 9 additions & 0 deletions app/controllers/posts/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Posts
class ApplicationController < ApplicationController
def resource_post
@resource_post ||= Post.find(params[:post_id])
end
end
end
24 changes: 24 additions & 0 deletions app/controllers/posts/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Posts
class CommentsController < ApplicationController
before_action :authenticate_user!, only: [:create]

def create
@comment = resource_post.comments.build(comments_params)
@comment.user = current_user

if @comment.save
redirect_to(resource_post, notice: t('.success'))
else
redirect_to(resource_post, alert: @comment.errors.full_messages.join('. '))
end
end

private

def comments_params
params.require(:post_comment).permit(:content, :parent_id)
end
end
end
Loading
Loading