Skip to content

Commit

Permalink
Rewrite query to only get unique hosts
Browse files Browse the repository at this point in the history
Previously it was grouping by the is_up column too,
returning duplicate host entries.
  • Loading branch information
nothingface0 committed Jan 7, 2025
1 parent f5e46e1 commit 6458955
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,20 +619,24 @@ def get_timeline_data(
return dict(dic)

def get_cluster_status(
self, timestamp_from: datetime = None, timestamp_to: datetime = None
self,
timestamp_from: datetime = datetime.now(),
timestamp_to: datetime = datetime.now(),
):
self.log.debug("DQM2MirrorDB.get_cluster_status()")
with self.engine.connect() as cur:
answer = cur.execute(
text(
"SELECT hostnames.name, hoststatuses.is_up, hoststatuses.message, max(hoststatuses.created_at) "
"SELECT h.name, t.is_up, t.message, t.created_at "
+ f"FROM {self.TB_NAME_HOST_STATUS} t "
+ "JOIN "
+ "(SELECT host_id, MAX(created_at) AS max_created_at "
+ f"FROM {self.TB_NAME_HOST_STATUS} "
+ f"INNER JOIN {self.TB_NAME_HOST_NAME} "
+ "ON hoststatuses.host_id = hostnames.id "
+ "GROUP BY hoststatuses.host_id, hostnames.id, hoststatuses.is_up, hoststatuses.message"
+ "GROUP BY host_id) "
+ "sub ON t.host_id = sub.host_id AND t.created_at = sub.max_created_at "
+ f"JOIN {self.TB_NAME_HOST_NAME} h ON t.host_id = h.id;"
)
).all()

answer = [
# hostname, is_up, message, timestamp
(_censor_hostname(a[0]), a[1], a[2], datetime.timestamp(a[3]))
Expand Down

0 comments on commit 6458955

Please sign in to comment.