Friday, December 21, 2012

Basic Anorm techniques using Scala

I started using Play! along with Scala and so I've been using Anorm as my ORM.  It reminds me very much of a project that I used, myBatis.  Anorm (not Another ORM) is a SQL data mapper.  

In my application, I need to create a summary of trades.  Here is the code for the controller and the model. The code uses a trade date (yyyymmmdd) to call the correct trade summary.

Controller:

Domain/model:

Tuesday, December 18, 2012

Playing around with Play framework

I did a presentation on the Play framework using Scala for the Miami Java User Group (JVM).  Below is the presentation along with the code to create a small application.

The first thing that you need to do is to download/install Scala and Play into your computer.

To create the application, go into your terminal console and do the following:
  1. play new foobar 
  2. Keep the same name of the application 
  3. Select the Scala template (1) 
  4. Change the directory to the foobar directory
  5. type "play run" 
  6. Go into http://localhost:9000 
You should be able to view the "Welcome" page for Play.

The structure is very similar to Ruby on Rails.
  • app: is the directory for all classes in the application 
    • controllers: all the controllers/actions for the applications 
    • models: domain objects 
    • views: the scala html pages 
  • conf: has all the properties files including the routes 
  • logs: the logs of the project 
  • project: build classes 
  • public: CSS and others 
  • target: compiled classes 

Looking at the conf/routes we can see the following: This says that the moment that the application goes to the "/" it will call the controller Application and invoke the "index" method.
The controller has the following code: The index method calls an Action and sends an OK, which is nothing but a HTTP 200 code, and calls the page views.html.index with the string parameter: "Your new application is ready".

If you look at the apps/views/index.scala.html you will see the following:
The message that was passed by the Application controller is stored in the message: String. The application calls a main html page and passes two parameters: "Welcome to Play 2.0" and "message".

If you look at the app/views/main.scala.html you can see that the first parameter is the "title" and the second parameter is the HTML content:
Remove the "@play20.welcome(message) so that the page looks like this: Render the page again, and you will see only the parameter passed by the controller. Lets go to the controller and edit the text by changing it to "Playing around".
Go back to the web browser and refresh the page. As you can see, the application renders automatically. The one thing that I like about the Scala programming language is that it is statically typed, so I can check in my editor. Lets configure the application as an Eclipse project.

Go to the console application and type control "D" or control "C" to stop the application. Now, type "play". You should see the play console. By typing "eclipsify" the play application will be ready to be imported as a project for Eclipse. Just go into Eclipse and import the project (file, import, select foobar directory). Once you are done type "run" to start the application.

As I mentioned, the conf/routes has the configuration for all the routes. If we add a route:
And go to http://localhost:9000/foo
You should see the same as if you go to http://localhost:9000.
Lets create an application that create a bar. The first thing we need to do is to create a model object named bar. Create a model by creating a Scala class in apps/models/Bar
The object will have only an id and a name. Now, we need to create a way to persist to a database. For Play the DB of choice is H2. It is a memory database and it is easy to work for development. To enable the database go to conf/application.conf and uncomment two fields:
Now that we have the drivers and the database, we need to create a script to create the database. Create a file for the DB schema inside conf/evolutions/default/1.sql
Refresh the page and you should be prompted to load up this script (or evolution). Just click on "apply this script now!"

Now, lets go back to the Bar model class and add the following:
This section will be the domain, which simply creates the bar and inserts it into the database. As you can see, we use the anorm API to map the SQL. This is not an ORM but more like a MyBatis (an SQL mapper).

Now, lets go to the Application controller and add the two things: the controller and the view. The first thing we need is to intercept the HTML form and add the action for the controller. Here we add the action and send a message depending on the type of transaction (error or success). We need to create the view form, but first we need to add the route to the controller.
Now, lets create the form view in the views/index.scala.html: You should be able to see the form in the application. Lets try to add a "test" bar. You shouldn't have any errors.

