How can I generate table with jinja2 with diferent index length? - html

For example I have 5 lists:
client_list: 1 data
machines_list: 3 data
instances_list: 6 | 2 per machine
ram_free: 3 data same as machines_list|length
{% set max_length = istances_list|length %}
{% for i in range(0, max_length) %}
<td>{{client_list[i]}}</td>
<td>{{machines_list[i]}}</td>
<td>{{instance_list[i]}}</td>
<td>{{ram_free[i]}}</td>
{% endfor %}
This is printing the table with repeated results because they don't have the same lengths and if I set max length to client list will have missing results.
I want to generate the table with each index of each list that can change on each request. for example:
Clients | Machines | Instances | Ram |
|-------------------------------------------|
|Mi | | instance 1.1| |
|cr | machine 1| instance 1.2| 2gb |
|os | | instance 1.3| |
|oft -------------------------|------- |
| | instance 2.1| |
|co | machine 2| instance 2.2| 3gb |
|mp | | instance 2.3| |
|ut -------------------------- ------ |
|er | | instance 3.1| |
| | machine 3| instance 3.2| 4gb |
| | | instance 3.3| |
--------------------------------------------|
Thank you in advance

Related

How do I get information from multiple MySQL tables?

I'm working on a way to keep track of physical connections between servers/switches in multiple racks. I set up my database so it is like this:
mysql> show tables;
+----------------------+
| Tables_in_System |
+----------------------+
| connections |
| racks |
| servers |
+----------------------+
mysql> select * from racks;
+----+-------------+-------------+----------+
| id | rack_number | rack_height | location |
+----+-------------+-------------+----------+
| 2 | 7430 | 43 | xxx |
| 3 | 7431 | 43 | xxx |
| 4 | 7432 | 43 | xxx |
+----+-------------+-------------+----------+
mysql> select * from servers;
+----+-------------+---------------+-----------------+---------------+
| id | server_type | rack_location | server_unit_loc | server_height |
+----+-------------+---------------+-----------------+---------------+
| 1 | Server 1 | 7430 | 2 | 3 |
| 2 | Cisco 2960 | 7431 | 1 | 1 |
| 3 | Server 2 | 7431 | 9 | 1 |
| 15 | Server 3 | 7432 | 27 | 2 |
| 16 | Cisco 2248 | 7432 | 29 | 1 |
+----+-------------+---------------+-----------------+---------------+
rack_location refers to the numbered racks in the first table.
server_unit_loc is the placement of the server in the rack, 1 being the top, 43 would be the bottom. And server_height is just how tall the server is.
mysql> select * from connections;
+----+----------+----------+------------+------------+---------+---------+------------+
| id | rack_id1 | rack_id2 | server_id1 | server_id2 | port_s1 | port_s2 | cable_type |
+----+----------+----------+------------+------------+---------+---------+------------+
| 1 | 7430 | 7430 | 2 | 1 | J01 | J08 | RJ-45 |
| 2 | 7430 | 7431 | 2 | 3 | J03 | J08 | RJ-45 |
| 3 | 7430 | 7432 | 2 | 15 | J02 | J09 | SFP+ |
+----+----------+----------+------------+------------+---------+---------+------------+
rack_id's again (I realize now these are probably pointless...) server_id1 & 2 are the id's from the servers table. ports_1&2 correlate to the jack the cable is plugged in. This is an internal system we use, so J01 means it is the first jack on the server, and it plugs into the 8th(J08) port on the connecting server.
The end goal here is to be able to quickly see how many servers are connected to any chosen server and also the details about that connection info. In this example I connected the Cisco 2960 to multiple servers in different racks.
+What kind of query would I have to put in to return all the servers that are connected to the Cisco (2). I'd like to be able to specify just the server_id# and it would go to the different tables and get all the information such as which rack it's in, it's type, and the height of the server
Cisco2960 7430 A01 J01 --> Server1 7430 A02 J08 "RJ-45"
Cisco2960 7430 A01 J03 --> Server2 7431 A09 J08 "RJ-45"
Cisco2960 7430 A01 J02 --> Server3 7432 A27 J09 "SFP+"
Something like that...^
Thanks! Also if you have a better idea of how to go about accomplishing this I'd love to hear everyone's thoughts.
You would need to create query that joins the related information together.
Think of label S being (source server). Label T being (target server).
Ex.
SELECT S.server_type, S.rack_location, CONCAT('A0',S.server_unit_loc), B.port_s1,T.server_type, T.rack_location, CONCAT('A0',T.server_unit_loc), B.port_s2,B.cable_type FROM servers S
JOIN connections B ON B.server_id1 = S.id
JOIN servers T ON B.server_id2 = T.ID
WHERE S.id = 2
I agree with one of the comments. You could normalize things (rid some redundant rack information).

