SNMP4j agent snmp table - snmp

I have created on SNMP agent using snmp4j api but getting issue with snmp table registration
Once i register a table and and rows in table. and after that if i set the values in table all the rows got set with same value.
I have snmp table created from JSON
In below table if i set value
.1.3.6.1.4.1.1.201.6.2. it set the values for all the rows that are registered in below table. Does anyone aware of how to register and set the values properly using snmmpj agent.
{
"tableName": "table1",
"tableId": ".1.3.6.1.4.1.1.201.6.1",
"columns": [
{
"columnName": "column1",
"columnOID": 1,
"dataType": 70,
"accessType": 1,
"defaultValue":0
},
{
"columnName": "column2",
"columnOID": 2,
"dataType": 70,
"accessType": 1,
"defaultValue":0
},
{
"columnName": "column3",
"columnOID": 3,
"dataType": 70,
"accessType": 1,
"defaultValue":0
},
]
}
public static MOTable<MOTableRow<Variable>, MOColumn<Variable>, MOTableModel<MOTableRow<Variable>>> createTableFromJSON(
JSONObject data) {
MOTable table = null;
if (data != null) {
MOTableSubIndex[] subIndex = new MOTableSubIndex[] { moFactory
.createSubIndex(null, SMIConstants.SYNTAX_INTEGER, 1, 100) };
MOTableIndex index = moFactory.createIndex(subIndex, false,
new MOTableIndexValidator() {
public boolean isValidIndex(OID index) {
boolean isValidIndex = true;
return isValidIndex;
}
});
Object indexesObj = data.get("indexValues");
if(indexesObj!=null){
String indexes = data.getString("indexValues");
String tableOID = data.getString("tableId");
JSONArray columnArray = data.getJSONArray("columns");
int columnSize = columnArray.size();
MOColumn[] columns = new MOColumn[columnSize];
Variable[] initialValues = new Variable[columnSize];
for (int i = 0; i < columnSize; i++) {
JSONObject columnObject = columnArray.getJSONObject(i);
columns[i] = moFactory.createColumn(columnObject
.getInt("columnOID"), columnObject.getInt("dataType"),
moFactory.createAccess(columnObject
.getInt("accessType")));
initialValues[i] = getVariable(columnObject.get("defaultValue"));
}
MOTableModel tableModel = moFactory.createTableModel(new OID(
tableOID), index, columns);
table = moFactory.createTable(new OID(tableOID), index, columns,
tableModel);
String[] indexArrString = indexes.split(";");
for(String indexStr: indexArrString){
MOTableRow<Variable> row = createRow(new Integer(indexStr.trim()), initialValues);
table.addRow(row);
}
}
}
return table;
}

First of all, OIDs do not start with a dot (as specified by ASN.1).
Second, you do not seem to use any row index data. Rows are indentified by their indexes. A row index is the instance identifier suffix of a tabular instance OID:
<tableOID>.1.<rowIndex>
The can consists several sub-index values encoded as OIDs.

Related

elasticsearch sort by price with currency

