Exemplo n.º 1
0
 async def _mine(cls, time_from: date, time_to: date,
                 repositories: Sequence[str], developers: Sequence[str],
                 db: databases.Database,
                 cache: Optional[aiomcache.Client]) -> List[pd.DataFrame]:
     filters = [
         sql.or_(
             sql.and_(PullRequest.updated_at >= time_from,
                      PullRequest.updated_at < time_to),
             sql.and_(
                 sql.or_(PullRequest.closed_at.is_(None),
                         PullRequest.closed_at > time_from),
                 PullRequest.created_at < time_to)),
         PullRequest.repository_fullname.in_(repositories),
     ]
     if len(developers) > 0:
         filters.append(PullRequest.user_login.in_(developers))
     async with db.connection() as conn:
         prs = await read_sql_query(
             select([PullRequest]).where(sql.and_(*filters)), conn,
             PullRequest)
         numbers = prs[PullRequest.number.key] if len(prs) > 0 else set()
         reviews = await cls._read_filtered_models(conn, PullRequestReview,
                                                   numbers, repositories,
                                                   time_to)
         review_comments = await cls._read_filtered_models(
             conn, PullRequestComment, numbers, repositories, time_to)
         comments = await cls._read_filtered_models(conn, IssueComment,
                                                    numbers, repositories,
                                                    time_to)
         commits = await cls._read_filtered_models(conn, PullRequestCommit,
                                                   numbers, repositories,
                                                   time_to)
     return [prs, reviews, review_comments, comments, commits]
Exemplo n.º 2
0
class DatabasesRecorder(BaseRecorder):
    def __init__(self, database_uri):
        self.db = Database(database_uri)

    async def connect(self):
        await self.db.connect()

        async with self.db.connection() as conn:
            for table in tables:
                create_expr = CreateTable(table)
                await conn.execute(create_expr)

    async def disconnect(self):
        await self.db.disconnect()

    async def record_commands(self, command_batch):
        await self.db.execute(commands.insert(), command_batch)
Exemplo n.º 3
0
async def upsert_point(database: Database,
                       icao,
                       ts,
                       lat=None,
                       lon=None,
                       callsign=None,
                       hd=None,
                       altitude=None,
                       gspd=None,
                       vrt=None):

    terms = []
    columns = []
    values = []
    if icao:
        columns.append('icao')
        values.append(f'"{icao}"')
    if ts:
        columns.append('ts')
        values.append(f'"{ts}"')
    if lat:
        terms.append(F'lat={lat}')
        columns.append('lat')
        values.append(f'{lat}')
    if lon:
        terms.append(F'lon={lon}')
        columns.append('lon')
        values.append(f'{lon}')
    if callsign:
        terms.append(F'callsign="{callsign}"')
        columns.append('callsign')
        values.append(f'"{callsign}"')
    if hd:
        terms.append(F'hd={hd}')
        columns.append('hd')
        values.append(f'{hd}')
    if altitude:
        terms.append(F'altitude={altitude}')
        columns.append('altitude')
        values.append(f'{altitude}')
    if gspd:
        terms.append(F'gspd={gspd}')
        columns.append('gspd')
        values.append(f'{gspd}')
    if vrt:
        terms.append(F'vrt={vrt}')
        columns.append('vrt')
        values.append(f'{vrt}')

    if len(terms) == 0:
        return

    terms_string = ',\n'.join(terms)
    columns_string = ','.join(columns)
    values_string = ','.join(values)

    where = F'WHERE icao = "{icao}" AND ts = "{ts}";'
    q = F"UPDATE track_point SET\n {terms_string} {where}"
    #print(q)

    try:

        transaction = await database.transaction()

        #print("@upsert_point: transaction started")

        # we need the cursor to determine how many rows are inserted so we can do the upsert

        conn = database.connection().raw_connection

        c = await conn.cursor()

        await c.execute(q)

        if c.rowcount == 0:
            # do an insert
            insert_q = F'INSERT INTO track_point ({columns_string}) VALUES ({values_string})'
            await c.execute(insert_q)
            #print(F"Inserted {c.rowcount}")
        else:
            #print(F"Updated {c.rowcount}")
            pass

        # check if there are any changes

        await transaction.commit()

        #print("@upsert_point: transaction commited")

    except aiosqlite.Error as e:
        LOG.error(F"@upsert_point: Caught aiosqlite.Error: {e}")
        raise
    except Exception as x:
        LOG.error(F"@upsert_point: Caught exception {x}")
        raise