Esempio n. 1
0
 def store(self,timeseries_dict):
     """ Input the list of Enki result timeseries_dict,
         where the keys are the wanted SmG ts-path names
         and the values are Enki result api.shyft_timeseries_double, time-series.
         If the named time-series does not exist, create it.
         Then store time-series data to the named entities.
         
     """
     # 0. First, get the list of ts identities that Tss uses
     list_of_names=timeseries_dict.keys()
     ListOf_TsIdentities=self._namelist_to_ListOf_TsIdentities(list_of_names)
     ok=False
     with repository(self.env) as tss:
         # 1. We check if any of the tsnames are missing..
         exists_kv_pairs=tss.repo.Exists(ListOf_TsIdentities)
         missing_list= List[MetaInfo]([])
         # 2. We create those missing..
         for e in exists_kv_pairs:
             if e.Value == False:
                 tsid=e.Key
                 mi= MetaInfo()
                 mi.Identity=tsid
                 mi.Description='Automatically created by enki '
                 # Here we might fill in some properties to the created timeseries
                 # e.g. unit, if we could figure out that
                 missing_list.Add(mi)
         if missing_list.Count > 0 : # Yes, something was missing, create them
             created_list=tss.repo.Create(missing_list)
         # 3. We store the datapoints (identity period, then  time,value)
         ssa_timeseries_list= List[SsaTimeSeries]([]) # This is what Tss Xts eats
         for name,shyft_ts in timeseries_dict.iteritems():
             ssa_ts=self._make_ssa_ts_from_shyft_ts(name,shyft_ts)
             ssa_timeseries_list.Add(ssa_ts)
         ok=tss.repo.Write(ssa_timeseries_list) # Write into SmG!
     return ok
Esempio n. 2
0
 def fetch(self, **kwargs):
     """Open a connection to the SMG database and fetch all the time series given in self.keys.
     Return the result as a dictionary of dictionaries."""
     result = {}
     raw_data = []
     with repository(self.env) as tsr:
         raw_data = tsr.readRawPoints(self.t_start, self.t_end, self.keys, useNameAsId=self.keys_are_names)
     if len(self.keys) != raw_data.Count:
         print "WARNING: Could only find {} out of {} requested timeseries".format(raw_data.Count, len(self.keys))
     for d in raw_data:
         times = [d.Time(i).ToUnixTime() for i in xrange(d.Count)]
         values = [d.Value(i).V for i in xrange(d.Count)]
         print min(values), max(values)
         print min(times), max(times)
         print "Getting timeseries {} with {} datapoints and end date {}".format(d.Name, len(times), str(datetime.utcfromtimestamp(times[-1])))
         key = d.Name if self.keys_are_names else d.Info.Id
         result[key] = {"times": times, "values": values}
     return result
Esempio n. 3
0
    def read(self,list_of_ts_id,period):
        """Open a connection to the SMG database and fetch all the time series given in list_of_ts_id.
        ts_id is currently the full unique name of the smg-ts. We could/should also support using
        unique number/keys instead. -more efficient, and more robust to namechanges.
        Return the result as a dictionary of shyft_ts."""
        if(not isinstance(period,api.UtcPeriod)):
           raise SmgDataError("period should be of type api.UtcPeriod")

        result = {}
        raw_data = []
        ListOf_TsIdentities=self._namelist_to_ListOf_TsIdentities(list_of_ts_id)
        ssa_period=self._make_ssa_Period_from_shyft_period(period)
        with repository(self.env) as tsr:
            raw_data = tsr.repo.ReadRawPoints(ListOf_TsIdentities,ssa_period)
        if len(list_of_ts_id) != raw_data.Count:
            print "WARNING: Could only find {} out of {} requested timeseries".format(raw_data.Count, len(list_of_ts_id))
        for d in raw_data:
            key = d.Name #todo : if self.keys_are_names else d.Info.Id
            result[key] = self._make_shyft_ts_from_ssa_ts(d)
        return result