<commit_before>import contextlib
import json
import boto3
import logging
from happybase import Connection


logger = logging.getLogger(__name__)


class HBaseClient:
    _hostname = None

    def __init__(self, hbase_hostname=None):
        self.tablename = 'main_summary'
        self.column_family = b'cf'
        self.column = b'cf:payload'
        if hbase_hostname is None:
            try:
                self.hbase_hostname = self._get_hbase_hostname()
            except Exception:
                logger.exception("Failed to get HBase hostname")
                raise
        else:
            self.hbase_hostname = hbase_hostname

    def _get_hbase_hostname(self):
        client = boto3.client('ec2')
        reservations = client.describe_instances(Filters=[
            {'Name': 'tag:Name', 'Values': ['telemetry-hbase']},
            {
                'Name': 'tag:aws:elasticmapreduce:instance-group-role',
                'Values': ['MASTER']
            }
        ])["Reservations"]

        if len(reservations) == 0:
            raise Exception("HBase master not found!")

        if len(reservations) > 1:
            raise Exception("Multiple HBase masters found!")

        return reservations[0]["Instances"][0]["NetworkInterfaces"][0]["PrivateIpAddress"]

    def get_client_profile(self, client_id):
        """Retrieve the latest row for the given client in HBase

        Only the last known version of the info is retrieved"""
        try:
            with contextlib.closing(Connection(self.hbase_hostname)) as connection:
                table = connection.table(self.tablename)
                row_start = "{}:{}".format(client_id, "99999999")
                for key, data in table.scan(row_start=row_start, limit=1,
                                            columns=[self.column_family],
                                            reverse=True):
                    return json.loads(data[self.column].decode("utf-8"))
        except Exception:
            logger.exception("Connection to HBase failed", extra={"client_id": client_id})

        return None<commit_msg>Fix HBaseClient to use the new HBase instance format<commit_after>import contextlib
import json
import boto3
import logging
from happybase import Connection


logger = logging.getLogger(__name__)


class HBaseClient:
    _hostname = None

    def __init__(self, hbase_hostname=None, table_name='addon_recommender_view'):
        self.tablename = table_name
        self.column_family = b'cf'
        self.column = b'cf:payload'
        if hbase_hostname is None:
            try:
                self.hbase_hostname = self._get_hbase_hostname()
            except Exception:
                logger.exception("Failed to get HBase hostname")
                raise
        else:
            self.hbase_hostname = hbase_hostname

    def _get_hbase_hostname(self):
        client = boto3.client('ec2')
        reservations = client.describe_instances(Filters=[
            {'Name': 'tag:Name', 'Values': ['telemetry-hbase']},
            {
                'Name': 'tag:aws:elasticmapreduce:instance-group-role',
                'Values': ['MASTER']
            }
        ])["Reservations"]

        if len(reservations) == 0:
            raise Exception("HBase master not found!")

        if len(reservations) > 1:
            raise Exception("Multiple HBase masters found!")

        return reservations[0]["Instances"][0]["NetworkInterfaces"][0]["PrivateIpAddress"]

    def get_client_profile(self, client_id):
        """Retrieve the latest row for the given client in HBase

        Only the last known version of the info is retrieved"""
        try:
            with contextlib.closing(Connection(self.hbase_hostname)) as connection:
                table = connection.table(self.tablename)
                client_row = table.row(client_id, columns=[self.column_family])
                if client_row:
                    return json.loads(client_row[self.column].decode("utf-8"))
        except Exception:
            logger.exception("Connection to HBase failed", extra={"client_id": client_id})

        logger.info("Client information not found", extra={"client_id": client_id})
        return None
{'fraction_non_alphanumeric': 0.10090218423551757, 'fraction_numerical': 0.00641025641025641, 'mean_word_length': 2.2557959814528594, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6397563', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1128, 'n_words': 270}