and has 0 comments
It was inevitable, both Naruto and Sasuke were getting ridiculously strong. In the end they fought the mother of all chakra and... of course they won, then they fought each other, but it was kind of underwhelming, since their power prevented any subtlety and they just went cowboy punching each other. The last color chapter is about how they leave it all to the next generation, although it is hard to think of anything more they could do to top their parents. I loved the entire series and it is easy to understand why: simple concept, positive feelings like friendship and camaraderie and weird magical ninja fights. I was a teen when I started watching the anime and now I am freakishly old. Well, life happens. After I got kind of tired of watching the anime, even if it was really well done and followed the manga faithfully, I went with reading the manga. I like to use Mangastream for my reading purposes, so you can read the entire thing here: Naruto Shippuden. Even if it appears they are writing some Naruto side stories, I am not sure I will ever read them. I am still looking for a manga that can grab me like Naruto has.

and has 1 comment
I was watching a video from GM Niclas Huschenbeth where he played lytura in an online game. Amazingly he lost, but that is what happens when you underestimate your opponent, which I think was what actually went wrong. At the end of the video it was difficult to see exactly what White could have done after a point, so I analysed the game with the computer and found some amazing moves. First I will show you the original game. I urge you to think it through and see what moves you would have done differently, like a chess puzzle, before you watch the game as the computer suggested it. You can also watch the video online at the end of the post and, if you like chess, I really recommend Huschenbeth's channel. Not only is he a great player, but also a decent and nice guy and young, too. His Blitz & Talk GM Special videos are especially cool, since he plays with other world class grand masters.

But enough of that. Here is the game, up to a point where it didn't really matter what happened: 1. e4 e5 2. f4 exf4 3. Nf3 g5 4. Nc3 g4 5. Ne5 Qh4+ 6. g3 fxg3 7. Qxg4 g2+ 8. Qxh4 gxh1=Q 9. Qh5 Nh6 10. d3 d6 11. Bxh6 Be6 12. Bxf8 Rxf8 13. Nf3 Nd7 14. O-O-O c6 15. Bh3 Nf6 16. Rxh1 Nxh5 17. Bf1 Rg8 18. Ne2 Kd7 19. Kd2 Rg7 20. Ke3 Rag8 21. a3 Nf6 22. h3 b6 23. d4 a5 24. Nf4 Rg3 25. Ne2

Here is the video of the game, to give you the time to think it through:


And finally, here are two lines that the computer recommended. This is considering that the fateful 10. d3 was played already: 1. e4 e5 2. f4 exf4 3. Nf3 g5 4. Nc3 g4 5. Ne5 Qh4+ 6. g3 fxg3 7. Qxg4 g2+ 8. Qxh4 gxh1=Q 9. Qh5 Nh6 10. d3 d6 11. Bxh6 Be6 12. Bxf8 Rxf8 13. Nf3 Nd7 14. O-O-O c6 15. Nb5 Nf6 (15. .. cxb5 16. Bh3 Qxd1+ 17. Kxd1 O-O-O 18. Bxe6 fxe6 19. Nd4 {White +1.2}) 16. Nxd6+ Kd7 17. Qh3 Bxh3 18. Bxh3+ Kxd6 19. Rxh1 {Black +0.3}

Did you see those Nb5 and Qh3 moves?! Who does that? :)

and has 0 comments
The wonder of .Net is that most of the time we don't really have to care about stuff like memory allocation, the framework does everything for you. One especially annoying thing, though, is when you are using a basic data structure that is supposed to be efficient and you get stuff like OutOfMemoryException. One of the cases is List<T> which in the background uses one big array. This means two things: one is that certain modifying operations are slow and the other is that it requires contiguous memory space for the items. If your memory space gets too fragmented, then there is not enough to allocate for hundred of thousands of items, even if in that memory space you only need a pointer for each item. That is why you end up with out of memory exceptions. It's not like you don't have enough memory, it's that you don't have a big enough contiguous block of it.

As a solution I give you... the BucketList<T> class. It has a bucket size that defaults to 10000 and it implements a list of lists that each will always have at most that amount of items as specified in the bucket size. This way operations that remove and add items will only operate on 10000 item big arrays and there is no need for only one big memory block. I implemented the IList interface explicitly, so that you will never find it comfortable to use an instance as a BucketList, but as an IList. This way you can replace the implementation of the interface with a normal List or whatever other form you like. Enjoy!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Siderite.DataStructures
{
/// <summary>
/// IList implementation that doesn't require a contiguous memory space (an array)
/// </summary>
/// <typeparam name="T"></typeparam>
public class BucketList<T>:IList<T>
{
private int _bucketSize;
private List<List<T>> _list;
private int _count;

/// <summary>
/// Default constructor
/// </summary>
public BucketList():this(10000)
{
_list = new List<List<T>>();
}

/// <summary>
/// Specify the bucket size (default 10000)
/// </summary>
/// <param name="bucketSize"></param>
public BucketList(int bucketSize)
{
_bucketSize = bucketSize;
}

/// <summary>
/// Create a bucket list from an IEnumerable
/// </summary>
/// <param name="enm"></param>
public BucketList(IEnumerable<T> enm):this()
{
var list = (IList<T>)this;
foreach (var itm in enm)
{
list.Add(itm);
}
}

/// <summary>
/// The item count
/// </summary>
public int Count
{
get
{
return ((IList<T>)this).Count;
}
}

#region IList<T> implementation

int IList<T>.IndexOf(T item)
{
var index = 0;
for (var i = 0; i < _list.Count; i++)
{
var idx = _list[i].IndexOf(item);
if (idx < 0)
{
index += _list[i].Count;
}
else
{
index += idx;
return index;
}
}
return -1;
}

void IList<T>.Insert(int index, T item)
{
var idx = 0;
for (var i = 0; i < _list.Count; i++)
{
var lst = _list[i];
if (index < idx + lst.Count)
{
lst.Insert(index - idx, item);
splitListIfTooLarge(i);
_count++;
return;
}
else
{
idx += lst.Count;
}
}
throw new IndexOutOfRangeException("index");
}

void IList<T>.RemoveAt(int index)
{
var idx = 0;
for (var i = 0; i < _list.Count; i++)
{
var lst = _list[i];
if (index < idx + lst.Count)
{
lst.RemoveAt(index - idx);
removeListIfEmpty(i);
_count--;
return;
}
else
{
idx += lst.Count;
}
}
throw new IndexOutOfRangeException("index");
}

T IList<T>.this[int index]
{
get
{
var idx = 0;
for (var i = 0; i < _list.Count; i++)
{
var lst = _list[i];
if (index < idx + lst.Count)
{
return lst[index - idx];
}
else
{
idx += lst.Count;
}
}
throw new IndexOutOfRangeException("index");
}
set
{
var idx = 0;
for (var i = 0; i < _list.Count; i++)
{
var lst = _list[i];
if (index < idx + lst.Count)
{
lst[index - idx]=value;
}
else
{
idx += lst.Count;
}
}
throw new IndexOutOfRangeException("index");
}
}

void ICollection<T>.Add(T item)
{
List<T> lst;
int index;
if (_list.Count == 0)
{
lst = new List<T>();
_list.Add(lst);
index=0;
}
else
{
index=_list.Count - 1;
lst = _list[index];
}
lst.Add(item);
splitListIfTooLarge(index);
_count++;
}

void ICollection<T>.Clear()
{
_list.Clear();
_count = 0;
}

bool ICollection<T>.Contains(T item)
{
return ((IList<T>)this).IndexOf(item) > -1;
}

void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
var index = arrayIndex;
foreach (var lst in _list)
{
lst.CopyTo(array, index);
index += lst.Count;
}
}

int ICollection<T>.Count
{
get
{
return _count;
}
}

bool ICollection<T>.IsReadOnly
{
get { return false; }
}

bool ICollection<T>.Remove(T item)
{
for (var i = 0; i < _list.Count; i++)
{
var lst = _list[i];
if (lst.Remove(item))
{
_count--;
removeListIfEmpty(i);
return true;
}
}
return false;
}

IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
foreach (var lst in _list)
{
foreach (var item in lst)
{
yield return item;
}
}
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return ((IList<T>)this).GetEnumerator();
}