how to insert average of a query into mysql table

I am trying to get average of latency for each items that holds into two separate mysql table. Let me more clarify that I have two mysql tables as below,
table: monitor_servers
+-----------+-----------------+
| server_id | label |
+-----------+-----------------+
| 1 | a.com |
| 2 | b.com |
+-----------+-----------------+
table: monitor_servers_uptime
+-------------------+-----------+-----------+
| servers_uptime_id | server_id | latency |
+-------------------+-----------+-----------+
| 1 | 1 | 0.4132809 |
| 3 | 1 | 0.4157769 |
| 6 | 1 | 0.4194210 |
| 9 | 1 | 0.4140880 |
| 12 | 2 | 0.4779439 |
| 15 | 2 | 0.4751789 |
| 18 | 2 | 0.4762829 |
| 22 | 2 | 0.4706681 |
+-------------------+-----------+-----------+
Basically, each domains associated with the same id_number in both tables. While I am running the query below, getting average of each items.
select monitor_servers.label, avg(monitor_servers_uptime.latency)
from monitor_servers,monitor_servers_uptime
where monitor_servers.server_id = monitor_servers_uptime.server_id
group by monitor_servers.server_id;
The query ended up,
+---------------------+-------------------------------------+
| label | avg(monitor_servers_uptime.latency) |
+---------------------+-------------------------------------+
| a.com | 0.41393792995 |
| b.com | 0.47551423171 |
+---------------------+-------------------------------------+
My questions are doing am i in wright way while getting average of the each items and how can i insert new average result of each items into a new column on table monitor_servers ? And also what happens if some of latency rows are NULL ?
**Edit : What i am trying to achieve in one query result is **
+-----------+----------+------------------+
| server_id | label | avg. |
+-----------+----------+------------------+
| 1 | a.com | 0.41393792995 |
| 2 | b.com | 0.47551423171 |
+-----------+-----------------------------+
Thanks in advance,
Your calculation seems to be correct.
You could add another column to the monitor_servers using sql:
ALTER TABLE monitor_servers ADD avg_latency DEFAULT 0.0 NOT NULL
For doing the AVG calculation check this answer.

Unique identifier across multiple tables?

Unique identifier across multiple tables?
Using MySQL, I'm trying to associate products with a purchase order.
Those products can become from different tables probably with different schema's.
I cant associate because of the identity of each product by schema, not by all kinds of products.
Cellphone
+-----+--------+---------+-------+----------+-------+
| id | brand | version | color | capacity | price |
+-----+--------+---------+-------+----------+-------+
| 1 | iphone | 5s | black | 16 | 9500 |
| 2 | iphone | 5s | white | 32 | 10000 |
| 3 | iphone | 5s | blue | 32 | 10000 |
+-----+--------+---------+-------+----------+-------+
Course
+-----+-----------+------------+-------+
| id | topic | idschedule | price |
+-----+-----------+------------+-------+
| 1 | Photoshop | 1 | 9500 |
| 2 | HTML5 | 2 | 10000 |
| 3 | CSS3 | 3 | 10000 |
+-----+-----------+------------+-------+
I've made a table Product, that together with a product identity, produce an unique identifier.
Product
+-----+-----------+
| id | Schema |
+-----+-----------+
| 1 | Cellphone |
| 2 | Course |
+-----+-----------+
ProductEspecification
+-----+-----------+------------------+----------+------------+
| id | idproduct | idespecification | quantity | idpurchase |
+-----+-----------+------------------+----------+------------+
| 1 | 1 | 1 | 10 | 1 |=> 10 iphones
| 2 | 2 | 3 | 1 | 1 |=> 1 CSS3 course
+-----+-----------+------------------+----------+------------+
Is there a better approach?
You need to use abstraction and Table Inheritance.
An abstract thing you sell (good, service, etc) is called a Product.
An abstract physical good you sell is called a Good. Good inherits Product.
An abstract service you offer is called a Service. Service inherits Product.
Cellphone inherits Good.
Course inherits Service.