Now, we need to displays all the bars added. To do this, lets first modify our domain/model Bar object and controller. Using the allBars, we will be able to get all of the records from the bar table. For the controller, we will add the following: Now, we need to add the route in the conf/routes: If we go to http://localhost:9000/bars

You should be able to see something like this: We will want to render this URL and get all the JSON objects and put them into the index page asynchronously via CoffeeScript. First, lets create a folder app/assets/javascripts/index.coffee Here, the application will go to bars URL and fetch all the records, then it will iterate through the records, and append a list of all the bar names. Lets add the JavaScript header and bars into the index.scala.html

Now, lets add the JavaScript into the app/views/index.scala.html
You should be able to notice two things. You should be able to see all the inserted bars, and when you upload a bar, it should render automatically.

Wednesday, October 31, 2012

Groovy's Eclipse GroovyRuntimeException error with Log4J

When I updated my Eclipse's Juno plugins, all my Groovy projects came in with exception.   The error seems to be with the Log4J annotation "@log4j". This is what they said:


I was able to solve it by adding the latest snapshot here:

Once I was able to add this, all my errors went away.

Sunday, October 14, 2012

What is the product of your company? - is a byproduct

I follow Zach Holman, he works at GitHub. I follow him not because the guy is a techy, or because he works at GitHub, but because the guy just makes sense. Many of his blogs resonate through the different start-ups and companies I've worked. Specially, how to run a company which he talks in hist post the product is a byproduct.

I worked in many places that cared more about the "perception" of the company rather than the people. Many manager/directors worry about the image of the company without realizing that they are setting the tone of the company's culture (their workers), and this will affect their bottom-line/product. For example, "we expect you to work from 9 am - 6 pm", "we expect you to work AT the company, not at your house", "we expect you to dress a certain way - we have a dress code", "we need to ship this feature, we don't care how you get it done…that's your problem."

When we started a my former start-up, we thought we understood our customers, we thought we knew what they wanted, and we just wanted an application that WE would like. After all, "build it, and they will come", right? WRONG! The bottom line is that we were assholes. We really didn't have it right. It was until hard lessons learned that we started listening to our customers and focused on what they wanted rather on what we thought they wanted.  But also, it was until we started changing our culture and hiring the right people that the company finds its product.

Zach talked about things like:
Your product should be a byproduct of the people, process, and technology of your company.

Foster a good environment, be more likely to create a good product.

Hire those bothered by suck. You want fixers
I'm very committed to my companies, but I'm also in LOVE with my family. This is constant tough of war in my head. I do want to help the company in everything I can, but I also want to help my son with homework, and spend some time with my daughter and wife.  I do feel that where I work is MY company. I know that there are some people that said that it shouldn't be like this, "you shouldn't live for work, but work to live". But that's not what I mean. I work as it's my company in the perspective of shutting down my MacBook Pro,with the feeling of "I kicked ASS today".  It is not always like this, but most of the time, they are.   I like the fact that people (coworkers and directors) say, "ever since we hired Marcelo, this place is TIGHT", but I also want my family to say, "we love were you work, because we can see you and spend quality time with us".

As Zach mentions:
Any time you interview a potential hire, you need to ask yourself not only if they're talented or collaborative but also if they're capable of literally running this company, because they will." - VALVe

Hire broad people. Hire diverse people.

Good culture attracts good people

Be family friendly

Flexible location, hours, workload

Wednesday, August 29, 2012

Groovy's each does not exit when using return

Groovy's each is a very useful closure in collections. However, if you want to exit out of the code using return, it does not work. Below is the code. In this case, if the listId is empty, it sets the value for the "isValid" variable but never returns or break from the loop. The way that I was able to solve it was using a "find" method. Inside the if statement, I set the value for the "isValid" and then return true. The "true" value tells the "find" statement that the value was found, and therefore, it exits out.

Wednesday, June 20, 2012

Installing Graphite on Ubuntu 12.04