#endregion

private void splitListIfTooLarge(int listIndex)
{
var lst = _list[listIndex];
if (lst.Count > _bucketSize)
{
var newList = lst.GetRange(0, _bucketSize);
lst.RemoveRange(0, _bucketSize);
_list.Insert(listIndex, newList);
}
}

private void removeListIfEmpty(int listIndex)
{
var lst = _list[listIndex];
if (lst.Count ==0)
{
_list.RemoveAt(listIndex);
}
}
}
}

and has 0 comments
You may have heard of the recent scandal about Internet leaks of nude or personal photos of female celebrities. Dubbed "The Fappening", a word play on the (horribly bad - my opinion) movie The Happening and the term "fap", which refers to masturbation, it is a huge collection of pictures that seem to have been taken by the celebs themselves or by close acquaintances in private surroundings. You know... selfies. They were obviously obtained through some underhanded methods and published in several waves, three at the moment. I am not here to give you torrent links to the leaked material, even if they are fairly easy to find, instead I am going to talk about the general reaction, as proven by seed/leech ratios of torrent downloads: after an initial boom in interest, the updates have been less and less interesting to people. Why is that?

At first I hypothesized that the vehement reaction of the media was part joining in the fray, like sharks smelling blood, and in their own way pointing people to search the net for the photos (yeah, I don't really believe in the difference between mentioning something that is easy to find and a link), but also because this affair was an obvious attack on the brands that the celebrities are standing for. Nobody really cares about how some of the actresses in this situation are actually acting if they look hot enough and also, very important, how unattainable they are. The reaction of the agencies that invested a lot in these brands was expectedly violent. However, there is another factor, one that I think makes it all meaningful to discuss: people expected something completely different from what was provided. No matter how much we understand the media processes involved in creating a celebrity personality, we don't really (emotionally) believe that it is happening or we don't understand the extent of the effort. Indeed, when people downloaded the pictures and guiltily and excitedly started to look at the images, they found out... real women. Without the expertise of professional photographers and without the extensive post processing and media censorship that occurs after the pictures are taken, the celebrity females that we collectively idolatrized appeared as less than goddesses and as just normal people, with zits and saggy tits and all that. Even if they look fabulous, like some of them do, the amateurish manner of the way the pictures were taken give little pleasure. Indeed, the only pleasure that can be extracted from this is akin to rape: they wanted to cheat, to show us just the Photoshopped images of themselves, but we showed them! We took what we wanted.

Look at the torrent statistics though. The October collection of pictures is at the top, over Fappening 2 that has more seeds than Fappening 3. People lost interest: they were curious, downloaded the stuff, then they didn't follow through with the rest. All because they were getting something other than they had bargained for. Instead of pictures showing more of the beautiful women we yearn for, they showed enough to make those women feel terribly human. The breasts, the asses and all the other hidden skin was hidden not because they were something amazing to hide, but because the myth was more beautiful and sexy, perfect in its imperfect sharing. It raises important questions that I believe to be worth exploring: what are we really falling for? What is beauty: just a branded illusion? Why do girls appearing fully clothed and smiling in a music video or a movie seem more desirable than the fully naked and active girls in porn films? Are we really interested in the "reality show" of someone's intimacy, or do we, really, secretly, want these people to show us only the beautiful parts, to make us believe that perfect people exist? Are we all victims of a global romcom? And who is it that is laughing at the comedy aspect of all this?

and has 0 comments
The autumn season for TV shows is beginning, so I am here again to discuss the ones that I have been watching lately.



  • The Legend of Korra - This third season was better than others, but we still have to contend with Korra's helplessness. Also I have this nagging feeling that her not being able to do anything and having to be saved by her friends repeatedly has less to do with what people can accomplish together and more with the fact that she is female and therefore must be perceived as in distress. And before you ask me when did I become a feminist, just read on and see what are the shows with female lead characters and what happens to them. The magnificent four villains also were ridiculously strong for people living in solitary confinement for years.
  • The Good Wife - Interesting new dynamic of the show. I am quite fascinated by how they achieve this dynamic equilibrium: giving people what they want, but always changing things one way or another, moving characters around, keeping things interesting. If nothing else, this is a brilliantly constructed TV show.
  • Homeland - Homeland will have a fourth season, which is to begin soon. They released a sort of recap of what happened so far, but I believe you should skip it as I think it contains spoilers for the upcoming season. Also it conveys nothing of the quality of the first three seasons. This is a good show, you should watch it, even if the lead female character is bipolar and prone to do crazy things in the name of love. See what I mean?
  • Gotham - A new superhero series will being shortly. Hurray! This time is about Gotham where every supervillain and superhero is young, at the beginning of their "careers". Why are you throwing an adolescent tantrum, Bruce? Because I'm Batman!
  • Ressurection - I've decided not to watch it anymore. It is a pale copy of the original.
  • Vikings - I love this show, however it is beginning to change. It started with eager actors doing a cool project, so they all gave their best and the focus was on the way of the Viking. But now, after a while, the actors are acting more like themselves and the focus of the story shifted towards feudal intrigue.
  • Suits - The fourth season just ended with Mike back at the law firm and the comic relief guy, Louis Litt, leaving the company. I thought the actor got tired of playing a ridiculous man that doesn't seem to do anything right, but the ending of the season goes a different direction.
  • Black Box - As expected, the show was cancelled.
  • Halt and Catch Fire - A lot of emotion, a lot of tension, a lot of drama. Of course it got renewed for the second season, even if it doesn't make a whole lot of sense.
  • Under the Dome - I kept watching and watching and watching until I realized this is the new Lost! Every episode something happens that is completely implausible and unrelated to anything in the previous episodes. I will not watch it anymore.
  • Crossbones - This is a well done show, with good acting and high production values. However it relates to pirates and not even the fun ones. Imagine watching a TV show about drug lords and instead of high powered automatic rifle fights you would see the accountants doing the job of inventorying the proceeds. Black Sails is like that and then they insert some artificial personal drama to spice it up.
  • The Honourable Woman - Described as "The daughter of an assassinated Zionist arms dealer seeks to legitimise the family business while righting the wrongs done to them in the past.", it is a strange little show. I started watching it, but then I stopped. It is heavy, well acted, good production values. I wasn't in the mood for it for a long time, though. The premise is nothing if not brave.
  • The Leftovers - I just couldn't watch it anymore. The entire show was about people feeling depressed and/or suicidal because they were not among the 2% of people who magically vanished. Depressing and pointless.
  • The Witches of East End - I guess it's like The Originals with witches instead of vampires. I keep watching it, though, even if I don't know why. Probably someone placed a spell on me.
  • Tyrant - Speaking of brave TV show premises, this is a show about an Arab-American, who left his birth country because his father was an ass - and the country's tyrannical ruler - who returns there after his father dies. I like the acting and the premise. What I don't like is the condescending viewpoint of the script: the smart educated moral good looking American comes and teaches his older brother how to rule the country based on American principles. But the ending of the first season implies that this is not what it is going to happen at all. Could it be that it will be the series to show Americans that all tyrants are manufactured, more or less, by circumstances? Check out this quote: "CIA guy: The US is not in the business of regime change. Al Fayed: Say that again with a straight face"
  • Taxi Brooklyn - A weird premise for a show that doesn't know what it wants to be: a comedy, a car thing or a police procedural. The idea is that a police detective (female and hot, but with daddy issues, of course) loses her right to drive. Instead, she coopts a French immigrant taxi driver to move her around. He serves as the comic relief most of the time, but also, probably, as the male reason why anything gets done. Decided not to watch it anymore for several reason: the premise, the scripting and the acting being at least three of them.
  • Extant - Female astronaut returns after a solo mission of more than nine months. And she is pregnant. Wonderful premise, however it has several things that are not going for the show: the main actress is Halle Berry, who I completely dislike. Then there is the electronic boy story arch (her husband and she have an artificial son that the husband created) which is either a good subject for another series or completely out of place here. And finally, the "corporate conspiracy" arch, where the guy from Helix is the bad guy. I don't know, imagine Gothica in space, with a little of AI sprinkled over for fun. Ugh!
  • The Bridge - Haven't watched any of the second season episodes, waiting for the wife to see it with me. I will wait for a long time more, I believe.
  • Tokyo Ghoul - I usually make separate blog posts with anime, but this one was nothing that deserves too much attention. Hybrid man and ghoul (something like a vampire that also likes to eat the flesh), the main character is a whinny boy who gets stepped on by just about everybody. In the end he is captured and tortured a lot, which makes him more aggressive and less whinny. But still it's nothing too interesting.
  • Ghost in the Shell - Arise - the modern reboot of GITS, it is not bad. Unfortunately there are only four OVA episodes, each one released months from the previous one. Still waiting for the fourth one. I like the show a lot, but then I am biased, since I love anything related to the Ghost in the Shell universe.
  • The Strain - Guillermo del Toro wrote a horror book with vampires and now he is creating the TV show based on the book. So far I like the series, although I've already read the book and a lot of the surprise is gone. It's brutal, with vampires that are neither sexy nor romantic, but just want to drink all your blood and answer unconditionally to their Master. The Master is even scarier. It is not brilliant, but certainly beats The Living Dead.
  • Blood Lad - Horribly stupid anime. This guy who is a prince in the magical world of demons accidentally meets a human girl who then gets killed. He pledges his support to help her ghost find a body again. Just boring.
  • Longmire - The second season is a lot darker, but for all the wrong reasons, if you ask me. The things I liked in Longmire were to see him being uncompromisingly moral, even if he appears lonely and withdrawn to everybody around him. This season everybody connected to him has to face a metaphorical demon or ten, including Longmire himself. It felt pushed too far, I think.
  • The Lottery - Just like Extant, The Lottery is a sci-fi series centered around a woman. Naturally, the only things she can possibly do is worry about children. You see, for some reason no one is able to sire children anymore. A scientist manages to impregnate 100 embryos and there will be a lottery to give the children to 100 couples. Lots of government conspiracies and child protecting going around. I may watch the rest of the episodes, but the pilot didn't convince me at all.
  • Manhattan - A show about the development of the first atomic bomb. Its take is interesting, focusing on the personal quirks, on the politically incorrect, on the compromises and mistakes, on scientists, military and their spouses alike. What I found fascinating is showing how the obnoxious arrogance of someone truly driven and brilliant is almost forced, as a defense mechanism against getting pulled down by the mediocre. Don't get me wrong, it is not another show about brilliant assholes a la Dr House and does not apologize gratuitous arrogance. Instead it shows how vital it is in the way to success. Given that, the character of Winter is so bloody annoying that I wonder why anyone would put up with such a guy and not kick his ass or just shoot him directly. Perhaps that is another strength of the show: describing how close to failure for some many different reasons the Manhattan Project truly was. It was a government project after all.
  • The Assets - The series ended with episode 8. In that sense, it is actually a miniseries, as the entire premise of the show comes to an end with the final episode. I wrote before that it got cancelled really soon and I believe that the reason is that all characters are really unlikable. Also, since it is based on a book that describes a real event that a lot of Americans know how it went down, the interest was probably small. Also, at the end of the show you realise something: spies are really boring.
  • Legends - A TV series for the sole purpose of keeping Sean Bean alive! :) Sean Bean is this deep undercover agent with a lot of prefabricated "legends", or fake lives, that he uses to infiltrate criminal or terrorist organizations. This leads him to have identity crises, even questioning if any of his lives, including the real one, are actually real. Sexy Ali Larter is his "handler", which can't hurt.
  • Outlander - Is this an attempt at a romantic Yankee in King Arthur's Court? A 1945 nurse is thrown back in time in 1743 Scotland. Her healing skills are helping her join this band of rebellious Scots and experience the life there. The synopsis of the show didn't give me much hope for it, but after watching all the episodes so far it got me hooked. The acting is good and the script is well written. I hope it doesn't deteriorate on the way. It is also intriguing that she has 200 years of extra knowledge, but she doesn't suddenly share antibiotics with the world or try to improve muskets or whatever. Being a woman in a sort of prisoner situation serves to explain that, but how long can it go on like that?
  • The Divide - About a woman that works for an organization that tries to help the wrongly accused in the US justice system. There is a coverup, a conspiracy, White men wrongly accused of killing a Black family, politics, etc. It started as intriguing, but outside the twist that is probably looming, I don't think there is anything really interesting to me in the show. Too political, I guess.
  • The Knick - This is one of the good ones. A look at the professional and personal lives of the staff at New York's Knickerbocker Hospital during the early part of the twentieth century, it is directed by Soderbergh, starring Clive Owen and it is both brutal and truthful. A must see for all the new age assholes that like to think medicine was better at that time.
  • Doctor Who - Season 8 with Capaldi is both interesting and dull. It was supposed to be darker, more intense, but it isn't really. What it is is confusing, though. I didn't like the pilot, but I enjoyed the second and third episodes. I don't know, let's see.
  • Forever - Another "special" person helping the police. Why?! Oh, why?!! This time it's about a guy who cannot die. Every time he dies, he appears somewhere in water, naked. A single, sexy, female police detective partners with him in order to solve crime. I like the actor, though, even if the script is eerily similar to any of the shows in the genre out there. Let's see how it goes.
  • Intruders - "Jack Whelan is a former LAPD officer who is asked to investigate some strange occurrences. He tries to find answers, but he's stonewalled at every turn. Baffled, he continues until he starts to concentrate his search around a secret society that chases immortality by seeking refuge in the bodies of others." The cast seems good. I still have to actually watch it, though.
  • Hysteria - It concerns the idea that people can get afflictions from social media. A perfect reason for Internet control! :) Anyway, the title is perfect as it seems the cause of the problem is hysteria, while the reaction of the people is mass hysteria. The Hannibal Lecter beginning, though, may either be the sign of bad writing or of some ingenious plot device. Wait and see.
  • Hand of God - Ron Perlman? Sign me up! I've seen the pilot though and it's kind of weird. You get this judge who's son just killed himself. He did it because someone raped his wife and made him watch. And so the judge gets born again in a shady church by a preacher who is an ex actor and a con artist. Then the judge starts hearing the voice of God. Some things clearly get lost in translation, because He is always putting Ron Perlman in the situation to be a total ass who everybody thinks is insane. Oh, except the insane people, who think he is the new Solomon. But is he? Weird, huh?

