{"action":"created","comment":{"url":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/comments/43677267","id":43677267,"diff_hunk":"@@ -4,6 +4,200 @@\n \"\"\"\n \n \n+from host import *\n+from link import *\n+from flow import *\n+\n+\n+class LinkStats(object):\n+    \"\"\"\n+    Contains information of a link related to per-link buffer occupancy,\n+    packet loss, and flow rate.\n+\n+    :ivar list buffer_occupancy: a list of (timestamp, buffer_occupancy)\n+    tuples.\n+    :ivar list packet_loss_times: a list of timestamps, each corresponding\n+    to a packet loss.\n+    :ivar list packet_transmit_times: a list of (timestamp, packet_size)\n+    corresponding to when a packet was transmitted. packet_size is in bits.\n+    \"\"\"\n+    def __init__(self, buffer_occupancy=[], packet_loss_times=[],\n+                 packet_transmit_times=[]):\n+        self.buffer_occupancy = buffer_occupancy\n+        self.packet_loss_times = packet_loss_times\n+        self.packet_transmit_times = packet_transmit_times\n+\n+\n+\n+class FlowStats(object):\n+    \"\"\"\n+    Contains information of a flow related to timestamps of each sending\n+    and receiving.\n+\n+    :ivar list packet_sent_times: a list of timestamps corresponding to\n+    when a data packet was sent. Time is in seconds.\n+    :ivar list packet_rec_times: a list of timestamps corresponding to\n+    when an ACK packet was received for this :class:`.Flow`. Time is in\n+    seconds.\n+    :ivar list packet_rtts: a list of RTTs (in seconds). Time is in\n+    seconds.\n+    \"\"\"\n+    def __init__(self, packet_sent_times=[], packet_rec_times=[],\n+                 packet_rtts=[]):\n+        packet_sent_times = packet_sent_times\n+        packet_rec_times = packet_rec_times\n+        packet_rtts = packet_rtts\n+\n+\n+\n+class HostStats(object):\n+    \"\"\"\n+     Contains information of a host related to per-host send/receive rate.\n+\n+    :ivar list packet_sent_times: a list of (timestamp, packet_size)\n+    tuples. packet_size is in bits. Time is in seconds.\n+    :ivar list packet_rec_times: a list of (timestamp, packet_size)\n+    tuples. packet_size is in bits. Time is in seconds.\n+    \"\"\"\n+    def __init__(self, packet_sent_times=[], packet_rec_times=[]):\n+        packet_sent_times = packet_sent_times\n+        packet_rec_times = packet_rec_times\n+\n+\n+\n class Statistics(object):\n-    # TODO(sharon): implement.\n-    pass\n\\ No newline at end of file\n+    \"\"\"\n+    Intended to be owned by the main loop to record all statistics within\n+    the network.\n+\n+    :ivar dict link_stats: a map from (end_1_id, end_2_id) to\n+    :class:`.LinkStats`. end_id's are strings.\n+    :ivar dict flow_stats: a map from flow ID to :class:`.FlowStats`. flow\n+    ids are strings.\n+    :ivar dict host_stats: a map from host ID to :class:`.HostStats`. host\n+    ids are strings.\n+    \"\"\"\n+    def __init__(self, link_stats={}, flow_stats={}, host_stats={}):\n+        self.link_stats = link_stats\n+        self.flow_stats = flow_stats\n+        self.host_stats = host_stats\n+\n+    def get_link_stats(self, link):\n+        \"\"\"\n+        Check whether a link has already existed in the dictionary; if so,\n+        skip the rest of the steps and return the stats object. If not,\n+        create a new mapping then return the stats object.\n+\n+        :param Link link: link to be determined.\n+        :return :class:`.LinkStats`.\n+        \"\"\"\n+        # Create a tuple of the pair and check if it exists in the dict.\n+        tup = (link.end_1_addr, link.end_2_addr)\n+\n+        if tup not in self.link_stats:\n+            self.link_stats[tup] = LinkStats()\n+        return self.link_stats[tup]\n+\n+    def get_flow_stats(self, flow):\n+        \"\"\"\n+        Check whether a flow has already existed in the dictionary; if so,\n+        skip the rest of the steps and return the stats object. If not,\n+        create a new mapping then return the stats object.\n+\n+        :param Flow flow: flow to be determined.\n+        :return :class:`.FlowStats`.\n+        \"\"\"\n+        if flow.flow_id not in self.flow_stats:\n+            self.flow_stats[flow.flow_id] = FlowStats()\n+        return self.flow_stats[flow.flow_id]\n+\n+    def get_host_stats(self, host):\n+        \"\"\"\n+        Check whether a host has already existed in the dictionary; if so,\n+        skip the rest of the steps and return the stats object. If not,\n+        create a new mapping then return the stats object.\n+\n+        :param Host host: host to be determined.\n+        :return :class:`.HostStats`.\n+        \"\"\"\n+        if host.address not in self.host_stats:\n+            self.host_stats[host.address] = HostStats()\n+        return self.host_stats[host.address]\n+\n+    def link_buffer_occ_change(self, link, curr_time):\n+        \"\"\"\n+        Update the buffer size as a link gains or loses a queue element.\n+\n+        :param Link link: link that has a change of buffer size.\n+        \"\"\"\n+        stats = self.get_link_stats(link)\n+        # TODO(sharon): Check with Cody if qsize() would work in this case.\n+        buffer_occ_packets = link.buffer.qsize()\n+        stats.buffer_occupancy.append((curr_time, buffer_occ_packets))\n+\n+    def link_packet_loss(self, link, curr_time):\n+        \"\"\"\n+        Record a packet loss of a link.\n+\n+        :param Link link: link of action.\n+        :param float curr_time: simulation time of loss.\n+        \"\"\"\n+        stats = self.get_link_stats(link)\n+        stats.packet_loss_times.append(curr_time)\n+\n+    def link_packet_transmitted(self, link, packet, curr_time):\n+        \"\"\"\n+        Record a packet transmission of a link.\n+\n+        :param Link link: link of action.\n+        :param int packet_size: the size in bits of packet transmitting.\n+        :param float curr_time: simulation time of transmission.\n+        \"\"\"\n+        stats = self.get_link_stats(link)\n+        stats.packet_transmit_times.append((curr_time, packet_size))\n+\n+    def flow_packet_sent(self, flow, curr_time):\n+        \"\"\"\n+        Record a packet sent from a flow.\n+\n+        :param Flow flow: flow of action.\n+        :param float curr_time: simulation time of sending.\n+        \"\"\"\n+        stats = self.get_flow_stats(flow)\n+        stats.packet_sent_times.append(curr_time)\n+\n+    def flow_packet_received(self, flow, curr_time):\n+        \"\"\"\n+        Record a packet received by a flow.","path":"network_simulator/statistics.py","position":171,"original_position":171,"commit_id":"92090eaa1c71fa7800b28de935104430cf42f957","original_commit_id":"92090eaa1c71fa7800b28de935104430cf42f957","user":{"login":"sharonyang","id":6145451,"avatar_url":"https://avatars.githubusercontent.com/u/6145451?v=3","gravatar_id":"","url":"https://api.github.com/users/sharonyang","html_url":"https://github.com/sharonyang","followers_url":"https://api.github.com/users/sharonyang/followers","following_url":"https://api.github.com/users/sharonyang/following{/other_user}","gists_url":"https://api.github.com/users/sharonyang/gists{/gist_id}","starred_url":"https://api.github.com/users/sharonyang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sharonyang/subscriptions","organizations_url":"https://api.github.com/users/sharonyang/orgs","repos_url":"https://api.github.com/users/sharonyang/repos","events_url":"https://api.github.com/users/sharonyang/events{/privacy}","received_events_url":"https://api.github.com/users/sharonyang/received_events","type":"User","site_admin":false},"body":"Done.","created_at":"2015-11-02T20:26:59Z","updated_at":"2015-11-02T20:26:59Z","html_url":"https://github.com/lakshbhasin/network_simulator/pull/6#discussion_r43677267","pull_request_url":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/6","_links":{"self":{"href":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/comments/43677267"},"html":{"href":"https://github.com/lakshbhasin/network_simulator/pull/6#discussion_r43677267"},"pull_request":{"href":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/6"}}},"pull_request":{"url":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/6","id":49361747,"html_url":"https://github.com/lakshbhasin/network_simulator/pull/6","diff_url":"https://github.com/lakshbhasin/network_simulator/pull/6.diff","patch_url":"https://github.com/lakshbhasin/network_simulator/pull/6.patch","issue_url":"https://api.github.com/repos/lakshbhasin/network_simulator/issues/6","number":6,"state":"open","locked":false,"title":"statistics.py initial setup.","user":{"login":"sharonyang","id":6145451,"avatar_url":"https://avatars.githubusercontent.com/u/6145451?v=3","gravatar_id":"","url":"https://api.github.com/users/sharonyang","html_url":"https://github.com/sharonyang","followers_url":"https://api.github.com/users/sharonyang/followers","following_url":"https://api.github.com/users/sharonyang/following{/other_user}","gists_url":"https://api.github.com/users/sharonyang/gists{/gist_id}","starred_url":"https://api.github.com/users/sharonyang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sharonyang/subscriptions","organizations_url":"https://api.github.com/users/sharonyang/orgs","repos_url":"https://api.github.com/users/sharonyang/repos","events_url":"https://api.github.com/users/sharonyang/events{/privacy}","received_events_url":"https://api.github.com/users/sharonyang/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-10-31T23:44:55Z","updated_at":"2015-11-02T20:26:59Z","closed_at":null,"merged_at":null,"merge_commit_sha":"73df1612a9ad5886e594907aad53be187fbf1779","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/6/commits","review_comments_url":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/6/comments","review_comment_url":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/comments{/number}","comments_url":"https://api.github.com/repos/lakshbhasin/network_simulator/issues/6/comments","statuses_url":"https://api.github.com/repos/lakshbhasin/network_simulator/statuses/92090eaa1c71fa7800b28de935104430cf42f957","head":{"label":"lakshbhasin:statistics-sharon","ref":"statistics-sharon","sha":"92090eaa1c71fa7800b28de935104430cf42f957","user":{"login":"lakshbhasin","id":1183879,"avatar_url":"https://avatars.githubusercontent.com/u/1183879?v=3","gravatar_id":"","url":"https://api.github.com/users/lakshbhasin","html_url":"https://github.com/lakshbhasin","followers_url":"https://api.github.com/users/lakshbhasin/followers","following_url":"https://api.github.com/users/lakshbhasin/following{/other_user}","gists_url":"https://api.github.com/users/lakshbhasin/gists{/gist_id}","starred_url":"https://api.github.com/users/lakshbhasin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lakshbhasin/subscriptions","organizations_url":"https://api.github.com/users/lakshbhasin/orgs","repos_url":"https://api.github.com/users/lakshbhasin/repos","events_url":"https://api.github.com/users/lakshbhasin/events{/privacy}","received_events_url":"https://api.github.com/users/lakshbhasin/received_events","type":"User","site_admin":false},"repo":{"id":44171299,"name":"network_simulator","full_name":"lakshbhasin/network_simulator","owner":{"login":"lakshbhasin","id":1183879,"avatar_url":"https://avatars.githubusercontent.com/u/1183879?v=3","gravatar_id":"","url":"https://api.github.com/users/lakshbhasin","html_url":"https://github.com/lakshbhasin","followers_url":"https://api.github.com/users/lakshbhasin/followers","following_url":"https://api.github.com/users/lakshbhasin/following{/other_user}","gists_url":"https://api.github.com/users/lakshbhasin/gists{/gist_id}","starred_url":"https://api.github.com/users/lakshbhasin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lakshbhasin/subscriptions","organizations_url":"https://api.github.com/users/lakshbhasin/orgs","repos_url":"https://api.github.com/users/lakshbhasin/repos","events_url":"https://api.github.com/users/lakshbhasin/events{/privacy}","received_events_url":"https://api.github.com/users/lakshbhasin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lakshbhasin/network_simulator","description":"A network simulator for the project portion of CS 143","fork":false,"url":"https://api.github.com/repos/lakshbhasin/network_simulator","forks_url":"https://api.github.com/repos/lakshbhasin/network_simulator/forks","keys_url":"https://api.github.com/repos/lakshbhasin/network_simulator/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lakshbhasin/network_simulator/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lakshbhasin/network_simulator/teams","hooks_url":"https://api.github.com/repos/lakshbhasin/network_simulator/hooks","issue_events_url":"https://api.github.com/repos/lakshbhasin/network_simulator/issues/events{/number}","events_url":"https://api.github.com/repos/lakshbhasin/network_simulator/events","assignees_url":"https://api.github.com/repos/lakshbhasin/network_simulator/assignees{/user}","branches_url":"https://api.github.com/repos/lakshbhasin/network_simulator/branches{/branch}","tags_url":"https://api.github.com/repos/lakshbhasin/network_simulator/tags","blobs_url":"https://api.github.com/repos/lakshbhasin/network_simulator/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lakshbhasin/network_simulator/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lakshbhasin/network_simulator/git/refs{/sha}","trees_url":"https://api.github.com/repos/lakshbhasin/network_simulator/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lakshbhasin/network_simulator/statuses/{sha}","languages_url":"https://api.github.com/repos/lakshbhasin/network_simulator/languages","stargazers_url":"https://api.github.com/repos/lakshbhasin/network_simulator/stargazers","contributors_url":"https://api.github.com/repos/lakshbhasin/network_simulator/contributors","subscribers_url":"https://api.github.com/repos/lakshbhasin/network_simulator/subscribers","subscription_url":"https://api.github.com/repos/lakshbhasin/network_simulator/subscription","commits_url":"https://api.github.com/repos/lakshbhasin/network_simulator/commits{/sha}","git_commits_url":"https://api.github.com/repos/lakshbhasin/network_simulator/git/commits{/sha}","comments_url":"https://api.github.com/repos/lakshbhasin/network_simulator/comments{/number}","issue_comment_url":"https://api.github.com/repos/lakshbhasin/network_simulator/issues/comments{/number}","contents_url":"https://api.github.com/repos/lakshbhasin/network_simulator/contents/{+path}","compare_url":"https://api.github.com/repos/lakshbhasin/network_simulator/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lakshbhasin/network_simulator/merges","archive_url":"https://api.github.com/repos/lakshbhasin/network_simulator/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lakshbhasin/network_simulator/downloads","issues_url":"https://api.github.com/repos/lakshbhasin/network_simulator/issues{/number}","pulls_url":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls{/number}","milestones_url":"https://api.github.com/repos/lakshbhasin/network_simulator/milestones{/number}","notifications_url":"https://api.github.com/repos/lakshbhasin/network_simulator/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lakshbhasin/network_simulator/labels{/name}","releases_url":"https://api.github.com/repos/lakshbhasin/network_simulator/releases{/id}","created_at":"2015-10-13T11:21:24Z","updated_at":"2015-10-25T21:16:11Z","pushed_at":"2015-11-02T13:19:06Z","git_url":"git://github.com/lakshbhasin/network_simulator.git","ssh_url":"git@github.com:lakshbhasin/network_simulator.git","clone_url":"https://github.com/lakshbhasin/network_simulator.git","svn_url":"https://github.com/lakshbhasin/network_simulator","homepage":null,"size":208,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"lakshbhasin:master","ref":"master","sha":"967e8448401d0e279dfb1e5c016a6d3e03a0a84b","user":{"login":"lakshbhasin","id":1183879,"avatar_url":"https://avatars.githubusercontent.com/u/1183879?v=3","gravatar_id":"","url":"https://api.github.com/users/lakshbhasin","html_url":"https://github.com/lakshbhasin","followers_url":"https://api.github.com/users/lakshbhasin/followers","following_url":"https://api.github.com/users/lakshbhasin/following{/other_user}","gists_url":"https://api.github.com/users/lakshbhasin/gists{/gist_id}","starred_url":"https://api.github.com/users/lakshbhasin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lakshbhasin/subscriptions","organizations_url":"https://api.github.com/users/lakshbhasin/orgs","repos_url":"https://api.github.com/users/lakshbhasin/repos","events_url":"https://api.github.com/users/lakshbhasin/events{/privacy}","received_events_url":"https://api.github.com/users/lakshbhasin/received_events","type":"User","site_admin":false},"repo":{"id":44171299,"name":"network_simulator","full_name":"lakshbhasin/network_simulator","owner":{"login":"lakshbhasin","id":1183879,"avatar_url":"https://avatars.githubusercontent.com/u/1183879?v=3","gravatar_id":"","url":"https://api.github.com/users/lakshbhasin","html_url":"https://github.com/lakshbhasin","followers_url":"https://api.github.com/users/lakshbhasin/followers","following_url":"https://api.github.com/users/lakshbhasin/following{/other_user}","gists_url":"https://api.github.com/users/lakshbhasin/gists{/gist_id}","starred_url":"https://api.github.com/users/lakshbhasin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lakshbhasin/subscriptions","organizations_url":"https://api.github.com/users/lakshbhasin/orgs","repos_url":"https://api.github.com/users/lakshbhasin/repos","events_url":"https://api.github.com/users/lakshbhasin/events{/privacy}","received_events_url":"https://api.github.com/users/lakshbhasin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lakshbhasin/network_simulator","description":"A network simulator for the project portion of CS 143","fork":false,"url":"https://api.github.com/repos/lakshbhasin/network_simulator","forks_url":"https://api.github.com/repos/lakshbhasin/network_simulator/forks","keys_url":"https://api.github.com/repos/lakshbhasin/network_simulator/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lakshbhasin/network_simulator/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lakshbhasin/network_simulator/teams","hooks_url":"https://api.github.com/repos/lakshbhasin/network_simulator/hooks","issue_events_url":"https://api.github.com/repos/lakshbhasin/network_simulator/issues/events{/number}","events_url":"https://api.github.com/repos/lakshbhasin/network_simulator/events","assignees_url":"https://api.github.com/repos/lakshbhasin/network_simulator/assignees{/user}","branches_url":"https://api.github.com/repos/lakshbhasin/network_simulator/branches{/branch}","tags_url":"https://api.github.com/repos/lakshbhasin/network_simulator/tags","blobs_url":"https://api.github.com/repos/lakshbhasin/network_simulator/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lakshbhasin/network_simulator/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lakshbhasin/network_simulator/git/refs{/sha}","trees_url":"https://api.github.com/repos/lakshbhasin/network_simulator/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lakshbhasin/network_simulator/statuses/{sha}","languages_url":"https://api.github.com/repos/lakshbhasin/network_simulator/languages","stargazers_url":"https://api.github.com/repos/lakshbhasin/network_simulator/stargazers","contributors_url":"https://api.github.com/repos/lakshbhasin/network_simulator/contributors","subscribers_url":"https://api.github.com/repos/lakshbhasin/network_simulator/subscribers","subscription_url":"https://api.github.com/repos/lakshbhasin/network_simulator/subscription","commits_url":"https://api.github.com/repos/lakshbhasin/network_simulator/commits{/sha}","git_commits_url":"https://api.github.com/repos/lakshbhasin/network_simulator/git/commits{/sha}","comments_url":"https://api.github.com/repos/lakshbhasin/network_simulator/comments{/number}","issue_comment_url":"https://api.github.com/repos/lakshbhasin/network_simulator/issues/comments{/number}","contents_url":"https://api.github.com/repos/lakshbhasin/network_simulator/contents/{+path}","compare_url":"https://api.github.com/repos/lakshbhasin/network_simulator/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lakshbhasin/network_simulator/merges","archive_url":"https://api.github.com/repos/lakshbhasin/network_simulator/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lakshbhasin/network_simulator/downloads","issues_url":"https://api.github.com/repos/lakshbhasin/network_simulator/issues{/number}","pulls_url":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls{/number}","milestones_url":"https://api.github.com/repos/lakshbhasin/network_simulator/milestones{/number}","notifications_url":"https://api.github.com/repos/lakshbhasin/network_simulator/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lakshbhasin/network_simulator/labels{/name}","releases_url":"https://api.github.com/repos/lakshbhasin/network_simulator/releases{/id}","created_at":"2015-10-13T11:21:24Z","updated_at":"2015-10-25T21:16:11Z","pushed_at":"2015-11-02T13:19:06Z","git_url":"git://github.com/lakshbhasin/network_simulator.git","ssh_url":"git@github.com:lakshbhasin/network_simulator.git","clone_url":"https://github.com/lakshbhasin/network_simulator.git","svn_url":"https://github.com/lakshbhasin/network_simulator","homepage":null,"size":208,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/6"},"html":{"href":"https://github.com/lakshbhasin/network_simulator/pull/6"},"issue":{"href":"https://api.github.com/repos/lakshbhasin/network_simulator/issues/6"},"comments":{"href":"https://api.github.com/repos/lakshbhasin/network_simulator/issues/6/comments"},"review_comments":{"href":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/6/comments"},"review_comment":{"href":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/lakshbhasin/network_simulator/pulls/6/commits"},"statuses":{"href":"https://api.github.com/repos/lakshbhasin/network_simulator/statuses/92090eaa1c71fa7800b28de935104430cf42f957"}}}}