// Convenience to build grid from array of HHisItem public static HGrid hisItemsToGrid(HDict meta, HHisItem[] items) { HGridBuilder b = new HGridBuilder(); b.Meta.add(meta); b.addCol("ts"); b.addCol("val"); for (int i = 0; i < items.Length; ++i) { b.addRow(new HVal[] { items[i].TimeStamp, items[i].hsVal }); } return(b.toGrid()); }
// Convenience to build an error grid from exception // Replace Tabs with 2 spaces // Filter out /r/n // YET TO DO - Test this - Adding is a bit strange // project-haystack: /* * Error Grid * If a operation failed after a request grid is successfully read by a server, * then the server returns an error grid. An error grid is indicated by the presence of the err marker tag in the grid metadata. * All error grids must also include a dis tag in the grid metadata with a human readable descripton of the problem. * If the server runtime supports stack traces, this should be reported as a multi-line string via the errTrace tag in the grid metadata. * * Example of an error grid encoded as Zinc: * ver:"3.0" err dis:"Cannot resolve id: badId" errTrace:"UnknownRecErr: badId\n ...." * empty */ public static HGrid errToGrid(Exception e) { // Java sucks - replaed with .NET StringBuilder sout = new StringBuilder(); string trace = e.StackTrace.ToString(); for (int i = 0; i < trace.Length; ++i) { int ch = trace[i]; if (ch == '\t') { sout.Append(" "); } else if (ch != '\r') { sout.Append((char)ch); } } HGridBuilder b = new HGridBuilder(); b.Meta.add("err") .add("dis", e.Message) .add("errTrace", sout.ToString()); b.addCol("empty"); return(b.toGrid()); }
////////////////////////////////////////////////////////////////////////// // Utils ////////////////////////////////////////////////////////////////////////// // Convenience to build one row grid from HDict. public static HGrid dictToGrid(HDict dict) { HGridBuilder b = new HGridBuilder(); int iIndex = 0; List <HVal> cells = new List <HVal>(); for (iIndex = 0; iIndex < dict.size(); iIndex++) { string strKey = dict.getKeyAt(iIndex, false); if (strKey != null) { HVal val = dict.get(strKey); b.addCol(strKey); cells.Add(val); } } b.addRow(cells.ToArray()); return(b.toGrid()); }
// Convenience to build grid from array of HDict. // Any null entry will be row of all null cells. public static HGrid dictsToGrid(HDict meta, HDict[] dicts) { HCol colEmpty = new HCol(0, "empty", HDict.Empty); // If empty return an empty cols collection and no values Grid if (dicts.Length == 0) { List <List <HVal> > rowListEmpty = new List <List <HVal> >(); List <HCol> rowEmpty = new List <HCol>(); rowEmpty.Add(colEmpty); return(new HGrid(meta, rowEmpty, rowListEmpty)); } HGridBuilder b = new HGridBuilder(); b.Meta.add(meta); // collect column names - why does this need to be a dictionary (hashmap in the java code)? // it only stores the col name twice. Dictionary <string, string> colsByName = new Dictionary <string, string>(); for (int i = 0; i < dicts.Length; ++i) { HDict dict = dicts[i]; if (dict != null) { for (int it = 0; it < dict.size(); it++) { string name = dict.getKeyAt(it, false); if (name != null) { if (!colsByName.Keys.Contains(name)) { colsByName.Add(name, name); b.addCol(name); } } } } } // if all dicts were null, handle special case // by creating a dummy column if (colsByName.Count == 0) { colsByName.Add("empty", "empty"); b.addCol("empty"); } // now map rows int numCols = b.colCount; for (int ri = 0; ri < dicts.Length; ++ri) { HDict dict = dicts[ri]; HVal[] cells = new HVal[numCols]; for (int ci = 0; ci < numCols; ++ci) { if (dict == null) { cells[ci] = null; } else { BCol colatci = b.GetColAt(ci); if (colatci != null) { cells[ci] = dict.get(colatci.Name, false); } } } b.addRow(cells); } return(b.toGrid()); }