<commit_before>from caleydo_server.dataset_def import ADataSetProvider
import caleydo_server.graph

class MongoGraph(caleydo_server.graph.Graph):
  def __init__(self, entry, db):
    super(MongoGraph, self).__init__(entry['name'], 'mongodb', entry.get('id', None), entry.get('attrs',None))
    self._entry = entry
    self._db = db
    from bson.objectid import ObjectId
    self._find_me = dict(_id=self._entry['_id'])
    self._find_data = dict(_id=ObjectId(self._entry['refid']))

    self._nodes = None
    self._edges = None

  @staticmethod
  def list(db):
    return [ MongoGraph(entry, db) for entry in db.graph.find()]

  @staticmethod
  def create(data, user, id, db):
    #if 'clone_from' in data:
    #  #clone from an existing graph
    #  from bson.objectid import ObjectId
    #  other_desc = db.graph.find_one(dict(_id=data['clone_from']))
    #  other_data = db.graph_data.find_one(dict(_id=ObjectId(other_desc['refid'])))
    #else
    #  other_desc = dict()
    #  other_data = dict()

    import datetime
    entry = dict(
      name=data['name'],
      description=data.get('description', ''),
      creator='unknown' if user.is_anonymous else user.name,
      nnodes=len(data['nodes']),
      nedges=len(data['edges']),
      attrs=data.get('attrs',{}),
      ts=datetime.datetime.utcnow())

    if id is not None:
      entry['id'] = id

    data_entry = dict(
      nodes = data['nodes'],
      edges = data['edges']
    )
    data_id = db.graph_data.insert_one(data_entry).inserted_id

    entry['refid'] = str(data_id)
    db.graph.insert_one(entry)



    return MongoGraph(entry, db)

  def nodes(self, range=None):
    if self._nodes is None:
      from bson.objectid import ObjectId
      data = self._db.graph_data.find_one(self._find_data, {'nodes': 1})
      self._nodes = [ caleydo_server.graph.GraphNode(n['type'],n['id'], n.get('attrs',None)) for n in data['nodes'] ]

    if range is None:
      return self._nodes
    return self._nodes[range.asslice()]

  @property
  def nnodes(self):
    return self._entry['nnodes']

  def edges(self, range = None):
    if self._edges is None:
      from bson.objectid import ObjectId
      data = self._db.graph_data.find_one(self._find_data, {'edges': 1})
      self._edges = [ caleydo_server.graph.GraphEdge(n['type'],n['id'], n['source'], n['target'], n.get('attrs',None)) for n in data['edges'] ]

    if range is None:
      return self._edges
    return self._edges[range.asslice()]

  @property
  def nedges(self):
    return self._entry['nedges']

  def to_description(self):
    r = super(MongoGraph, self).to_description()
    return r

  def add_node(self, data):
    self._db.graph.update(self._find_me, { '$inc': dict(nnodes=1) })
    self._db.graph_data.update(self._find_data, { '$push': dict(nodes=data) })
    self._entry['nnodes'] += 1
    if self._nodes:
      self._nodes.append(caleydo_server.graph.GraphNode(data['type'],data['id'], data.get('attrs',None)))
    return True

  def update_node(self, data):
    q = self._find_data.copy()
    q['nodes.id'] = data['id']
    self._db.graph_data.update(q, { '$set': { 'nodes.$.attrs' : data.get('attrs',{}) } })
    #update({ "item.two" : "24" },
    #   { $set : { "item.$.two" : "" }}, false, true);
    if self._nodes:
      for n in self._nodes:
        if n.id == id:
          n.attrs = data.get('attrs',{})
          break

    return True

  def remove_node(self, id):
    if self._nodes:
      n = self.get_node(id)
      self._nodes.remove(n)
    self._entry['nnodes'] -= 1
    #remove node and all associated edges
    x = self._db.graph_data.update(self._find_data, {'$pull': dict(nodes=dict(id=id))}, multi=False)<commit_msg>Exposed additional graph information in to_description<commit_after>from caleydo_server.dataset_def import ADataSetProvider
import caleydo_server.graph