Install dependencies: Install whisper: Install and configure Carbon (data aggregator) Install Graphite (webapp): At the moment, uncomment the following line Configure Apache:

Edit the following file:/etc/apache2/sites-available/default
Make sure that the configuration for WSGISocketPrefix is set as follows:

Otherwise, you will get the following error:
[Tue Jun 19 13:21:28 2012] [error] [client 192.168.0.112] (2)No such file or directory: mod_wsgi (pid=19506): Unable to connect to WSGI daemon process 'graphite' on '/etc/apache2/run/wsgi.19365.1.1.sock' after multiple attempts.
Create initial database: Create the admin for graphite: Start carbon (data aggregator)

Check the log file for any error:
/opt/graphite$ find . -name '*.log' | xargs tail -F

To Feed the data do the following:

Go into the server localhost in your prefer browser and you should see graphite site.

Tuesday, April 24, 2012

First and last day of the month using Joda text

I had to create a SQL dump right after a new quarter starts. For example, if today is April 1st, then I had to create a dump file with the dates from 2012-01-01 to 2012-03-31.  The only problem was generating the first and end of the month using Java.  The best calendar/date API is by far Joda Date.  Here is the script on how I generated the two dates:

Wednesday, February 29, 2012

My two cents on failure

Dan North once said: "Fear leads to risk, risk leads to process, process leads to hate...and suffering and Gantt charts." I've worked for many companies (banks, financial institutions, and government) and most of them do not have the type of tolerance for failure. As a matter of fact, they encourage a "blame game" culture - people point the finger at the person who makes the failure. In these companies I was told that if you are the person that is given more bad news than good news to the boss, then you will be labeled as incompetent. The first thing out of my boss mouth when he noticed a problem/failure was, "Who did it? Who's responsible for that? Who's fault is it?" In these companies, if there was something wrong, people would fix it and didn't tell anyone. This created a nightmare when there were bugs on the system because no one would admit to the problem or how it happened. For the last five years, my career took a different turn. I joined a startup and eventually I end up in management, and I found a different paradigm, "failure is part of the business."

According to Harvarv Business Review, there are different type of failures:
  • Preventable: this is when it could have been prevented. For example, testing in production, deploying code before testing it, logged in as root and doing an "rm -rf /".
  • Complexity related: a large number of organizational failure are due to the inherent uncertainty of work: A particular combinations of needs, people, and problems may have never occurred before. Although serious failures can be averted
  • Intelligent: failure when experimentation is necessary. An example will be like "spike" in Agile.

Intelligent failures are normal in startups because you are trying something that hasn't been tried before. The company IDEO has the slogan:
Fail often in order to succeed sooner
This is something that not so many companies have. Entrepreneurs embrace failure and uncertainty. In startups, there is just too much uncertainty and as Steve Blank puts it,
In a startup no business plan ever survives first contact with customers.
So what happens if the original business plan fails? An entrepreneur adapts! He/she learns what went wrong, makes adjustments, and then monitors the new business plan. This means that failure is a natural part of doing business.

I am sure that all these previous businesses are aware of this issue. Obviously, they want to learn from their mistakes and avoid them in the future. But more often than not, bosses get irritated when a person presents them a problem. This is the difference between startups and stablished companies. As per HBR, companies can learn through three different activities:
  1. Detection: the should be to surface failure early.
  2. Analysis: find out what is the root-cause of the problem, or in agile terms: root-cause analysis (RCA).
  3. Experimentation: try to generate intelligent failure for the express purpose of learning and innovation.
It is rare for a business to be an overnight success. There are just a few FaceBook and YouTubes out there. It is going to be hard, and for many failure is the beginning of the journey. As per North, "uncertainty isn't just about expecting change, it's assuming that some unexpected bad thing will happen during the project and we can't even know about them when we start."

Friday, February 17, 2012

Implementing the "No Asshole Rule" at work

