Wednesday, December 5, 2018

Generating Complementary Color Pairs with JavaScript

When using a library, such as Chart.js, you will sometimes want to assign colors to elementt, such as bars or pie wedges, in a functional way so that n items have unique colors.  Chart.js in particular allows you to provide a fill and a border color, each as an array in length equal to your dataset.

For the purpose of creating pleasant colors with "half tone" complements I recently rolled a few helper functions based on an example for generating random rgba color strings.  I hope they are useful to you.

 // biased towards the upper end of the brightness spectrum 
 function random_rgba() {
           var o = Math.round, r = Math.random, s = 100;
           var rgba = [];     
           rgba.push(o(r()*s)+155);
           rgba.push(o(r()*s)+155);
           rgba.push(o(r()*s)+155);
           rgba.push(1);
           return rgba;
       }
      
       function dimRGBA(rgba){
              var tone = .7;
              var dimtone = [];
              dimtone.push(Math.abs(rgba[0] * tone));
              dimtone.push(Math.abs(rgba[1] * tone));
              dimtone.push(Math.abs(rgba[2] * tone));
              dimtone.push(1);
              return dimtone;
       }
      
       function stringifyRgba(rgba){
              return 'rgba(' + rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ',' + rgba[3] + ')';
       }
      
       function rgbaComplementaryPair(){
              var pair = [];
              var sourcecolor = random_rgba();
              var complement = dimRGBA(sourcecolor);
              pair.push(stringifyRgba(sourcecolor));
              pair.push(stringifyRgba(complement));
              return pair;
       }

Thursday, September 27, 2018

Tomcat Manager Dark Theme

If you use Tomcat as a web server, you know the manager console isn't much to look at.  It's adequate, but not pleasant.

As a developer, I enjoy a lot of the dark themes for things like eclipse and other editors . So, to prevent blindness when switching to the tomcat management console, I created a set of stylesheet overrides you can use in Chrome with the Stylish plugin. It offers a little more user friendliness by highlighting the table row for the application you are about to interact with when you click Stop, Undeploy or Reload .  There are also some UI hints for hovering over controls.

Please enjoy, and feel free to comment with suggested improvements.

