bigtable

import os
import time
import json
from google.cloud import bigtable
from google.cloud import happybase

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/jlh/Desktop/bigdata-group.json"


class status(object):
    def __init__(self):
        self.client = bigtable.Client(project="heidao-market", admin=True)
        self.instance = self.client.instance("yotta-bigtable")
        self.connection = happybase.Connection(instance=self.instance)
        self.table = self.connection.table("mafia1_SnapShot_Status_result_test_solar")
        pass

    def get_scan_list(self, player_id, player_time):
        # end_time = round(time.time() * 1000)
        end_time = player_time
        start_time = end_time - 86400000
        player_id = player_id
        row_key = str(player_id)[::-1]
        row = self.table.row(row_key)
        result = {}
        if len(row) == 0:
            return result
        last_vals = {}
        last_times = {}
        for i in range(len(row)):
            col = bytes("cf1:status_" + str(i), encoding="utf8")
            if col not in row.keys():
                continue
            row_dic = json.loads(row[col])
            time_str = row_dic["time"]
            time_list = time_str.split('#')
            for key in row_dic:
                if key != "time":
                    val = row_dic[key]
                    val_list = val.split("#")
                    sums = 0.0
                    last_val = float(val_list[0])
                    last_time = start_time
                    if key in last_vals:
                        last_val = last_vals[key]
                    if key in last_times:
                        last_time = last_times[key]
                    for j in range(len(time_list)):
                        cur_time = int(time_list[j])
                        if cur_time >= last_time and cur_time <= end_time:
                            sums += (cur_time - last_time) * last_val
                            last_time = cur_time
                        if cur_time <= end_time:
                            last_val = float(val_list[j])
                    last_vals[key] = last_val
                    last_times[key] = last_time
                    if key in result:
                        result[key] = result[key] + sums
                    else:
                        result[key] = sums
                    if i == len(row) - 1:
                        result[key] = result[key] + (end_time - last_time) * last_val
                        result[key] = result[key] / 86400000
        return result

    # 根据时间寻找状态
    def get_status_by_time(self, player_id, player_time):
        row_key = str(player_id)[::-1]
        end_time = player_time
        row = self.table.row(row=row_key)
        result = {}
        if len(row) == 0:
            return result
        for i in range(len(row)):
            col = bytes("cf1:status_" + str(i), encoding="utf8")
            if col not in row.keys():
                continue
            row_dic = json.loads(row[col])
            time_str = row_dic["time"]
            time_list = time_str.split('#')
            index = -1
            for j in range(len(time_list)):
                if int(time_list[j]) >= end_time:
                    break
                index = j
            if index == -1:
                return result
            for key in row_dic:
                val = row_dic[key]
                val_list = val.split('#')
                result[key] = val_list[index]
        return result


if __name__ == "__main__":


    status = status()
    # dic = status.get_scan_list(1014223004)
    # print(dic)
    start = time.time()
    dic = status.get_scan_list(1087110896, 1583810937000)
    print(dic)

    dic = status.get_status_by_time(1087110896, 1683810937000)
    print(dic)
    stop = time.time()
    print(stop - start)
posted @ 2022-08-19 22:51  luoganttcc  阅读(12)  评论(0)    收藏  举报