Well, a la prossima!

Just a quick solution for a problem that seems to be @font-face not functioning at all. You define the @font-face CSS rule, you then add a font-family CSS rule for your element and nothing seems to be happening. The element has the correct family when you look in the browser inspection tool, but the font is never loaded. Nor is it displayed. What could be wrong?

The simple answer in my case is that the element I wanted to style had a rule like this: font-family: 'My Special Font, Verdana, Arial';. The correct rule should have been font-family: 'My Special Font', Verdana, Arial;. The quotes are just for escaping spaces and the like for the individual family names, not for encapsulating the "value" of the css rule. I know, stupid, but I wasted half an hour on it!

Update: If you are behind a proxy, here is some additional code to add right after creating the update session:
'updateSession.WebProxy.AutoDetect = true 'try this first. It doesn't work so well in some environments if no authentication windows appears (*cough* Windows 8 *cough*)

strProxy = "proxy name or address:proxy port" 'ex: 1234:999
strProxyUser = "your username"
strProxyPass = "your password"

updateSession.WebProxy.Address=strProxy
updateSession.WebProxy.UserName=strProxyUser
updateSession.WebProxy.SetPassword(strProxyPass)

I am working behind a "secured" web proxy that sometimes skips a beat. As a result there are days in which I cannot install Window Updates, the normal Windows update application just fails (with Error Code: 0x80246002) and I am left angry and powerless. Well, there are options. First of all, none of the "solutions" offered by Microsoft seem to work. The most promising one (which may apply to you, but it did not apply to me) was that you may have corrupted files in the Download folder for Windows updates. As a result you need to:
  • Stop the Windows Update service issuing the command line command: net stop wuauserv or by going to Control Panel, Services and manually stopping it.
  • Go to the download folder parent found at %systemroot%\SoftwareDistribution (cd %systemroot%\SoftwareDistribution) and rename the Download folder (ren Download Download.old)
  • Start the Windows Update service issuing the command line command: net start wuauserv or by going to Control Panel, Services and manually starting it.

