Basic where clause with InfluxDB and Grafana - grafana

I'm struggling with a real simple task:
Make a SELECT property_a WHERE property_b == "my_value" using InfluxDB.
I have have the following datastruct:
type RegionsJsonData struct {
Data string `json:"data"`
Stato string `json:"stato"`
CodiceRegione int `json:"codice_regione"`
DenominazioneRegione string `json:"denominazione_regione"`
Lat float64 `json:"lat"`
Long float64 `json:"long"`
RicoveratiConSintomi int `json:"ricoverati_con_sintomi"`
TerapiaIntensiva int `json:"terapia_intensiva"`
TotaleOspedalizzati int `json:"totale_ospedalizzati"`
IsolamentoDomiciliare int `json:"isolamento_domiciliare"`
TotaleAttualmentePositivi int `json:"totale_attualmente_positivi"`
NuoviAttualmentePositivi int `json:"nuovi_attualmente_positivi"`
DimessiGuariti int `json:"dimessi_guariti"`
Deceduti int `json:"deceduti"`
TotaleCasi int `json:"totale_casi"`
Tamponi int `json:"tamponi"`
Datetime time.Time
}
This structure will be populated with some data, than i insert the struct into InfluxDB with the following statement:
var m map[string]interface{} = make(map[string]interface{})
m["codice_regione"] = provinceData[i].CodiceRegione
m["denominazione_regione"] = provinceData[i].DenominazioneRegione
m["lat"] = provinceData[i].Lat
m["long"] = provinceData[i].Long
m["ricoverati_con_sintomi"] = provinceData[i].RicoveratiConSintomi
m["terapia_intensiva"] = provinceData[i].TerapiaIntensiva
m["totale_ospedalizzati"] = provinceData[i].TotaleOspedalizzati
m["isolamento_domiciliare"] = provinceData[i].IsolamentoDomiciliare
m["totale_attualmente_positivi"] = provinceData[i].TotaleAttualmentePositivi
m["nuovi_attualmente_positivi"] = provinceData[i].NuoviAttualmentePositivi
m["dimessi_guariti"] = provinceData[i].DimessiGuariti
m["deceduti"] = provinceData[i].Deceduti
m["totale_casi"] = provinceData[i].TotaleCasi
m["tamponi"] = provinceData[i].Tamponi
pts[i] = client.Point{
Measurement: "regions_data",
Tags: nil,
Time: provinceData[i].Datetime,
Fields: m}
The data are inserted into InfluxDB, I'm able to display some graph using Grafana.
However, i need to create a graph for every "denominazione_regione" field.
So, from Grafana, I've made the following query:
But unfortunately no data are displayed, am i missing something?
How to make a WHERE clause from Grafana using InfluxDB?

As pointed by #JanGaraj, if you want to make some query you need to save the field as a tag as following:
var m map[string]interface{} = make(map[string]interface{})
m["total_deaths"] = worldData[i].TotalDeaths
m["total_cases"] = worldData[i].TotalCases
m["new_deaths"] = worldData[i].NewDeaths
m["new_cases"] = worldData[i].NewCases
pts[i] = client.Point{
Measurement: "all_world_data",
Tags: map[string]string{"nation": worldData[i].State},
Time: worldData[i].Date,
Fields: m}
Than you can run a query as following:

Related

How to create a GORM model from GRPC protobuf [duplicate]

