racks.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import decimal
  2. import svgwrite
  3. from svgwrite.container import Hyperlink
  4. from svgwrite.image import Image
  5. from svgwrite.gradients import LinearGradient
  6. from svgwrite.shapes import Rect
  7. from svgwrite.text import Text
  8. from django.conf import settings
  9. from django.core.exceptions import FieldError
  10. from django.db.models import Q
  11. from django.urls import reverse
  12. from django.utils.http import urlencode
  13. from netbox.config import get_config
  14. from utilities.utils import foreground_color, array_to_ranges
  15. from dcim.constants import RACK_ELEVATION_BORDER_WIDTH
  16. __all__ = (
  17. 'RackElevationSVG',
  18. )
  19. def get_device_name(device):
  20. if device.virtual_chassis:
  21. name = f'{device.virtual_chassis.name}:{device.vc_position}'
  22. elif device.name:
  23. name = device.name
  24. else:
  25. name = str(device.device_type)
  26. if device.devicebay_count:
  27. name += ' ({}/{})'.format(device.get_children().count(), device.devicebay_count)
  28. return name
  29. def get_device_description(device):
  30. return '{} ({}) — {} {} ({}U) {} {}'.format(
  31. device.name,
  32. device.device_role,
  33. device.device_type.manufacturer.name,
  34. device.device_type.model,
  35. device.device_type.u_height,
  36. device.asset_tag or '',
  37. device.serial or ''
  38. )
  39. class RackElevationSVG:
  40. """
  41. Use this class to render a rack elevation as an SVG image.
  42. :param rack: A NetBox Rack instance
  43. :param unit_width: Rendered unit width, in pixels
  44. :param unit_height: Rendered unit height, in pixels
  45. :param legend_width: Legend width, in pixels (where the unit labels appear)
  46. :param margin_width: Margin width, in pixels (where reservations appear)
  47. :param user: User instance. If specified, only devices viewable by this user will be fully displayed.
  48. :param include_images: If true, the SVG document will embed front/rear device face images, where available
  49. :param base_url: Base URL for links within the SVG document. If none, links will be relative.
  50. :param highlight_params: Iterable of two-tuples which identifies attributes of devices to highlight
  51. """
  52. def __init__(self, rack, unit_height=None, unit_width=None, legend_width=None, margin_width=None, user=None,
  53. include_images=True, base_url=None, highlight_params=None):
  54. self.rack = rack
  55. self.include_images = include_images
  56. self.base_url = base_url.rstrip('/') if base_url is not None else ''
  57. # Set drawing dimensions
  58. config = get_config()
  59. self.unit_width = unit_width or config.RACK_ELEVATION_DEFAULT_UNIT_WIDTH
  60. self.unit_height = unit_height or config.RACK_ELEVATION_DEFAULT_UNIT_HEIGHT
  61. self.legend_width = legend_width or config.RACK_ELEVATION_DEFAULT_LEGEND_WIDTH
  62. self.margin_width = margin_width or config.RACK_ELEVATION_DEFAULT_MARGIN_WIDTH
  63. # Determine the subset of devices within this rack that are viewable by the user, if any
  64. permitted_devices = self.rack.devices
  65. if user is not None:
  66. permitted_devices = permitted_devices.restrict(user, 'view')
  67. self.permitted_device_ids = permitted_devices.values_list('pk', flat=True)
  68. # Determine device(s) to highlight within the elevation (if any)
  69. self.highlight_devices = []
  70. if highlight_params:
  71. q = Q()
  72. for k, v in highlight_params:
  73. q |= Q(**{k: v})
  74. try:
  75. self.highlight_devices = permitted_devices.filter(q)
  76. except FieldError:
  77. pass
  78. @staticmethod
  79. def _add_gradient(drawing, id_, color):
  80. gradient = LinearGradient(
  81. start=(0, 0),
  82. end=(0, 25),
  83. spreadMethod='repeat',
  84. id_=id_,
  85. gradientTransform='rotate(45, 0, 0)',
  86. gradientUnits='userSpaceOnUse'
  87. )
  88. gradient.add_stop_color(offset='0%', color='#f7f7f7')
  89. gradient.add_stop_color(offset='50%', color='#f7f7f7')
  90. gradient.add_stop_color(offset='50%', color=color)
  91. gradient.add_stop_color(offset='100%', color=color)
  92. drawing.defs.add(gradient)
  93. def _setup_drawing(self):
  94. width = self.unit_width + self.legend_width + self.margin_width + RACK_ELEVATION_BORDER_WIDTH * 2
  95. height = self.unit_height * self.rack.u_height + RACK_ELEVATION_BORDER_WIDTH * 2
  96. drawing = svgwrite.Drawing(size=(width, height))
  97. # Add the stylesheet
  98. with open(f'{settings.STATIC_ROOT}/rack_elevation.css') as css_file:
  99. drawing.defs.add(drawing.style(css_file.read()))
  100. # Add gradients
  101. RackElevationSVG._add_gradient(drawing, 'reserved', '#b0b0ff')
  102. RackElevationSVG._add_gradient(drawing, 'occupied', '#d7d7d7')
  103. RackElevationSVG._add_gradient(drawing, 'blocked', '#ffc0c0')
  104. return drawing
  105. def _get_device_coords(self, position, height):
  106. """
  107. Return the X, Y coordinates of the top left corner for a device in the specified rack unit.
  108. """
  109. x = self.legend_width + RACK_ELEVATION_BORDER_WIDTH
  110. y = RACK_ELEVATION_BORDER_WIDTH
  111. if self.rack.desc_units:
  112. y += int((position - 1) * self.unit_height)
  113. else:
  114. y += int((self.rack.u_height - position + 1) * self.unit_height) - int(height * self.unit_height)
  115. return x, y
  116. def _draw_device(self, device, coords, size, color=None, image=None):
  117. name = get_device_name(device)
  118. description = get_device_description(device)
  119. text_color = f'#{foreground_color(color)}' if color else '#000000'
  120. text_coords = (
  121. coords[0] + size[0] / 2,
  122. coords[1] + size[1] / 2
  123. )
  124. # Determine whether highlighting is in use, and if so, whether to shade this device
  125. is_shaded = self.highlight_devices and device not in self.highlight_devices
  126. css_extra = ' shaded' if is_shaded else ''
  127. # Create hyperlink element
  128. link = Hyperlink(href=f'{self.base_url}{device.get_absolute_url()}', target='_blank')
  129. link.set_desc(description)
  130. # Add rect element to hyperlink
  131. if color:
  132. link.add(Rect(coords, size, style=f'fill: #{color}', class_=f'slot{css_extra}'))
  133. else:
  134. link.add(Rect(coords, size, class_=f'slot blocked{css_extra}'))
  135. link.add(Text(name, insert=text_coords, fill=text_color, class_=f'label{css_extra}'))
  136. # Embed device type image if provided
  137. if self.include_images and image:
  138. image = Image(
  139. href=f'{self.base_url}{image.url}',
  140. insert=coords,
  141. size=size,
  142. class_=f'device-image{css_extra}'
  143. )
  144. image.fit(scale='slice')
  145. link.add(image)
  146. link.add(
  147. Text(name, insert=text_coords, stroke='black', stroke_width='0.2em', stroke_linejoin='round',
  148. class_=f'device-image-label{css_extra}')
  149. )
  150. link.add(
  151. Text(name, insert=text_coords, fill='white', class_=f'device-image-label{css_extra}')
  152. )
  153. self.drawing.add(link)
  154. def draw_device_front(self, device, coords, size):
  155. """
  156. Draw the front (mounted) face of a device.
  157. """
  158. color = device.device_role.color
  159. image = device.device_type.front_image
  160. self._draw_device(device, coords, size, color=color, image=image)
  161. def draw_device_rear(self, device, coords, size):
  162. """
  163. Draw the rear (opposite) face of a device.
  164. """
  165. image = device.device_type.rear_image
  166. self._draw_device(device, coords, size, image=image)
  167. def draw_border(self):
  168. """
  169. Draw a border around the collection of rack units.
  170. """
  171. border_width = RACK_ELEVATION_BORDER_WIDTH
  172. border_offset = RACK_ELEVATION_BORDER_WIDTH / 2
  173. frame = Rect(
  174. insert=(self.legend_width + border_offset, border_offset),
  175. size=(self.unit_width + border_width, self.rack.u_height * self.unit_height + border_width),
  176. class_='rack'
  177. )
  178. self.drawing.add(frame)
  179. def draw_legend(self):
  180. """
  181. Draw the rack unit labels along the lefthand side of the elevation.
  182. """
  183. for ru in range(0, self.rack.u_height):
  184. start_y = ru * self.unit_height + RACK_ELEVATION_BORDER_WIDTH
  185. position_coordinates = (self.legend_width / 2, start_y + self.unit_height / 2 + RACK_ELEVATION_BORDER_WIDTH)
  186. unit = ru + 1 if self.rack.desc_units else self.rack.u_height - ru
  187. self.drawing.add(
  188. Text(str(unit), position_coordinates, class_='unit')
  189. )
  190. def draw_margin(self):
  191. """
  192. Draw any rack reservations in the right-hand margin alongside the rack elevation.
  193. """
  194. for reservation in self.rack.reservations.all():
  195. for segment in array_to_ranges(reservation.units):
  196. u_height = 1 if len(segment) == 1 else segment[1] + 1 - segment[0]
  197. coords = self._get_device_coords(segment[0], u_height)
  198. coords = (coords[0] + self.unit_width + RACK_ELEVATION_BORDER_WIDTH * 2, coords[1])
  199. size = (
  200. self.margin_width,
  201. u_height * self.unit_height
  202. )
  203. link = Hyperlink(
  204. href='{}{}'.format(self.base_url, reservation.get_absolute_url()),
  205. target='_blank'
  206. )
  207. link.set_desc(f'Reservation #{reservation.pk}: {reservation.description}')
  208. link.add(
  209. Rect(coords, size, class_='reservation')
  210. )
  211. self.drawing.add(link)
  212. def draw_background(self, face):
  213. """
  214. Draw the rack unit placeholders which form the "background" of the rack elevation.
  215. """
  216. x_offset = RACK_ELEVATION_BORDER_WIDTH + self.legend_width
  217. url_string = '{}?{}&position={{}}'.format(
  218. reverse('dcim:device_add'),
  219. urlencode({
  220. 'site': self.rack.site.pk,
  221. 'location': self.rack.location.pk if self.rack.location else '',
  222. 'rack': self.rack.pk,
  223. 'face': face,
  224. })
  225. )
  226. for ru in range(0, self.rack.u_height):
  227. y_offset = RACK_ELEVATION_BORDER_WIDTH + ru * self.unit_height
  228. text_coords = (
  229. x_offset + self.unit_width / 2,
  230. y_offset + self.unit_height / 2
  231. )
  232. link = Hyperlink(href=url_string.format(ru), target='_blank')
  233. link.add(Rect((x_offset, y_offset), (self.unit_width, self.unit_height), class_='slot'))
  234. link.add(Text('add device', insert=text_coords, class_='add-device'))
  235. self.drawing.add(link)
  236. def draw_face(self, face, opposite=False):
  237. """
  238. Draw any occupied rack units for the specified rack face.
  239. """
  240. for unit in self.rack.get_rack_units(face=face, expand_devices=False):
  241. # Loop through all units in the elevation
  242. device = unit['device']
  243. height = unit.get('height', decimal.Decimal(1.0))
  244. device_coords = self._get_device_coords(unit['id'], height)
  245. device_size = (
  246. self.unit_width,
  247. int(self.unit_height * height)
  248. )
  249. # Draw the device
  250. if device and device.pk in self.permitted_device_ids:
  251. if device.face == face and not opposite:
  252. self.draw_device_front(device, device_coords, device_size)
  253. else:
  254. self.draw_device_rear(device, device_coords, device_size)
  255. elif device:
  256. # Devices which the user does not have permission to view are rendered only as unavailable space
  257. self.drawing.add(Rect(device_coords, device_size, class_='blocked'))
  258. def render(self, face):
  259. """
  260. Return an SVG document representing a rack elevation.
  261. """
  262. # Initialize the drawing
  263. self.drawing = self._setup_drawing()
  264. # Draw the empty rack, legend, and margin
  265. self.draw_legend()
  266. self.draw_background(face)
  267. self.draw_margin()
  268. # Draw the rack face
  269. self.draw_face(face)
  270. # Draw the rack border last
  271. self.draw_border()
  272. return self.drawing