func mcQuery()

in tools/mconnect/commands/views/views.go [186:212]


func mcQuery(projectID, datasetID string) string {
	// The query is built with fmt.Printf since creating views can't do any update/delete operations, hence won't be able to ruin any data.
	// Also, as this is intended to be used locally, it is safe to assume a user wouldn't try to inject parameters that would mess his project.
	assetsTable := fmt.Sprintf("%v.%v.assets", projectID, datasetID)
	groupsTable := fmt.Sprintf("%v.%v.groups", projectID, datasetID)

	query := `WITH FilteredGroups AS (
		SELECT name, display_name
		FROM %v
		WHERE EXISTS (
		  SELECT 1
		  FROM UNNEST(labels) AS label
		  WHERE label.key = '%v'
		)
	  )
	  
	  SELECT grp.display_name as Application, machine_details.machine_name AS VMs, machine_details.core_count AS vCPUs, machine_details.memory_mb/1024 AS MemoryGBs, machine_details.disks.total_capacity_bytes/(1024*1024*1024) AS StorageGBs
	  FROM %v AS assets
	  CROSS JOIN
		UNNEST(assets.assigned_groups) AS application
	  INNER JOIN
		FilteredGroups AS grp
	  ON
		application = grp.name`

	return fmt.Sprintf(query, groupsTable, gp.LabelKey, assetsTable)
}