grr/server/grr_response_server/databases/mysql_flows.py
def WriteFlowRequests(self, requests, cursor=None):
    """Writes a list of flow requests to the database."""
    args = []
    templates = []
    flow_keys = []
    needs_processing = {}
    now_str = mysql_utils.RDFDatetimeToMysqlString(rdfvalue.RDFDatetime.Now())
    for r in requests:
      if r.needs_processing:
        needs_processing.setdefault((r.client_id, r.flow_id),
                                    []).append(r.request_id)

      flow_keys.append((r.client_id, r.flow_id))
      templates.append("(%s, %s, %s, %s, %s, %s)")
      args.extend([
          mysql_utils.ClientIDToInt(r.client_id),
          mysql_utils.FlowIDToInt(r.flow_id), r.request_id, r.needs_processing,
          r.SerializeToString(), now_str
      ])

    if needs_processing:
      flow_processing_requests = []
      nr_conditions = []
      nr_args = []
      for client_id, flow_id in needs_processing:
        nr_conditions.append("(client_id=%s AND flow_id=%s)")
        nr_args.append(mysql_utils.ClientIDToInt(client_id))
        nr_args.append(mysql_utils.FlowIDToInt(flow_id))

      nr_query = ("SELECT client_id, flow_id, next_request_to_process "
                  "FROM flows WHERE ")
      nr_query += " OR ".join(nr_conditions)

      cursor.execute(nr_query, nr_args)

      db_result = cursor.fetchall()
      for client_id_int, flow_id_int, next_request_to_process in db_result:
        client_id = mysql_utils.IntToClientID(client_id_int)
        flow_id = mysql_utils.IntToFlowID(flow_id_int)
        if next_request_to_process in needs_processing[(client_id, flow_id)]:
          flow_processing_requests.append(
              rdf_flows.FlowProcessingRequest(
                  client_id=client_id, flow_id=flow_id))

      if flow_processing_requests:
        self._WriteFlowProcessingRequests(flow_processing_requests, cursor)

    query = ("INSERT INTO flow_requests "
             "(client_id, flow_id, request_id, needs_processing, request, "
             "timestamp) VALUES ")
    query += ", ".join(templates)
    try:
      cursor.execute(query, args)
    except MySQLdb.IntegrityError as e:
      raise db.AtLeastOneUnknownFlowError(flow_keys, cause=e)
Writes a list of flow requests to the database.
Writes a list of flow requests to the database.
Writes a list of flow requests to the database .
def WriteFlowRequests(self, requests, cursor=None):
    args = []
    templates = []
    flow_keys = []
    needs_processing = {}
    now_str = mysql_utils.RDFDatetimeToMysqlString(rdfvalue.RDFDatetime.Now())
    for r in requests:
      if r.needs_processing:
        needs_processing.setdefault((r.client_id, r.flow_id),
                                    []).append(r.request_id)
      flow_keys.append((r.client_id, r.flow_id))
      templates.append("(%s, %s, %s, %s, %s, %s)")
      args.extend([
          mysql_utils.ClientIDToInt(r.client_id),
          mysql_utils.FlowIDToInt(r.flow_id), r.request_id, r.needs_processing,
          r.SerializeToString(), now_str
      ])
    if needs_processing:
      flow_processing_requests = []
      nr_conditions = []
      nr_args = []
      for client_id, flow_id in needs_processing:
        nr_conditions.append("(client_id=%s AND flow_id=%s)")
        nr_args.append(mysql_utils.ClientIDToInt(client_id))
        nr_args.append(mysql_utils.FlowIDToInt(flow_id))
      nr_query = ("SELECT client_id, flow_id, next_request_to_process "
                  "FROM flows WHERE ")
      nr_query += " OR ".join(nr_conditions)
      cursor.execute(nr_query, nr_args)
      db_result = cursor.fetchall()
      for client_id_int, flow_id_int, next_request_to_process in db_result:
        client_id = mysql_utils.IntToClientID(client_id_int)
        flow_id = mysql_utils.IntToFlowID(flow_id_int)
        if next_request_to_process in needs_processing[(client_id, flow_id)]:
          flow_processing_requests.append(
              rdf_flows.FlowProcessingRequest(
                  client_id=client_id, flow_id=flow_id))
      if flow_processing_requests:
        self._WriteFlowProcessingRequests(flow_processing_requests, cursor)
    query = ("INSERT INTO flow_requests "
             "(client_id, flow_id, request_id, needs_processing, request, "
             "timestamp) VALUES ")
    query += ", ".join(templates)
    try:
      cursor.execute(query, args)
    except MySQLdb.IntegrityError as e:
      raise db.AtLeastOneUnknownFlowError(flow_keys, cause=e)
def WriteFlowRequests ( self , requests , cursor = None ) : args = [ ] templates = [ ] flow_keys = [ ] needs_processing = { } now_str = mysql_utils . RDFDatetimeToMysqlString ( rdfvalue . RDFDatetime . Now ( ) ) for r in requests : if r . needs_processing : needs_processing . setdefault ( ( r . client_id , r . flow_id ) , [ ] ) . append ( r . request_id ) flow_keys . append ( ( r . client_id , r . flow_id ) ) templates . append ( "(%s, %s, %s, %s, %s, %s)" ) args . extend ( [ mysql_utils . ClientIDToInt ( r . client_id ) , mysql_utils . FlowIDToInt ( r . flow_id ) , r . request_id , r . needs_processing , r . SerializeToString ( ) , now_str ] ) if needs_processing : flow_processing_requests = [ ] nr_conditions = [ ] nr_args = [ ] for client_id , flow_id in needs_processing : nr_conditions . append ( "(client_id=%s AND flow_id=%s)" ) nr_args . append ( mysql_utils . ClientIDToInt ( client_id ) ) nr_args . append ( mysql_utils . FlowIDToInt ( flow_id ) ) nr_query = ( "SELECT client_id, flow_id, next_request_to_process " "FROM flows WHERE " ) nr_query += " OR " . join ( nr_conditions ) cursor . execute ( nr_query , nr_args ) db_result = cursor . fetchall ( ) for client_id_int , flow_id_int , next_request_to_process in db_result : client_id = mysql_utils . IntToClientID ( client_id_int ) flow_id = mysql_utils . IntToFlowID ( flow_id_int ) if next_request_to_process in needs_processing [ ( client_id , flow_id ) ] : flow_processing_requests . append ( rdf_flows . FlowProcessingRequest ( client_id = client_id , flow_id = flow_id ) ) if flow_processing_requests : self . _WriteFlowProcessingRequests ( flow_processing_requests , cursor ) query = ( "INSERT INTO flow_requests " "(client_id, flow_id, request_id, needs_processing, request, " "timestamp) VALUES " ) query += ", " . join ( templates ) try : cursor . execute ( query , args ) except MySQLdb . IntegrityError as e : raise db . AtLeastOneUnknownFlowError ( flow_keys , cause = e )
Writes a list of flow requests to the database.
Writes a list of flow requests to the database .
"""Writes a list of flow requests to the database."""
self
requests
cursor