How to organize ids for resources in hyperledger composer - hyperledger

In our model file, both participants and assets are identified by an id. When we are creating new instances of these, we are required to give it an id. like
var newAsset = getFactory().newResource(org.biznet,'ourAsset',assetID);
My question is how do we organize the IDs for our assets. I want the assets in ourAsset registry to be in some kind of order like 1,2,3 and I want to automate that so that every time a new ourAsset is being created it reads the id of the previous entry in the registry and increments it by 1.

Hyperledger dont have an auto-increment logic.
You should create your own logic.
However I sugest you to use hash instead of auto-increment.
There is a lib in Nodejs for that: uuid

Related

Is it possible to define templates in yaml for Alice Fixtures, but creating the object themselves via PHP

I'm new to Alice, and I'm trying to find my way with it. If you think the paradigm I chose is wrong, I would love to hear a better approach.
Let's assume I have the following template
# temlpate.yaml
stdClass:
user (template):
userId: '<randomNumber()>'
username: <email()>
firstName: <firstName()>
I know I can create the template user by creating the following yaml (that is working)
# user_fixtrue.yaml
stdClass:
user1 (extends user):
But sometimes I would like to create a user and override one or any mix of the user properties. Which means I would need to create a file per scenario, and injecting params, something like
# user_fixtrue_params.yaml
stdClass:
user2 (extends user):
userId: <{userIdFromPhp}>
And using it via PHP with
$loader->loadFile("user_fixtrue_params.yaml", ["userIdFromPhp" => 1234]);
If I would like to override the username as well I'll need another yaml to define it.
And if on top of it if I would like different ranges (different amount of objects, feature request can be seen here) I would need to specify a yaml file for each combination of range and properties.
I would rather load the templates for yaml, and create the users using PHP, but I didn't find any way to do so. Any idea, or do I miss something?
As an alternative I thought having all the templates definitions on PHP, but I couldn't manage to create an object in one file, where the template defined on another file using PHP. If that can be done I would love to see an example.
Thank you

Redis delete by pattern is too slow

