Parcourir la source

Humanized Circuit speed display

Jeremy Stretch il y a 8 ans
Parent
commit
ccc52348be

+ 1 - 1
netbox/circuits/forms.py

@@ -105,7 +105,7 @@ class CircuitForm(forms.ModelForm, BootstrapMixin):
             'cid': "Unique circuit ID",
             'install_date': "Format: YYYY-MM-DD",
             'port_speed': "Physical circuit speed",
-            'commit_rate': "Commited rate (in Mbps)",
+            'commit_rate': "Commited rate",
             'xconnect_id': "ID of the local cross-connect",
             'pp_info': "Patch panel ID and port number(s)"
         }

+ 25 - 0
netbox/circuits/models.py

@@ -91,3 +91,28 @@ class Circuit(models.Model):
             self.xconnect_id,
             self.pp_info,
         ])
+
+    def _humanize_speed(self, speed):
+        """
+        Humanize speeds given in Kbps (e.g. 10000000 becomes '10 Gbps')
+        """
+        if speed >= 1000000000 and speed % 1000000000 == 0:
+            return '{} Tbps'.format(speed / 1000000000)
+        elif speed >= 1000000 and speed % 1000000 == 0:
+            return '{} Gbps'.format(speed / 1000000)
+        elif speed >= 1000 and speed % 1000 == 0:
+            return '{} Mbps'.format(speed / 1000)
+        elif speed >= 1000:
+            return '{} Mbps'.format(float(speed) / 1000)
+        else:
+            return '{} Kbps'.format(speed)
+
+    @property
+    def port_speed_human(self):
+        return self._humanize_speed(self.port_speed)
+
+    @property
+    def commit_rate_human(self):
+        if not self.commit_rate:
+            return ''
+        return self._humanize_speed(self.commit_rate)

+ 3 - 3
netbox/circuits/tables.py

@@ -62,12 +62,12 @@ class CircuitTable(tables.Table):
     type = tables.Column(verbose_name='Type')
     provider = tables.LinkColumn('circuits:provider', args=[Accessor('provider.slug')], verbose_name='Provider')
     site = tables.LinkColumn('dcim:site', args=[Accessor('site.slug')], verbose_name='Site')
-    port_speed = tables.Column(verbose_name='Port Speed')
-    commit_rate = tables.Column(verbose_name='Commit (Mbps)')
+    port_speed_human = tables.Column(verbose_name='Port Speed')
+    commit_rate_human = tables.Column(verbose_name='Commit Rate')
 
     class Meta:
         model = Circuit
-        fields = ('pk', 'cid', 'type', 'provider', 'site', 'port_speed', 'commit_rate')
+        fields = ('pk', 'cid', 'type', 'provider', 'site', 'port_speed_human', 'commit_rate_human')
         empty_text = "No circuits found."
         attrs = {
             'class': 'table table-hover',

+ 2 - 2
netbox/templates/circuits/circuit.html

@@ -79,11 +79,11 @@
                 </tr>
                 <tr>
                     <td>Port Speed</td>
-                    <td>{{ circuit.port_speed }} Kbps</td>
+                    <td>{{ circuit.port_speed_human }}</td>
                 </tr>
                 <tr>
                     <td>Commit Rate</td>
-                    <td>{% if circuit.commit_rate %}{{ circuit.commit_rate }} Kbps{% else %}<span class="text-muted">N/A</span>{% endif %}</td>
+                    <td>{% if circuit.commit_rate %}{{ circuit.commit_rate_human }}{% else %}<span class="text-muted">N/A</span>{% endif %}</td>
                 </tr>
                 <tr>
                     <td>Cross-Connect</td>

+ 2 - 2
netbox/templates/circuits/circuit_import.html

@@ -55,12 +55,12 @@
 				</tr>
 				<tr>
 					<td>Port Speed</td>
-					<td>Speed in Mbps (optional)</td>
+					<td>Physical speed in Kbps/td>
 					<td>10000</td>
 				</tr>
 				<tr>
 					<td>Commit rate</td>
-					<td>Speed in Mbps (optional)</td>
+					<td>Commited rate in Kbps (optional)</td>
 					<td>2000</td>
 				</tr>
 				<tr>

+ 1 - 1
netbox/templates/circuits/provider.html

@@ -90,7 +90,7 @@
                                 <a href="{% url 'dcim:device' pk=c.interface.device.pk %}">{{ c.interface.device }}</a>
                             {% endif %}
                         </td>
-                        <td>{{ c.port_speed }} Kbps</td>
+                        <td>{{ c.port_speed_human }}</td>
                     </tr>
                 {% empty %}
                     <tr>