@@ -5,11 +5,19 @@ This guide is to walk you step by step through the process of creating a Data Se
55This assumes that you want a user interface for querying your data service via a form.
66Many of these steps can be skipped if your service is only intended to be accessed internally.
77
8+ Once fully implemented, a dataservice should automatically show up in the proper nav bar drop downs, and be able to
9+ query a service via a form, displaying results to a custom table, then finally saving desired results to a TOM's DB.
10+
811Setting up the Basic Data Service:
912**********************************
1013
1114First we will build the bare bones of our data service. This is the bare minimum to get the service to show up in the
12- TOM. We'll start with three peices of code:
15+ TOM. We'll start with three pieces of generic code:
16+
17+ - Our Query class (an extension of `tom_dataservices.dataservice.DataService `)
18+ - Our Form Class (an extension of `tom_dataservices.forms.BaseQueryForm `)
19+ - An integration point for our data service in `Apps.py `
20+
1321
1422First the actual query class:
1523+++++++++++++++++++++++++++++
@@ -19,10 +27,10 @@ First the actual query class:
1927 :caption: my_dataservice.py
2028 :linenos:
2129
22- from tom_dataservices.dataservices import BaseDataService
30+ from tom_dataservices.dataservices import DataService
2331 from my_dataservice.forms import MyServiceForm
2432
25- class MyDataService (BaseDataService ):
33+ class MyDataService (DataService ):
2634 """
2735 This is an Example Data Service with the minimum required
2836 functionality.
@@ -88,18 +96,31 @@ Adding the integration point:
8896 """
8997 return [{' class' : f ' { self .name} .my_dataservice.MyDataService ' }]
9098
99+ Once all of these are done, you should be able to see your basic form in a test TOM:
100+
101+
102+ |image0 |
91103
92104Customizing your Data Service:
93105******************************
94106
95- The next step is to update our code to have all specific features relevent for our data service. Here we will focus on
96- extending several methods of `BaseDataService ` to be relevent for your data service.
107+ The next step is to update our code to have all of the specific features relevant for our data service. Here we will focus on
108+ extending several methods of `DataService ` to perform the specific tasks needed to interface with your data service.
109+ Ultimately there are many things that can be customized for your DataService, and many tools built into the base class
110+ to help you do this. This section will take you through the fundamentals to get you started, but you should review the
111+ :doc: `full class documentation <../api/tom_dataservices/data_services >` before you precede.
97112
98113
99- `BaseDataService.build_query_parameters `
114+ Filling out our `MyServiceForm `
115+ +++++++++++++++++++++++++++++++
116+ First, we will need actual fields in our Form. For more on this, see the `official Django
117+ docs <https://docs.djangoproject.com/en/stable/topics/forms/> `__.
118+
119+
120+ `DataService.build_query_parameters `
100121++++++++++++++++++++++++++++++++++++++++
101122
102- For starters , let's make our `build_query_parameters ` function inside of `MyDataService ` actually do something.
123+ Next , let's make our `build_query_parameters ` function inside of `MyDataService ` actually do something.
103124This code is to convert all of the form fields into a data dictionary or set of query parameters that is understood by
104125the data service (or more specifically our `query_service ` method.)
105126
@@ -123,10 +144,10 @@ In some cases, this can be very straightforward, while in others this can involv
123144commands. Ultimately this is based on the API or client of your Data Service, and how you chose to name your form
124145fields.
125146
126- `BaseDataService .query_service `
147+ `DataService .query_service `
127148+++++++++++++++++++++++++++++++
128149
129- Next we will need to fill out our `query_service ` module. This is the function that actualy goes and calls the query
150+ Next we will need to fill out our `query_service ` module. This is the function that actually goes and calls the query
130151service using the parameters created by `build_query_parameters `. This function produces query results that can then be
131152interpreted by `query_targets `, `query_photometry `, or other functions to produce specific kinds of results that can be
132153interpreted by your TOM.
@@ -149,41 +170,44 @@ interpreted by your TOM.
149170
150171 Again, depending on the nature of your data service, the `query_service ` function could take many different forms.
151172This may also require you to create a `build_headers ` method, or make use of the `urls `, `get_configuration `, or
152- `get_credentials`methods. Saving the results to `self.query_results ` could save time in other methods by not requireing
173+ `get_credentials`methods. Saving the results to `self.query_results ` could save time in other methods by not requiring
153174you to redo the query.
154175
155- `BaseDataService.query_target `
176+ `DataService.query_targets `
156177++++++++++++++++++++++++++++++
157178
158- We will just use `query_target ` as an example. The same ideas apply to any of the individual query functions.
179+ We will just use `query_targets ` as an example. The same ideas apply to any of the individual query functions.
159180This is the function that pulls useful data from the query results in a way that the TOM understands. In this case, we
160- will be extracting Target data from the query results and creating a dictionary .
181+ will be extracting Target data from the query results and creating a list of dictionaries containing this target data .
161182
162183.. code-block :: python
163184 :caption: my_dataservice.MyDataService
164185 :linenos:
165186
166- def query_target (self , data , ** kwargs ):
187+ def query_targets (self , query_parameters , ** kwargs ):
167188 """
168- This code calls `query_dataservice ` and returns a dictionary of results.
169- This call and the results should be tailroed towards describing targets.
189+ This code calls `query_service ` and returns a list of dictionaries containing target results.
190+ This call and the results should be tailored towards describing targets.
170191 """
171- query_results = super ().query_targets(data)
192+ # I can update my query parameters to include target specific information here if necessary
193+ query_results = self .query_service(query_parameters)
172194 targets = []
173195 for result in query_results:
174196 result[' name' ] = f " MyService: { result[' ra' ]} , { result[' dec' ]} "
175197 targets.append(result)
176- return targets
198+ return targets # This should always be a list of dictionaries.
177199
178200 In this example, we create or modify the name of a query result so we will have something to enter into the TOM.
179201Line 6 calls the super which will either retrieve `self.query_results ` if it exists or run `query_service `.
180- The final output should be a dictionary of results.
202+ The final output should be a list of dictionaries containing target results.
181203
182- `BaseDataService.create_target_from_query `
204+ At this point you should be seeing a list of Targets showing up in your TOM after you perform a query.
205+
206+ `DataService.create_target_from_query `
183207++++++++++++++++++++++++++++++++++++++++++
184208
185209Continuing with our `target ` example, we need to be able to `create_target_from_query ` in order to actually save the
186- target object resulting from a succesful result for `query_target ` above. This function expects a single instance with
210+ target object resulting from a sucessful result for `query_target ` above. This function expects a single instance with
187211the same format as the list of dictionaries created by `query_targets ` and converts that dictionary into a Target Object
188212returning that object.
189213
@@ -218,5 +242,7 @@ Each of these different kinds of data will require functions in `MyDataService`
218242the data service and returning a list of dictionaries in `query_foo() `, and then translating an instance of that dictionary
219243into a model object with `create_foo_from_query() `.
220244
221- Depending on the specifics of your data service, it may be reasonable to call the `query_foo() ` methods indipendently,
222- and/or part of `query_targets `.
245+ Depending on the specifics of your data service, it may be reasonable to call the `query_foo() ` methods independently,
246+ and/or part of `query_targets `.
247+
248+ .. |image0 | image :: /_static/dataservices_doc/demo_Data_Service.png
0 commit comments