RENAME TABLE graphite TO old_graphite;

CREATE TABLE graphite ENGINE = GraphiteMergeTree (
    'graphite_rollup'
)
PARTITION BY toYYYYMM (Date)
ORDER BY
    (Path, Time) SETTINGS index_granularity = 8192 AS
    SELECT
        multiIf (
            like (Path, '%.extInputCounter.%'), replaceOne (Path, 'extInputCounter', 'router.extInputCounter'),
            like (Path, '%.extOutputCounter.%'), replaceOne (Path, 'extOutputCounter', 'router.extOutputCounter'),
            like (Path, '%.extLimiterDropCounter.%'), replaceOne (Path, 'extLimiterDropCounter', 'router.extLimiterDropCounter'),
            like (Path, '%.intInputCounter.%'), replaceOne (Path, 'intInputCounter', 'router.intInputCounter'),
            like (Path, '%.intOutputCounter.%'), replaceOne (Path, 'intOutputCounter', 'router.intOutputCounter'),
            like (Path, '%.intLimiterDropCounter.%'), replaceOne (Path, 'intLimiterDropCounter', 'router.intLimiterDropCounter'),
            like (Path, '%.outputLimiterDropCounter.%'), replaceOne (Path, 'outputLimiterDropCounter', 'router.outputLimiterDropCounte'),
            like (Path, '%.routerDropCounter.%'), replaceOne (Path, 'routerDropCounter', 'router.dropCounter'),

            like (Path, '%.generalInputCounter.%'), replaceOne (Path, 'generalInputCounter', 'general.inputCounter'),
            like (Path, '%.generalPassCounter.%'), replaceOne (Path, 'generalPassCounter', 'general.passCounter'),
            like (Path, '%.generalBackCounter.%'), replaceOne (Path, 'generalBackCounter', 'general.backCounter'),
            like (Path, '%.generalDropCounter.%'), replaceOne (Path, 'generalDropCounter', 'general.dropCounter'),

            like (Path, '%.ipv6.inputCounter.%'), replaceOne (Path, 'inputCounter', 'general.inputCounter'),
            like (Path, '%.ipv6.passCounter.%'), replaceOne (Path, 'passCounter', 'general.passCounter'),
            like (Path, '%.ipv6.backCounter.%'), replaceOne (Path, 'backCounter', 'general.backCounter'),
            like (Path, '%.ipv6.dropCounter.%'), replaceOne (Path, 'dropCounter', 'general.dropCounter'),

            match (Path, '\d.inputCounter'), replaceOne (Path, 'inputCounter', 'policy.inputCounter'),
            match (Path, '\d.passCounter'), replaceOne (Path, 'passCounter', 'policy.passCounter'),
            match (Path, '\d.backCounter'), replaceOne (Path, 'backCounter', 'policy.backCounter'),
            match (Path, '\d.dropCounter'), replaceOne (Path, 'dropCounter', 'policy.dropCounter'),


            Path) AS Path, Value, Time, Date, Timestamp
    FROM
        old_graphite;

RENAME TABLE graphite_tagged TO old_graphite_tagged;

CREATE TABLE graphite_tagged ENGINE = ReplacingMergeTree (
    Version
)
PARTITION BY toYYYYMM (Date)
ORDER BY
    (Tag1, Path, Date) SETTINGS index_granularity = 8192 AS
    SELECT
        Date, Tag1, multiIf (
            like (Path, '%.extInputCounter.%'), replaceOne (Path, 'extInputCounter', 'router.extInputCounter'),
            like (Path, '%.extOutputCounter.%'), replaceOne (Path, 'extOutputCounter', 'router.extOutputCounter'),
            like (Path, '%.extLimiterDropCounter.%'), replaceOne (Path, 'extLimiterDropCounter', 'router.extLimiterDropCounter'),
            like (Path, '%.intInputCounter.%'), replaceOne (Path, 'intInputCounter', 'router.intInputCounter'),
            like (Path, '%.intOutputCounter.%'), replaceOne (Path, 'intOutputCounter', 'router.intOutputCounter'),
            like (Path, '%.intLimiterDropCounter.%'), replaceOne (Path, 'intLimiterDropCounter', 'router.intLimiterDropCounter'),
            like (Path, '%.outputLimiterDropCounter.%'), replaceOne (Path, 'outputLimiterDropCounter', 'router.outputLimiterDropCounte'),
            like (Path, '%.routerDropCounter.%'), replaceOne (Path, 'routerDropCounter', 'router.dropCounter'),

            like (Path, '%.generalInputCounter.%'), replaceOne (Path, 'generalInputCounter', 'general.inputCounter'),
            like (Path, '%.generalPassCounter.%'), replaceOne (Path, 'generalPassCounter', 'general.passCounter'),
            like (Path, '%.generalBackCounter.%'), replaceOne (Path, 'generalBackCounter', 'general.backCounter'),
            like (Path, '%.generalDropCounter.%'), replaceOne (Path, 'generalDropCounter', 'general.dropCounter'),

            like (Path, '%.ipv6.inputCounter.%'), replaceOne (Path, 'inputCounter', 'general.inputCounter'),
            like (Path, '%.ipv6.passCounter.%'), replaceOne (Path, 'passCounter', 'general.passCounter'),
            like (Path, '%.ipv6.backCounter.%'), replaceOne (Path, 'backCounter', 'general.backCounter'),
            like (Path, '%.ipv6.dropCounter.%'), replaceOne (Path, 'dropCounter', 'general.dropCounter'),

            match (Path, '\d.inputCounter'), replaceOne (Path, 'inputCounter', 'policy.inputCounter'),
            match (Path, '\d.passCounter'), replaceOne (Path, 'passCounter', 'policy.passCounter'),
            match (Path, '\d.backCounter'), replaceOne (Path, 'backCounter', 'policy.backCounter'),
            match (Path, '\d.dropCounter'), replaceOne (Path, 'dropCounter', 'policy.dropCounter'),                           
            
            Path) AS Path, Tags, Version
    FROM
        old_graphite_tagged;

TRUNCATE TABLE graphite_index;

DROP TABLE old_graphite;

DROP TABLE old_graphite_tagged;