input[type="submit"]{background-color: #919090!important; color: #823d01!important}

input[type="submit"]:hover{background-color: #737373!important; color: #ffb829!important; border-color: #555555!important; cursor: pointer}


body, table {background-color: #332a1f!important; border: 0px solid!important}

table{border-collapse: collapse!important; border-color:#866635!important}

button, input {background-color: #333333!important; border: 1px solid white!important; border-radius: 5px; color: #ffb300}

input:hover{background-color:#aaaaaa!important;color:#aa6900!important}



tr {background-color: #777777!important}

tr:hover{background-color: #919191!important} 
tr:hover a {color: #333333}


td {background-color: rgba(0,0,0,0)!important; color:inherit!important}

a{background-color:rgba(0,0,0,0)!important}

*{color:white}

a{color: #ffb300}

tr:hovor{border-color: #aaaaaa!important; background-color:#999999!important; color:#aa6900!important}

img {height:32px;border-radius:20px;border:#a8a8a8 2px solid}

Tuesday, March 27, 2018

Upgrading Performance with a Solid State Drive

You want to receive the best performance from your computer, but you may not want to have to spend a lot of money to get it.

One simple way to increase performance is to add an inexpensive SSD drive to host your most heavily used programs and files.  Normally, just copying files around between hard drives on Windows produces broken file short-cut links and invalid registry entries.  You could go track all of those down and edit them by hand, or you could use symbolic links (or hard links) to tell Windows that the files have moved.  Then, any time a file is requested from the old location, Windows sees it at the new location, as though it is still in the old location.  

Hard Links, a feature in Windows since version 7, makes this task much easier than uninstalling and reinstalling your programs or editing registry entries by hand.  Here's an example from my computer.  I hand noticed that loading my desktop and many documents I use regularly had become slow on the spinning hard disk, so this made my User Profile directory the first candidate for migration.

First, I used Windows explorer to simply drag and drop the folder from one place to another.  Then, launching the Command Window as Administrator (find it by hitting the Windows key and then typing "CMD", right click on the short-cut that appears and select "Run as Administrator"), I issued the following command:


C:\Users\JC>mklink /J "C:\Users\JC\Documents" "G:\Users\JC\Documents"
Junction created for C:\Users\JC\Documents <<===>> G:\Users\JC\Documents

This produced immediate benefits. For starters, when I start windows and login, my Desktop appears almost immediately.  Opening any files that I may have been working on, such as large CAD or Sketchup files, has extremely low lag. What used to take many seconds is almost now instantaneous. 

This is a powerful feature for getting more useful life out of your ageing systems.  While we wait for Intel and AMD to re-engineer their chip sets to exclude the vulnerabilities published early this year, $40 - $50 spent on a solid state drive is a tenth or twentieth what you would pay for a full system upgrade at this time.  


Bench Notes:

Something I noticed, however, when I began copying a large folder containing around 79 GB of data, was that about 30% of the way in, the data transfer rate topped out around 35.1 MB/second and then began slowly falling. I deduced that the chips responsible for I/O were getting hot, increasing resistance, and slowing data transfer.  So I fired up SpeedFan, a tool for tuning the speed of your variable speed on board fans, and it immediately increased the RPM of one internal fan.  Over the next several seconds, the data transfer rate rose from 34.5 to 38.4 before slowly declining again.  While I don't know for sure that the heat build up was slowing the data transfer rate, SpeedFan did report that the physical hard disk was a desiccating 124F. 

Also, in my case, I sacrificed having a connected DVD drive for the addition of the SSD drive due to a lack of SATA cables. If you order an SSD, make sure you order a connection cable set as well.  You'll need one for power and one for data or a combo connector.  Take a peak at your mother board to determine what you need or have a trusted service technician do this for you.

A word on backups: always have a backup solution in place for your important data.  While SSD drives are now a mature technology, when an SSD drive fails, the data is almost always lost unless you have a skillful electrical engineer with some experience in repairing them handy.  I recommend the freeware app "Create Synchronicity" for scheduling backups, and suggest you have a home NAS (Network Attached Storage) somewhere on premises to serve as an archive.  

Thursday, January 25, 2018

Understanding @Autowired

The life of a full stack developer is an adventure full of new things to learn daily.  This week, after using some templates for about a year based in Spring Boot, I finally came to understand what is going on with @Autowired.

Spring Boot, if you use it for Java development, provides a very clean and easy to use dependency injection model that falls under the @Autowired annotation.  It’s "easy to use", that is, once you wrap your head around top-down dependency injection and what it means for instantiated classes.  The trick is starting with the SpringWebConfig class, where your instantiated classes will no longer be created with the “new” operator, but annotated wth the @Bean annotation.

In SpringWebConfig.class:
@Bean
public MyClass getMyClass(){
       return new MyClass(); // the one place you call new
}

What @Bean buys you is a registration into the Spring context loader, making it available throughout all other classes annotated with @Component.  The downside for this is that if you want to use any @Autowired dependency in a class, such as Environment, you have to register that class as a bean and then auto wire an instance of the bean wherever you would have instantiated the class, as with a Test class or main execution class like a Controller or Main.  This means classes will be eagerly loaded, creating slightly longer start up times for applications, but less code to write and manage.

Update: I neglected to point out that a benefit of using beans in this way is that they are effectively all singletons. This means they will exist in memory once, which enforces tidy memory management and can improve your application performance at run time.  A small recent anecdote: migrating from using jdbc connections in just a handful of classes to using a bean to register a single Spring JdbcTemplate reduced our database open connections from 117 to less than 20.  While this means less memory consumed on your application server, it also frees up database server resources that would otherwise be spent maintaining so many connections.

The trick here is to remember to avoid things like...

MyClass myClassInstance = new MyClass(); // this will cause your autowired instance to throw a null pointer exception.

...and prefer instead something that seems a bit more complex at first glance, but ties lots of things together as if by magic. In the places you would use your class, such as in a test class, access the context scoped class instance this way:
@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes ={SpringWebConfig.class})
public class TestClass{
                @Autowired
                private MyClass myClassInstance;  // at run time, this will be instantiated by the SpringWebConfig class and will be scoped to TestClass
 
                @Test
                public void testTheClass(){
                                assertNotNull(myClassInstance); // success!
               } 
}
This does change the way you'll work with constructors.  Passing values to constructors doesn't work well in this model.  Prefer instead to write methods in your classes that accept configuration parameters, if needed.

You can read much more about Spring annotations here or check out the official reference guide.

Monday, January 15, 2018

SQL-fu: Modify a Data Bearing Table

I was faced with a challenge: add a column to a table.  It sounds simple enough, but the table already had data in it, and had an Identity column, so care had to be taken to preserve data as well as identities as they were used as part of a key on another table.  After discussing possibilities with my DBA, we came up with the following approach.

1.       Copy the main table to a backup table on the same database.
2.       Drop the original table
3.       Create the modified table structure including Identity declaration
4.       Turn on Identity Insert so that columns usually protected and written only by the server can be written from the backup data, thus preserving the identities
5.       Insert the backup data, sans new column, into the new table
6.       Update the new table with default values for the new column (optional)
7.       Turn off identity insert
8.       Drop the backup table

You can run these steps one at a time to confirm they are working properly, or run them all at once provided you break the queries into separate work units with the GO statement, otherwise you’ll be attempting to write to columns that don’t exist at compile time.

Here’s a sample SQL script that accomplishes the above task.

SELECT * INTO BACKUP_MYTABLE from MYTABLE
GO
DROP TABLE MYTABLE
GO
CREATE TABLE MYTABLE
(
                oldIdentityCol int NOT NULL IDENTITY(1,1),
Oldcolumn1 varchar(10),  -- as appropriate to your original data structure
Newcolumn int
)
Go
SET IDENTITY_INSERT MYTABLE ON
GO
INSERT INTO MYTABLE (oldIdentityCol, oldColumn1,) SELECT oldIdentityCol, oldColumn1 from BACKUP_MYTABLE
GO
SET IDENTITY_INSERT MYTABLE OFF
GO
UPDATE MYTABLE SET NEWCOLUMN = 1 where NEWCOLUMN IS NULL – optionally populate your new colum
GO
Drop TABLE BACKUP_MYTABLE
GO