MySQL: Hundreds of tables or one big table?

I want to create a webpage where user can organize things they collect. As everyone collects something else I want to have the users create their own datamodell (with strict limitations). If two people are collection the same "things" they can share a datastructure.
My idea was to give every collection an ID and all the tables belonging to that collection will have the ID as a prefix.
Table: Collections
ID | Collection
1 | Poststamps
2 | Barbie Dolls
Table: 1_Base
ID | StampValue | StampPic
....
Table: 2_Base
ID | EAN | Pic
....
Thus I would create many tables as each user could in theory create their own collection. I could also use only one very big table and a mapping table. Example:
Table: Colleactions
ID | Collection
1 | Poststamps
2 | Barbie Dolls
Table: Mapping
fkCollection | FieldName | Mapping
1 | DoubleField1 | StampValue
1 | BlobField1 | StampPic
2 | StringField1 | EAN
2 | BlobField1 | StampPic
Table: CollectionData
fkCollection | DoubleField1 | ... | DoubleField10 | StringField1 | ... | Stringfield10 | BlobField1 | ...
1 | 30 | | | | | | ... |
2 | | | | 21312412414 | | | ... |
Any other ideas?
Thanks for your help!
From what I can see, your second way of attempting this is going to be the easiest way... your queries will be ten fold simpler to handle, and you wouldn't need to programmably create tables on the fly... so my suggestion would be to modify your second idea slightly... Just to clarify something also, A blob will slow down the query speed so I am changing the block to hold the source link to the image instead.
TABLE: Collections
ID| Collection
1 | Poststamps
2 | Barbie Dolls
Table: CollectionData
fkCollection | DataType | VALUE | FieldName |
1 | Double | 30 | StampID |
1 | String | London | StampName |
1 | ImgSrc | ../loc | StampPic |
2 | String | Ken | BarbieName |
2 | ImgSrc | ../loc | BarbiePic |

MySQL Newbie - Select all False in One to Many

Devices Network_Card
================== ===========================================
| id | hostname | | id | device_id | ip_address | dns |
================== ===========================================
| 1 | desktop1 | | 1 | 1 | 10.0.0.1 | desktop1 |
| 2 | laptop1 | | 2 | 2 | 10.0.0.2 | laptop1 |
| 3 | laptop2 | | 3 | 2 | 10.0.0.3 | laptop1w |
| 4 | desktop2 | | 4 | 3 | 10.0.0.4 | george |
| .. | ... | | 5 | 4 | 10.0.0.5 | desktop2w |
================== ===========================================
Hi folks, here's my situation. We have a home grown computer inventory with 500+ devices and the db gets populated from different sources. I'm trying to find the discrepancies in this case, so I'm trying to select devices.id where I don't have a DNS record is NOT LIKE the hostname. In this case, it would return devices.id = 3
I haven't done relational db queries in a long time, and my brain can't process the double negatives, so any help would be great.
This query might do it for you:
select d.id, d.hostname, n.dns
from devices d
left join network_card n on d.id = n.device_id
where d.hostname != substring(n.dns, 1, LENGTH(d.hostname))
I'm using substring since I'm not sure how to do a field + wildcard to use within a NOT LIKE expression. Using substring lets you do a straight != comparison.
Update: Thanks to PaparazzoKid for the sqlfiddle at: http://www.sqlfiddle.com/#!2/3a260/1