What to do if you need to work with data that is not Big data by volume, but already more than what fits in the computer's memory and for which Excel features are enough? For lovers of cycling, the answer is obvious - you need to write something of your own (yes, we are not looking for easy ways).
But what if the code you wrote in the past is terrible and does not give you the opportunity to develop the project? To set aside old practices, to give way to the new, eternal and bright (yes, and here, too, with no options).
')
A quote from the Matrix: “One tablet is enough, Neo.” Director: Wachowski brothers and sisters. 1999. United States
Already more than 10 years ago, I wrote the code for the ASH Viewer (for more information about my path
here ), I first posted it on
sourceforge.net , and then on
github so that people can connect and add functionality, correct errors. The project was assembled with the help of Gradle, they solved the questions with graphs: an almost complete list of improvements
by reference .
However, I understood that the code written ten years ago is, to put it mildly, imperfect. Outwardly, everything looked good, the functionality was developed, people actively used and thanked. But inside the program contained all the errors of the first coding experience, and, of course, this greatly hampered the development of the project.
To anyone who was ready to seriously begin work on the application (for example,
dcvetkov ), I said that the code requires complete rework. And with each attempt to introduce any functionality or to fix errors, I was convinced of this. I will report to you, rule the legacy-code - a terrible torture, especially your own :). I hope that in the future, robots will learn to rewrite everything themselves, but for now, it was decided to start from scratch, taking into account the experience already gained in creating programs and writing code.
Since the first release, ASH Viewer has been actively working in this direction. There were 3 fairly large
JfreeChart- based
projects for data analysis. In addition to these projects, I tried different approaches, paradigms and libraries. As a result, I stopped at the fact that you need to write everything in pure Java, without using libraries specifically designed to create a graphical interface from scratch. But still, it is quite acceptable to use third-party libraries for solving some highly specialized tasks - this will save the necessary level of flexibility and does not require a significant investment of time to fix bugs and develop the required functionality with your own hands.
How it all began
I was always worried, but is there an example in free access that would give the basics of correct writing applications of average complexity in Java Swing? Of course, I was referred to the library itself or to some simple examples from textbooks. And in some ways they were right.
But I persistently searched for application code that could be used as an example of “how to do it right” in Java Swing. And I wanted to have a working system in front of my eyes so that it could be “felt”.
Began to study the source code of applications for Java Swing (all of them can not be counted). Somewhere it was simple ToDo, somewhere too complex (for me at that time), in some quality failed, and sometimes it was both. I read articles on Habré, wrote the code. But still something was missing. Perhaps, I was just then gaining a critical amount of knowledge to solve this problem.
One day I found
Angon IP Scanner of the respected Anton Cake
antonkeks , looked at it and immediately understood - that's it! Java Swing, simple functionality, clean code, modularity - nice to read! In general, I used the approaches used in it as a basis for writing one of my previous projects, and then when rewriting ASH Viewer.
Software and libraries that helped improve the quality of the code and simplify the work
IDEA : for five years now I have been using this IDE for programming in Java. I confirm the opinion of the majority - this is a really useful program and a very convenient tool for writing code. When I moved to it from Eclipse
www.eclipse.org/ide (and the first version was written on this IDE), after a short training I realized IDEA leads you and tells you when you try to go to the dark side :). Highlighting repeats in the code keeps you in good shape and does not allow you to engage in stupid copy-paste. Ave JetBrains!
Java 8 : lambda expressions that make it possible to write a shorter code, a new Time API, which allows you to cancel the use of the third-party Joda Time library.
Dagger 2 : I didn’t use the Dependency injection framework before. But somehow I saw how this library was used by Anton
antonkeks , and began to do it in a pattern. To divide the program into modules, where possible, use dependency injection. Where this was
not possible , I used the creation of shell objects in advance, and then set the necessary attributes or simply did not use DI.
Maven build system. It is this build system that is de facto standard, so I decided to add libraries cleanly via pom.xml and use the Maven module system to work with the JFreeChart and Gantt code in one project.
Lombok : is also an incredibly convenient library, in order not to write and not support the "wrappers" of uniform code (getters, setters, etc.). True, in some cases I refused to use it, because I had to redefine equals and compareTo, but I did not find how quickly to do this.
Logging: making the perfect Java program? So, without modern means of logging anywhere. Therefore, we take as a basis
Simple Logging Facade for Java SLF4J and
Logback .
Layout Manager: I mainly use
Miglayout . Rather difficult to master (in some places I use Swing layout managers in the old manner), but short. Allows you to do these interesting
effects , as in the Detail tab.
Swingx from Swinglabs: a long-abandoned Java Swing UI tooklit. I actively use JXTable. Arbitrary selection of table columns and built-in search by cell contents facilitate detailed analysis of data from the history of active sessions.
Commons-dbcp2 : useful for creating a connection pool connections to the database. In the old version I used a modified implementation that I found on the Internet.
Libraries that have moved from the old version
Oracle Berkeley DB Java Edition v. 5.0.73: embedded key-value storage. To store aggregated data history of active sessions.
JFreeChart : Thousands of data analysis projects written using this library. Based on the experimental version, which is laid out on github and added it as a module. This was done for the convenience of working with the code, since changes were
required so that the Stacked Chart would display the graph as needed.
E-Gantt : a library for creating gantt graphs in Java Swing. Traces of it now can not be found even on the Internet, alas. Also posted a separate Maven module in the project.
Of the interesting in the code that you can pay attention
Architectural changes:
- Now the settings are stored in a separate embedded database, not in plain text files. Since there is little data, the advanced EAV pattern is used to store the connection settings;
- To store the monitoring data I decided to make a similarity to the OLAP-engine. First, to speed up the display of Gantt granularity for SQL_ID / SESSION_ID on the selected range. Secondly, to be able to get quick details on SQL_ID / SESSION_ID on stacked and Gantt charts. Thirdly, the formation in the future of viewing the history of active sessions (Top of expectations, detailing on expectations, detailing on SQL_ID / SESSION_ID). Everything is stored in one entity (data by second, 15-second and, in perspective, by other extended intervals are physically separated);
- A side effect of clean architecture is the ability to support monitoring the history of active sessions of other databases. Postgres support is currently available. To connect other databases, you need either a ready-made interface to the data of the history of active sessions (which was added to Postgres or such an implementation ) or a self-configured collection of the history of active sessions into a separate table, which can later be accessed.
How to enable support for another database- Create a new class and implement the IProfile interface. Do the same as for Postgres;
- Add to the loadProfile procedure of the ConnectToDbArea class and the enum Function of the ConstantManager class the implementation for the new database version;
- Connect and test the application.
Graphical interface
Database Connection Form
Completely rewritten from scratch, used earlier developments c open project
Squirrel-sql . Now everything is in
one file. Beauty!
How to connect to the database- Create a new connection;
- Specify the name, URL (JDBC standard for Oracle: jdbc: oracle: thin: @host: port: SID, for Postgres: jdbc: postgresql: // host: port: database), username / password, profile, and select the jdbc library;
- For Oracle, everything works with ojdbc6.jar, for PostgresDB, work with postgresql-42.2.5 is checked
Interface Top activity / Detail
Here, without significant changes, similarly as in the old version, only without viewing the history.
Drilling by SQL_ID / SESSION_ID
SQL
ASH : activity graph for a certain SQL_ID, caused by double clicking on the line with SQL_ID from the Gantt graph.
Sql text / plan : for Oracle / Postgres it is possible to get the full text of the request. Oracle-only provides query execution plans for all plan_hash_value.
Statistics : tabulated data by SQL_ID: selection from V $ SQL. In the code, it is possible to add more entities for which you can make a selection (see
implementation ). but you need to be very careful, as there may be performance problems: for example, sampling from
V $ SQLAREA on loaded systems is very slow).
Session
ASH : activity graph for session_id, similarly to SQL, invoked by double clicking on the session string from the gantt graph.
Statistics : tabular data for SQL_ID: selection from V $ SESSION and V $ PROCESS. In the code, it is possible to add more entities for which you can make a selection (see
implementation ).
Future plans
- Fix API. Conduct final code refactoring. Implement dynamic storage of the initial monitoring data, which would not depend on the versions and types of databases;
- There are a lot of tests that check the key modules of the system, CI and other best practices.
Project code on
github ,
project files ;
Link to a group in Telegrams
t.me/ashviewer for information on the latest updates;
PS Whoever decides to connect to the development - write in the LAN, without undue agiotage and without creating a crush, of course :).
That's all. Thanks for attention!