I am trying to figure out how to integrate the gorm.Model fields (deleted_at, create_at, id, etc) into my proto3 definitions. However, I can't a datetime type for proto3. I tried looking for documentation on how to serialize the gorm fields to strings (since proto3 handles strings) but I have not found anything.
Has anyone been able to successfully use the gorm model fields in their proto definitions? I'm using go-micro's plugin to generate *pb.go files.
Here's my current message definition which doesn't work. It seems like empty strings are being stored in the database for deleted_at since when querying for deleted_at is null the postgres database returns nothing.
message DatabaseConfig {
string address = 1;
int32 port = 2;
string databaseName = 3;
string username = 4;
string password = 5;
string databaseType = 6;
string quertStatement = 7;
int32 id = 8;
string createdAt = 9;
string updatedAt = 10;
string deletedAt = 11;
}
UPDATE:
I've updated my proto def to the following but gorm still isn't properly using the Id, CreatedAt, UpdatedAt, and DeletedAt fields
syntax = "proto3";
package go.micro.srv.importer;
import "google/protobuf/timestamp.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
service ImporterService {
rpc CreateDatabaseConfig(DatabaseConfig) returns (Response) {}
rpc RetrieveDatabaseConfig(GetRequest) returns (Response) {}
rpc UpdateDatabaseConfig(DatabaseConfig) returns (Response) {}
rpc DeleteDatabaseConfig(DatabaseConfig) returns (Response) {}
}
message GetRequest {}
message DatabaseConfig {
string address = 1;
int32 port = 2;
string databaseName = 3;
string username = 4;
string password = 5;
string databaseType = 6;
string quertStatement = 7;
int32 id = 8;
google.protobuf.Timestamp createdAt = 9 [(gogoproto.stdtime) = true];
google.protobuf.Timestamp updatedAt = 10 [(gogoproto.stdtime) = true];
google.protobuf.Timestamp deletedAt = 11 [(gogoproto.stdtime) = true];
}
message Response {
bool created = 1;
DatabaseConfig database_config = 2;
repeated DatabaseConfig databaseConfigs = 3;
}
The protoc-gen-gorm project did not work for me. It looks like there is some blending of proto2 and proto3 happening, and ultimately I was unable to get it to work.
My solution was to create a script to do post processing after I generate the go files from protobuf.
If this was my proto profile/profile.proto:
message Profile {
uint64 id = 1;
string name = 2;
bool active = 3;
// ...
}
Which created profile/profile.pb.go with standard protoc command:
// ...
type Profile struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Active bool `protobuf:"varint,3,opt,name=active,proto3" json:"active,omitempty"`
}
// ...
I use this script gorm.sh:
#!/bin/bash
g () {
sed "s/json:\"$1,omitempty\"/json:\"$1,omitempty\" gorm:\"type:$2\"/"
}
cat $1 \
| g "id" "primary_key" \
| g "name" "varchar(100)" \
> $1.tmp && mv $1{.tmp,}
Which I invoke on my go file after it's generated with ./gorm.sh profile/profile.pb.go and the result of profile/profile.pb.go is:
// ...
type Profile struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty" gorm:"type:primary_key"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty" gorm:"type:varchar(100)"`
Active bool `protobuf:"varint,3,opt,name=active,proto3" json:"active,omitempty"`
}
// ...
Try to use protoc-gen-gorm. It will create another file .pb.gorm.go
Might be an option to use something like this: https://github.com/favadi/protoc-go-inject-tag to generate the tags automatically (I am still looking into this myself)

apply FilterCriteria "whenDateEqualToAny(dates)" - What is the correct form of the date array (dates) to parse?

I want to add some quick filters using the ui of google sheets. Currently I want to allow the user to click "show last month" to only see the data of the last month. The dates are written in the first column.
Now I prefer to use the filter of google sheets before just printing the values into the sheet, to allow the user to further modify that filter.
Thus I am trying to build filterCriteria using SpreadsheetApp.newFilterCriteria().whenDateEqualToAny(dates) and I am parsing an array of valid dates. In the documentation it says I have to put a "Date[]" - doesn't that mean an array of dates?
Below the error message and my code:
Error message (linked to the line "var filterCriteria..."):
"Exception: The boolean condition can not have multiple values for equality checks for non-data source objects"
My code:
function showLastMonth() {
var ss = SpreadsheetApp.getActive()
var sheet = ss.getSheetByName('evaluation')
var now = new Date()
var thisYear = now.getFullYear()
var thisMonth = now.getMonth()
if(thisMonth == 0){var startMonth = 11; var startYear = thisYear - 1}
else{var startMonth = thisMonth - 1; var startYear = thisYear}
var startDate = new Date(startYear, startMonth, 1)
var endDate = new Date(thisYear, thisMonth, 0)
var dates = getDateArray(startDate, endDate)
var filter = sheet.getFilter()
if(filter == null ){
var range = sheet.getDataRange()
var filter = range.createFilter()
}
var filterCriteria = SpreadsheetApp.newFilterCriteria().whenDateEqualToAny(dates)
filter.setColumnFilterCriteria(1, filterCriteria)
}
getDateArray = function(startDate, endDate){
var startYear = startDate.getFullYear()
var startMonth = startDate.getMonth()
var dateArray = []; dateArray.push(startDate)
var date = startDate; var day = date.getDay()-1
while(date<endDate){
day++
date = new Date(startYear, startMonth, day)
if(date<=endDate){dateArray.push(date)}
}
return dateArray;
}
I believe your goal as follows.
You want to hide the rows of the values except for dates using the basic filter.
You want to achieve this using Google Apps Script.
Issue and workaround:
In the current stage, it seems that array of whenDateEqualToAny(array) is required to be the length of 1. I think that this is the reason of your issue. So for example, when var filterCriteria = SpreadsheetApp.newFilterCriteria().whenDateEqualToAny([dates[0]]) is used, no error occurs. This situation is the same with the setBasicFilter request of Sheets API. Unfortunately, it seems that this is the current specification. But, the official document says The acceptable values. which uses the plural form. Ref So I also think that this is not correct for the actual situation as mentioned by TheMaster's comment.
In order to achieve your goal, in this case, I would like to propose the following 2 patterns.
Pattern 1:
In this pattern, using setHiddenValues(), the values except for the values of dates in your script are set as the hidden values.
Modified script:
When your script is modified, please modify as follows.
From:
var filterCriteria = SpreadsheetApp.newFilterCriteria().whenDateEqualToAny(dates)
To:
var obj = dates.reduce((o, e) => Object.assign(o, {[`${e.getFullYear()}\/${e.getMonth() + 1}\/${e.getDate()}`]: true}), {});
var range = sheet.getRange("A1:A");
var dispValues = range.getDisplayValues();
var hiddenValues = range.getValues().reduce((ar, [a], i) => {
if (a instanceof Date && !obj[`${a.getFullYear()}\/${a.getMonth() + 1}\/${a.getDate()}`]) {
ar.push(dispValues[i][0]);
}
return ar;
}, []);
var filterCriteria = SpreadsheetApp.newFilterCriteria().setHiddenValues(hiddenValues).build();
Pattern 2:
In this pattern, using whenNumberBetween(), the values of dates in your script are shown. In this case, it is required to convert the date object to the serial number.
Modified script:
When your script is modified, please modify as follows.
From:
var filterCriteria = SpreadsheetApp.newFilterCriteria().whenDateEqualToAny(dates)
To:
var filterCriteria = SpreadsheetApp.newFilterCriteria().whenNumberBetween(
(dates[0].getTime() / 1000 / 86400) + 25569,
(dates.pop().getTime() / 1000 / 86400) + 25569
).build();
The conversion from the date object to the serial number was referred from this thread.
References:
setHiddenValues(values)
whenNumberBetween(start, end)

How do I populate a LiteDB database if some of my values are null

I am populating a dataGridView from an incomplete excel spreadsheet and generating the LiteDB from the dataGridView. Before the recent update, I wasn't having any issues. Now I am getting the error 'Objects cannot be cast from DBNull to other types'. I can work around this by including dummy values in the original spreadsheet. But ultimately we need to see what information we're missing. How can I accomplish this?
public void CreateDatabase()
{
using (var db = new LiteDatabase(#"C:\Users\BThatcher\Documents\_Personal\Revit\MDL-Sheets\SheetDatabase04.db")) //< ----------------
{
var sheets = db.GetCollection<Sheet>("sheets");
foreach (DataGridViewRow row in dataGridView2.Rows)
{
var sheet = new Sheet
{
SheetSequence = Convert.ToInt32(row.Cells[0].Value),
SheetNumber = row.Cells[1].Value.ToString(),
SheetName = row.Cells[2].Value.ToString(),
AreaNumber = Convert.ToInt32(row.Cells[3].Value),
AreaName = row.Cells[4].Value.ToString(),
SheetDiscipline = row.Cells[5].Value.ToString(),
DisciplineSort = Convert.ToInt32(row.Cells[6].Value),
SheetSort = Convert.ToInt32(row.Cells[7].Value),
ModelName = row.Cells[8].Value.ToString(),
AppearsInSheetlist = row.Cells[9].Value.ToString(),
DesignedBy = row.Cells[10].Value.ToString(),
DrawnBy = row.Cells[11].Value.ToString(),
CheckedBy = row.Cells[12].Value.ToString(),
ApprovedBy = row.Cells[13].Value.ToString(),
SheetTotal = Convert.ToInt32(row.Cells[14].Value),
DesignSoftware = row.Cells[15].Value.ToString(),
HasPdf = row.Cells[16].Value.ToString(),
PdfPath = row.Cells[17].Value.ToString(),
};
sheets.Insert(sheet);
this.Close();
}
}
}
This error are not from LiteDB (there is no DBNull in LiteDB). It's possible this DBNull are getting from row.Cells[n].Value when you try to convert.
LiteDB support nulls and nullable values (like int?).

Integrating gorm.Model fields into protobuf definitions

I am trying to figure out how to integrate the gorm.Model fields (deleted_at, create_at, id, etc) into my proto3 definitions. However, I can't a datetime type for proto3. I tried looking for documentation on how to serialize the gorm fields to strings (since proto3 handles strings) but I have not found anything.
Has anyone been able to successfully use the gorm model fields in their proto definitions? I'm using go-micro's plugin to generate *pb.go files.
Here's my current message definition which doesn't work. It seems like empty strings are being stored in the database for deleted_at since when querying for deleted_at is null the postgres database returns nothing.
message DatabaseConfig {
string address = 1;
int32 port = 2;
string databaseName = 3;
string username = 4;
string password = 5;
string databaseType = 6;
string quertStatement = 7;
int32 id = 8;
string createdAt = 9;
string updatedAt = 10;
string deletedAt = 11;
}
UPDATE:
I've updated my proto def to the following but gorm still isn't properly using the Id, CreatedAt, UpdatedAt, and DeletedAt fields
syntax = "proto3";
package go.micro.srv.importer;
import "google/protobuf/timestamp.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
service ImporterService {
rpc CreateDatabaseConfig(DatabaseConfig) returns (Response) {}
rpc RetrieveDatabaseConfig(GetRequest) returns (Response) {}
rpc UpdateDatabaseConfig(DatabaseConfig) returns (Response) {}
rpc DeleteDatabaseConfig(DatabaseConfig) returns (Response) {}
}
message GetRequest {}
message DatabaseConfig {
string address = 1;
int32 port = 2;
string databaseName = 3;
string username = 4;
string password = 5;
string databaseType = 6;
string quertStatement = 7;
int32 id = 8;
google.protobuf.Timestamp createdAt = 9 [(gogoproto.stdtime) = true];
google.protobuf.Timestamp updatedAt = 10 [(gogoproto.stdtime) = true];
google.protobuf.Timestamp deletedAt = 11 [(gogoproto.stdtime) = true];
}
message Response {
bool created = 1;
DatabaseConfig database_config = 2;
repeated DatabaseConfig databaseConfigs = 3;
}
The protoc-gen-gorm project did not work for me. It looks like there is some blending of proto2 and proto3 happening, and ultimately I was unable to get it to work.
My solution was to create a script to do post processing after I generate the go files from protobuf.
If this was my proto profile/profile.proto:
message Profile {
uint64 id = 1;
string name = 2;
bool active = 3;
// ...
}
Which created profile/profile.pb.go with standard protoc command:
// ...
type Profile struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Active bool `protobuf:"varint,3,opt,name=active,proto3" json:"active,omitempty"`
}
// ...
I use this script gorm.sh:
#!/bin/bash
g () {
sed "s/json:\"$1,omitempty\"/json:\"$1,omitempty\" gorm:\"type:$2\"/"
}
cat $1 \
| g "id" "primary_key" \
| g "name" "varchar(100)" \
> $1.tmp && mv $1{.tmp,}
Which I invoke on my go file after it's generated with ./gorm.sh profile/profile.pb.go and the result of profile/profile.pb.go is:
// ...
type Profile struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty" gorm:"type:primary_key"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty" gorm:"type:varchar(100)"`
Active bool `protobuf:"varint,3,opt,name=active,proto3" json:"active,omitempty"`
}
// ...
Try to use protoc-gen-gorm. It will create another file .pb.gorm.go
Might be an option to use something like this: https://github.com/favadi/protoc-go-inject-tag to generate the tags automatically (I am still looking into this myself)

