|
| 1 | +Coding Standards and Practices |
| 2 | +============================== |
| 3 | + |
| 4 | +This is not meant to be a style document so much as a practices document for ensuring performance and convention in the Ansible Tower API. |
| 5 | + |
| 6 | +Paginate Everything |
| 7 | +=================== |
| 8 | + |
| 9 | +Anything that returns a collection must be paginated. |
| 10 | + |
| 11 | +Assume large data sets |
| 12 | +====================== |
| 13 | + |
| 14 | +Don't test exclusively with small data. Assume 1000-10000 hosts in all operations, with years of event data. |
| 15 | + |
| 16 | +Some of our users have 30,000 machines they manage. |
| 17 | + |
| 18 | +API performance |
| 19 | +=============== |
| 20 | + |
| 21 | +In general, the expected response time for any API call is something like 1/4 of a second or less. Signs of slow API |
| 22 | +performance should be regularly checked, particularly for missing indexes. |
| 23 | + |
| 24 | +Missing Indexes |
| 25 | +=============== |
| 26 | + |
| 27 | +Any filters the UI uses should be indexed. |
| 28 | + |
| 29 | +Migrations |
| 30 | +========== |
| 31 | + |
| 32 | +Always think about any existing data when adding any new fields. It's ok to wait in upgrade time to get the database to be |
| 33 | +consistent. |
| 34 | + |
| 35 | +Limit Queries |
| 36 | +============= |
| 37 | + |
| 38 | +The number of queries made should be constant time and must not vary with the size of the result set. |
| 39 | + |
| 40 | +Consider RBAC |
| 41 | +============= |
| 42 | + |
| 43 | +The returns of all collections must be filtered by who has access to view them, without exception |
| 44 | + |
| 45 | +Discoverability |
| 46 | +=============== |
| 47 | + |
| 48 | +All API endpoints must be able to be traversed from "/", and have comments, where possible, explaining their purpose |
| 49 | + |
| 50 | +Friendly Comments |
| 51 | +================= |
| 52 | + |
| 53 | +All API comments are exposed by the API browser and must be fit for customers. Avoid jokes in API comments and error |
| 54 | +messages, as well as FIXME comments in places that the API will display. |
| 55 | + |
| 56 | +UI Sanity |
| 57 | +========= |
| 58 | + |
| 59 | +Where possible the API should provide API endpoints that feed raw data into the UI, the UI should not have to do lots of |
| 60 | +data transformations, as it is going to be less responsive and able to do these things. |
| 61 | + |
| 62 | +When requiring a collection of times of size N, the UI must not make any extra API queries for each item in the result set |
| 63 | + |
| 64 | +Effective Usage of Query Sets |
| 65 | +============================= |
| 66 | + |
| 67 | +The system must return Django result sets rather than building JSON in memory in nearly all cases. Use things like |
| 68 | +exclude and joins, and let the database do the work. |
| 69 | + |
| 70 | +Serializers |
| 71 | +=========== |
| 72 | + |
| 73 | +No database queries may be made in serializers because these are executed once per item, rather than paginated. |
| 74 | + |
| 75 | +REST verbs |
| 76 | +========== |
| 77 | + |
| 78 | +REST verbs should be RESTy. Don't use GETs to do things that should be a PUT or POST. |
| 79 | + |
| 80 | +Unit tests |
| 81 | +========== |
| 82 | + |
| 83 | +Every URL/route must have unit test coverage. Consider both positive and negative tests. |
| 84 | + |
| 85 | + |
0 commit comments