So my solution was to use a script that downloads and installs the Windows updates from the command line and I found this link: Searching, Downloading, and Installing Updates that pretty much provided the solution I was looking for. There are two issues with the script. The first is that it prompts you to accept any EULA that the updates may present. The second is that it downloads all updates, regardless of severity. So I am publishing here the script that I am using who fixes these two problems: EULA is automatically accepted and only Important and Critical updates are downloaded and installed:
Set updateSession = CreateObject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "Siderite :) Sample Script"

Set updateSearcher = updateSession.CreateUpdateSearcher()

WScript.Echo "Searching for updates..." & vbCRLF

Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")

WScript.Echo "List of applicable items on the machine:"

For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
WScript.Echo I + 1 & "> " & update.Title
Next

If searchResult.Updates.Count = 0 Then
WScript.Echo "There are no applicable updates."
WScript.Quit
End If

WScript.Echo vbCRLF & "Creating collection of updates to download:"

Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")

For I = 0 to searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)

addThisUpdate = false
If update.InstallationBehavior.CanRequestUserInput = true Then
WScript.Echo I + 1 & "> skipping: " & update.Title & _
" because it requires user input"
Else
If update.EulaAccepted = false Then
update.AcceptEula()
WScript.Echo I + 1 & "> Accept EULA " & update.Title
addThisUpdate = true
'WScript.Echo I + 1 & "> note: " & update.Title & " has a license agreement that must be accepted:"
'WScript.Echo update.EulaText
'WScript.Echo "Do you accept this license agreement? (Y/N)"
'strInput = WScript.StdIn.Readline
'WScript.Echo
'If (strInput = "Y" or strInput = "y") Then
' update.AcceptEula()
' addThisUpdate = true
'Else
' WScript.Echo I + 1 & "> skipping: " & update.Title & _
' " because the license agreement was declined"
'End If
Else
addThisUpdate = true
End If
End If

If addThisUpdate AND (update.MsrcSeverity = "Important" OR update.MsrcSeverity = "Critical") Then
'wscript.echo ("This item is " & update.MsrcSeverity & " and will be processed!")
Else
'comment these lines to make it download everything
wscript.echo (update.Title & " has severity [" & update.MsrcSeverity & "] and will NOT be processed!")
addThisUpdate=false
End If

If addThisUpdate = true Then
wscript.echo(I + 1 & "> adding: (" & update.MsrcSeverity & ") " & update.Title)
updatesToDownload.Add(update)
End If
Next

If updatesToDownload.Count = 0 Then
WScript.Echo "All applicable updates were skipped."
WScript.Quit
End If

WScript.Echo vbCRLF & "Downloading updates..."

Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

rebootMayBeRequired = false

WScript.Echo vbCRLF & "Successfully downloaded updates:"

For I = 0 To searchResult.Updates.Count-1
set update = searchResult.Updates.Item(I)
If update.IsDownloaded = true Then
WScript.Echo I + 1 & "> " & update.Title
updatesToInstall.Add(update)
If update.InstallationBehavior.RebootBehavior > 0 Then
rebootMayBeRequired = true
End If
End If
Next

If updatesToInstall.Count = 0 Then
WScript.Echo "No updates were successfully downloaded."
WScript.Quit
End If

If rebootMayBeRequired = true Then
WScript.Echo vbCRLF & "These updates may require a reboot."
End If

WScript.Echo vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo

If (strInput = "Y" or strInput = "y") Then
WScript.Echo "Installing updates..."
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()

'Output results of install
WScript.Echo "Installation Result: " & _
installationResult.ResultCode
WScript.Echo "Reboot Required: " & _
installationResult.RebootRequired & vbCRLF
WScript.Echo "Listing of updates installed " & _
"and individual installation results:"

For I = 0 to updatesToInstall.Count - 1
WScript.Echo I + 1 & "> " & _
updatesToInstall.Item(i).Title & _
": " & installationResult.GetUpdateResult(i).ResultCode
Next
End If
WScript.StdIn.Readline()

Save the code above in a file called Update.vbs and then creating a batch file that looks like this:
@ECHO OFF
start "Command line Windows update" cscript Update.vbs

Run the script and you will get the .vbs executed in a command line window that will also wait for pressing Enter at the end of execution so you can see the result.

For other solutions that are more system admin oriented, follow this link which provides you with a lot of possibilities, some in PowerShell, for example.

Also, I didn't find a way to install the updates without the Windows annoyance that asks me to reboot the computer popping up. If you know how to do that, I would be grateful.

and has 0 comments
I have been watching this weekly space show made by a husband and wife couple working for SpaceX. Initially called Spacevidcast, now it is called TMRO (pronounced Tomorrow). It is a great show, great quality, nice humor and, more than anything, a comprehensive video report on weekly events in space exploration, commercial or otherwise. If you are even remotely interested in space, you should subscribe. And they have been doing it all from their own resources and crowdfunding for seven years! You gotta love that.

But the selfish reason I am blogging about them is that I got mentioned in the TMRO show! Click here to see how they are trying and even succeeding to pronounce my Internet nom de guerre. The effort is appreciated.


and has 0 comments
Because of idiotic firewall rules at my workplace I am forced to use Hangouts rather than Yahoo Messenger as an instant messenger. I am not going to rant here about which one is best, enough to say that most of my friends are on YM and being on Hangouts doesn't help. Hangouts has many annoyances for me, like its propensity to freeze when you lose Internet connection often or the lack of features that YM had. In fact I was so annoyed that I planned to do my own professional messenger to rule them all. But that's another story.

I am writing this post because of a behaviour of the Google Hangouts instant messenger (which, to be fair, is only a Chrome extension), mainly that after a while, the green traybar icon of the messenger goes in the "hidden icons" group. I have to customize it every day, sometimes twice, as it seems to reset this behavior after a period of use, not just on restarts. There is a Google product forum that discusses this here: System tray icon resets every time Chrome is started where you also see a few comments from your truly.

I immediately wanted to create a script or a C# program to fix this, but at first I just searched for a solution on the web and I found TrayManager, a C# app that does what the "Customize..." tray link does and more. One of the best features is a command line! So here is what you do after downloading the software and installing it somewhere: TrayManager.exe -t "Hangouts" 2. Now, probably that doesn't solve the problem long term. It is just as you would go into the Customize... link, but it's faster. Also, it has no side effects if run multiple times, so you can use Task Scheduler to run it periodically. Yatta!

BBC's show The Sky at Night did a coverage of the Rosetta mission, called How to Catch a Comet. It is the standard popular science show, with a lot of fake enthusiasm from the reporters and simple language and explanations, but for people who read this blog entry and wonder what the hell Rosetta is, it does the job. The fat black reporter is really annoying, and not because she's black, but because she feels completely fake whenever she says anything. Other than that the show is decent.

You get to learn about comet 67P, the Rosetta probe features and mission, walk around ESA, talk to scientists and even see a how-to about photographing comets - it was funny to see a shooting star in the night sky while the guy was preparing his camera and talking in the video. Of course, for me the show stopped just when it was getting interesting. I know you can't do much in 29 minutes, but still. I hope they do follow-up shows on Rosetta and I can't wait for November when the lander module will try to grapple the comet and land.

