Friday, August 26, 2011

Safari User Agent Detection in JavaScript

Here's a quick snippit for detecting if a browser is Safari version 4 or 5. It works for both desktop and mobile (iPhone, iPod, iPad) versions.


function detectSafari45()
{
  var safariRegEx = /Mozilla\/5\.0 \([^\)]*\) AppleWebKit\/\d+\.\d+(\.\d+)? \(KHTML, like Gecko\) Version\/[45]\.\d+\.\d+( Mobile\/\w*)? Safari\/\d+\.\d+(\.\d+)?/i
  var match = safariRegEx.exec(navigator.userAgent);

  if (match !== null && match.length > 0)
  {
    return true;
  }
  return false;
}

Tuesday, May 10, 2011

Averaging Timespans in T-SQL

I'm doing some work with an operations queue. All of the operations are added to a small table on our SQL Server instance. We have a start_datetime and a stop_datetime for each operation. It took me a little time but here's a select statement for profiling the wait times for operations in our queue.


select
  CONVERT(VARCHAR(13),creation_date,120) as [hour],
  CONVERT(VARCHAR(8), max(stop_datetime - start_datetime), 108) as MaxWaitTime,
  CONVERT(VARCHAR(8), min(stop_datetime - start_datetime), 108) as MinWaitTime,
  CONVERT(VARCHAR(8), cast(avg(cast(stop_datetime - start_datetime as float)) as datetime), 108) as AvgWaitTime
from
  [OpQueue].[Op]
where
  start_datetime > '2011-05-07'
group by
  CONVERT(VARCHAR(13),start_datetime ,120)


Here are a few things to note.
This statement will spit out the min, max, and average running time for operations in the queue that start in the same hour.
The third argument for convert is pretty handy for DATETIMEs. Here is the page where it is described: http://msdn.microsoft.com/en-us/library/ms187928.aspx.
The AVG() function doesn't work for DATETIMEs, so we need to convert it to a float and then back again to get what we are looking for.

Here's the graph we ended up with.

Seems like it's time to invest in some more processing power.

Friday, April 22, 2011

Running Hudson from OS X: the .war.zip Fiasco

It took me a while to figure this out and maybe it's just because I'm not a java guy, but I couldn't figure out how to start the Hudson continuous integration tool on my Mac. I downloaded the 2.0.0 version from the Hudson site (http://hudson-ci.org/). The file was hudson-2.0.0.war.zip. The instructions say to run

java -jar hudson-2.0.0.war

to start up the service. I unzipped the file and ended up with a bunch of .class files and no .war. Ah, looks like I also unzipped the .war file. So then I found this nice post (http://superuser.com/questions/159260/in-mac-os-x-how-can-i-unzip-a-zip-file-without-unzipping-its-contents) on how to unzip a file without unzipping its contents. Now I have a

hudson-2.0.0.war/

directory. I ran

$ java -jar hudson-2.0.0.war
Invalid or corrupt jarfile hudson-2.0.0.war

Hmm, not sure what to do next. Somewhere in my search I read that a .war is basically just a zip file. So I tried

$ java -jar hudson-2.0.0.war.zip

on the original file, which worked. I renamed hudson-2.0.0.war.zip

$ mv hudson-2.0.0.war.zip hudson-2.0.0.war

and I was off and running.

So I hope this helps some other java n00bs who may have run into this. The moral of the story is .war == .zip

Friday, December 10, 2010

Moving Replicated FullText Index in SQL Server

We needed to move the location of our full text index (FTI) on a subscriber. I thought this would be a pain because the replication, but it turns out to be pretty straightforward. Here's the system I am working with:

SQL Server 2005
Transactional Replication with an Updateable Subscriber.

Here are the steps I took:


0. Make sure you have appropriate backups.
1. Point all of the apps to the Publisher because we will need to take the Subscriber offline.
2. On the Subscriber, open the synchronization status by right-clicking on Replication->Local Subscriptions-> ans selecting View Synchronization Status.
3. Stop the Synchronization Service. (This really just pauses updates).
4. On the Subscriber run SELECT name FROM sys.database_files WHERE type_desc = 'FULLTEXT'; to get the name of the FTI.
5. On the Subscriber run ALTER DATABASE [DB_NAME] SET OFFLINE;
6. Move the FTI where to it's new home.
7. On the subscriber run ALTER DATABASE [DB_NAME] MODIFY FILE (Name=[FTI_NAME], Filename = "'new/location/on/disk/');
8. On the Subscriber run ALTER DATABASE [DB_NAME] SET ONLINE;
9. Start the Sync Service in View Synchronization Status.

That should be it. Hope this helps.

Wednesday, November 24, 2010

T-SQL Query: Tables Without a Primary Keys

I used this when I was setting up a Transactional Replication system on SQL Server. I needed to figure out which tables did not have primary keys assigned.


select s.name+'.'+ t.name
from
  sys.schemas s
  join sys.tables t on s.schema_id = t.schema_id
  left join sys.indexes i on i.object_id = t.object_id and
                             i.is_primary_key = 1
where i.name is null
order by (s.name+'.'+ t.name) asc;

Thursday, May 27, 2010

Great Post on SQL Server Stored Procedure Variables

It took me a while to find this about using variables in the TOP clause of a MSSQL stored prodedure. This post answered all my questions:

http://sqlserver2000.databases.aspfaq.com/how-do-i-use-a-variable-in-a-top-clause-in-sql-server.html

Here's the best part


CREATE PROCEDURE dbo.getFoo
  @top INT 

AS
  BEGIN
    SET ROWCOUNT @top

    SELECT foo
      FROM blat
      ORDER BY foo DESC

    -- never forget to set it back to 0!
    SET ROWCOUNT 0
  END
GO

Thursday, March 25, 2010

Programmaticly Changing the File Attribute of an App.config File.

The requirement for this assignment was to use a command line argument to change the settings of a C# console application. I didn't want to compile the settings into the app itself, but to use an app.config file instead. Here's how it works:


public static bool SetEnvironment(string env)
{
  string configFilePath = string.Empty;

  env = env.ToLowerInvariant();
  switch (env)
  {
    case "dev":
       configFilePath = @"Config\Dev.config";
       break;
    case "prod":
       configFilePath = @"Config\Prod.config";
       break;
    default:
       return false;
  }

  try
  {
    System.Configuration.Configuration config =
       ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    AppSettingsSection appSetSec = config.AppSettings;
    appSetSec.File = configFilePath;
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
  }
  catch (Exception ex)
  {
    OfflineDemoTool.WriteError(ex.Message);
    return false;
  }

  return true;
}



One thing that I forgot to do was to make sure that the Config\Dev.config and Config\Prod.config files were set to "Copy if newer" in their properties list, otherwise they won't be copied to the output directory.

Most of this comes straight from the MSDN article found here: http://msdn.microsoft.com/en-us/library/system.configuration.appsettingssection.aspx