# Copyright (c) 2022, JINGROW
# For license information, please see license.txt

import jingrow
from jingrow.desk.utils import slug


def execute():
	for row in jingrow.get_all(
		"Virtual Machine",
		{"status": "Running"},
		["cluster", "series", "max(`index`) as `index`"],
		group_by="cluster, series",
		order_by="series, cluster",
	):
		jingrow.db.sql(
			f"""
			INSERT INTO `tabSeries` (`name`, `current`)
			VALUES ("{row.series}-{slug(row.cluster)}", {row.index})
		"""
		)
