Dear Patryk, Mathias

As promised, I had some free time today after the talks, so here is the
initial sample script to sync patients and studies from an Orthanc that
can be run in a cronjob at operating system level.

We'll work more on health_orthanc, to get more return codes, and
details and pass them to the script, but is a good start :)

Once you have your Orthanc server label setup, you can invoke the
script as follow:

Usage : sync_orthanc_server <hostname> <port> <user> <password>
<dbname> <orthanc_server_label>

For example:

python ./sync_orthanc_server.py localhost 8000 admin gnusolidario
health42 FAA

(where "FAA" correspond to the given label at GNU Health.

The expected output would be something like...

Connecting to GNU Health Server
http://admin:gnusolidario@localhost:8000/health42/
Connected !
Last index: 2843
Last Synced on: 2023-05-24 21:39:58
Orthanc Server URL: https://demo.orthanc-server.com

At GNU Health HMIS Tryton server, you would get this type of output.

2255 37818935296 [2023-05-24 21:39:47,263] INFO apiron.client GET
https://demo.orthanc-server.com/changes/?since=2200&limit=100 2255
37818935296 [2023-05-24 21:39:47,857] INFO apiron.client 200
...

trytond.modules.health_orthanc.health_orthanc <FAA> at newest change

2255 37818935296 [2023-05-24 21:39:50,048] INFO apiron.client GET
https://demo.orthanc-server.com/patients/46e6332c-677825b6-202fcf7c-f787bc5f-7b07c382/
2255 37818935296 [2023-05-24 21:39:52,800] INFO apiron.client 200
https://demo.orthanc-server.com/patients/0946fcb6-cf12ab43-bad958c1-bf057ad5-0fc6f54c/
2255 37818935296 [2023-05-24 21:39:52,804] INFO apiron.client GET
...

2255 37818935296 [2023-05-24 21:39:58,972] INFO

And at the end of the run, the result:

<FAA> sync complete: 8 new patients, 0  
update patients, 8 new studies, 0 updated studies

You can find the latest version of the script at our GNU Savannah
mercurial repository (development branch):

https://hg.savannah.gnu.org/hgweb/health/file/tip/tryton/doc/samples/interfaces/orthanc

Let me know how it went 

Have a great day! 
Luis

On Fri, 19 May 2023 11:37:58 +0200
p.ro...@stud.uni-hannover.de <p.ro...@stud.uni-hannover.de> wrote:

> Hello Mathias,
> 
> there were 2 reasons why I decided to use low-level programming. In
> the module "health_orthanc" a button for the one-time manual
> synchronisation is already provided and therefore I thought it would
> be useful to have the synchronisation in one place to increase the
> usability. The other reason is that I saw in the wiki
> (https://en.wikibooks.org/wiki/GNU_Health/Imaging) that there should
> already be a tryton cronjob by activating the module. But since this
> is not the case, I thought that maybe there were problems with the
> execution of the cronjob. If you think that the solution with the use
> of the cronjob should actually be feasible, I would be grateful for
> feedback.
> 
> Best regards
> Patryk
> 
> 
> 
> * p.ro...@stud.uni-hannover.de: " [Health-dev] Integration of Orthanc
> (module health_orthanc)" (Wed, 17 May 2023 16:53:01 +0200):
> 
> Hello Patryk,
> 
> is there any special reason to do this via low level programming?
> 
> Usually regular returning tasks are performed with the included
> scheduler (cron). It also offers to run the task manually once. You
> can have a look at the Tryton core modules 'stock' or
> 'account_payment_stripe' to find examples.
> 
> Best
> Mathias
> 
> 
> > Hello dear GNU Health community ,
> >
> > I am currently writing my master thesis and trying to implement a
> > periodic synchronisation of the Orthanc and GNU Health DB.
> > Unfortunately I run into some problems. I create a background
> > thread in which the synchronisation should be done periodically. My
> > idea was to initialise the variable "sync_thread = None" in the
> > class "OrthancServerConfig" in order to be able to start and stop
> > the synchronisation via a start and stop button. In addition, I use
> > the already implemented method "sync" of the same class. I have
> > already tried this:
> >
> > class SyncThread(threading.Thread):
> >     """A background thread that periodically synchronizes data with
> > an Orthanc server.""" def __init__(self, servers=None, timeout=180):
> >         """
> >         Initialize the SyncThread object as a background service.
> >         :param servers: A list of Orthanc servers.
> >         :type servers: list
> >         :param timeout: The time interval (in seconds) between each
> > synchronization. :type timeout: int
> >         """
> >         threading.Thread.__init__(self,
> > name="OrthancPeriodicSyncronizationThread") self.daemon = True
> >         self.servers = servers
> >         self.timeout = timeout
> >         self.stop_event = threading.Event()
> >
> >     def run(self):
> >         """Call loop with timeout to synchronize the Orthanc DB
> > with the GNU Health DB.""" with Transaction().start():         <--
> > (line in which error 1 occurs) while not self.stop_event.is_set():
> >                 OrthancServerConfig.sync(self.servers)
> >                 self.stop_event.wait(self.timeout)
> >     
> >     def stop(self):
> >         """Stop the synchronization thread."""
> >         self.stop_event.set()
> >
> >
> > ​​​​​​Class OrthancServerConfig​(ModelSQL, ModelView):
> >     sync_thread = None
> >     ...
> >     @classmethod
> >     @ModelView.button
> >     def start_periodic_sync(cls, servers):
> >         """
> >         Initializes the synchronization of data with Orthanc as a
> > background service. :param servers: A list of Orthanc servers.
> >         :type servers: list
> >         """
> >         if cls.sync_thread is not None:
> >             raise UserWarning("Synchronization is already running!")
> >         else:
> >             cls.sync_thread = SyncThread(servers=servers)
> >             cls.sync_thread.start()
> >
> >     @classmethod
> >     @ModelView.button
> >     def stop_periodic_sync(cls):  <-- (error 2 occurs here, when
> > "stop"-button is pressed) """
> >         Stop the synchronization thread.
> >         """
> >         if cls.sync_thread is None:
> >             raise UserWarning("No synchronization is performed!")
> >         else:
> >             cls.sync_thread.stop()
> >             cls.sync_thread.join()
> >             cls.sync_thread = None
> >
> > 2 errors occur, for which I unfortunately don't know an answer to
> > fix them. Error 1 occurs, when the "start" button is pressed and
> > the "run"-method of the class "SyncThread" is triggered:
> > orthanc/health_orthanc.py", line 66, in run with
> > Transaction().start(): TypeError: Transaction.start() missing 2
> > required positional arguments: 'database_name' and 'user' I'm not
> > sure, if "with Transaction.start():" is necessary here, but i tried
> > this, because i got this error before: orthanc/health_orthanc.py",
> > line 269, in sync pool = Pool() File
> > "/opt/gnuhealth/venv/lib/python3.10/site-packages/trytond/pool.py",
> > line 61, in __new__ database_name = Transaction().database.name
> > AttributeError: 'NoneType' object has no attribute 'name' If "with
> > Transaction.start():" is required, unfortunately I wasn't able to
> > figure out which are the two required arguments or if the solution
> > is generally in a completely different direction.
> >
> > The second error occurs, when the "stop" button is pressed:
> >     TypeError: OrthancServerConfig.stop_periodic_sync() takes 1
> > positional argument but 2 were given Here I was also not able to
> > interpret the error, because both my button and the stop method are
> > not passed 2 arguments.
> >
> > I would be grateful for any help and am available for queries!
> >
> > Best regards,
> > ​​​​​​​Patryk
> >    
> 
> 
> 
> --
> 
> Mathias Behrle
> MBSolutions
> Gilgenmatten 10 A
> D-79114 Freiburg
> 
> Tel: +49(761)471023
> Fax: +49(761)4770816
> http://www.m9s.biz
> UStIdNr: DE 142009020
> PGP/GnuPG key availabable from any keyserver, ID: 0xD6D09BE48405BBF6
> AC29 7E5C 46B9 D0B6 1C71 7681 D6D0 9BE4 8405 BBF6
> 
>  


Reply via email to