I'm usually the person that does the interviews when we have a programming position. There are three criteria that I look for when I interview potential candidates:
  1. Technically savvy - can the candidate do the job? Does he/she has a good foundation on computer science (data structures, algorithms, etc)
  2. Quick learner or persistent when it comes to finding a solution.
  3. Is he/she an asshole
As I mentioned before, my interviews are very technical. For the first criteria, I start with a phone interview and present problems that deal with algorithms and basic fundamentals of computer science. If the candidate does well, then I schedule a personal interview and give them a small project to bring to the company. In person, we do a code review of the project along with the team and then along with the candidate. This is simply a "creative criticism" and a way to find out more about the programming skills of the candidate.

The second criteria is hard to gauge. I usually try to give some questions and if I see them struggle, then I see how they handle it. I also ask questions like "what is the latest thing (programming language, framework, tool) that you learned?", "what do you do when you get stuck on a problem?", etc.

The third criteria is extremely important to the team and the hardest to catch. Everyone has worked for or with an asshole. Just so we are clear, I really liked the definition from Robert Sutton's "The No Asshole Rule":
THE DIRTY DOZEN
Common Everyday Actions That Assholes Use
1. Personal insults
2. Invading one’s 'personal territory'
3. Uninvited physical contact
4. Threats and intimidation, both verbal and nonverbal
5. 'Sarcastic jokes' and 'teasing' used as insult delivery
systems
6. Withering e-mail flames
7. Status slaps intended to humiliate their victims
8. Public shaming or 'status degradation' rituals
9. Rude interruptions
10. Two-faced attacks
11. Dirty looks
12. Treating people as if they are invisible
I usually call the candidate's references and asks some "what if" scenarios. Despite all of this, it is hard to identify an asshole.

It is very hard to build a cohesive team, but when you have one...it's amazing. The team works like well-greased machine. Introducing an asshole is almost like throwing a monkey wrench to the machine. As Jim Collin's mentioned in his famous book "Good To Great:"
If you have the wrong people, it doesn’t matter whether you discover the right
direction—you still won’t have a great company. Great vision without great people is
irrelevant
Managers, if you have an asshole, you need to handle them right away! You need to let them know that this type of behavior is simply unacceptable and if it continue, they will be terminated. It is simply not fair for the team or to the company. Simply put, assholes will bring down production and cohesiveness to the team. Worse, you can potentially loose some great resources.

Questions:
How do you find people that are quick learners?
How do you handle or filter assholes?

Tuesday, February 14, 2012

IT and productivity - what does that mean?

Today I was reading Wall Street & Technology and I stumbled into an interesting article by Howard Rubin, "How to Assess IT Productivity". It looked very interesting, but when I finished reading it, I had more questions than answers. The author makes a good point
Defining an accurate (an universally agreed-to) measure of information technology productivity is perhaps the 'holy grail' of IT measurement.
That's absolutely correct. If you're a manager, or have been a manager, then you know that is true. He composed a list of "6 Core Assessment Areas for Measuring IT Productivity":
  1. Computer Technology Measures of Processor Economic Efficiency
  2. Supply-Side Measures of Reduce Economic Efficiency and Productivity
  3. Demand-Side Measures of Economic Efficiency And Productivity
  4. IT Portfolio Measures of "Run the Business" vs. "Change the Business"
  5. IT Budget Agility Measures of Fixed vs. Variable Costs
  6. Operational Leverage Measures
This is where I started doubting the article. The truth is that there is really not a right answer for "IT productivity." It is one of those subjective questions where you get many answers and probably all are correct. The answers will vary depending on the companies and/or its department. In other words, its all about context and domain. But, what if we go back to basics. How about the following measurements:
  • Release cycles: how often we make deployments of new features or fixes
  • How many bugs were introduced on the release/deployment
  • Turn-around time on bug fixes: how fast do we find them?
  • Performance and scalability of application: how fast is our application? How much can it handle?
  • Hight Availability measurements. For example, what is the turn-around time for an event failure (going from primary to secondary)
