def make_groupby(period, groupby):
    """Make GROUP BY part of the InfluxDB query.

    :param period - time in second for the grouping by.
    :param groupby - list of fields for group by

    :return str like `field1, field2, time(10s)`
    """
    if groupby:
        groupby = [field.replace("resource_metadata", "metadata")
                   for field in groupby]
    return ", ".join((groupby or []) +
                     (["time({period}s)".format(period=period)]
                      if period > 0 else []))