class MongoGraph(caleydo_server.graph.Graph):
  def __init__(self, entry, db):
    super(MongoGraph, self).__init__(entry['name'], 'mongodb', entry.get('id', None), entry.get('attrs',None))
    self._entry = entry
    self._db = db
    from bson.objectid import ObjectId
    self._find_me = dict(_id=self._entry['_id'])
    self._find_data = dict(_id=ObjectId(self._entry['refid']))

    self._nodes = None
    self._edges = None

  @staticmethod
  def list(db):
    return [ MongoGraph(entry, db) for entry in db.graph.find()]

  @staticmethod
  def create(data, user, id, db):
    #if 'clone_from' in data:
    #  #clone from an existing graph
    #  from bson.objectid import ObjectId
    #  other_desc = db.graph.find_one(dict(_id=data['clone_from']))
    #  other_data = db.graph_data.find_one(dict(_id=ObjectId(other_desc['refid'])))
    #else
    #  other_desc = dict()
    #  other_data = dict()

    import datetime
    entry = dict(
      name=data['name'],
      description=data.get('description', ''),
      creator='unknown' if user.is_anonymous else user.name,
      nnodes=len(data['nodes']),
      nedges=len(data['edges']),
      attrs=data.get('attrs',{}),
      ts=datetime.datetime.utcnow())

    if id is not None:
      entry['id'] = id

    data_entry = dict(
      nodes = data['nodes'],
      edges = data['edges']
    )
    data_id = db.graph_data.insert_one(data_entry).inserted_id

    entry['refid'] = str(data_id)
    db.graph.insert_one(entry)



    return MongoGraph(entry, db)

  def nodes(self, range=None):
    if self._nodes is None:
      from bson.objectid import ObjectId
      data = self._db.graph_data.find_one(self._find_data, {'nodes': 1})
      self._nodes = [ caleydo_server.graph.GraphNode(n['type'],n['id'], n.get('attrs',None)) for n in data['nodes'] ]

    if range is None:
      return self._nodes
    return self._nodes[range.asslice()]

  @property
  def nnodes(self):
    return self._entry['nnodes']

  def edges(self, range = None):
    if self._edges is None:
      from bson.objectid import ObjectId
      data = self._db.graph_data.find_one(self._find_data, {'edges': 1})
      self._edges = [ caleydo_server.graph.GraphEdge(n['type'],n['id'], n['source'], n['target'], n.get('attrs',None)) for n in data['edges'] ]

    if range is None:
      return self._edges
    return self._edges[range.asslice()]

  @property
  def nedges(self):
    return self._entry['nedges']

  def to_description(self):
    r = super(MongoGraph, self).to_description()

    if self._entry is not None:
      r['description'] = self._entry['description']
      r['creator'] = self._entry['creator']
      r['ts'] = self._entry['ts']

    return r

  def add_node(self, data):
    self._db.graph.update(self._find_me, { '$inc': dict(nnodes=1) })
    self._db.graph_data.update(self._find_data, { '$push': dict(nodes=data) })
    self._entry['nnodes'] += 1
    if self._nodes:
      self._nodes.append(caleydo_server.graph.GraphNode(data['type'],data['id'], data.get('attrs',None)))
    return True

  def update_node(self, data):
    q = self._find_data.copy()
    q['nodes.id'] = data['id']
    self._db.graph_data.update(q, { '$set': { 'nodes.$.attrs' : data.get('attrs',{}) } })
    #update({ "item.two" : "24" },
    #   { $set : { "item.$.two" : "" }}, false, true);
    if self._nodes:
      for n in self._nodes:
        if n.id == id:
          n.attrs = data.get('attrs',{})
          break

    return True

  def remove_node(self, id):
    if self._nodes:
      n = self.get_node(id)
      self._nodes.remove(n)
    self._entry['nnodes'] -= 1
    #remove node and all associated edges
    x = self._db.graph_data.update(self._find_data, {'$pull': dict(nodes=dict(id=id))}, multi=False)
{'fraction_non_alphanumeric': 0.1611411966715097, 'fraction_numerical': 0.0018491612732796196, 'mean_word_length': 3.3219178082191783, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '543510', 'n_tokens_mistral': 2642, 'n_tokens_neox': 2576, 'n_words': 531}