| OLD | NEW |
| 1 # Copyright 2015 The LUCI Authors. All rights reserved. | 1 # Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """This module facilitates conversion from dictionaries to ProtoRPC messages. | 5 """This module facilitates conversion from dictionaries to ProtoRPC messages. |
| 6 | 6 |
| 7 Given a dictionary whose keys' names and values' types comport with the | 7 Given a dictionary whose keys' names and values' types comport with the |
| 8 fields defined for a protorpc.messages.Message subclass, this module tries to | 8 fields defined for a protorpc.messages.Message subclass, this module tries to |
| 9 generate a Message instance that corresponds to the provided dictionary. The | 9 generate a Message instance that corresponds to the provided dictionary. The |
| 10 "normal" use case is for ndb.Models which need to be represented as a | 10 "normal" use case is for ndb.Models which need to be represented as a |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 Returns None when input is 0 or None. | 63 Returns None when input is 0 or None. |
| 64 """ | 64 """ |
| 65 if not value: | 65 if not value: |
| 66 return None | 66 return None |
| 67 try: | 67 try: |
| 68 return utils.timestamp_to_datetime(value*1000000.) | 68 return utils.timestamp_to_datetime(value*1000000.) |
| 69 except OverflowError as e: | 69 except OverflowError as e: |
| 70 raise ValueError(e) | 70 raise ValueError(e) |
| 71 | 71 |
| 72 | 72 |
| 73 def bot_info_to_rpc(entity, now): | 73 def bot_info_to_rpc(entity, now, deleted=False): |
| 74 """"Returns a swarming_rpcs.BotInfo from a bot.BotInfo.""" | 74 """"Returns a swarming_rpcs.BotInfo from a bot.BotInfo.""" |
| 75 return _ndb_to_rpc( | 75 return _ndb_to_rpc( |
| 76 swarming_rpcs.BotInfo, | 76 swarming_rpcs.BotInfo, |
| 77 entity, | 77 entity, |
| 78 dimensions=_string_list_pairs_from_dict(entity.dimensions), | 78 dimensions=_string_list_pairs_from_dict(entity.dimensions), |
| 79 is_dead=entity.is_dead(now), | 79 is_dead=entity.is_dead(now), |
| 80 bot_id=entity.id, | 80 bot_id=entity.id, |
| 81 deleted=deleted, |
| 81 state=json.dumps(entity.state, sort_keys=True, separators=(',', ':'))) | 82 state=json.dumps(entity.state, sort_keys=True, separators=(',', ':'))) |
| 82 | 83 |
| 83 | 84 |
| 84 def bot_event_to_rpc(entity): | 85 def bot_event_to_rpc(entity): |
| 85 """"Returns a swarming_rpcs.BotEvent from a bot.BotEvent.""" | 86 """"Returns a swarming_rpcs.BotEvent from a bot.BotEvent.""" |
| 86 return _ndb_to_rpc( | 87 return _ndb_to_rpc( |
| 87 swarming_rpcs.BotEvent, | 88 swarming_rpcs.BotEvent, |
| 88 entity, | 89 entity, |
| 89 dimensions=_string_list_pairs_from_dict(entity.dimensions), | 90 dimensions=_string_list_pairs_from_dict(entity.dimensions), |
| 90 state=json.dumps(entity.state, sort_keys=True, separators=(',', ':')), | 91 state=json.dumps(entity.state, sort_keys=True, separators=(',', ':')), |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 kwargs['costs_usd'].append(entity.cost_usd) | 253 kwargs['costs_usd'].append(entity.cost_usd) |
| 253 kwargs['properties_hash'] = None | 254 kwargs['properties_hash'] = None |
| 254 kwargs['tags'] = [] | 255 kwargs['tags'] = [] |
| 255 kwargs['user'] = None | 256 kwargs['user'] = None |
| 256 else: | 257 else: |
| 257 assert entity.__class__ is task_result.TaskResultSummary, entity | 258 assert entity.__class__ is task_result.TaskResultSummary, entity |
| 258 kwargs['properties_hash'] = ( | 259 kwargs['properties_hash'] = ( |
| 259 entity.properties_hash.encode('hex') | 260 entity.properties_hash.encode('hex') |
| 260 if entity.properties_hash else None) | 261 if entity.properties_hash else None) |
| 261 return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs) | 262 return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs) |
| OLD | NEW |