<commit_before>import datetime
import asyncio
import uuid

import aiocoap
import aiocoap.resource as resource

from cbor2 import loads, dumps

import psycopg2
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSON, JSONB
from sqlalchemy import exc

from aiopg.sa import create_engine

from foglamp.env import DbConfig


metadata = sa.MetaData()

tbl = sa.Table(
    'sensor_values_t'
    , metadata
    , sa.Column('key', sa.types.VARCHAR(50))
    , sa.Column('data', JSONB))


class SensorValues(resource.Resource):
    def __init__(self):
        super(SensorValues, self).__init__()

    def register(self, root):
        root.add_resource(('other', 'sensor-values'), self);
        return

    async def render_post(self, request):
        r = loads(request.payload)

        key = r.get('id')

        if key is None:
            key = <KEY>().hex
        # key = 'terris'

        # See
        async with create_engine(DbConfig.conn_str) as engine:
            async with engine.acquire() as conn:
                try:
                    await conn.execute(tbl.insert().values(data=r, key=key))
                    # except exc.IntegrityError as e:
                except psycopg2.IntegrityError as e:
                    print(e)
                    # TODO log the error
        return aiocoap.Message(payload=''.encode("utf-8"))
<commit_msg>Add comments. Remove unused imports. Log duplicate key error.<commit_after>import datetime
import asyncio
import uuid
import psycopg2
import aiocoap
import aiocoap.resource as resource
import logging
import sqlalchemy as sa
from cbor2 import loads
from sqlalchemy.dialects.postgresql import JSONB
from aiopg.sa import create_engine
from foglamp.env import DbConfig

metadata = sa.MetaData()

__tbl__ = sa.Table(
    'sensor_values_t'
    , metadata
    , sa.Column('key', sa.types.VARCHAR(50))
    , sa.Column('data', JSONB))
'''Incoming data is inserted into this table'''


class SensorValues(resource.Resource):
    '''Handles other/sensor_values requests'''
    def __init__(self):
        super(SensorValues, self).__init__()

    def register(self, resourceRoot):
        '''Registers URI with aiocoap'''
        resourceRoot.add_resource(('other', 'sensor-values'), self);
        return

    async def render_post(self, request):
        '''Sends incoming data to database'''
        original_payload = loads(request.payload)
        
        payload = dict(original_payload)

        key = payload.get('key')

        if key is None:
            key = uuid.uuid4()
        else:
            del payload['key']
            
        # Demonstrate IntegrityError
        key = 'same'
        
        async with create_engine(DbConfig.conn_str) as engine:
            async with engine.acquire() as conn:
                try:
                    await conn.execute(__tbl__.insert().values(data=payload, key=key))
                except psycopg2.IntegrityError as e:
                    logging.getLogger('coap-server').exception(
                        "Duplicate key (%s) inserting sensor values: %s"
                        , key # Maybe the generated key is the problem
                        , original_payload)
        return aiocoap.Message(payload=''.encode("utf-8"))


{'fraction_non_alphanumeric': 0.0971709717097171, 'fraction_numerical': 0.003997539975399754, 'mean_word_length': 2.3605371900826446, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4220725', 'n_tokens_mistral': 940, 'n_tokens_neox': 909, 'n_words': 255}