'Loop to find the count the repeat of data

my data set is composed of time and data. I want to count the data until there is gap in time between the two data recorded. I tried doing this way but its not working, and is there a better way to do in dataframe

def convertToEvents(entries, gap):
    results = []
    i = 0
    prevTime = 0
    while i < len(entries):
        time = entries[i][0]
        current = entries[i][1]
        if time > prevTime + gap:
            results.append((time, current,1))
        elif current > entries[i-1][1]:
            results[-1] = (results[-1][0], current, time-results[-1][0])
        else:
            results[-1] = (results[-1][0], results[-1][1], time-results[-1][0])
        prevTime = entries[i][0]
        i+=1
    return results


Solution 1:[1]

There maybe two scenarios -

  1. One single Cloud SQL instance in multiple locations
  2. Different Cloud SQL instances in multiple locations

When you create a Cloud SQL instance, you choose a region where the instance and its data are stored. To reduce latency and increase availability, choose the same region for your data and your Compute Engine instances, standard environment apps, and other services.

Location types are of mainly two types, regional location i.e. a specific geographic place and multi-regional location which contains at least two geographic places. Multi-regional locations are only used for backup operations in Cloud SQL.

You choose a location when you first create the instance. The location can't be changed after the instance is created.

One single region consists of many small data centers called zones. While creating a Cloud SQL instance you can specify the instance to be available in a single zone or in two different zones within the selected region. Selecting the Cloud SQL instance to be in two different zones is called High Availability (HA) configuration.

The purpose of an HA configuration is to reduce downtime when a zone or instance becomes unavailable which might happen during a zonal outage, or when an instance becomes corrupted. With HA, your data continues to be available to client applications.

The HA configuration provides data redundancy. A Cloud SQL instance configured for HA is also called a regional instance and is located in a primary and secondary zone within the configured region. Within a regional instance, the configuration is made up of a primary instance and a standby instance.

So considering the first scenario when you say if a Cloud SQL instance can be located in multiple locations then it is yes if you consider different zones as different locations (This is correct as two zones are physically separated data centers within a single GCP region). But it can only be located in two zones and for that you have to configure High Availability(HA) for the instance.

For the second scenario, you can always create different Cloud SQL instances in different regions.

You can go through instance locations in Cloud SQL and overview of HA configuration to have a brief understanding of the above.

There is another option in Cloud SQL called read replicas.

You use a read replica to offload work from a Cloud SQL instance. The read replica is an exact copy of the primary instance. Data and other changes on the primary instance are updated in almost real time on the read replica.

Read replicas are read-only; you cannot write to them. The read replica processes queries, read requests, and analytics traffic, thus reducing the load on the primary instance.

If you want the data to be available in multiple locations you may consider using cross-region read replicas.

Cross-region replication lets you create a read replica in a different region from the primary instance.

Cross-region read replicas has many advantages -

  • Improve read performance by making replicas available closer to your application's region.
  • Provide additional disaster recovery capability to guard against a regional failure.
  • Let you migrate data from one region to another.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prabir