I have data
{
"id": 1000,
"price": "99,01USA",
},
{
"id": 1001,
"price": "100USA",
},
{
"id": 1002,
"price": "780USA",
},
{
"id": 1003,
"price": "20USA",
},
How I sort order by price (ASC , DESC)
You can alter it a little to parse price to integer and then sort it
You can create a dynamic sort function that sorts objects by their value that you pass:
function dynamicSort(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
/* next line works with strings and numbers,
* and you may want to customize it to your needs
*/
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
So you can have an array of objects like this:
var People = [
{Name: "Name", Surname: "Surname"},
{Name:"AAA", Surname:"ZZZ"},
{Name: "Name", Surname: "AAA"}
];
...and it will work when you do:
People.sort(dynamicSort("Name"));
People.sort(dynamicSort("Surname"));
People.sort(dynamicSort("-Surname"));
Actually this already answers the question. Below part is written because many people contacted me, complaining that it doesn't work with multiple parameters.
Multiple Parameters
You can use the function below to generate sort functions with multiple sort parameters.
function dynamicSortMultiple() {
/*
* save the arguments object as it will be overwritten
* note that arguments object is an array-like object
* consisting of the names of the properties to sort by
*/
var props = arguments;
return function (obj1, obj2) {
var i = 0, result = 0, numberOfProperties = props.length;
/* try getting a different result from 0 (equal)
* as long as we have extra properties to compare
*/
while(result === 0 && i < numberOfProperties) {
result = dynamicSort(props[i])(obj1, obj2);
i++;
}
return result;
}
}
Which would enable you to do something like this:
People.sort(dynamicSortMultiple("Name", "-Surname"));
Subclassing Array
For the lucky among us who can use ES6, which allows extending the native objects:
class MyArray extends Array {
sortBy(...args) {
return this.sort(dynamicSortMultiple(...args));
}
}
That would enable this:
MyArray.from(People).sortBy("Name", "-Surname");

How can I change the dropdownlist's options?

I'm a newbie in asp.net so I'm hoping you could give me some help on my dropdownlist bound to a table.
Here's the scenario:
I have a table Account with fields UserId, UserName and Type. The Type field contains 3 items: 'S', 'A', and 'U'. Each user has his own Type. I have a dropdownlist named 'ddlType' which is already
bound on the Account table. However, I want the options of the dropdownlist to be displayed as 'Stakeholder', 'Approver', and 'User' instead of displaying letters/initials only. Since I do not prefer making any changes in the database, how can I change those options through code behind?
Here's my code:
public void BindControls(int selectedUserId)
{
DataTable dtAccount = null;
try
{
dtAccount = LogBAL.GetAccountDetails(selectedUserId);
if (dtAccount.Rows.Count > 0)
{
lblUserId.Text = dtAccount.Rows[0]["UserId"].ToString();
txtUserName.Text = dtAccount.Rows[0]["UserName"].ToString();
ddlType.SelectedValue = dtAccount.Rows[0]["Type"].ToString();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
dtAccount.Dispose();
}
}
Any help from you guys will be appreciated. Thanks in advanced! :D
You can bind Dropdownlist in codebehind.
Get your data "Type" into array. Make array two dimentional and assign values to it.
After having data your array will looks like this,
string[,] Types = { { "Stakeholder", "S" }, { "Approver", "A" }, { "User", "U" } };
Now assign values to Dropdown,
int rows = Types.GetUpperBound(0);
int columns = Types.GetUpperBound(1);
ddlType.Items.Clear();
for (int currentRow = 0; currentRow <= rows; currentRow++)
{
ListItem li = new ListItem();
for (int currentColumn = 0; currentColumn <= columns; currentColumn++)
{
if (currentColumn == 0)
{
li.Text = Types[currentRow, currentColumn];
}
else
{
li.Value = Types[currentRow, currentColumn];
}
}
ddlType.Items.Add(li);
}
I haven't tested it but hopefully it will work.

Problem with sorting list

I have a problem with sorting collection. User saves few values and he sorts it.
At next launch program must order a new collection.
Values which have been saved by user can be in new collection, but It can be situation , when those values aren't in new collection.
I wrote some code, and I want your feedback If it has sense
var oldValues = new List<string>(new[] { "id5", "id3", "id1" });
var valuesToOrder = new List<string>(new[] { "id1", "id2", "id3", "id4" });
int numberOfReorderedValues = 0;
for (int i = 0; i < oldValues.Count; i++)
{
if (valuesToOrder.Contains(oldValues[i]))
{
int indexOfValueWhichShouldBeBefore = valuesToOrder.IndexOf(oldValues[i]);
string valueWhichShouldBeBefore = valuesToOrder[indexOfValueWhichShouldBeBefore];
string valueWhichShouldBeAfter = valuesToOrder[numberOfReorderedValues];
valuesToOrder[numberOfReorderedValues] = valueWhichShouldBeBefore;
valuesToOrder[indexOfValueWhichShouldBeBefore] = valueWhichShouldBeAfter;
numberOfReorderedValues++;
This code works, but I must tomorrow show it to my boss, and I don't go to fool
It's not clear what you're after.
It sounds like you've got 2 lists. One of them contains a list of 'required', and the other contains a list of 'pick from'.
How about use LINQ to sort?
var oldValues = new List<string>(new[] { "id5", "id3", "id1" });
var valuesToOrder = new List<string>(new[] { "id1", "id2", "id3", "id4" });
var sorted = valuesToOrder.Intersect(oldValues).OrderBy(x=>x);
// sorted now has id1 and id3, sorted alphabetically.

How to select same column's name from different table in Linq?

I've 2 tables with same column's name, for example, both table A and table B has column's name "Test". I want to select column Test from both table A and B to entity class. How can I do this?
It sounds like you want the two entities of TableA and TableB merged into a new object. You can use the .Select() extension method to create a new anonymous type, or into a class that you already have defined.
The requirement here is that you've got to find a common attribute between TableA and TableB. Here I assume you've got something like ID to match them together.
Anonymous Type
var mergedTests = from a in db.TableA
join b in db.TableB on a.CommonID equals b.CommonID
select new
{ TestFromA = a.Test, TestFromB = b.Test }
.ToList();
Existing Class
List<MyCustomTests> mergedTests = from a in db.TableA
join b in db.TableB on a.CommonID equals b.CommonID
select new MyCustomTests
{ TestName= a.Test, ShortName= b.Test }
.ToList();
class Program
{
static void Main(string[] args)
{
var A = new Data[] {
new Data { Test = 1, Relation = 1 },
new Data { Test = 2, Relation = 2 },
new Data { Test = 3, Relation = 3 },
new Data { Test = 4, Relation = 4 },
new Data { Test = 5, Relation = 5 },
};
var B = new Data[] {
new Data { Test = 2, Relation = 2 },
new Data { Test = 3, Relation = 3 },
new Data { Test = 5, Relation = 5 },
};
var res = from a in A
join b in B on a.Relation equals b.Relation
select new { TestA = a.Test, TestB = b.Test };
}
}
class Data
{
public int Test;
public int Relation;
}

Sorting datatable column by day name

I have a datatable with day name column. I want to sort this column by day name e.g. if I have [Friday, Monday,Sunday] sorting should return [Monday ,Friday, Sunday] (ascending) and [Sunday,Friday, Monday] (descending).
I tried to use custom sorting but I wasn't able to represent my custom order.
Do you have ideas ?
Thanks
I found a solution for my problem. I created a hidden column with numeric values. The sorting will be based in this column. This is the custom sort function
// Custom function to sort Column by another Column
var mysortFunction = function(a, b, desc) {
// Deal with empty values
if(!YAHOO.lang.isValue(a)) {
return (!YAHOO.lang.isValue(b)) ? 0 : 1;
} else if(!YAHOO.lang.isValue(b)) {
return -1;
}
// compare column values
var comp = YAHOO.util.Sort.compare;
var compState = comp(a.getData("myhiddenColumn"), b.getData("myhiddenColumn"), desc);
return compState;
};
And the column defs :
var myColumnDefs = [
{ key: "A", sortable:true,hidden:true },
{ key: "columnToSort",label:"ABC", sortable:true, sortOptions: { sortFunction: mysortFunction }
{ key: "myhiddenColumn", sortable:true, hidden:true }
}
];
Hope this helps

Resources