Skip to content
Open
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions app/main/apis/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ..util.dto import MinerDto
from ..service.miner_service import (
create_new_miner,
update_miner,
get_all_miners,
get_miner,
get_healths_by_miner,
Expand All @@ -21,6 +22,7 @@

api = MinerDto.namespace
_miner = MinerDto.miner
_miner_with_gpus = MinerDto.miner_with_gpus
_health = MinerDto.health
_health_aggregate = MinerDto.health_aggregate
_share = MinerDto.share
Expand All @@ -31,24 +33,25 @@
@api.route('/')
@api.response(201, 'Miner successfully created.')
@api.doc('create a new miner')
@api.expect(_miner, validate=True)
class Miner(Resource):
# TODO: remove before full prod deploy
# @token_required
@api.expect(_miner, validate=True)
def post(self):
"""Creates a new Miner """
data = request.json
return create_new_miner(data)

@api.expect(_miner_with_gpus, validate=True)
def put(self):
"""Updates a User's miner """
miner_id = request.json.get('miner_id')
miner_id = request.json.get('id')
miner = get_miner(miner_id)
if not miner:
api.abort(404)
api.abort(404, error="no miner exists for the specified id")
else:
data = request.json
return update(miner, data)
return update_miner(miner, data)


@api.route('/<user_id>/')
Expand Down
6 changes: 5 additions & 1 deletion app/main/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ def delete(obj):

def save_changes(data):
try:
db.session.add(data)
if isinstance(data, list):
db.session.add_all(data)
else:
db.session.add(data)
db.session.commit()
except exc.SQLAlchemyError as e:
logger.error(e)
return Response(response="Operation failed, check logs", status=500, mimetype='text/plain')

37 changes: 37 additions & 0 deletions app/main/service/miner_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,43 @@ def create_new_miner(data):
return response_object, 409


def update_miner(miner, data):
if data.get('name'):
miner.name = data.get('name')

if data.get('user_id'):
miner.user_id = data.get('user_id')

updated_gpus = {
g.get('gpu_no'):
{
'gpu_no': g.get('gpu_no'),
'gpu_name': g.get('gpu_name'),
'miner_id': miner.id
} for g in data.get('gpus')}

new_gpus = []
for gpu_no, gpu in updated_gpus.items():
existing = db.session.query(Gpu).filter_by(gpu_no=gpu_no, miner_id=miner.id).first()
if existing:
for attr, val in gpu.items():
setattr(existing, attr, val)
else:
new_gpus.append(gpu)

save_error = save_changes(miner)
if save_error: return save_error

save_error = save_changes(new_gpus)
if save_error: return save_error

response_object = {
'status': 'success',
'message': 'Successfully updated.'
}
return response_object, 200


def get_all_miners(user):
return db.session.query(Miner).filter_by(user=user).all()

Expand Down
12 changes: 12 additions & 0 deletions app/main/util/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,20 @@ class MinerDto:

miner = namespace.model('miner', {
'id': fields.String(required=False, description="the miners UUID"),
'name': fields.String(required=True, description="name of the miner set by the user"),
'user_id': fields.String(required=False, description="user id of the user that the miner belongs to"),
})

gpu = namespace.model('gpu', {
'gpu_no': fields.Integer(required=True, description="index of the gpu in the miner"),
'gpu_name': fields.String(required=True, description="the brand and name of the gpu"),
})

miner_with_gpus = namespace.model('miner_update', {
'id': fields.String(required=True, description="the miners UUID"),
'name': fields.String(required=False, description="name of the miner set by the user"),
'user_id': fields.String(required=False, description="user id of the user that the miner belongs to"),
'gpus': fields.List(fields.Nested(gpu), required=False, description="an array of gpu objects")
})

share = namespace.model('share', {
Expand Down