How to convert an Int to a String inside a LINQ query

I am simply trying to derive an array[] using the following Linq query with EFF and Mysql:
DM_ItemGroup[] array =
(from item_grp in DbContext.hexa_item_group
join item_btn in DbContext.hexa_button
on item_grp.GroupID equals item_btn.ButtonID
where item_btn.ButtonType.Equals("G", StringComparison.Ordinal)
select new DM_ItemGroup
{
GroupID = SqlFunctions.StringConvert((decimal)item_grp.GroupID),
GroupName = item_grp.GroupName,
ButtonID = item_btn.ButtonID,
Default_TaxId = item_grp.Default_TaxId,
Out_Of_Sales = item_grp.Out_Of_Sales,
Sales_Seq = item_grp.Sales_Seq,
DataModel_Button = new DM_Button(),
}).ToArray<DM_ItemGroup>();
I initially tried .ToString() but it gave an exception. After trying SqlFunctions.StringConvert I am getting the following error:-
The specified method 'System.String StringConvert(System.Nullable`1[System.Decimal])'
on the type 'System.Data.Objects.SqlClient.SqlFunctions'
cannot be translated into a LINQ to Entities store expression.
How to convert the GroupID (which is a Int in my Table) to String (which is required by my DataModel)?
Thanks for down voting a perfectly legitimate question.Wish someone had answered the question with equal promptness.
I found the solution to my problem which is as follows:-
using (HexaEntities hh=new HexaEntities())
{
var cc = hh.hexa_item_group.ToDictionary(k => k.GroupID, k => k);
var lst = from l in cc
orderby l.Key
select new DM_ItemGroup {GroupID = l.Key.ToString(), GroupName = l.Value.GroupName, Default_TaxId=l.Value.Default_TaxId,ButtonID=l.Value.ButtonID.Value,Out_Of_Sales=l.Value.Out_Of_Sales,Sales_Seq=l.Value.Sales_Seq };
AllRecords = lst.ToList<DM_ItemGroup>();
}