Just in case I've stirred your interest, here are some links that can cover the subject in a lot more detail:
ESA Euronews: Comet Hunters: Rosetta's race to map 67P - 8 minutes and a half of Euronews report from 11 August.
ESAHangout: How do we journey to a comet? - Google Hangout from ESA explaining the mission. It's one hour long and it dates from the 26th of June. Many other videos about Rosetta can be found on the ESA channel.
A playlist about Rosetta from Mars Underground. The most interesting is this video, published on 11 Aug 2014. It lasts an hour and a half and shows the first mission images and science results.
Comets - A wonder to Behold, A continuing Stream of Surprises - The Beauty and the Danger, not about Rosetta, but one hour and a half about comets. The documentary is trying to justify a controversial theory about the electric nature of comets. It is well done with a lot of proof, but I know too little about the theory so I can't recommend it. Interesting, though.

and has 0 comments
In this post I will try to bring to your attention something that will probably change the world significantly. In 1909, German chemist Fritz Haber successfully fixed atmospheric nitrogen as ammonia in a laboratory and five years later a research team from BASF, led by Carl Bosch, developed the first industrial-scale application of the Haber process, sometimes called the Haber-Bosch process. Ammonia is extremely useful for many applications, the least of each is gunpowder and explosives and one of the most important is fertilizers. Without the Haber-Bosch process we probably wouldn't have the Green Revolution.

So today I found this article in Ars Technica that says that Researchers have developed a method to produce ammonia starting only with air and water. Not only is it more energy efficient than the century-old Haber-Bosch process that’s currently in use, but it’s also greener. The article goes on to say that almost 2% of the entire world energy is used to create ammonia; making the process more efficient is great! But I have to say that this is probably just the tip of the iceberg. Lowering the production cost of such a basic article will ripple throughout many industries, lead to innovation or the possibility to use some old innovation that until now was unfeasible.

I am not a chemist, so my enthusiasm may be way off-base, but my gut feeling is that this improvement on a century old process will have a great and positive effect.

I have been working on a REST API lately and, while using Entity Framework or some other similar framework to abstract the database is certainly possible, I wanted to control every aspect of the implementation. I know, reinventing wheels, but this is how one learns. One of the most annoying bits was trying to translate some complex object from JSON to the (two dimensional) database relational tables. This post will explore my attempts and the solutions I have found.

My first attempt was straightforward: just send all types as DataTables, with some extra property to define identity and parent entity. This relies on the Microsoft Server SQL mechanism that allows sending of table variables to stored procedures. But this approach has several downsides. One of them is that in order to send a datatable to SQL you need... DataTables. As I have pointed out in several blog posts, the DataTable object is slow, and sometimes downright buggy. Even if I didn't care about performance that much, in order for SQL to receive the content of the DataTable one must create corresponding User Defined Types on the database side. Working with UDTs is very difficult for several reasons: you cannot alter a UDT (unless employing some SQL voodoo that changes system tables), you can only drop it and recreate it. This does not work if you use the UDT anywhere, so a lot of renaming needs to be done. Even if you automate the process, it's still very annoying. Then the UDT definition has to be an exact duplicate of the DataTable definition. Move some columns around and it fails. Debugging is also made difficult by the fact that the SQL profiler does not see the content of table variables when sending them to the server.

Long story short, I was looking for alternatives and I found XML. Now, you might think that this leads to a simple, and maybe even obvious, solution. But it's not that easy. Imagine that you send a list of objects in an XML. Each object is represented by an XML element and each property by a child element. In order to get the value of a property you need to do iterate through all the nodes, for each node find the properties, for each property find the one element that defines it, then get the attribute value or content of the property, all while making sure you select everything in a table. It's not that easy.

The solution I found, which simplifies the SQL code (and hopefully brings some well needed performance to the table) is to serialize the objects in a way that makes the selection simple enough. Here is an example: I have a Configuration object with an Id and a Name that also has a property called Servers, containing Server objects having an Id and a Url. Here is an example of XML serialization from the DataContractSerializer:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://schemas.datacontract.org/2004/07/SerializationTest" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Id>1</Id>
<Name>Test Config</Name>
<Servers>
<Server>
<Id>1</Id>
<Url>http://some.url</Url>
</Server>
<Server>
<Id>2</Id>
<Url>http://some.other.url</Url>
</Server>
</Servers>
</Configuration>

The SQL code to get the information from an XML variable with this content would look like this:

DECLARE @Xml XML='<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://schemas.datacontract.org/2004/07/SerializationTest" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Id>1</Id>
<Name>Test Config</Name>
<Servers>
<Server>
<Id>1</Id>
<Url>http://some.url</Url>
</Server>
<Server>
<Id>2</Id>
<Url>http://some.other.url</Url>
</Server>
</Servers>
</Configuration>'


;WITH XMLNAMESPACES(DEFAULT 'http://schemas.datacontract.org/2004/07/SerializationTest')
SELECT T.Item.value('(Id/text())[1]','INT') as Id,
T.Item.value('(Name/text())[1]','NVARCHAR(100)') as Name
FROM @Xml.nodes('//Configuration') as T(Item)

;WITH XMLNAMESPACES(DEFAULT 'http://schemas.datacontract.org/2004/07/SerializationTest')
SELECT T.Item.value('(Id/text())[1]','INT') as Id,
T.Item.value('(Url/text())[1]','NVARCHAR(100)') as Url
FROM @Xml.nodes('//Configuration/Servers/Server') as T(Item)


This works, but look at that code. In my case, the situation was worse, the object I was using was a wrapper which implemented IDictionary<string,object> and, even if it did implement ISerializable, both XmlSerializer and DataContractSerializer use the dictionary as their data and in the end I get ugly key elements and value elements that are even harder to get to and, I suppose, more inefficient to parse. Therefore I found the solution in IXmlSerializable, (yet) another serialization interface used exclusively by XML serializer classes. If every simple value would be saved as an attribute and every complex object in an element, then this could be the SQL code:

DECLARE @Xml XML='<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://schemas.datacontract.org/2004/07/SerializationTest" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Config Id="1" Name="Test Config">
<Servers>
<List>
<Server Id="1" Url="http://some.url" />
<Server Id="2" Url="http://some.other.url" />
</List>
</Servers>
</Config>
</Configuration>'


;WITH XMLNAMESPACES(DEFAULT 'http://schemas.datacontract.org/2004/07/SerializationTest')
SELECT T.Item.value('@Id','INT') as Id,
T.Item.value('@Name','NVARCHAR(100)') as Name
FROM @Xml.nodes('//Configuration/Config') as T(Item)

;WITH XMLNAMESPACES(DEFAULT 'http://schemas.datacontract.org/2004/07/SerializationTest')
SELECT T.Item.value('@Id','INT') as Id,
T.Item.value('@Url','NVARCHAR(100)') as Url
FROM @Xml.nodes('//Configuration/Config/Servers/List/Server') as T(Item)


Much easier to read and hopefully to parse.

I am not going to write here about the actual implementation of IXmlSerializable. There are plenty of tutorials on the Internet about that. It's not pretty, :) but not too difficult, either.

What was the purpose of this exercise? Now I can send a complex object to SQL in a single query, making inserts and updates simple and not requiring at a call for each instance of each type of complex object. Now, is it fast? I have no idea. Certainly if performance is needed, perhaps the UDT/DataTable approach is faster. However you will have to define a type for each type that you send as a DataTable to a stored procedure. An alternative can be a binary serializer and a CLR SQL function that translates it into tables. However, in my project I need to easily implement very custom API methods and to control every aspect, including tracing and profiling the various SQL calls. I believe the customized IXmlSerializable/XML in SQL approach is a reasonable one.

As always, I hope this helps someone.

and has 0 comments
I am going to tell you about how I worked on an object that can wrap any object, serialize it, deserialize it, send it to a database, all efficiently and generically. But first, let me start with the beginning: you need a way to efficiently set/get values of properties in objects of different types. That's where TypeCache comes in. I am sure not everything is perfect with the class, but hopefully it will put you on the right track.

