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

Feature/versioning #779

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"no-console": ["off"],
"comma-dangle": [1,"only-multiline"],
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
"prefer-destructuring": ["error", {"object": true, "array": false}]
"prefer-destructuring": ["error", {"object": true, "array": false}],
"react/forbid-prop-types": ["off"]
}
}
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ gem 'whenever', require: false

gem 'yaml_db'

gem 'logidze'

group :development do
gem 'better_errors' # allows to debug exception on backend from browser

Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ GEM
listen (3.3.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
logidze (1.2.0)
activerecord (>= 5.0)
railties (>= 5.0)
ruby-next-core (~> 0.9)
loofah (2.17.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -641,6 +645,7 @@ GEM
activesupport
memoist
ruby-mailchecker (3.2.29)
ruby-next-core (0.13.1)
ruby-ole (1.2.12.2)
ruby-progressbar (1.10.1)
ruby2_keywords (0.0.2)
Expand Down Expand Up @@ -828,6 +833,7 @@ DEPENDENCIES
ketcherails!
launchy (~> 2.4.3)
listen
logidze
memory_profiler
meta_request
net-sftp
Expand Down
3 changes: 3 additions & 0 deletions app/api/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'grape-swagger'

class API < Grape::API
include LogidzeModule

format :json
prefix :api
version 'v1'
Expand Down Expand Up @@ -143,6 +145,7 @@ def to_json_camel_case(val)
mount Chemotion::NmrdbAPI
mount Chemotion::MeasurementsAPI
mount Chemotion::ConverterAPI
mount Chemotion::VersionAPI

add_swagger_documentation(info: {
"title": "Chemotion ELN",
Expand Down
126 changes: 126 additions & 0 deletions app/api/chemotion/version_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
require 'open-uri'

module Chemotion
class VersionAPI < Grape::API
include Grape::Kaminari
helpers ParamsHelpers

namespace :versions do
resource :samples do
desc 'Return versions of the given sample'

params do
requires :id, type: Integer, desc: 'Sample id'
end

paginate per_page: 10, offset: 0, max_per_page: 100

route_param :id do
get do
# find specific sample and load only required data
sample = Sample.select(:id, :name, :log_data, :updated_at).find(params[:id])

analyses = sample.analyses.flat_map { |analysis| analysis.self_and_descendants.select(:id, :name, :updated_at, :log_data) }

# create cache key for sample
timestamp = [
sample.updated_at,
analyses.map(&:updated_at).max,
Attachment.where(attachable_id: analyses.map(&:id), attachable_type: 'Container').maximum(:updated_at)
].reject(&:nil?).max.to_i
cache_key = "versions/samples/#{sample.id}/#{timestamp}"

# cache processed and sorted versions to speed up pagination
versions = Rails.cache.fetch cache_key do
all_versions = sample.versions_hash
all_versions += sample.residues.select(:sample_id, :log_data).flat_map do |residue|
residue.versions_hash(sample.name)
end
all_versions += sample.elemental_compositions.select(:sample_id, :log_data).flat_map do |elemental_composition|
elemental_composition.versions_hash(sample.name)
end

analyses.each do |analysis|
all_versions += analysis.versions_hash
all_versions += analysis.attachments.select(:attachable_id, :attachable_type, :filename, :log_data).flat_map do |attachment|
attachment.versions_hash(attachment.filename)
end
end

all_versions.sort_by! { |version| -version['t'].to_i } # sort versions with the latest changes in the first place
.each_with_index { |record, index| record['v'] = all_versions.length - index } # adjust v to be uniq and in right order
end

{ versions: paginate(Kaminari.paginate_array(versions)) }
end
end
end

resource :reactions do
desc 'Return versions of the given reaction'

params do
requires :id, type: Integer, desc: 'Reaction id'
end

paginate per_page: 10, offset: 0, max_per_page: 100

route_param :id do
get do
# find specific sample and load only required data
reaction = Reaction.select(:id, :name, :log_data, :updated_at).find(params[:id])

analyses = (
reaction.analyses +
reaction.samples.includes(:container).pluck('containers.id').flat_map { |container_id| Container.analyses_for_root(container_id) }
).flat_map { |analysis| analysis.self_and_descendants.select(:id, :name, :updated_at, :log_data) }

# create cache key for reaction
timestamp = [
reaction.updated_at,
reaction.samples.with_deleted.maximum(:updated_at),
reaction.reactions_samples.with_deleted.maximum(:updated_at),
analyses.map(&:updated_at).max,
Attachment.where(attachable_id: analyses.map(&:id), attachable_type: 'Container').maximum(:updated_at)
].reject(&:nil?).max.to_i
cache_key = "versions/reactions/#{reaction.id}/#{timestamp}"

# cache processed and sorted versions of all reaction dependent records and merge them into one list to speed up pagination
versions = Rails.cache.fetch cache_key do
all_versions = reaction.versions_hash

analyses.each do |analysis|
all_versions += analysis.versions_hash
all_versions += analysis.attachments.select(:attachable_id, :attachable_type, :filename, :log_data).flat_map do |attachment|
attachment.versions_hash(attachment.filename)
end
end

samples = reaction.samples.with_deleted.select('samples.id, samples.name, samples.log_data')
samples.each do |sample|
all_versions += sample.versions_hash
all_versions += sample.residues.select(:sample_id, :log_data).flat_map do |residue|
residue.versions_hash(sample.name)
end
all_versions += sample.elemental_compositions.select(:sample_id, :log_data).flat_map do |elemental_composition|
elemental_composition.versions_hash(sample.name)
end
end

reactions_samples = reaction.reactions_samples.with_deleted.select(:sample_id, :log_data, :type)
all_versions += reactions_samples.flat_map do |reactions_sample|
sample = samples.detect { |s| s.id == reactions_sample.sample_id }
reactions_sample.versions_hash(sample.name)
end

all_versions.sort_by! { |version| -version['t'].to_i } # sort versions with the latest changes in the first place
.each_with_index { |record, index| record['v'] = all_versions.length - index } # adjust v to be uniq and in right order
end

{ versions: paginate(Kaminari.paginate_array(versions)) }
end
end
end
end
end
end
21 changes: 21 additions & 0 deletions app/api/modules/logidze_module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

# A helper to help logidize track current_user
module LogidzeModule
extend ActiveSupport::Concern

included do
before do
if current_user.present? && request.request_method.in?(%w[PATCH POST PUT DELETE])
@logidze_meta_set ||= begin
Logidze.with_responsible!(current_user.id)
true
end
end
end

after do
Logidze.clear_responsible! if @logidze_meta_set.present?
end
end
end
24 changes: 24 additions & 0 deletions app/assets/stylesheets/version.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.row.row-version-history { // overwrite bootstrap
display: flex;
flex-wrap: wrap;
font-size: 10px;
margin: 0;
word-break: break-all;

& + .row-version-history {
margin-top: 15px;
}

& > [class*="col-"] {
padding: 10px;
}

.ql-editor {
padding: 0;
}

.ql-tooltip.ql-hidden {
height: 0;
padding-top: 10px;
}
}
6 changes: 6 additions & 0 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@


class Attachment < ApplicationRecord
include Versionable

include AttachmentJcampAasm
include AttachmentJcampProcess
include AttachmentConverter
Expand All @@ -52,6 +54,10 @@ class Attachment < ApplicationRecord

belongs_to :attachable, polymorphic: true, optional: true
has_one :report_template
<<<<<<< HEAD
=======

>>>>>>> Add a change history /w logidze

scope :where_research_plan, lambda { |c_id|
where(attachable_id: c_id, attachable_type: 'ResearchPlan')
Expand Down
Loading