fossasia / visdom

A flexible tool for creating, organizing, and sharing visualizations of live, rich data. Supports Torch and Numpy.
Apache License 2.0
9.93k stars 1.13k forks source link

Deduplicate Socket-Handler Code #898

Closed da-h closed 1 year ago

da-h commented 1 year ago

Description

This PR merges duplicated code in the six classes from the file py/visdom/server/handers/socket_handlers.py:

reducing also the total file size by about 29%.

Notes:

Motivation and Context

Some duplicated code in the four classes has been quite entangled. This PR aims to reuse the respective code segments to make code changes more consistent.

How Has This Been Tested?

-

Screenshots (if appropriate):

-

Types of changes

Checklist:

da-h commented 1 year ago

Suggestions

  1. I propose a further unification by allowing any client to be both Sockets (i.e., reading data access) and VisSockets (i.e., writing data access). As both classes are function-wise completely orthogonal, the feature should be easily implementable in a backward-compatible way, and the change would simplify the code quite a bit.
  2. Currently, the REST-API and the socket-API provide mostly orthogonal functions/commands to any client. For instance, the REST-API provides win_exists, fork_env, etc., while the socket-API provides save_layouts, delete_env, etc. I suggest merging the REST-API endpoints with the socket endpoints; both could offer the same commands to any client capable of handling these. Also, an easier-to-use endpoint structure could pave the way for further client implementations.

What do you think?

JackUrb commented 1 year ago

Currently, the REST-API and the socket-API provide mostly orthogonal functions/commands to any client.

This is an astute observation, and I agree that further unification would be possible. Perhaps this could be improved by a VisAPI base class, which could then be extended for versions using REST vs socket-based communication in either direction, and then people could initialize Visdom with configuration options that assist setup.

class VisAPI(...):
  ...

  @staticmethod
  def from_config(...) -> "VisAPI":
    ...

Then connecting clients to the server could be either, so long as they're using the agreed VisAPI.

da-h commented 1 year ago

This is an astute observation, and I agree that further unification would be possible. Perhaps this could be improved by a VisAPI base class, which could then be extended for versions using REST vs socket-based communication in either direction

I completely agree here!

and then people could initialize Visdom with configuration options that assist setup.

I am not sure if I understand it correctly. Do you mean: there could be a handshake between server and client that contains what commands they both agree with? So to speak a rolling API-version?

JackUrb commented 1 year ago

there could be a handshake between server and client that contains what commands they both agree with? So to speak a rolling API-version?

Ah not quite, that's more complicated than what I imagine. Just that the Visdom object can have different instances of the API abstraction to allow for all the weird network settings that exist out there, and so long as they adhere to the API (and either REST or sockets) then the server can register them

da-h commented 1 year ago

Then connecting clients to the server could be either, so long as they're using the agreed VisAPI.

One further question here: What would be the benefit of such an predefined agreement instead of just allowing any client to open any socket-type at any time? (The latter seems easier to implement & to manage, however, I am also open to implement the former if it is better.)

JackUrb commented 1 year ago

Then connecting clients to the server could be either, so long as they're using the agreed VisAPI.

One further question here: What would be the benefit of such an predefined agreement instead of just allowing any client to open any socket-type at any time? (The latter seems easier to implement & to manage, however, I am also open to implement the former if it is better.)

It's mostly just about establishing some common API layer, then we don't really need to care which one someone is opening.

da-h commented 1 year ago

Got it. I think we have simply misunderstood each other a bit here. :sweat_smile: Thanks for clarifying. :)

da-h commented 1 year ago

Rebased onto master to check if everything works with the new functional main-app. (Also applied the new black formatting rules).

da-h commented 1 year ago

Ok, some tests regarding moving an Image in the ImagePane fail. It seems the error is the same as in #901, i.e. the expected image dimensions are off by one or two pixels. Somehow the combination of #900 and #902 has changed the behavior of mouse-positions a bit.

I am converting this to a draft as well, until the test is fixed / the bug is found.

da-h commented 1 year ago

With #908 in-place, this PR succeeds all tests now & is thus now ready for review.