I will start with some of the code, the class that does everything. Something will be missing, though.
/// <summary>
/// It caches the property setters and getters for the public properties of a type
/// </summary>
public class TypeCache
{
private static ConcurrentDictionary<Type, TypeCache> _typeCacheDict = new ConcurrentDictionary<Type, TypeCache>();

public static TypeCache Get(Type type)
{
TypeCache cache;
if (!_typeCacheDict.TryGetValue(type, out cache))
{
cache = new TypeCache(type);
_typeCacheDict[type] = cache;
}
return cache;
}

private TypeCache(Type type)
{
Type = type;
Setters = new ConcurrentDictionary<string, Action<object, object>>();
Getters = new ConcurrentDictionary<string, Func<object, object>>();
var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
Properties = new ConcurrentDictionary<string, PropertyInfo>();
foreach (var prop in props)
{
if (prop.CanRead)
{
var objGetter = prop.GetValueGetter();
Getters[prop.Name] = objGetter.Compile();
}
if (prop.CanWrite)
{
var objSetter = prop.GetValueSetter();
Setters[prop.Name] = objSetter.Compile();
}
Properties[prop.Name] = prop;
}
}

public Type Type { get; private set; }
public ConcurrentDictionary<string, Action<object, object>> Setters { get; private set; }
public ConcurrentDictionary<string, Func<object, object>> Getters { get; private set; }
public ConcurrentDictionary<string, PropertyInfo> Properties { get; private set; }
}

public static class TypeCacheExtensions
{
/// <summary>
/// Set the value of a property by name
/// </summary>
/// <param name="cache"></param>
/// <param name="item"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Set<T>(this TypeCache cache, object item, string key, T value)
{
if (cache == null || item == null) return;
Action<object, object> setter;
if (!cache.Setters.TryGetValue(key, out setter)) return;
setter(item, (object)value);
}

/// <summary>
/// Get the value of a property by name
/// </summary>
/// <param name="cache"></param>
/// <param name="item"></param>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(this TypeCache cache, object item, string key)
{
if (cache == null || item == null) return default(T);
Func<object, object> getter;
if (!cache.Getters.TryGetValue(key, out getter)) return default(T);
return (T)getter(item);
}

/// <summary>
/// Set the value for a property to default by name
/// </summary>
/// <param name="cache"></param>
/// <param name="item"></param>
/// <param name="key"></param>
public static void Delete(this TypeCache cache, object item, string key)
{
if (cache == null || item == null) return;
Action<object, object> setter;
if (!cache.Setters.TryGetValue(key, out setter)) return;
var value = cache.Properties[key].PropertyType.GetDefaultValue();
setter(item, value);
}

/// <summary>
/// Set the values for all the public properties of a class to their default
/// </summary>
/// <param name="cache"></param>
/// <param name="item"></param>
public static void Clear(this TypeCache cache, object item)
{
if (cache == null || item == null) return;
Action<object, object> setter;
foreach (var pair in cache.Properties)
{
if (!cache.Setters.TryGetValue(pair.Key, out setter)) continue;
var value = pair.Value.PropertyType.GetDefaultValue();
setter(item, value);
}
}
}

(I used extension methods so that there would be no problems using a null value as the TypeCache) This class would be used something like this:
class Program
{
static void Main(string[] args)
{
var obj = new TestObject();
var cache = TypeCache.Get(obj.GetType());
Stopwatch sw = new Stopwatch();
sw.Start();
for (var i = 0; i < 100000; i++)
{
cache.Get<int>(obj, "TestProperty");
cache.Set<int>(obj, "TestProperty",i);
}
sw.Stop();
Console.WriteLine("Time: " + sw.Elapsed.TotalMilliseconds);
Console.ReadKey();
}
}

If you try to compile this, after adding all the namespaces required (System.Collections.Concurrent and System.Reflection), you will get an error, because you are missing the extension methods PropertyInfo.GetValueGetter and PropertyInfo.GetValueSetter. These should be returning Expressions that compiled get or set the value of an object. Here is the first attempt, using the normal PropertyInfo methods:
    public static class ReflectionExtensions
{
public static Expression<Func<object, object>> GetValueGetter(this PropertyInfo propertyInfo)
{
var getter=propertyInfo.GetGetMethod();
return (obj) => getter.Invoke(obj, new object[] { });
}

public static Expression<Action<object, object>> GetValueSetter(this PropertyInfo propertyInfo)
{
var setter = propertyInfo.GetSetMethod();
return (obj,val) => setter.Invoke(obj, new[] { val });
}

public static object GetDefaultValue(this Type type)
{
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
}
Wonderful thing! GetGet and GetSet :) Basically I am returning expressions that use reflection to get the getter/setter and then execute them. How long would the program with one million tries of get and set take? 1000 milliseconds. Could we improve on that?

Before .Net 4.0 the only solution to do that would have been to emit IL and compile it inside the code. Actually, even in 4.0 it is the most efficient option. But given my incompetence in that direction, I will give you the (slightly) easier to understand solution. Here we sacrifice a little speed for the readability of code:
    public static class ReflectionExtensions
{
/// <summary>
/// Get the expression of a value getter for a property. Compile the expression to execute or combine it with other expressions.
/// </summary>
/// <param name="propertyInfo"></param>
/// <returns></returns>
public static Expression<Func<object, object>> GetValueGetter(this PropertyInfo propertyInfo)
{
var instance = Expression.Parameter(typeof(object), "i");
var convertObj = Expression.TypeAs(instance, propertyInfo.DeclaringType);
var property = Expression.Property(convertObj, propertyInfo);
var convert = Expression.Convert(property, typeof(object));
return (Expression<Func<object, object>>)Expression.Lambda(convert, instance);
}

/// <summary>
/// Get the expression of a value setter for a property. Compile the expression to execute or combine it with other expressions.
/// </summary>
/// <param name="propertyInfo"></param>
/// <returns></returns>
public static Expression<Action<object, object>> GetValueSetter(this PropertyInfo propertyInfo)
{
var instance = Expression.Parameter(typeof(object), "i");
var argument = Expression.Parameter(typeof(object), "a");
var convertObj = Expression.TypeAs(instance, propertyInfo.DeclaringType);
var convert = Expression.Convert(argument, propertyInfo.PropertyType);
var setterCall = Expression.Call(convertObj,propertyInfo.GetSetMethod(),convert);
return (Expression<Action<object, object>>)Expression.Lambda(setterCall, instance, argument);
}

/// <summary>
/// Get the default value of a type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static object GetDefaultValue(this Type type)
{
return type.IsValueType ? New.Instance(type) : null;
}
}

/// <summary>
/// Class used to get instances of a type
/// </summary>
public static class New
{
private static ConcurrentDictionary<Type, Func<object>> _dict = new ConcurrentDictionary<Type, Func<object>>();

public static object Instance(Type type)
{
Func<object> func;
if (!_dict.TryGetValue(type, out func))
{
func = Expression.Lambda<Func<object>>(Expression.New(type)).Compile();
_dict[type] = func;
}
return func();
}
}

The rest of the article will be about explaining what these extension methods are doing. How fast were they? 300ms! 3.3 times faster. How did we do that?

Well, first of all let's get the New class out of the way. In this simple version it just creates instances of a type that has a parameterless constructor. In the case that I had, using simple objects to transfer information, I only needed that. What is does is create a lambda expression that represents the parameterless constructor of a type, compiles it and caches it. Even a simple thing like this is faster than Activator.GetInstance(type). Not by much, about 10%, but still. Anyway, we are only using this to delete the value of a property (by setting it to its default value), which is not really in the scope of our test. However, being a simple expression build, it shows you the things to come.

Now for the GetValueGetter and GetValueSetter methods. I will humanly read you the GetValueGetter method. You can correlate it with the code quite easily. Basically it says this:
  • the expression has one parameter, of type object
  • we will attempt to safely convert it (as) into the declaring type (the object the property belongs to)
  • we will access a property of an object of the declaring type
  • which we then convert to object so that the resulting expression returns object not the specific type of the property
  • transform it all into a lambda expression and return it