for i, name in ipairs(redis.call('KEYS''cache:user_transaction_logs:*:8866666')) do redis.call('DEL', name); end"
How can I Optimise this redis query?
We are using Redis as cache store in Rails.Whenever auser makes a successfull transaction The receivers and initiators transaction history is expired from redis
The query can not be optimized - it should be replaced in its entirety because the use of KEYS is discouraged for anything other than debugging purposes on non-production environments.
A preferable approach, instead of trying to fetch the relevant key names ad-hoc, is to manage them in a data structure (e.g. Set or List) and read from it when you perform the deletions.
You need to change the approach for how you are storing cache entries for your users.
Your keys should look something like cache:user_transaction_logs:{user_id}.
Then you will be able to just delete the entry by its key (user_id).
In case if you need several cache entries per user_id - use Redis hashes (https://redis.io/commands#hash), and then again you will be able to delete all entries per user_id with one command DELETE or needed entry with HDEL.
Also a good idea to use Redis database numbers (default 0, 1-15 available) and put separate functionalities on separate database numbers. Then in case if you need to wipe cache of whole functionality that can be done with one command FLUSHDB

Cloning documents within a Mongo database

I have a mongodb database that I use mongoid to access via a rails 3 application. The database consists of around 10-15 collections. Some of the documents in these collections have embedded documents and other documents are linked by id.
I need to clone most of the data in the database to create new records. These new records will need to co-exist with their cloned counterparts while they are translated by our client. These new records must maintain the same relationships as they did before however the newly cloned records need to point to their newly cloned counterparts.
Considerations include: A number of has one relationships that have a "foreign key" that will need to be updated on clone. Some documents have embedded documents that will need to be cloned with their parents. Clonee documents will not be able to relate to their cloned documents in anyway.
Solutions Considered: The first option was to duplicate the database and try and merge everything that does not need to be cloned. Might be a little messy and I am assuming that existing ID would get cloned too. The second option I considered was to write a script that would iterate though each Mongoid document class and called clone however I found out that monogid.clone does a shallow copy not a deep drudge. So for this solution I would have to write a case in which embedded relationships where detected in order to perform a deep copy. This also could get messy.
Is there an option I have not considered here? Is there a better way to go about one of the considered solutions? Am I up against it?
Watching the discussion in the comments, I'd say that if .clone does not work, you can easily do that in a compact way with the attributes, read_attribute, write_attribute methods. Excerpt from here.
# Get the field values as a hash.
person.attributes
# Set the field values in the document.
Person.new(first_name: "Jean-Baptiste", middle_name: "Emmanuel")
person.attributes = { first_name: "Jean-Baptiste", middle_name: "Emmanuel" }
person.write_attributes(
first_name: "Jean-Baptiste",
middle_name: "Emmanuel"
)

What command can I use to create this Ruby on Rails model?

I'm creating a model in Ruby on Rails that will act like a file system. You'll have assets (like files) that could either be folders or files themselves. How can I create a command for this?
Asset
id (unique auto-incrementing number)
name
is_directory (bool)
user_id (id of the owner)
parent_asset_id (id of parent directory, or null if under the root)
access_token (randomly generated token, used to send shareable links)
contents
I'm thinking something like:
rails generate model Asset
name:string
is_directory:boolean
user_id:integer
parent_asset_id:integer
access_token:string contents:??
Some questions I have:
What's the difference betweeen blob vs longblob vs mediumblob vs longtext vs etc, and which would I want to use? (The assets are essentially text... not sure what the max size will be yet)
Is the parent_asset_id a good naming convention, or is there something else that would make Rails give me some secret sauce, similar to why I picked the name user_id (to match the User model)?
Is there a way to declare a default random string value for the access_token? (The access token will be used for a shareable link to the asset)
Anything else I'm overlooking?
This is a detailed question, so I hope it serves as a case study for anyone looking to implement something like a file system in RoR.
Obviously if you really wanted to implement a file system you'd use an actual file system or Amazon S3... but if you want a light-weight file-like system in RoR, this seems like the best approach.
First off, I strongly recommend you consider an existing gem like Paperclip, which will handle a lot of these details for you.
Answers in order:
You would use the binary field type to store general data, but if you use Paperclip there are some specific fields you would need to use instead, which are explained in Paperclip's docs.
If by parent_asset_id you really mean the asset can 'belong' to many other models, then look into setting up a polymorphic relationship, with an id and type field. If instead you mean storing the path to the stored file, then Paperclip handles this for you. See #3 for details...
You can access a stored file on Paperclip by calling something as simple as asset.url in your view. If you wish to go manual and insert a random code, you can insert a callback into your Asset.rb model that does something like:
before_create :generate_key
def generate_key
self.key = ActiveSupport::SecureRandom.hex
end
S3 is not a complex system to set up on Rails, and it is far more flexible and scaleable than storing the files elsewhere - however, if you want to, then use the 'assets' path.

How to create a new database from within a Rails app?

I'm working on a Rails app that has one database per account. (I know this is a controversial approach in itself, but I'm confident it's the right one in this case.)
I'd like to automate entirely the process of creating a new user account, which means I need to be able create a new database and populate it with some seed data programatically from within a Rails app.
My question, then, is how best to do this? I don't think I can just run migrations from within the app (or, if I can, how?), and just running the straight SQL queries within the app with hardcoded CREATE TABLE statements seems a really unwieldy way of doing things. What approach should I take, then?
Thanks in advance for your help!
David
This is an approach that my application requires. The app provides a web front-end onto a number of remote embedded devices which in turn monitor sensors. Each embedded device runs a ruby client process which reads a config file to determine its setup. There is a need to be able to add a new sensor type.
The approach I have is that each sensor type has it's own data table, which is written into by every device which has that sensor. So in order to be able to create a new sensor type, I need to be able to set up new tables.
One initial issue is that the remote embedded devices do not have a rails app on them - therefore table name pluralization is a bad plan, as the pluralization rules are not accessible to the remote devices. Therefore I set
ActiveRecord::Base.pluralize_table_names = false
in config/environment.rb
The data on each sensor device type is held in a SensorType model - which has two fields - the sensor name, and the config file contents.
Within the SensorType model class, there are methods for:
Parsing the config file to extract field names and types
Creating a migration to build a new model
Altering a particular field in the DB from a generic string to char(17) as it is a MAC address used for indexing
Altering the new model code to add appropriate belongs_to relationships
Build partial templates for listing the data in the table (a header partial and a line_item partial)
These methods are all bound together by a create_sensor_table method which calls all the above, and performs the appropriate require or load statements to ensure the new model is immediately loaded. This is called from the create method in the SensorTypeController as follows:
# POST /device_types
# POST /device_types.xml
def create
#sensor_type = SensorType.new(params[:sensor_type])
respond_to do |format|
if #sensor_type.save
#sensor_type.create_sensor_tables
flash[:notice] = 'SensorType was successfully created.'
#etc

Resources