1# -*- coding: utf-8 -*- 2# 3# Copyright 2021, Haiku, Inc. All rights reserved. 4# Distributed under the terms of the MIT License. 5# 6# Authors: 7# Alexander von Gluck IV <kallisti5@unixzen.com> 8from .Utils import info, warn 9 10try: 11 from pymongo import MongoClient 12except ImportError: 13 MongoClient = None 14 15class ReporterMongo(object): 16 def __init__(self, uri, branch, architecture): 17 self.uri = uri 18 self.branch = branch 19 self.architecture = architecture 20 if MongoClient: 21 self.client = MongoClient(uri) 22 else: 23 self.client = None 24 25 def connected(self): 26 if self.client == None: 27 warn('pymongo unavailable') 28 return False 29 try: 30 self.client.server_info() 31 except pymongo.errors.ServerSelectionTimeoutError as err: 32 warn('unable to connect to MongoDB @ ' + self.uri) 33 return False 34 info('connected to MongoDB @ ' + self.uri + ' for reporting') 35 return True 36 37 def updateBuildrun(self, buildNumber, status): 38 db = self.client[self.branch + '-' + self.architecture] 39 buildrunCollection = db.buildruns 40 mdbStatus = status.copy() 41 mdbStatus["_id"] = buildNumber 42 buildrunCollection.update_one({'_id': buildNumber}, {"$set": mdbStatus}, upsert=True) 43 self._updateBuilders(status); 44 return 45 46 def _updateBuilders(self, status): 47 db = self.client[self.branch + '-' + self.architecture] 48 builderCollection = db.builders 49 for state in status["builders"].keys(): 50 for builder in status["builders"][state]: 51 bldStatus = builder.copy() 52 bldStatus["_id"] = builder["name"] 53 bldStatus["status"] = state 54 builderCollection.update_one({'_id': builder["name"]}, {"$set": bldStatus}, upsert=True) 55