The major difficulty, for me at least, was to grasp that there is no object (there is no spoon), but only a lambda expression that receives a parameter of type object and returns another object. The expression would be compiled and then applied on an actual instance.

And that's that. The object that does the serialization and transformation to SqlParameters and gets/sets the values of the wrapped object is more complicated and I will probably not write a blog entry about it, but think about the concept a little: an object that receives another object as the constructor and works like a dictionary. The keys and values of the dictionary are filled by the property names and values of the original object, while any change in the values of the dictionary will be set to the properties of the object. It makes it very easy to access objects generically, controlling what accessors do, but without the need for PostSharp or T4 templating, real time, programmatic. The task of serialization is taken by the ISerializable interface, which also uses a type of dictionary, meaning the object can be passed around from web services to C# code and then to SQL via SqlParameters.



I had this AngularJS grid that I wanted to show totals for certain categories and types. To be more precise, I had a list of items with Category and Type and I want to know the total for all categories and, for each category, the total for each type. This works perfectly if I load all items as individual, but I had hundred of thousands of items so it was clearly impractical. The solution? Send totals for every category and type, then just add them in the grid. In order to do that, though, I had to change the template of the "grouping row", the one that in ngGrid has the ngAggregate class.

It seems that all that is required for that is to change the aggregate template in the grid options. If you are not interested in the details, jump directly to the solution.

There is already an aggregate template in ngGrid.js, one that (at this time) looks like this:
<div ng-click="row.toggleExpand()" ng-style="rowStyle(row)" class="ngAggregate">
<span class="ngAggregateText">{{row.label CUSTOM_FILTERS}} ({{row.totalChildren()}} {{AggItemsLabel}})</span>
<div class="{{row.aggClass()}}"></div>
</div>

So we see that that the number displayed in an aggregate row is coming from a function of the row object called totalChildren, which is defined in ngAggregate.prototype and looks like this:
ngAggregate.prototype.totalChildren = function () {
if (this.aggChildren.length > 0) {
var i = 0;
var recurse = function (cur) {
if (cur.aggChildren.length > 0) {
angular.forEach(cur.aggChildren, function (a) {
recurse(a);
});
} else {
i += cur.children.length;
}
};
recurse(this);
return i;
} else {
return this.children.length;
}
};
Maybe one could change the function to cover specific types of objects and return a sum instead of a count, but that is not the scope of the current post.

The solution described here will involve a custom function and a custom template. Here is how you do it:
  • Define the options for the grid. I am sure you already have it defined somewhere, if not, it is advisable you would. Sooner or later you will want to customize the output and functionality.
  • Add a new property to the options called aggregateTemplate. This will look probably like the default template, but with another function instead of totalChildren.
  • Define the function that will aggregate the items.

Solution 1:

Define template:
$scope.gridOptions = {
data: 'Data.Units',
enableColumnResize: true,
showColumnMenu: false,
showFilter: true,
multiSelect: false,
showGroupPanel: true,
enablePaging: false,
showFooter: true,
columnDefs: [
{ field: 'Country', displayName: 'Country', width: 190 },
{ field: 'Type', displayName: 'Unit Type' },
{ field: 'Count', displayName: 'Count', width: 180}
],
aggregateTemplate: "<div ng-click=\"row.toggleExpand()\" ng-style=\"rowStyle(row)\" class=\"ngAggregate\">" +
" <span class=\"ngAggregateText\">{{row.label CUSTOM_FILTERS}} ({{aggFunc(row)}} {{AggItemsLabel}})</span>" +
" <div class=\"{{row.aggClass()}}\"></div>" +
"</div>"
};
Define function:
$scope.aggFunc = function (row) {
var sumColumn='Count';
var total = 0;
angular.forEach(row.children, function(entry) {
total+=entry.entity[sumColumn];
});
angular.forEach(row.aggChildren, function(entry) {
total+=$scope.aggFunc(entry);
});
return total;
};

What we did here is we replaced row.totalChildren() with aggFunc(row) which we defined in the scope. What it does is add to the total the value of 'Count' rather than just count the items. It goes through row.children, which contains normal row items, then through aggChildren, which contains aggregate rows, which we pass through the same function in order to get their total.

Well, this works perfectly, but doesn't that mean we need to use this for each grid? There is a lot of code duplication. Let's first put the template in the cache so we can reuse it:
module.run(["$templateCache", function($templateCache) {

$templateCache.put("aggregateCountTemplate.html",
"<div ng-click=\"row.toggleExpand()\" ng-style=\"rowStyle(row)\" class=\"ngAggregate\">" +
" <span class=\"ngAggregateText\">{{row.label CUSTOM_FILTERS}} ({{aggFunc(row)}} {{AggItemsLabel}})</span>" +
" <div class=\"{{row.aggClass()}}\"></div>" +
"</div>"
);

}]);
Now the gridOptions change to
$scope.gridOptions = {
[...]
aggregateTemplate: "aggregateCountTemplate.html"
};
and we can reuse the template anytime we want. Alternatively we can create a file and reference it, without using the cache:
$scope.gridOptions = {
[...]
aggregateTemplate: "/templates/aggregateCountTemplate.html"
};

Now, if we could replace the aggFunc function with a row function, adding it to ngAggregate.prototype. Unfortunately we cannot do that, since ngAggregate is a 'private' object. The only thing we can do is to add some sort of static function. The solution is to add it in the root scope, so that is available everywhere.

Solution 2:

Here is the content of the file aggregateCountTemplateCache.js, that I created and load every time in the site. It does two things: inject the function in the root scope of the application and add the template to the cache. The only other thing to do is to use the aggregateTemplate: "aggregateCountTemplate.html" grid options.
var module = angular.module('app', ['ngResource', 'ui.bootstrap', 'ui', 'ngGrid', 'importTreeFilter', 'ui.date', 'SharedServices', 'ui.autocomplete']);

module.run(["$templateCache","$rootScope", function($templateCache,$rootScope) {

$rootScope.aggregateCountFunc = function (row) {
var total = 0;
angular.forEach(row.children, function(entry) {
total+=entry.entity.Count;
});
angular.forEach(row.aggChildren, function(entry) {
total+=$rootScope.aggregateCountFunc(entry);
});
return total;
};

$templateCache.put("aggregateCountTemplate.html",
"<div ng-click=\"row.toggleExpand()\" ng-style=\"rowStyle(row)\" class=\"ngAggregate\">" +
" <span class=\"ngAggregateText\">{{row.label CUSTOM_FILTERS}} ({{aggregateCountFunc(row)}}{{AggItemsLabel}})</span>" +
" <div class=\"{{row.aggClass()}}\"></div>" +
"</div>"
);

}]);

Enjoy!