These might not be "productivity" per se, but they are value added to our customer, and they should be monitored. You might even considered them as quality rather than productivity. But one might argue that they go hand-by-hand. Quality has a tremendous impact on productivity. In my industry (financial trading) we always go back to the 2010 Flash Crash which plunged the Dow Jones Industrial Average to about 1000 points due to an error on a high-frequency trading.

I really like what I read in Net Objectives:
Creating software is about delivering business value. Without some measure of business value, it's hard to determine whether the software has any.
Here are some examples of business Values:
  • Increased revenue (sales, royalties, fees)
  • Decreased expenses
  • Using less resources
  • More efficient use of resources
  • Customer satisfaction
  • Product promoters / satisfiers/ detractors
  • Staying in business
  • Avoiding risk
  • Innovate!
I like what Jonathan Rasmusson said in The Agile Samurai when wondering whether you are doing things the "agile way", instead ask yourself two questions:
  • Are you delivering something of value every week?
  • Are you striving to continuously improve?
At the end of the day, IT and productivity means what you and your company thinks it means.

Thursday, February 9, 2012

Using logwarn to manage logs along with Nagios

I needed to check on a reject file and notify Nagios right away. The reject file rotate everyday with the following format: "C"yyyymmdd.rej (C20120209.rej). The file looks like this:


The objectives were the following:
- Check file every 1 minute from 9:30 am - 4:00 pm
- The first line contains "ACMEFixLogs" and it is not a reject payload
- Notify the number of rejects and the name of the file
- If after the notification, there are no rejects, do not notify

The simplest way is to leverage some type of log checkers that reads the content and saves the last line in a temporary file. This way, the next check will start from the saved line and the content will not be repeated. I found logwarn and I really like it. Mostly because it is has a Nagios plugin and this is my notification engine. Also, logwarn has a regex filter that I could use for my file.

Here is how I solve the problem:
- Create a shell script as a Nagios plugin (exit 0 if everything is "OK", exit 2 if there are any errors.
- Leverage the capabilities of logwarn to handle the reading of the file
- Use the regex so that I disregard the first line "ACMEFixLogs" using the logwarn "!"
- Use Linux's word count to find out how many rejected lines there are
- If there are any reject payloads notify (exit 2)
- If there are none, notify that everything is fine (exit 0)

Here is the script:

Monday, February 6, 2012

Executing a command line via Groovy to Nagios

The other day I got stuck trying to execute a Linux command via Groovy. The command needed to pass several parameters to Nagios (my monitoring system). When I was trying to pass all the parameter via String, this did not work. The way that I was able to resolve it is using a string array. Here is an example:

Friday, February 3, 2012

Find information of Stocks using Yahoo's YQL

I needed to find information about specific ticker symbols and get their stock market(NASDAQ, NYSE, etc). I found my answer with Yahoo's Query Language (YQL) - very nice API! Here is a quick Groovy application that basically goes and fetches information from the site and retrieves the ticker symbols and their correspondent exchange/stock market.


Here is the unit test cases:

Thursday, February 2, 2012

Mount drives into Linux via CIFS

I thought about this blog from Nestor Urquiza. If you are like me, then your Linux servers are like an island in a sea of Windows servers. Here is how you can mount drives without the use of samba.

First, create a directory: sudo mkdir /mnt/ultralord-logs/

Now, lets try to make the mount:


Here, I am mounting the //ultralord-main/logs with the username "gandalf" and the domain "ultralord". If the username is only a local user (not in the domain), then the domain will be the name of the computer "domain=ultralord-main".

Now, check if the mount was valid by doing: ls -l /mnt/ultralord-logs

I always recommend to setup a credential file to hide the username and password.


If you are able to see logs in the directory, then you are ready to edit the /etc/fstab so that the mount happens automatically.




If when trying to mount a drive you get the following:


The solution is to install smbfs