and has 1 comment
The summer season is fast approaching and there have been a lot of changes in the TV series arena. As usual, the red indicates shows that I don't recommend and green the ones that I recommend.



  • Elementary - I was correct, there was an entire arch of the story regarding the brother of Sherlock, French mafia, MI6, moles, etc. It lacked subtlety and it was mostly boring. How come normal murders are so much more interesting than archvillain trickery?
  • The Tomorrow People (2013) - the show was cancelled in May. I can't say I am sorry, though. It was typical young adolescent idiocy.
  • The Originals - I am however completely not happy for NOT cancelling this crap.
  • Marvel's agents of S.H.I.E.L.D. - Yay! Hail Shield! Good guys win again. And also Samuel L. Jackson appears in the end season, bringing with him what appear to be Shield agent clones? Did he steal them from Star Wars?
  • Ripper Street - Filming for the Amazon online channel was supposed to begin in May. I think it has.
  • Babylon - No news yet, but there are certainly no news of cancellation, so I guess we will see the next episodes being released later this year.
  • Banshee - Third season confirmed.
  • Bitten - Another show that was renewed for a second season for no apparent reason. It is just awful.
  • Black Sails - Renewed for a second season, I partly like it. It is at least well done, with decent acting and an eye for detail.
  • From Dusk Till Dawn - As a funny commenter observed: in an incredible moves the owners of the El Rey network renew the show produced by El Rey for a second season. I understand the difficulty of translating a movie into a series... wait, there is a sequel, too? Wait, there is a From Dusk Till Dawn third part? I've never heard of them before. Nevermind.
  • Helix - I think I got it now. When a show makes an effort to really suck ass, they renew it. It's like an executive learned behaviour to promote ass sucking. Renewed for a second season, Helix is the poster child of bad movie making.
  • House of Cards - It's good that I had to write this review, because it reminded me to watch the second season. Still haven't done it, though, which indicates either a problem with me or with the show. Probably me. Again, Kevin Spacey can't go wrong.
  • Intelligence - Amazingly enough, a show that I sort of enjoyed, without liking it too much, was cancelled. Very *intelligent* move...
  • Ressurection - I really tried to make myself watch this show, but I just procrastinated for a really long time. It says something about my feelings for this show. I was really waiting for the second season of the French one and the American remake doesn't do it for me.
  • Star Crossed - Teenage sci fi show about alien romance? Really beautiful actors, incredibly stupid script and acting. I just can't believe it was cancelled. It ruins my theory, because it trully sucked ass.
  • The 100 - The show started really well, predictably degenerated in artificial drama that had no place in the series, while going soft on the problems that someone would meet in that exact situation. Suddenly 100 teenagers from space have a functional colony, fight "grounders" for no good reason and keep alternating between screwing, screwing over and screwing up. Typical teenager, one might say, but their parents on "The Ark" are even worse. The show was renewed for a second season. The season finale, though, didn't give me much hope. They added two (and a half, one might say) human factions to the mix in just the one episode
  • The After - The pilot got positive reviews to they ordered it to series. I am not holding my breath.
  • The Americans - I liked the second season, although the arch with the American mole that turns homicidal and discovers the entire KGB operation by himself was really ridiculous. It was a nice touch about the murder of the KGB agents, also what happened to Nina and the reactions of the people. However the Moscow verdict makes no sense.
  • The Red Road - Ah, maybe exceptions just confirm a rule. Another crappy show that gets renewed for a second season.
  • True Detective - The first season was great, so there will be new seasons. New actors and new stories, though, for each season.
  • Vikings - Politics keep interfering with nice raiding and pillaging. That is the moment in history when things started going downwards and global warming started. True fact!
  • Suits - The fourth season just started. They try to keep it exciting, but the feel is gone.
  • Continuum - The third season went all over the place. The season finale has Cameron cooling it off with a new boyfriend from (yet) another timeline, then another mysterious traveler from a very far future that seems to have started the Freelancer cult, Liber8 split off after realizing they do more harm than good (while each character in the group starts to get a "nice" shine on them) and finally the Freelancers getting mowed down. But then, they never really die, do they?
  • Crisis - For me the scene when a stupid kid sees Francis killing a kid and then tells all of the others that the kidnappers made him do it has done it for me! Oh, or the one when he forces a Chinese spy to unleash a virus that effectively destroys the Internet in order to gain time. Where do you get this idiocy from? BTW, it was cancelled.
  • Da Vinci's Demons - Enemies get together, they eventually run into Mayas on America and, of course, traces of DaVinci's parents, then they return just in time to stave off the invasion of Italy by the Turkish navy which just appeared out of nowhere. Not only will this be renewed, but it won prizes! It is a guilty pleasure that I like it, most of the time, I have to admit.
  • Turn - Interesting show. It lacks something, probably sympathetic characters. I really don't see myself liking any of them, including the generously bosomed women who have casual sex all the time. That is saying something.
  • The Crimson Field - Was it cancelled because it featured female main characters? Or because it was well acted? I just don't know.
  • Silicon Valley - Elon Musk accused the show of showing the wrong weirdness of Silicon Valley. I don't know how things are there, but to me it feels like he probably is right. A lot of crap about sex, race, manhood, geekiness seen as emasculating, random genius, etc. For me the scene when in one night the guy develops what his entire team failed to in five months has done it for me. I like parts of the show, but I don't think I liked the show overall.
  • Bad Teacher - I could stand literally 2 minutes of it. Then I stopped and deleted everything. It is the type of ha ha ha sitcom that brings nothing.
  • Black Box - A movie about a brilliant woman psychiatrist. Of course she is very sexually active, a bit insane and a sexy redhead. It's basically Doctor House meets Unforgettable. I bet it will be cancelled because she is a crazy broad and only male characters are allowed to be brilliant. Plus she is annoying. Nice ass, though.
  • Californication - The seventh season will be the last, thank you! They butchered a lovely idea that was fantastic in the first two seasons and let it die in agony for the last five, blood and guts pouring out of it until nothing but a boring husk remained. Duchovny was my god at the beginning of the show.
  • Deadbeat - comedy about a deadbeat seeing ghosts and solving their problems. I actually commented on Imdb (I usually don't for TV series) to warn people off it. It is offensively boring and formulaic and completely not funny.
  • Dracula - It was a weird reinvention of Dracula, but one that I kind of liked. Guess what! They cancelled it! Perhaps the trick is to not like anything that I like and like everything that I dislike. In this way they will renew the shows that I hate to love, rather than the ones that I love to hate. Yes, that's a good plan.
  • Game of Thrones - Thank God for Peter Dinklage! One realizes how tedious the books are when they watch this well done series that compresses whole books in half a day of video and you still feel nothing happens. And then someone dies and the episode ends.
  • Halt and Catch Fire - Imagine Mad Men, but with a real psychopath as the main character, the field changed from advertising to personal computer manufacturing and a clone of Angelina Jolie's persona from the movie Hackers as the "tech genius". So, in other words, with more Mad, and less Men. I really dislike the main character and I don't see where this is going. Hardware is not glamorous, software is! *cough* *cough*
  • In the Flesh - I really liked the first season, but I waited to see episodes from the new one as well. Somehow I can't bring myself to start watching it again. I will, though, soon.
  • Penny Dreadful - Ok, it is a horror series that mixes in all the Victorian themes like Jack the Ripper, vampires, werewolves, tuberculosis, Frankenstein, Dorian Gray, etc. But it's well done and it has a nice cast: Eva Green, Timothy Dalton, Billie Piper, Josh Hartnett and others.
  • Prey - John Simm is a detective in this British drama, and he is framed for the murder of his wife and son by a mysterious man. He is a... fugitive from the law trying to solve his own case. Kind of bland and hard to believe in this day and age. The Fugitive was much more credible because not only movies were silly at that time, but also most people. Now we have evolved quite a lot *cough* *I should stop with these asterisks everywhere* *cough*
  • Quirke - Gabriel Byrne is the lead in this British miniseries, playing a doctor that somehow is connected to everyone in the city and where the problems usually stem from one member of his extended family or another. Feels like In Treatment set in the past and in which Byrne is both doctor and patient. I think it could have been more. As such, it's a little bit annoying and slow.
  • Salem - It is still the season of the witch. Salem is a new show, but I haven't started watching it. The premise seemed a little bit forced.
  • The Wil Wheaton Project - Wesley from Star Trek is hosting a show that is not a TV series. It is a humorous take on the latest sci-fi news. You might not know it, but Wheaton is very active online in all kind of geeky indie shows, together with his friend Felicia Day (The Guild). I found it a little too satirical, but I liked it.
  • Under the Dome - the boring and annoying show has been renewed for a second season that will air at the end of the month.
  • Crossbones - Pirates! Again! This time starring John Malkovich. Haven't started watching yet.
  • Fargo - Based on a Coen brothers movie with the same name, it stars Billy Bob Thornton and has the brothers as executive producers. Haven't started watching yet.
  • From There to Here - British drama covering life from the Manchester bombing up to 2000. Haven't started watching yet.

That's about it so far.